-
Notifications
You must be signed in to change notification settings - Fork 6
/
Parser.hs
564 lines (455 loc) · 15.3 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
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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
module Parser where
import Control.Monad (liftM)
import Text.ParserCombinators.Parsec hiding (satisfy)
import Text.ParserCombinators.Parsec.Expr
import Lexer
data TopDecl = Decl Decl
| DataDecl (Context, String, [String], [Constr])
| TypeDecl (String, [String], Type)
| ClassDecl (Context, String, Type, [Decl])
| InstDecl (Context, String, Type, [Decl])
| ImpDecl
deriving Show
type Constr = (String, [Type])
type Context = [(String, Type)] -- Eq a : [("Eq", TyVar "a")]
data Decl = VarDecl (String, [Pat], Rhs)
| PatBind (Pat, Rhs)
| SigDecl ([String], Context, Type)
deriving Show
data Rhs = Rhs Expr [Decl]
| Guarded [(Expr, Expr)] [Decl]
deriving Show
data Type = TyTuple [Type]
| TyVar String
| TyCon String
| TyAp Type Type
deriving Show
data Expr = Var String
| Con String
| Tuple [Expr]
| LitInt Int
| LitChar String
| LitStr String
| Ap Expr Expr
| Let [Decl] Expr
| Lambda [Pat] Expr
| ESign Expr Context Type
| Case Expr [(Pat, Rhs)]
deriving Show
data Pat = PVar String
| PCon String [Pat]
| PTuple [Pat]
| PInt Int
| PChar String
| PStr String
| AsPat String Pat
| Wildcard
deriving Show
data Fixity = InfixN Int | InfixL Int | InfixR Int deriving Eq
type HsParser a = GenParser (Token, SourcePos) () a
-----------------------------------------------------------------------------
-- declarations
program :: HsParser [TopDecl]
program = braceList topdecl
topdecl :: HsParser TopDecl
topdecl = datadecl <|> typedecl <|> classdecl <|> instdecl <|> liftM Decl decl <|> impdecl
impdecl :: HsParser TopDecl
impdecl = do tokenImport
_ <- sepBy1 conid varsym
skipMany tokenHiding
skipMany (parenList var)
return ImpDecl
datadecl :: HsParser TopDecl
datadecl = do tokenData
(ctx, (c, vs)) <- context_simpletype
tokenEq
cs <- constrs
return (DataDecl (ctx, c, vs, cs))
typedecl :: HsParser TopDecl
typedecl = do tokenType
(c, vs) <- simpletype
tokenEq
t <- type_
return (TypeDecl (c, vs, t))
classdecl :: HsParser TopDecl
classdecl = do tokenClass
(ctx, (id, t)) <- qualType scontext test
decls <- whereList cdecl
return (ClassDecl (ctx, id, t, decls))
where test (TyAp (TyCon id) t@(TyVar _)) = return (id, t)
test x = unexpected (show x)
instdecl :: HsParser TopDecl
instdecl = do tokenInstance
(ctx, (id, t)) <- qualType scontext pred
decls <- whereList idecl
return (InstDecl (ctx, id, t, decls))
where pred (TyAp (TyCon id) t) = do { t' <- test inst t; return (id, t) }
pred t = unexpected (show t)
inst (TyCon _) = True
inst (TyAp t (TyVar _)) = inst t
inst (TyTuple []) = True
inst (TyTuple (TyVar _ : ts)) = inst (TyTuple ts)
decls :: HsParser [Decl]
decls = braceList decl
decl :: HsParser Decl
decl = gendecl
<|> do (id, pats) <- try funlhs
r <- rhs
return (VarDecl (id, pats, r))
<|> do pat <- opPat
r <- rhs
return (PatBind (pat, r))
cdecl :: HsParser Decl
cdecl = gendecl <|> idecl
idecl :: HsParser Decl
idecl = do (id, pats) <- funlhs
r <- rhs
return (VarDecl (id, pats, r))
gendecl :: HsParser Decl
gendecl = do vs <- try varsCoco
(ctx, t) <- context_type
return (SigDecl (vs, ctx, t))
-- <|> fixity
whereList :: HsParser a -> HsParser [a]
whereList p = option [] (do { tokenWhere; braceList p })
varsCoco :: HsParser [String]
varsCoco = do { vs <- vars; tokenCoco; return vs }
rhs :: HsParser Rhs
rhs = do tokenEq
e <- expr
ds <- whereDecls
return (Rhs e ds)
<|> do gds <- many1 (guard tokenEq)
ds <- whereDecls
return (Guarded gds ds)
constrs :: HsParser [Constr]
constrs = sepBy1 constr tokenBar
constr :: HsParser Constr
constr = do id <- con
ts <- many atype
return (id, ts)
-- <|> infix conop <|> fielddecl
-----------------------------------------------------------------------------
-- types
context_type :: HsParser (Context, Type)
context_type = qualType context return
context_simpletype :: HsParser (Context, (String, [String]))
context_simpletype = qualType context toSimpletype
qualType :: (Type -> HsParser Context) -> (Type -> HsParser a)
-> HsParser (Context, a)
qualType toContext toType =
do t1 <- type_
do tokenImply
ctx <- toContext t1
t2 <- type_
t <- toType t2
return (ctx, t)
<|> do { t <- toType t1; return ([], t) }
context :: Type -> HsParser Context
context (TyTuple ts) = mapM class_ ts
context t = mapM class_ [t]
scontext :: Type -> HsParser Context
scontext (TyTuple ts) = mapM simpleclass ts
scontext t = mapM simpleclass [t]
class_ :: Type -> HsParser (String, Type)
class_ (TyAp (TyCon cls) t) = if test t then return (cls, t)
else unexpected (show t)
where test (TyVar _) = True
test (TyAp l _) = test l
test _ = False
class_ t = unexpected (show t)
simpleclass :: Type -> HsParser (String, Type)
simpleclass (TyAp (TyCon cls) t@(TyVar var)) = return (cls, t)
simpleclass t = unexpected (show t)
toSimpletype :: Type -> HsParser (String, [String])
toSimpletype (TyCon con) = return (con, [])
toSimpletype (TyAp l (TyVar v)) = do (c, vs) <- toSimpletype l
return (c, vs++[v])
toSimpletype t = unexpected (show t)
simpletype :: HsParser (String, [String])
simpletype = do c <- conid
vs <- many varid
return (c, vs)
type_ :: HsParser Type
type_ = chainr1 btype ap
where ap = do { tokenRArrow; return arrow }
btype :: HsParser Type
btype = chainl1 atype (return TyAp)
atype :: HsParser Type
atype = do { t <- brackets type_; return (TyAp (TyCon "[]") t) }
<|> do ts <- parenList type_
case ts of
[t] -> return t
_ -> return (TyTuple ts)
<|> qtycon
<|> tyvar
qtycon :: HsParser Type
qtycon = do { i <- conid; return (TyCon i) }
tyvar :: HsParser Type
tyvar = do { i <- varid; return (TyVar i) }
-----------------------------------------------------------------------------
-- expressions
expr :: HsParser Expr
expr = do e <- opExp
option e $ do tokenCoco
(ctx, t) <- context_type
return (ESign e ctx t)
opExp :: HsParser Expr
opExp = buildExpressionParser (opTable qop) exp10
opTable op = [[Infix (op (InfixN n)) AssocNone,
Infix (op (InfixL n)) AssocLeft,
Infix (op (InfixR n)) AssocRight] | n <- reverse [0..9]]
qop :: Fixity -> HsParser (Expr -> Expr -> Expr)
qop f = qvarop f <|> qconop f
qvarop :: Fixity -> HsParser (Expr -> Expr -> Expr)
qvarop f = do { op <- varsymF f; return (ap2 (Var op)) }
qconop :: Fixity -> HsParser (Expr -> Expr -> Expr)
qconop f = do { op <- consymF f; return (ap2 (Con op)) }
exp10 :: HsParser Expr
exp10 = do tokenLambda
ps <- many1 apat
tokenRArrow
e <- expr
return (Lambda ps e)
<|> do { tokenLet; ds <- decls; tokenIn; e <- expr; return (Let ds e) }
<|> do tokenIf; e1 <- expr
tokenThen; e2 <- expr
tokenElse; e3 <- expr
return (bltinApp "IF" [e1, e2, e3])
<|> do { tokenCase; e <- expr; tokenOf; a <- alts; return (Case e a) }
<|> do { tokenDo; fail "not implemented" }
<|> fexp
fexp :: HsParser Expr
fexp = chainl1 aexp (return Ap)
aexp :: HsParser Expr
aexp = do { i <- qvar; return (Var i) }
<|> do { c <- gcon; return (Con c) }
<|> do { i <- litInt; return (LitInt i) }
<|> do { c <- litChar; return (LitChar c) }
<|> do { s <- litStr; return (LitStr s) }
<|> do es <- parenList expr
if length es == 1 then return (head es) else return (Tuple es)
<|> do es <- bracketList expr
return (foldr cons (Con "[]") es)
-- まだいろいろ足りない
alts :: HsParser [(Pat, Rhs)]
alts = braceList alt
alt :: HsParser (Pat, Rhs)
alt = do{ p <- pat
; do tokenRArrow
e <- expr
ds <- whereDecls
return (p, Rhs e ds)
<|> do gds <- many1 (guard tokenRArrow)
ds <- whereDecls
return (p, Guarded gds ds)
}
whereDecls :: HsParser [Decl]
whereDecls = option [] (do { tokenWhere; decls })
guard :: HsParser Token -> HsParser (Expr, Expr)
guard sep = do tokenBar
gd <- opExp
sep
e <- expr
return (gd, e)
-----------------------------------------------------------------------------
-- patterns
funlhs :: HsParser (String, [Pat])
funlhs = do v <- var
ps <- many apat
return (v, ps)
pat :: HsParser Pat
pat = opPat
opPat :: HsParser Pat
opPat = buildExpressionParser (opTable qconopPat) pat10
qconopPat :: Fixity -> HsParser (Pat -> Pat -> Pat)
qconopPat f = do { op <- consymF f; return (\l r -> PCon op [l, r]) }
pat10 :: HsParser Pat
pat10 = do c <- gcon
ps <- many apat
return (PCon c ps)
<|> apat
apat :: HsParser Pat
apat = do v <- var
option (PVar v) (do { tokenAt; p <- apat; return (AsPat v p) })
<|> do { c <- gcon; return (PCon c []) }
<|> do { i <- litInt; return (PInt i) }
<|> do { c <- litChar; return (PChar c) }
<|> do { s <- litStr; return (PStr s) }
<|> do { tokenWildcard; return Wildcard }
<|> do ps <- parenList pat
if length ps == 1 then return (head ps) else return (PTuple ps)
<|> do ps <- bracketList pat
return (foldr pcons (PCon "[]" []) ps)
-- <|> ...
-----------------------------------------------------------------------------
-- primitive constructs
gcon :: HsParser String
gcon = qcon
qcon :: HsParser String
qcon = con
con :: HsParser String
con = try (parens consym) <|> conid
qconid :: HsParser String
qconid = conid
conid :: HsParser String
conid = token' test
where test (TokenConId id) = Just id
test _ = Nothing
consym :: HsParser String
consym = token' test
where test (TokenConOp id) = Just id
test _ = Nothing
consymF :: Fixity -> HsParser String
consymF f = token' test
where test (TokenConOp id) | fixity id == f = Just id
test _ = Nothing
vars :: HsParser [String]
vars = sepBy1 var tokenComma
qvar :: HsParser String
qvar = var
var :: HsParser String
var = try (parens varsym) <|> varid
varid :: HsParser String
varid = token' test
where test (TokenId id) = Just id
test _ = Nothing
varsym :: HsParser String
varsym = token' test
where test (TokenOp id) = Just id
test _ = Nothing
varsymF :: Fixity -> HsParser String
varsymF f = token' test
where test (TokenOp id) | fixity id == f = Just id
test _ = Nothing
-- fixity and precedence are now hardcoded
fixity :: String -> Fixity
fixity "." = InfixR 9
fixity "!!" = InfixL 9
fixity "^" = InfixR 8
fixity "^^" = InfixR 8
fixity "**" = InfixR 8
fixity "*" = InfixL 7
fixity "/" = InfixL 7
fixity "+" = InfixL 6
fixity "-" = InfixL 6
fixity ":" = InfixR 5
fixity "++" = InfixR 5
fixity "==" = InfixN 4
fixity "/=" = InfixN 4
fixity "<" = InfixN 4
fixity "<=" = InfixN 4
fixity ">=" = InfixN 4
fixity ">" = InfixN 4
fixity "&&" = InfixR 3
fixity "||" = InfixR 2
fixity ">>" = InfixL 1
fixity ">>="= InfixL 1
fixity "=<<"= InfixR 1
fixity "$" = InfixR 0
fixity "$!" = InfixR 0
fixity _ = InfixL 9
litInt :: HsParser Int
litInt = token' test
where test (TokenInt i) = Just i
test _ = Nothing
litChar :: HsParser String
litChar = token' test
where test (TokenChar i) = Just i
test _ = Nothing
litStr :: HsParser String
litStr = token' test
where test (TokenStr i) = Just i
test _ = Nothing
tokenIf = hsToken TokenIf
tokenThen = hsToken TokenThen
tokenElse = hsToken TokenElse
tokenLet = hsToken TokenLet
tokenIn = hsToken TokenIn
tokenCase = hsToken TokenCase
tokenOf = hsToken TokenOf
tokenDo = hsToken TokenDo
tokenData = hsToken TokenData
tokenType = hsToken TokenType
tokenClass = hsToken TokenClass
tokenInstance = hsToken TokenInstance
tokenWhere = hsToken TokenWhere
tokenImport = hsToken TokenImport
tokenHiding = hsToken TokenHiding
tokenDotDot = hsToken TokenDotDot
tokenCoco = hsToken TokenCoco
tokenEq = hsToken TokenEq
tokenLambda = hsToken TokenLambda
tokenBar = hsToken TokenBar
tokenLArrow = hsToken TokenLArrow
tokenRArrow = hsToken TokenRArrow
tokenAt = hsToken TokenAt
tokenTilde = hsToken TokenTilde
tokenImply = hsToken TokenImply
tokenLParen = hsToken TokenLParen
tokenRParen = hsToken TokenRParen
tokenLBrace = hsToken TokenLBrace
tokenRBrace = hsToken TokenRBrace
tokenLBracket = hsToken TokenLBracket
tokenRBracket = hsToken TokenRBracket
tokenComma = hsToken TokenComma
tokenSemicolon = hsToken TokenSemicolon
tokenWildcard = hsToken TokenWildcard
-----------------------------------------------------------------------------
-- utilities
token' :: (Token -> Maybe a) -> HsParser a
token' test = token showToken posToken testToken
where showToken (tok, pos) = show tok
posToken (tok, pos) = pos
testToken (tok, pos) = test tok
satisfy :: (Token -> Bool) -> HsParser Token
satisfy test = token' (\t -> if test t then Just t else Nothing)
hsToken :: Token -> HsParser Token
hsToken tok = satisfy (== tok)
parens :: HsParser a -> HsParser a
parens = hsBetween tokenLParen tokenRParen
brackets :: HsParser a -> HsParser a
brackets = hsBetween tokenLBracket tokenRBracket
parenList :: HsParser a -> HsParser [a]
parenList p = hsBetween tokenLParen tokenRParen (sepBy p tokenComma)
braceList :: HsParser a -> HsParser [a]
braceList p = hsBetween tokenLBrace tokenRBrace (sepBy p tokenSemicolon)
bracketList :: HsParser a -> HsParser [a]
bracketList p = hsBetween tokenLBracket tokenRBracket (sepBy p tokenComma)
hsBetween :: HsParser Token -> HsParser Token -> HsParser a -> HsParser a
hsBetween open close = between open close
test :: Show a => (a -> Bool) -> a -> HsParser a
test f a = if f a then return a else unexpected (show a)
-----------------------------------------------------------------------------
-- expression constructors
arrow :: Type -> Type -> Type
arrow l r = TyAp (TyAp (TyCon "(->)") l) r
ap2 :: Expr -> Expr -> Expr -> Expr
ap2 op l r = Ap (Ap op l) r
bltinApp :: String -> [Expr] -> Expr
bltinApp i es = foldl Ap (Var i) es
cons :: Expr -> Expr -> Expr
cons e1 e2 = (Con ":") `Ap` e1 `Ap` e2
pcons :: Pat -> Pat -> Pat
pcons p1 p2 = PCon ":" [p1, p2]
-----------------------------------------------------------------------------
-- runner
parse :: [(Token, SourcePos)] -> [TopDecl]
parse input = case runParser program () "stdin" input of
Left err -> error ("parse error at " ++ show err)
Right tds -> tds
-----------------------------------------------------------------------------
-- test
{-
main = do source <- argf
parseTest program (lexer "argf" source)
argf :: IO String
argf = do argv <- getArgs
if argv == []
then getContents
else do conts <- mapM getFileContents argv
return (concat conts)
getFileContents :: String -> IO String
getFileContents fname = do handle <- IO.openFile fname IO.ReadMode
IO.hGetContents handle
-}