-
Notifications
You must be signed in to change notification settings - Fork 19
/
parser.mly
321 lines (266 loc) · 7.91 KB
/
parser.mly
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
%token <Symbol.t> IDENT
%token <int> INT
%token EOF NL
%token LPAR RPAR LBRACE RBRACE LBRACK RBRACK
%token DEF DO END CONS MATCH CASE
%token TYPE REC ANY NOTHING ARROW
%token COMMA SEMI COLON DOT AND OR EQUALS UNDER
%token LET TRUE FALSE IF THEN ELSE
%token EQEQUALS LT GT LTE GTE PLUS MINUS
%token SUBSUME
%right EQUALS
%right ARROW
%right AND
%right OR
%nonassoc EQEQUALS
%nonassoc LT
%nonassoc GT
%nonassoc LTE
%nonassoc GTE
%left PLUS
%left MINUS
%right CONS
%{
open Variance
open Typector
open Types
open Typecheck
open Exp
open Location
%}
%start <Exp.exp> prog
%start <Exp.modlist> modlist
%start <Typector.typeterm> onlytype
%start <Typector.typeterm * Typector.typeterm> subsumption
%parameter <L : Location.Locator>
%%
%inline located(X): e = X { (L.pos ($startpos(e), $endpos(e)), e) }
%inline mayfail(X): e = X { Some e } | error { None }
%inline nofail(X): e = X { Some e }
prog:
e = lambda_exp; EOF { e }
%inline onl: NL* { () }
snl:
| NL; onl { () }
| onl; SEMI; onl { () }
(* List with optional trailing seperator and error handling *)
sep_list(Sep, Item):
|
{ [] }
| x = Item { [x] }
| x = Item; Sep; xs = sep_list(Sep, Item) { x :: xs }
| error; Sep; xs = sep_list(Sep, Item) { xs }
modlist:
| onl; e = sep_list(snl, located(moditem)); EOF { e }
moditem:
| LET; v = IDENT; EQUALS; onl; e = lambda_exp { MLet (v, e) }
| DEF; f = IDENT; LPAR; p = params; RPAR; e = funbody { MDef (f, p, e) }
| TYPE; n = IDENT; args = loption(delimited(LBRACK,
separated_nonempty_list(COMMA, typeparam), RBRACK));
EQUALS; t = typeterm
{ MType (n, args, t) }
| TYPE; n = IDENT; args = loption(delimited(LBRACK,
separated_nonempty_list(COMMA, typeparam), RBRACK));
{ MOpaqueType (n, args) }
funbody: e = located(mayfail(funbody_r)) { e }
funbody_r:
| e = funbody_code_r { e }
| COLON; t = typeterm; e = funbody_code { Typed (e, t) }
%inline funbody_code: e = located(mayfail(funbody_code_r)) { e }
funbody_code_r:
| EQUALS; onl; e = lambda_exp_r { e }
| DO; e = block_r; END { e }
(* blocks contain at least one expression and eat surrounding newlines *)
block: e = located(mayfail(block_r)) { e }
block_r:
| onl; e = block_exp_r
{ e }
%inline block_exp: e = located(nofail(block_exp_r)) { e }
block_exp_r:
| LET; v = IDENT; EQUALS; onl; e1 = lambda_exp; snl; e2 = block_exp
{ Let (v, e1, e2) }
| e1 = lambda_exp; snl; e2 = block_exp
{ Seq (e1, e2) }
| e1 = lambda_exp; onl; e2 = located(nofail(terminating_semi)); onl
{ Seq (e1, e2) }
| e = lambda_exp_r; onl
{ e }
%inline terminating_semi: SEMI { Unit }
%inline lambda_exp: e = located(nofail(lambda_exp_r)) { e }
lambda_exp_r:
| LT; ps = params; GT; onl; e = lambda_exp
{ Lambda (ps, e) }
| DO; e = block_r; END
{ e }
| e = simple_exp_r
{ e }
params: p = separated_list(COMMA, located(paramtype)) { p }
paramtype:
| p = param
{ p, None }
| p = param; COLON; t = typeterm
{ p, Some t }
param:
| v = IDENT
{ Ppositional v }
| v = IDENT; EQUALS;
{ Preq_keyword v }
| v = IDENT; EQUALS; e = term
{ Popt_keyword (v, e) }
argument:
| e = lambda_exp
{ Apositional e }
| v = IDENT; EQUALS; e = lambda_exp
{ Akeyword (v, e) }
| v = IDENT; EQUALS;
{ Akeyword (v, (L.pos ($startpos(v), $endpos(v)), Some (Var v))) }
simple_exp_r:
| e1 = simple_exp; op = binop; e2 = simple_exp
{ App((L.pos ($startpos(op), $endpos(op)), Some (Var (Symbol.intern op))), [(L.pos ($startpos(e1), $endpos(e1)), Apositional e1); (L.pos ($startpos(e1), $endpos(e2)), Apositional e2)]) }
| x = term; CONS; xs = simple_exp
{ Cons(x, xs) }
| e = term_r
{ e }
simple_exp:
| e = located(nofail(simple_exp_r))
{ e }
%inline binop:
| EQEQUALS { "(==)" }
| LT { "(<)" }
| GT { "(>)" }
| LTE { "(<=)" }
| GTE { "(>=)" }
| PLUS { "(+)" }
| MINUS { "(-)" }
tag: DOT; t = IDENT { t }
term_r:
| IF; cond = simple_exp; THEN; tcase = block; ELSE; fcase = block; END
{ If (cond, tcase, fcase) }
(*| MATCH; e = simple_exp; WITH; onl;
LBRACK; RBRACK; ARROW; onl; n = lambda_exp; onl;
OR; x = IDENT; CONS; xs = IDENT; ARROW; onl; c = lambda_exp; onl; END
{ Match (e, n, x, xs, c) }*)
| MATCH; e = separated_nonempty_list(COMMA, simple_exp); snl; c = nonempty_list(case); END
{ Match (e, c) }
| v = IDENT
{ Var v }
| LPAR; e = lambda_exp_r; RPAR
{ e }
| LPAR; e = lambda_exp; COLON; t = typeterm; RPAR
{ Typed (e, t) }
| f = term; LPAR; x = separated_list(COMMA, located(argument)); RPAR
{ App (f, x) }
| LPAR; RPAR
{ Unit }
| LBRACK; RBRACK
{ Nil }
| LBRACK; e = nonemptylist_r; RBRACK
{ e }
| e = term; DOT; f = IDENT
{ GetField (e, f) }
| i = INT
{ Int i }
| t = tag
{ Object (Some t, []) }
| t = tag; LBRACE; o = separated_list(COMMA, objfield(lambda_exp)); RBRACE
{ Object (Some t, o) }
| LBRACE; o = separated_list(COMMA, objfield(lambda_exp)); RBRACE
{ Object (None, o) }
| TRUE
{ Bool true }
| FALSE
{ Bool false }
term:
| t = located(nofail(term_r))
{ t }
%inline objfield(body):
| v = IDENT; EQUALS
{ v, (L.pos ($startpos(v), $endpos(v)), Some (Var v)) }
| v = IDENT; EQUALS; e = body
{ v, e }
%inline objfield_pat(body):
| v = IDENT; EQUALS
{ v, (L.pos ($startpos(v), $endpos(v)), Some (PVar v)) }
| v = IDENT; EQUALS; e = body
{ v, e }
nonemptylist_r:
| x = lambda_exp
{ Cons(x, (let (l, _) = x in l, Some Nil)) }
| x = lambda_exp; COMMA; xs = nonemptylist
{ Cons(x, xs) }
%inline nonemptylist:
| e = located(nofail(nonemptylist_r))
{ e }
(* Pattern matching *)
pat_r:
| UNDER
{ PWildcard }
| v = IDENT
{ PVar v }
| t = tag
{ PObject(Some t, []) }
| t = tag; LBRACE; o = separated_list(COMMA, objfield_pat(pat)); RBRACE
{ PObject(Some t, o) }
| LBRACE; o = separated_list(COMMA, objfield_pat(pat)); RBRACE
{ PObject(None, o) }
| p1 = pat; OR; p2 = pat
{ PAlt (p1, p2) }
| LPAR; p = pat_r; RPAR
{ p }
| n = INT
{ PInt n }
pat:
| p = located(nofail(pat_r)) { p }
case_r: (* what's a good syntax here? snl for onl? *)
| p = separated_nonempty_list(COMMA, pat);
{ p }
case: CASE; ps = located(case_r); snl; e = lambda_exp; snl { ps, e }
subsumption:
| t1 = typeterm; SUBSUME; t2 = typeterm; EOF { (t1, t2) }
onlytype:
| t = typeterm; EOF { t }
variance:
| PLUS { VPos }
| MINUS { VNeg }
| PLUS; MINUS { VNegPos }
| MINUS; PLUS { VNegPos }
typearg:
| PLUS; t = typeterm { VarSpec (APos t) }
| MINUS; t = typeterm { VarSpec (ANeg t) }
| MINUS; s = typeterm; PLUS; t = typeterm
| PLUS; t = typeterm; MINUS; s = typeterm { VarSpec (ANegPos (s, t)) }
| t = typeterm { VarUnspec t }
typeparam:
| v = option(variance); x = IDENT { TParam (v, x) }
| UNDER { TNoParam }
objtype:
| v = IDENT; COLON; t = typeterm
{ (v, fun _ -> t) }
(* FIXME: detect duplicate parameters *)
funtype:
| RPAR; { [] }
| t = typeterm; COMMA; ts = funtype { (None, t) :: ts }
| t = typeterm; RPAR { [None, t] }
| v = IDENT; COLON; t = typeterm; COMMA; ts = funtype { (Some v, t) :: ts }
| v = IDENT; COLON; t = typeterm; RPAR { [Some v, t] }
typeterm:
| v = IDENT; LBRACK; ps = separated_nonempty_list(COMMA, typearg); RBRACK
{ TNamed (v, ps) }
| v = IDENT
{ TNamed (v, []) }
(* funtype includes its closing paren as a hack to avoid a conflict *)
| LPAR; ts = funtype; ARROW; tr = typeterm
{ TCons (ty_fun
(ts |> List.filter (function (None, _) -> true | _ -> false) |> List.map (fun (_, t) -> fun _ -> t))
(ts |> List.fold_left (fun s (v, t) -> match v with Some v -> Typector.SMap.add v ((fun _ -> t), true) s | None -> s) Typector.SMap.empty)
(fun _ -> tr)
(L.pos ($startpos, $endpos))) }
| ANY { TZero Neg }
| NOTHING { TZero Pos }
| LBRACE; o = separated_list(COMMA, objtype); RBRACE
{ TCons (ty_obj_l o (L.pos ($startpos, $endpos))) }
| t1 = typeterm; p = meetjoin; t2 = typeterm { TAdd (p, t1, t2) } %prec AND
| REC; v = IDENT; EQUALS; t = typeterm { TRec (v, t) }
| UNDER { TWildcard }
| LPAR; t = typeterm; RPAR { t }
%inline meetjoin : AND { Variance.Neg } | OR { Variance.Pos }