-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathParser.hs
359 lines (256 loc) · 7.04 KB
/
Parser.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
{-|
Module : Parser
Description : Monadic Parser Combinators for LF in Haskell.
Copyright : (c) Luke Geeson, 2019
License : GPL-3
Maintainer : [email protected]
Stability : stable
Portability : POSIX
The "Parser" module provides the monadic parser combinators, grammars, and top-level functions needed to parse a human friendly (read whiteboard) version of LF.
-}
module Parser where
-- Sol Imports.
import qualified LF
-- Tool Imports.
import qualified Control.Monad as M (liftM, ap)
import qualified Data.Char as C
{-
Implementation based on ideas in Monadic Parser Combinators paper
http://www.cs.nott.ac.uk/~pszgmh/monparsing.pdf
-}
-- | Parser type takes input string and returns a list of possible parses
newtype Parser a = Parser (String -> [(a, String)])
-- | Necessary AMP additions for Parser instance.
instance Functor Parser where
fmap = M.liftM
-- | Necessary AMP additions for Parser instance.
instance Applicative Parser where
pure a = Parser (\cs -> [(a,cs)])
(<*>) = M.ap
-- | Monad instance, generators use the first parser then apply f to the result
instance Monad Parser where
return = pure
p >>= f = Parser (\cs -> concat [parse (f a) cs' | (a,cs') <- parse p cs])
-- | Parser deconstructor.
parse (Parser p) = p
-- | Item takes a string and splits on the first char or fails
item :: Parser Char
item = let split cs = case cs of
"" -> []
(c:cs) -> [(c,cs)]
in Parser split
-- | Combines the results of 2 parsers on an input string
-- shortcircuits on the first result returned or fails
(+++) :: Parser a -> Parser a -> Parser a
p +++ q = let apply cs = case parse p cs ++ parse q cs of
[] -> []
(x:_) -> [x]
in Parser apply
-- | Failure parser.
zerop = Parser (const [])
-- | Parses an element and returns if they satisfy a predicate.
sat :: (Char -> Bool) -> Parser Char
sat p = do
c <- item
if p c
then return c
else zerop
-- | Parses chars only.
char :: Char -> Parser Char
char c = sat (c ==)
-- | Parses a string of chars.
string :: String -> Parser String
string = mapM char
-- | Parses single digits
digit :: Parser Char
digit = sat (\x -> '0' <= x && x <= '9')
-- | Parsers a natural number
nat :: Parser Int
nat = do n <- many1 digit
let maybeInt = read n :: Int
return maybeInt
-- | Parses 0 or more elements.
many :: Parser a -> Parser [a]
many p = many1 p +++ return []
-- | Parses 1 or more elements.
many1 :: Parser a -> Parser [a]
many1 p = do
a <- p
as <- many p
return (a:as)
-- | Parses 0 or more whitespace.
space :: Parser String
space = many (sat C.isSpace)
-- | Parsers 1 or more whitespace.
space1 :: Parser String
space1 = many1 (sat C.isSpace)
-- | Trims whitespace between an expression.
spaces :: Parser a -> Parser a
spaces p = do
space
x <- p
space
return x
-- | Parses a single string.
symb :: String -> Parser String
symb = string
-- | Apply a parser to a string.
apply :: Parser a -> String -> [(a,String)]
apply = parse
-- | set of reserved words for LF
keywords :: [String]
keywords = ["let", "lett", "=", ".", ":", "Pi",
"Nat", "Vec", "(", ")", "\x3a0", "cons", "nil", "succ"]
-- | 1 or more chars
str :: Parser String
str = do
s <- many1 $ sat C.isLower
if s `elem` keywords
then zerop
else return s
-- | 1 or more chars
strT :: Parser String
strT = do
s1 <- many1 $ sat C.isUpper
s2 <- many $ sat C.isAlpha
let ss = s1 ++ s2
if ss `elem` keywords
then zerop
else return ss
-- | Left recursion.
chainl1 :: Parser a -> Parser (a -> a -> a) -> Parser a
p `chainl1` op = let rest a = (do f <- op
b <- p
rest (f a b)) +++ return a
in do a <- p
rest a
-- | Parses away brackets as you'd expect.
bracket :: Parser a -> Parser a
bracket p = do
symb "("
x <- p
symb ")"
return x
-- | Type vars are Strings packaged up
typVar :: Parser LF.T
typVar = LF.TVar <$> strT
-- | Nat type is just Nat
typNat :: Parser LF.T
typNat = symb "Nat" >> return LF.TNat
-- | Parser for type-level terms
typLevelTerm :: Parser LF.T
typLevelTerm = LF.TTerm <$> expr
-- | Type-level abstractions
typPi :: Parser LF.T
typPi = do
spaces (symb "Pi" +++ symb "\x3a0")
x <- str
spaces (symb ":")
ty1 <- typExpr
spaces (symb ".")
LF.TPi x ty1 <$> typTerm
-- | Lam parser parses type abstractions
typLam :: Parser LF.T
typLam = do
spaces $ identifier lambdas
x <- str
spaces (symb ":")
t1 <- typTerm
spaces (symb ".")
t2 <- spaces typTerm
return $ LF.TAbs x t1 t2
-- | arrow types are non-dependent pi types
typArr :: Parser LF.T
typArr = do
x <- typExpr
spaces (symb "->")
LF.TPi "_" x <$> typTerm -- show inst. prints ->
-- | Parser for Vector types
typVec :: Parser LF.T
typVec = symb "Vec" >> return LF.TVec
-- | App has zero or more spaces
typApp :: Parser LF.T
typApp = chainl1 typExpr $ do
space1
return LF.TApp
-- | Top level CFG for types
typTerm :: Parser LF.T
typTerm = typPi +++ typLam
+++ typArr +++ typApp
-- | Final level of CFG for types
typExpr :: Parser LF.T
typExpr = typVar +++ typNat +++ typVec
+++ typLevelTerm +++ bracket typTerm
-- | Parser for term variables
termVar :: Parser LF.LFTerm
termVar = LF.Var <$> str
-- | Parser for term nats
termNat :: Parser LF.LFTerm
termNat = LF.Nat <$> nat
-- | Parser for empty lists
termNil :: Parser LF.LFTerm
termNil = symb "nil" >> return LF.Nil
-- | Parser for list cons
termCons :: Parser LF.LFTerm
termCons = symb "cons" >> return LF.Cons
-- | Parser for the successor term
termSucc :: Parser LF.LFTerm
termSucc = symb "succ" >> return LF.Succ
-- | Abstraction allows escaped backslash or lambda
lambdas :: String
lambdas = ['\x03bb','\\']
-- | Lam parser parses abstractions
lam :: Parser LF.LFTerm
lam = do
spaces $ identifier lambdas
x <- str
spaces (symb ":")
t <- typTerm
spaces (symb ".")
e <- spaces term
return $ LF.Abs x t e
-- | App parses application terms, with one or more spaces in between terms.
app :: Parser LF.LFTerm
app = chainl1 expr $ do
space1
return LF.App
-- | Parsed expressions are either terms or terms in lets
data PExpr
= PTerm LF.LFTerm
| PType LF.T
-- | Parser for let expressions
pLet :: Parser (String, PExpr)
pLet = do
space
symb "let"
space1
v <- str
spaces $ symb "="
t <- term
return (v, PTerm t)
-- | Parser for type let expressions
pTypeLet :: Parser (String, PExpr)
pTypeLet = do
space
symb "lett"
space1
v <- strT
spaces $ symb "="
t <- typTerm
return (v, PType t)
-- | Parser for regular terms.
pTerm :: Parser (String, PExpr)
pTerm = do
t <- term
return ("", PTerm t)
-- | Expression follows CFG form with bracketing convention.
expr :: Parser LF.LFTerm
expr = termNat +++ termNil
+++ termCons +++ termVar
+++ termSucc +++ bracket term
-- | Top level of CFG Grammar
term :: Parser LF.LFTerm
term = lam +++ app
-- | Identifies key words.
identifier :: String -> Parser Char
identifier xs = sat (`elem` xs)