-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParser.hs
305 lines (264 loc) · 7.31 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
-- Parser języka PP4
module Parser (parseProgram) where
import Text.Parsec
import Text.Parsec.Language
import Text.Parsec.Token
import AST
setPos :: SourcePos -> Expr SourcePos -> Expr SourcePos
setPos = setData
lang :: LanguageDef st
lang = emptyDef
{ commentStart = "(*"
, commentEnd = "*)"
, nestedComments = True
, reservedNames =
[ "and", "bool", "div", "elif", "else", "false", "fn", "fst", "fun", "if"
, "in", "input", "int", "let", "list", "match", "mod", "not", "or", "snd"
, "then", "true", "unit", "with"
]
, reservedOpNames =
[ "|", ",", ":", "->", "::"
, "+", "-", "*", "=", "<>", "<", ">", "<=", ">=" ]
}
tokenParser :: TokenParser st
tokenParser = makeTokenParser lang
ws = whiteSpace tokenParser
keyword :: String -> Parser ()
keyword = reserved tokenParser
kwAnd = keyword "and"
kwBool = keyword "bool"
kwDiv = keyword "div"
kwElif = keyword "elif"
kwElse = keyword "else"
kwFalse = keyword "false"
kwFn = keyword "fn"
kwFst = keyword "fst"
kwFun = keyword "fun"
kwIf = keyword "if"
kwIn = keyword "in"
kwInput = keyword "input"
kwInt = keyword "int"
kwLet = keyword "let"
kwList = keyword "list"
kwMatch = keyword "match"
kwMod = keyword "mod"
kwNot = keyword "not"
kwOr = keyword "or"
kwSnd = keyword "snd"
kwThen = keyword "then"
kwTrue = keyword "true"
kwUnit = keyword "unit"
kwWith = keyword "with"
op :: String -> Parser ()
op = reservedOp tokenParser
opBar = op "|"
opComma = op ","
opColon = op ":"
opArrow = op "->"
opCons = op "::"
opEq = op "="
opNeq = op "<>"
opLt = op "<"
opGt = op ">"
opLe = op "<="
opGe = op ">="
opPlus = op "+"
opMinus = op "-"
opAster = op "*"
ident :: Parser String
ident = identifier tokenParser <?> "identifier"
type Parser = Parsec String ()
program :: Parser (Program SourcePos)
program = ws *>
(uncurry Program <$> programPreamble <*> expr <* eof)
programPreamble :: Parser ([FunctionDef SourcePos], [(Var,SourcePos)])
programPreamble = choice
[ (,) <$> many1 functionDef <*> inputVarsIn
, (,) [] <$> (kwInput *> many1 preambleVar <* kwIn)
, return ([], [])
] <?> "program preamble"
where
inputVarsIn = choice
[ (kwInput *> many1 preambleVar <* kwIn)
, (kwIn *> return [])
]
preambleVar :: Parser (Var,SourcePos)
preambleVar =
(\ pos x -> (x,pos)) <$> getPosition <*> ident
functionDef :: Parser (FunctionDef SourcePos)
functionDef =
mkFunctionDef
<$> (getPosition <* kwFun)
<*> ident
<*> parens tokenParser
((,) <$> ident <*> (opColon *> typeExpr))
<*> (opColon *> typeExpr)
<*> (opEq *> expr)
where
mkFunctionDef p f (x, t1) t2 e =
FunctionDef p f x t1 t2 e
typeExpr :: Parser Type
typeExpr =
(buildArrowType <$> pairTypeExpr `sepBy1` opArrow) <?> "type"
where
buildArrowType = foldr1 TArrow
buildPairType = foldr1 TPair
buildListType = foldl (\ t _ -> TList t)
pairTypeExpr = buildPairType <$> appTypeExpr `sepBy1` opAster
appTypeExpr = buildListType <$> simpleTypeExpr <*> many kwList
simpleTypeExpr = choice
[ kwInt *> return TInt
, kwBool *> return TBool
, kwUnit *> return TUnit
, parens tokenParser typeExpr
]
expr :: Parser (Expr SourcePos)
expr = choice
[ letExpr
, ifExpr
, fnExpr
, matchExpr
, opExpr
] <?> "expression"
letExpr :: Parser (Expr SourcePos)
letExpr =
ELet <$> getPosition <*>
(kwLet *> ident) <*> (opEq *> expr) <*> (kwIn *> expr)
ifExpr :: Parser (Expr SourcePos)
ifExpr =
EIf <$> getPosition <*> (kwIf *> expr) <*> (kwThen *> expr) <*> ifExprCont
where
ifExprCont = choice
[ kwElse *> expr
, EIf <$> getPosition <*>
(kwElif *> expr) <*> (kwThen *> expr) <*> ifExprCont
]
fnExpr :: Parser (Expr SourcePos)
fnExpr =
(uncurry . EFn) <$> getPosition
<*> (kwFn *> parens tokenParser ((,) <$> ident <*> (opColon *> typeExpr)))
<*> (opArrow *> expr)
matchExpr :: Parser (Expr SourcePos)
matchExpr =
EMatchL <$> getPosition <*>
(kwMatch *> expr <* kwWith) <*> nilClause <*> consClause
where
nilClause =
optional opBar *>
brackets tokenParser (return ()) *>
opArrow *>
expr
consClause =
(\ x y e -> (x, y, e))
<$> (opBar *> ident)
<*> (opCons *> ident)
<*> (opArrow *> expr)
opExpr :: Parser (Expr SourcePos)
opExpr =
opLeftAssoc opOr $
opLeftAssoc opAnd $
opUnary opNot $
opNoAssoc opCmp $
consExpr $
opLeftAssoc opAdditive $
opLeftAssoc opMultiplicative $
opUnary opUMinus $
appExpr
where
opOr = kwOr *> return BOr
opAnd = kwAnd *> return BAnd
opNot = kwNot *> return UNot
opCmp = choice
[ opEq *> return BEq
, opNeq *> return BNeq
, opLt *> return BLt
, opGt *> return BGt
, opLe *> return BLe
, opGe *> return BGe
]
opAdditive = choice
[ opPlus *> return BAdd
, opMinus *> return BSub
]
opMultiplicative = choice
[ opAster *> return BMul
, kwDiv *> return BDiv
, kwMod *> return BMod
]
opUMinus = opMinus *> return UNeg
consExpr :: Parser (Expr SourcePos) -> Parser (Expr SourcePos)
consExpr p = parser where
parser = buildCons <$> p <*> consTail
consTail = choice
[ Just <$> (opCons *> parser)
, return Nothing
]
buildCons e1 Nothing = e1
buildCons e1 (Just e2) = ECons (getData e1) e1 e2
appExpr :: Parser (Expr SourcePos)
appExpr =
buildApp <$> getPosition <*> appHead <*> many simpleExpr
where
appHead = choice
[ EFst <$> getPosition <*> (kwFst *> simpleExpr)
, ESnd <$> getPosition <*> (kwSnd *> simpleExpr)
, simpleExpr
]
buildApp pos = foldl (EApp pos)
simpleExpr :: Parser (Expr SourcePos)
simpleExpr = choice
[ setPos <$> getPosition <*> parens tokenParser parenExpr
, listExpr
, ENum <$> getPosition <*> natural tokenParser
, EBool <$> getPosition <*> boolConst
, EVar <$> getPosition <*> ident
]
parenExpr :: Parser (Expr SourcePos)
parenExpr =
buildParenExpr <$> expr `sepBy` opComma <*> getPosition
where
buildParenExpr [] pos = EUnit pos
buildParenExpr es _ = foldr1 buildPair es
buildPair e1 e2 = EPair (getData e1) e1 e2
listExpr :: Parser (Expr SourcePos)
listExpr =
mkList
<$> getPosition
<*> brackets tokenParser ((,) <$> expr `sepBy` opComma <*> getPosition)
<*> (opColon *> typeExpr)
where
mkList p1 (es, p2) tp =
setPos p1 (foldr buildList (ENil p2 tp) es)
buildList e1 e2 = ECons (getData e1) e1 e2
boolConst = choice
[ kwTrue *> return True
, kwFalse *> return False
]
-- Helper functions
opLeftAssoc op exp =
buildExpr <$> getPosition <*> exp <*> cont
where
cont = choice
[ contExpr <$> op <*> exp <*> cont
, return (const id)
]
contExpr o e2 f pos e1 = f pos (EBinary pos o e1 e2)
opNoAssoc op exp =
buildExpr <$> getPosition <*> exp <*> cont
where
cont = choice
[ buildBinary <$> op <*> exp
, return (const id)
]
buildBinary o e2 pos e1 = EBinary pos o e1 e2
buildExpr pos e1 f = f pos e1
opUnary op exp = p
where
p :: Parser (Expr SourcePos)
p = choice
[ EUnary <$> getPosition <*> op <*> p
, exp
]
-- Main function
parseProgram :: SourceName -> String -> Either ParseError (Program SourcePos)
parseProgram = parse program