-
Notifications
You must be signed in to change notification settings - Fork 84
/
parse.hs
478 lines (394 loc) · 11.3 KB
/
parse.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
module Parse where
import Control.Monad
import Text.Parsec
import Text.Parsec.Prim
import Text.Parsec.Pos
import Data.List as List
import Data.Char as Char
import Tokens
import Ast
import Data.Maybe
import Data.Functor.Identity as DFI
o f g x = f (g x)
destLongidT (LongidT x y,pos) = Just (ModN x pos,y,pos)
destLongidT _ = Nothing
destAlphaT (AlphaT x,pos) = Just (x,pos)
destAlphaT _ = Nothing
destSymbolT (SymbolT x,pos) = Just (x,pos)
destSymbolT _ = Nothing
destAlphaSym (AlphaT x,pos) = Just (x,pos)
destAlphaSym (SymbolT x,pos) = Just (x,pos)
destAlphaSym _ = Nothing
destIntT (IntT i,pos) = Just (i,pos)
destIntT _ = Nothing
destStringT (StringT s,pos) = Just (s,pos)
destStringT _ = Nothing
destTyvarT (TyvarT tv,pos) = Just (TvarN tv pos)
destTyvarT _ = Nothing
tok :: ((Token,SourcePos) -> Maybe a) -> Parsec [(Token,SourcePos)] u a
tok f = token (\t -> show (fst t)) snd f
tokeq t = tok (\(x,pos) -> if t == x then Just pos else Nothing)
linfix exp op = exp `chainl1` fmap (\(t,pos) e1 e2 -> App Opapp [App Opapp [Var (Short (VarN t pos)), e1], e2]) op
longV =
tok (\t -> do (str,s,pos) <- destLongidT t;
guard (s /= "" && (not (isAlpha (List.head s)) || not (isUpper (List.head s))));
return (Long str (VarN s pos)))
nV' c =
tok (\t -> do (s,pos) <- destAlphaT t;
guard (not (s `elem` ["before", "div", "mod", "o", "true", "false","ref", "nil"]) &&
s /= "" &&
not (isUpper (List.head s)));
return (c s pos))
<|>
tok (\t -> do (s,pos) <- destSymbolT t;
guard (not (s `elem` ["+", "-", "/", "<", ">", "<=", ">=", "<>", ":=", "*", "::", "@"]));
return (c s pos))
nV = nV' VarN
nVlist1 = many1 nV
nFQV = fmap Short nV <|> longV
mkApp e1 e2 =
case e1 of
Con (Just (Short (ConN "ref" pos))) [] pos' -> App Opapp [Var (Short (VarN "ref" pos)), e2]
Con s [] pos ->
case e2 of
Con Nothing es pos' -> Con s es pos
_ -> Con s [e2] pos -- Because e2 is an Ebase
_ -> App Opapp [e1,e2]
nEapp = do exps <- many1 nEbase;
return (case exps of
[e] -> e
(e:es) -> List.foldl mkApp e es)
mk_op tok str =
tokeq tok >>= (\pos -> return (str,pos))
nMultOps =
(mk_op StarT "*")
<|>
(mk_op (SymbolT "/") "/")
<|>
(mk_op (AlphaT "mod") "mod")
<|>
(mk_op (AlphaT "div") "div")
nAddOps =
(mk_op (SymbolT "+") "+")
<|>
(mk_op (SymbolT "-") "-")
nRelOps = choice (mk_op EqualsT "=" : List.map (\s -> mk_op (SymbolT s) s) ["<", ">", "<=", ">=", "<>"])
nListOps = choice (List.map (\s -> mk_op (SymbolT s) s) ["::", "@"])
nCompOps =
(mk_op (SymbolT ":=") ":=")
<|>
(mk_op (AlphaT "o") "o")
eseq_encode pos [] = Con Nothing [] pos
eseq_encode pos [e] = e
eseq_encode pos (e:es) = Let Nothing e (eseq_encode pos es)
nEbaseParen =
do pos <- tokeq LparT;
((tokeq RparT >> return (Con Nothing [] pos))
<|>
do e1 <- nE;
choice [tokeq RparT >> return e1,
do tokeq CommaT;
es <- sepBy1 nE (tokeq CommaT);
tokeq RparT;
return (Con Nothing (e1:es) pos),
do tokeq SemicolonT;
es <- nEseq;
tokeq RparT;
return (eseq_encode pos (e1:es))])
do_let_dec (Left (x,e1)) e2 = Let x e1 e2
do_let_dec (Right funs) e = Letrec funs e
nEbase =
do (i,pos) <- tok destIntT;
return (Lit (IntLit i) pos)
<|>
do (s,pos) <- tok destStringT;
return (Lit (StrLit s) pos)
<|>
nEbaseParen
<|>
do tokeq LetT;
lets <- nLetDecs;
pos <- tokeq InT;
es <- nEseq;
tokeq EndT;
return (List.foldr do_let_dec (eseq_encode pos es) lets)
<|>
do pos <- tokeq LbrackT;
es <- sepBy nE (tokeq CommaT);
tokeq RbrackT;
return (List.foldr (\e ast -> Con (Just (Short (ConN "::" pos))) [e, ast] pos) (Con (Just (Short (ConN "nil" pos))) [] pos) es)
<|>
fmap Var nFQV
<|>
do n <- nConstructorName;
return (Con (Just n) [] (getPos n))
nEseq = sepBy1 nE (tokeq SemicolonT)
nEmult = linfix nEapp nMultOps
nEadd = linfix nEmult nAddOps
nElist = nEadd `chainr1` fmap (\(t,pos) e1 e2 -> if t == "::" then
Con (Just (Short (ConN t pos))) [e1,e2] pos
else
App Opapp [App Opapp [Var (Short (VarN t pos)), e1], e2])
nListOps
nErel = linfix nElist nRelOps;
nEcomp = linfix nErel nCompOps
nEbefore = linfix nEcomp (mk_op (AlphaT "before") "before")
nEtyped =
do e <- nEbefore;
option e (tokeq ColonT >> nType >> return e)
nElogicAND = nEtyped `chainl1` (tokeq AndalsoT >>= (\pos -> return (\e1 e2 -> Log (And pos) e1 e2)))
nElogicOR = nElogicAND `chainl1` (tokeq OrelseT >>= (\pos -> return (\e1 e2 -> Log (Or pos) e1 e2)))
nEhandle =
do e1 <- nElogicOR;
option e1 (do tokeq HandleT;
pes <- nPEs;
return (Handle e1 pes))
nE =
(tokeq RaiseT >> fmap Raise nE)
<|>
nEhandle
<|>
do tokeq IfT;
e1 <- nE;
tokeq ThenT;
e2 <- nE;
tokeq ElseT;
e3 <- nE;
return (If e1 e2 e3)
<|>
do pos <- tokeq FnT;
x <- nV;
tokeq DarrowT;
e <- nE;
return (Fun x e pos)
<|>
do tokeq CaseT;
e <- nE;
tokeq OfT;
pes <- nPEs;
return (Mat e pes)
nE' =
(tokeq RaiseT >> fmap Raise nE)
<|>
nEhandle
<|>
do tokeq IfT;
e1 <- nE;
tokeq ThenT;
e2 <- nE;
tokeq ElseT;
e3 <- nE';
return (If e1 e2 e3)
nPEs = choice [try (do pe <- nPE'; tokeq BarT; pes <- nPEs; return (pe:pes)),
fmap (\x -> [x]) nPE]
nPE = do p <- nPattern;
tokeq DarrowT;
e <- nE;
return (p,e)
nPE' = do p <- nPattern;
tokeq DarrowT;
e <- nE';
return (p,e)
nAndFDecls = sepBy1 nFDecl (tokeq AndT)
multi_funs x [y] e = (x,y,e)
multi_funs x (y:ys) e = (x,y,List.foldr (\v e -> Fun v e (getPos v)) e ys)
nFDecl = do x <- nV;
ys <- nVlist1;
tokeq EqualsT;
e <- nE;
return (multi_funs x ys e)
nType = nPType `chainr1` (tokeq ArrowT >> return (\t1 t2 -> Tapp [t1,t2] TC_fn))
nDType = do t <- nTbase;
ts <- many nTyOp;
return (List.foldr (\tc t -> Tapp [t] (TC_name tc)) t ts)
nTbase =
fmap Tvar (tok destTyvarT)
<|>
fmap (\tc -> (Tapp [] (TC_name tc))) nTyOp
<|>
do tokeq LparT;
t <- nType;
((tokeq RparT >> return t)
<|>
do tokeq CommaT;
ts <- nTypeList1;
tokeq RparT;
tc <- nTyOp;
return (Tapp (t:ts) (TC_name tc)))
nTypeList1 = sepBy1 nType (tokeq CommaT)
nTyOp =
fmap (Short `o` uncurry TypeN) nUQTyOp
<|>
fmap (\(x,y,pos) -> Long x (TypeN y pos)) (tok destLongidT)
nUQTyOp = tok destAlphaSym
nPType = fmap tuplify (sepBy1 nDType (tokeq StarT))
tuplify [ty] = ty
tuplify tys = Tapp tys TC_tup
nTypeName =
fmap (\n -> (uncurry TypeN n,[])) nUQTyOp
<|>
do tokeq LparT;
tvs <- nTyVarList;
tokeq RparT;
n <- nUQTyOp;
return (uncurry TypeN n, tvs)
<|>
do tv <- tok destTyvarT;
n <- nUQTyOp;
return (uncurry TypeN n, [tv])
nTyVarList = sepBy1 (tok destTyvarT) (tokeq CommaT)
nTypeDec =
tokeq DatatypeT >> sepBy1 nDtypeDecl (tokeq AndT)
nDtypeDecl =
do (tn,tvs) <- nTypeName;
tokeq EqualsT;
ctors <- sepBy1 nDconstructor (tokeq BarT);
return (tvs,tn,ctors)
detuplify (Tapp args TC_tup) = args
detuplify ty = [ty]
nDconstructor =
do cn <- nUQConstructorName;
option (cn,[])
(do tokeq OfT;
t <- nType;
return (cn, detuplify t))
nStructName =
tok (\t -> do (s,pos) <- destAlphaT t;
guard (s /= "");
return (ModN s pos))
nUQConstructorName =
tok (\t -> do (s,pos) <- destAlphaT t;
guard (s /= "" && isUpper (List.head s) || s `elem` ["true", "false", "ref", "nil"]);
return (ConN s pos))
nConstructorName =
fmap Short nUQConstructorName
<|>
tok (\t -> do (str,s,pos) <- destLongidT t;
guard (s /= "" && Char.isAlpha (List.head s) && Char.isUpper (List.head s));
return (Long str (ConN s pos)))
mk_pat_app n p =
case n of
Short (ConN "ref" pos) -> Pref p pos
_ -> case p of
Pcon Nothing ps pos' -> Pcon (Just n) ps (getPos n)
_ -> Pcon (Just n) [p] (getPos n)
-- TODO: negative numbers
-- TODO: underscore patterns
nPbase =
fmap Pvar nV
<|>
do n <- nConstructorName;
return (Pcon (Just n) [] (getPos n))
<|>
do (i,pos) <- tok destIntT;
return (Plit (IntLit i) pos)
<|>
do (s,pos) <- tok destStringT;
return (Plit (StrLit s) pos)
<|>
nPtuple
<|>
(tokeq UnderbarT >>= (\pos -> return (Pvar (VarN "_" pos))))
<|>
do pos <- tokeq LbrackT;
ps <- sepBy nPattern (tokeq CommaT);
tokeq RbrackT;
return (List.foldr (\p ast -> Pcon (Just (Short (ConN "::" pos))) [p, ast] pos) (Pcon (Just (Short (ConN "nil" pos))) [] pos) ps)
nPapp = choice [try (do n <- nConstructorName;
pat <- nPbase;
return (mk_pat_app n pat)),
nPbase]
nPattern = nPapp `chainr1` fmap (\pos p1 p2 -> Pcon (Just (Short (ConN "::" pos))) [p1,p2] pos) (tokeq (SymbolT "::"))
nPtuple =
do pos <- tokeq LparT;
((tokeq RparT >> return (Pcon Nothing [] pos))
<|>
do pat <- nPattern;
r <- option pat (do tokeq CommaT
pats <- sepBy1 nPattern (tokeq CommaT)
return (Pcon Nothing (pat:pats) pos));
tokeq RparT;
return r)
nLetDec =
do tokeq ValT;
x <- nV;
tokeq EqualsT;
e <- nE;
return (Left (Just x,e))
<|>
(tokeq FunT >> fmap Right nAndFDecls)
nLetDecs =
do ld <- nLetDec; lds <- nLetDecs; return (ld : lds)
<|>
(tokeq SemicolonT >> nLetDecs)
<|> many (fail "")
nDecl =
do pos <- tokeq ValT;
p <- nPattern;
tokeq EqualsT;
e <- nE;
return (Dlet p e pos)
<|>
(tokeq FunT >> fmap Dletrec nAndFDecls)
<|>
fmap Dtype nTypeDec
<|>
(tokeq ExceptionT >> fmap (uncurry Dexn) nDconstructor)
<|>
do tokeq TypeT;
(tn,tvs) <- nTypeName;
tokeq EqualsT;
t <- nType;
return (Dtabbrev tvs tn t)
nDecls =
do d <- nDecl;
ds <- nDecls;
return (d:ds)
<|>
(tokeq SemicolonT >> nDecls)
<|>
many (fail "")
nSpecLine =
do tokeq ValT;
x <- nV;
tokeq ColonT;
t <- nType;
return (Sval x t)
<|>
(tokeq TypeT >> fmap (\(tn,tvs) -> Stype_opq tvs tn) nTypeName)
<|>
fmap Stype nTypeDec
nSpecLineList =
do s <- nSpecLine;
ss <- nSpecLineList
return (s:ss)
<|>
(tokeq SemicolonT >> nSpecLineList)
<|>
many (fail "")
nSignatureValue = between (tokeq SigT) (tokeq EndT) nSpecLineList
nOptionalSignatureAscription = optionMaybe (tokeq SealT >> nSignatureValue)
nStructure =
do tokeq StructureT;
x <- nStructName;
s_opt <- nOptionalSignatureAscription;
tokeq EqualsT;
tokeq StructT;
ds <- nDecls;
tokeq EndT;
return (Tmod x s_opt ds)
nTopLevelDec =
nStructure
<|>
fmap Tdec nDecl
nREPLTop :: ParsecT [(Token,SourcePos)] u DFI.Identity Top
nREPLTop =
do e <- nE;
pos <- tokeq SemicolonT;
return (Tdec (Dlet (Pvar (VarN "it" pos)) e pos))
<|>
do d <- nTopLevelDec;
tokeq SemicolonT;
return d
parseTop :: [(Token,SourcePos)] -> Either ParseError Top
parseTop toks = parse nREPLTop "" toks