forked from asutton/waffle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.cpp
693 lines (635 loc) · 16.6 KB
/
parser.cpp
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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
#include "parser.hpp"
#include "syntax.hpp"
#include "lang/parsing.hpp"
#include "lang/debug.hpp"
#include <iostream>
// -------------------------------------------------------------------------- //
// Parsers
//
// The grammar of the term language is laid out like much like the
// that of C or C++. That is, there are several "extension points" in
// the grammar where a term may be added. These are:
//
// primary -- Parsed at the highest precedence
// postfix -- A left-associative binary operator
// prefix -- A unary expression whose operator precedes the operand
//
// Note that the same structure exists for the type parser.
Tree* parse_primary_expr(Parser&);
Tree* parse_postfix_expr(Parser&);
Tree* parse_prefix_expr(Parser&);
Tree* parse_expr(Parser&);
Tree* parse_name(Parser&);
// Parse a name.
//
// name ::= identifier
Tree*
parse_name(Parser& p) {
if (const Token* k = parse::accept(p, identifier_tok))
return new Id_tree(k);
return nullptr;
}
// Parse a unit literal.
//
// unit-lit ::= 'unit'
Tree*
parse_unit_lit(Parser& p) {
if (const Token* k = parse::accept(p, unit_tok))
return new Lit_tree(k);
return nullptr;
}
// Parse a boolean literal.
//
// boolean-lit ::= 'true' | 'false'
Tree*
parse_boolean_lit(Parser& p) {
if (const Token* k = parse::accept(p, true_tok))
return new Lit_tree(k);
if (const Token* k = parse::accept(p, false_tok))
return new Lit_tree(k);
return nullptr;
}
// Parse an integer literal.
//
// integer-literal ::= decimal-literal-token
//
// TODO: Allow for binary, octal, and hexadecimal integers.
Tree*
parse_integer_lit(Parser& p) {
if (const Token* k = parse::accept(p, decimal_literal_tok))
return new Lit_tree(k);
return nullptr;
}
// Parse a string literal.
//
// string-literal ::= string-literal-token
Tree*
parse_string_lit(Parser& p) {
if (const Token* k = parse::accept(p, string_literal_tok))
return new Lit_tree(k);
return nullptr;
}
// Parse a type literal.
//
// type-literal ::= 'Unit' | 'Bool' | 'Nat'
Tree*
parse_type_lit(Parser& p) {
if (const Token* k = parse::accept(p, unit_type_tok))
return new Lit_tree(k);
if (const Token* k = parse::accept(p, bool_type_tok))
return new Lit_tree(k);
if (const Token* k = parse::accept(p, nat_type_tok))
return new Lit_tree(k);
return nullptr;
}
// Parse a literal expression.
//
// literal-expr ::= unit-literal
// | boolean-literal
// | integer-literal
// | string-literal
// | type-literal
Tree*
parse_literal_expr(Parser& p) {
if (Tree* t = parse_unit_lit(p))
return t;
if (Tree* t = parse_boolean_lit(p))
return t;
if (Tree* t = parse_integer_lit(p))
return t;
if (Tree* t = parse_string_lit(p))
return t;
if (Tree* t = parse_type_lit(p))
return t;
return nullptr;
}
// Parse an identifer.
//
// id-expr ::= name
Tree*
parse_id_expr(Parser& p) { return parse_name(p); }
// Parse an inititializer.
//
// init-expr ::= identifier '=' expr
Tree*
parse_init_expr(Parser& p) {
if (Tree* n = parse_name(p))
if (parse::expect(p, equal_tok)) {
if (Tree* t = parse_expr(p))
return new Init_tree(n, t);
else
parse::parse_error(p) << "expected 'expr' after '='";
}
return nullptr;
}
// Parse a variable declaration in a lambda expression.
//
// parm-decl ::= identifier ':' expr
Tree*
parse_parm_decl(Parser& p) {
if (Tree* n = parse_name(p))
if (parse::expect(p, colon_tok)) {
if (Tree* t = parse_expr(p))
return new Var_tree(n, t);
else
parse::parse_error(p) << "expected 'expr' after ':'";
}
return nullptr;
}
// Parse a parameter list.
//
// parm-list ::= parm | parm-list ',' parm
Tree_seq*
parse_parm_list (Parser& p) {
Tree_seq* ts = new Tree_seq();
while (1) {
// Parse the next paramter.
if (Tree* t = parse_parm_decl(p)) {
ts->push_back(t);
} else {
parse::parse_error(p) << "expected 'parm'" ;
return nullptr;
}
// Check for the end of the list.
if (parse::next_token_is(p, rparen_tok))
break;
// Ensure that the next element is a comma.
if (not parse::expect(p, comma_tok))
return nullptr;
}
return ts;
}
// Parse a parameter clause.
//
// parm-list ::= '(' [parm-list]? ')'
Tree_seq*
parse_parm_clause(Parser& p) {
if (parse::accept(p, lparen_tok))
if (Tree_seq* ps = parse_parm_list(p))
if (parse::expect(p, rparen_tok))
return ps;
return nullptr;
}
// Parse a lambda abstraction.
//
// lambda-expr ::= '\' parm-decl ':' type '=>' term
// | '\' parm-list '=>' term
Tree*
parse_lambda_expr(Parser& p) {
if (const Token* k = parse::accept(p, backslash_tok)) {
if(Tree* v = parse_parm_decl(p)) {
if (parse::expect(p, map_tok)) {
if (Tree* t = parse_expr(p))
return new Abs_tree(k, v, t);
else
parse::parse_error(p) << "expected 'expr' after '.'";
}
} else if (Tree_seq* ps = parse_parm_clause(p)) {
if (parse::expect(p, map_tok)) {
if (Tree* t = parse_expr(p))
return new Fn_tree(k, ps, t);
else
parse::parse_error(p) << "expected 'expr' after '.'";
}
} else {
parse::parse_error(p) << "expected 'var-decl' or 'parm-clause' after '\\'";
}
}
return nullptr;
}
// Parse an element of a tuple or a variant.
//
// elem ::= var | init | expr
Tree*
parse_elem(Parser& p) {
if (parse::next_token_is(p, identifier_tok)) {
if (parse::nth_token_is(p, 1, colon_tok))
return parse_parm_decl(p);
if (parse::nth_token_is(p, 1, equal_tok))
return parse_init_expr(p);
parse::parse_error(p) << "expected ':' or '=' after 'identifier'";
return nullptr;
}
return parse_expr(p);
}
// Parse a comma separated sequence of elements in
// either a tuple or a variant.
//
// elem-list ::= elem | elem-list ',' elem
Tree_seq*
parse_elem_list(Parser& p, Token_kind close_tok) {
Tree_seq* ts = new Tree_seq();
while (true) {
// Try parsing the next element.
if (Tree* t = parse_elem(p))
ts->push_back(t);
else
return nullptr;
// Either break or continue.
if (parse::next_token_is(p, close_tok))
break;
parse::expect(p, comma_tok);
}
return ts;
}
// Parse a comma-separated sequence of expressions that are by a
// enclosed by pair of tokens.
template<typename T>
Tree*
parse_enclosed_expr(Parser& p, Token_kind open_tok, Token_kind close_tok) {
if (const Token* k = parse::accept(p, open_tok)) {
if (Tree_seq* ts = parse_elem_list(p, close_tok)) {
if (parse::expect(p, close_tok))
return new T(k, ts);
} else {
// TODO: expected after the open_token
parse::parse_error(p) << "expected 'elem-list'";
}
}
return nullptr;
}
// Parse a tuple expression.
//
// tuple-expr ::= '{' t1, ..., tn '}'
Tree*
parse_tuple_expr(Parser& p) {
return parse_enclosed_expr<Tuple_tree>(p, lbrace_tok, rbrace_tok);
}
// Parse a list expression.
//
// list-expr ::= '[' t1, ..., tn ']'
Tree*
parse_list_expr(Parser& p) {
return parse_enclosed_expr<List_tree>(p, lbracket_tok, rbracket_tok);
}
// Parse a variant expression.
//
// tuple-expr ::= '<' t1, ..., tn '>'
Tree*
parse_variant_expr(Parser& p) {
parse_enclosed_expr<Variant_tree>(p, langle_tok, rangle_tok);
}
// Parse a grouped expression.
//
// grouped-expr ::= '(' [comma-expr]? ')'s
//
// Note the this is only a grouped expression when the form
// is '(e)' where 'e' is some expression. If the expression
// is of the form '()' or '(e1, ..., en)', then this is a comma
// epxression.
Tree*
parse_grouped_expr(Parser& p) {
if (const Token* k = parse::accept(p, lparen_tok)) {
// This is a comma expression.
if (parse::accept(p, rparen_tok))
return new Comma_tree(k, new Tree_seq());
if (Tree* t = parse_expr(p)) {
// This is a grouped subexpression.
if (parse::accept(p, rparen_tok)) {
return t;
}
// This is a grouped sub-expression.
Tree_seq* ts = new Tree_seq {t};
while (parse::accept(p, comma_tok)) {
if (Tree* t = parse_expr(p))
ts->push_back(t);
else
return nullptr;
}
// Make sure we have a closing rparen.
if (parse::expect(p, rparen_tok)) {
return new Comma_tree(k, ts);
}
} else {
parse::parse_error(p) << "expected 'expr' after '('";
}
}
return nullptr;
}
// Parse a primary expression.
//
// primary-term ::= primary-lambda-term | grouped-term
Tree*
parse_primary_expr(Parser& p) {
if (Tree* t = parse_literal_expr(p))
return t;
if (Tree* t = parse_lambda_expr(p))
return t;
if (Tree* t = parse_id_expr(p))
return t;
if (Tree* t = parse_tuple_expr(p))
return t;
if (Tree* t = parse_list_expr(p))
return t;
if (Tree* t = parse_variant_expr(p))
return t;
if (Tree* t = parse_grouped_expr(p))
return t;
return nullptr;
}
// Parse an application expr.
//
// application-expr ::= postfix-expr primary-expr
Tree*
parse_application_expr(Parser& p, Tree* t1) {
if (Tree* t2 = parse_primary_expr(p))
return new App_tree(t1, t2);
return nullptr;
}
// Parse a dot expression.
//
// dot-expr ::= postfix-expr '.' primary-expr
Tree*
parse_dot_expr(Parser& p, Tree* t1) {
if (parse::accept(p, dot_tok))
if (Tree* t2 = parse_primary_expr(p))
return new Dot_tree(t1, t2);
else
parse::parse_error(p) << "expected 'primary-expr' after '.'";
return nullptr;
}
// Parse a postfix expr.
//
// postfix-expr ::= applicaiton-expr
Tree*
parse_postfix_expr(Parser& p) {
if (Tree* t1 = parse_primary_expr(p)) {
while (t1) {
if (Tree* t2 = parse_dot_expr(p, t1))
t1 = t2;
else if (Tree* t2 = parse_application_expr(p, t1))
t1 = t2;
else
break;
}
return t1;
}
return parse_primary_expr(p);
}
// Parse an if-term.
//
// if-term ::= 'if' term 'then' term 'else' term
Tree*
parse_if_expr(Parser& p) {
if (const Token* k = parse::accept(p, if_tok))
if (Tree* t1 = parse_expr(p)) {
if (parse::expect(p, then_tok))
if (Tree* t2 = parse_expr(p)) {
if (parse::expect(p, else_tok)) {
if (Tree* t3 = parse_expr(p))
return new If_tree(k, t1, t2, t3);
else
parse::parse_error(p) << "expected 'expr' after 'else'";
}
} else {
parse::parse_error(p) << "expected 'expr' after 'then'";
}
} else {
parse::parse_error(p) << "expected 'expr' after 'if'";
}
return nullptr;
}
// Parse a successor expression.
//
// succ-expr ::= 'succ' prefix-expr
Tree*
parse_succ_expr(Parser& p) {
if (const Token* k = parse::accept(p, succ_tok)) {
if (Tree* t = parse_prefix_expr(p))
return new Succ_tree(k, t);
else
parse::parse_error(p) << "expected 'prefix-expr' after 'succ'";
}
return nullptr;
}
// Parse a predecessor expression.
//
// pred-expr ::= 'pred' prefix-expr
Tree*
parse_pred_expr(Parser& p) {
if (const Token* k = parse::accept(p, pred_tok)) {
if (Tree* t = parse_prefix_expr(p))
return new Pred_tree(k, t);
else
parse::parse_error(p) << "expected 'prefix-expr' after 'pred'";
}
return nullptr;
}
// Parse an is-zero expression.
//
// iszero-expr ::= 'iszero' prefix-expr
Tree*
parse_iszero_expr(Parser& p) {
if (const Token* k = parse::accept(p, iszero_tok)) {
if (Tree* t = parse_prefix_expr(p))
return new Iszero_tree(k, t);
else
parse::parse_error(p) << "expected 'prefix-expr' after 'iszero'";
}
return nullptr;
}
// Parse a print expression.
//
// print-expr ::= 'print' expr
Tree*
parse_print_expr(Parser& p) {
if (const Token* k = parse::accept(p, print_tok)) {
if (Tree* t = parse_expr(p))
return new Print_tree(k, t);
else
parse::parse_error(p) << "expected 'expr' after 'print'";
}
return nullptr;
}
// Parse a typeof expression.
//
// typeof-expr ::= 'typeof' expr
Tree*
parse_typeof_expr(Parser& p) {
if (const Token* k = parse::accept(p, typeof_tok)) {
if (Tree* t = parse_expr(p))
return new Typeof_tree(k, t);
else
parse::parse_error(p) << "expected 'expr' after 'typeof'";
}
return nullptr;
}
// Parse a ls expression.
// ls-expr ::= 'ls' string-expr
Tree*
parse_ls(Parser& p) {
if (const Token* k = parse::accept(p, ls_tok)) {
if (Tree* t = parse_primary_expr(p))
return new Ls_tree(k, t);
else
parse::parse_error(p) << "expected 'string-expr' after 'ls'";
}
return nullptr;
}
// Parse a mkdir expression.
// mkdir-expr ::= 'mkdir' string-expr
Tree*
parse_mkdir(Parser& p) {
if (const Token* k = parse::accept(p, mkdir_tok)) {
/* if (parse::expect(p, equal_tok)){
parse::parse_error(p) << "expected 'string' after";
}
else{ */
if (Tree* t = parse_primary_expr(p))
return new Mkdir_tree(k, t);
else
parse::parse_error(p) << "expected 'string-expr' after 'mkdir'";
} //}
return nullptr;
}
// Parse a rmdir expression.
// rmdir-expr ::= 'rmdir' string-expr
Tree*
parse_rmdir(Parser& p) {
if (const Token* k = parse::accept(p, rmdir_tok)) {
if (Tree* t = parse_primary_expr(p))
return new Rmdir_tree(k, t);
else
parse::parse_error(p) << "expected 'string-expr' after 'rmdir'";
}
return nullptr;
}
// Parse a cd expression.
// cd-expr ::= 'cd' string-expr
Tree*
parse_cd(Parser& p) {
if (const Token* k = parse::accept(p, cd_tok)) {
if (Tree* t = parse_primary_expr(p))
return new Cd_tree(k, t);
else
parse::parse_error(p) << "expected 'string-expr' after 'cd'";
}
return nullptr;
}
// Parse a mv expression.
// mv-expr ::= 'mv' string-expr string-expr
Tree*
parse_mv(Parser& p) {
if (const Token* k = parse::accept(p, mv_tok)) {
if (Tree* t = parse_primary_expr(p))
if (Tree* t1 = parse_primary_expr(p))
return new Mv_tree(k, t, t1);
else
parse::parse_error(p) << "expected 'string-expr' after 'mv'";
}
return nullptr;
}
// Parse a prefix expr.
//
// prefix-expr ::= if-expr | succ-epxr | pred-expr | iszero-expr
// | print-expr | typeof-expr
Tree*
parse_prefix_expr(Parser& p) {
if (Tree* t = parse_if_expr(p))
return t;
if (Tree* t = parse_succ_expr(p))
return t;
if (Tree* t = parse_pred_expr(p))
return t;
if (Tree* t = parse_iszero_expr(p))
return t;
if (Tree* t = parse_print_expr(p))
return t;
if (Tree* t = parse_typeof_expr(p))
return t;
if (Tree* t = parse_ls(p))
return t;
if (Tree* t = parse_mkdir(p))
return t;
if (Tree* t = parse_rmdir(p))
return t;
if (Tree* t = parse_cd(p))
return t;
if (Tree* t = parse_mv(p))
return t;
return parse_postfix_expr(p);
}
// Parse an arrow expression.
//
// imp-expr ::= prefix-expr
// | prefix-expr '->' imp-expr
Tree*
parse_arrow_expr(Parser& p) {
if (Tree* l = parse_prefix_expr(p)) {
if (parse::accept(p, arrow_tok))
if (Tree* r = parse_arrow_expr(p))
return new Arrow_tree(l, r);
return l;
}
return nullptr;
}
// Parse an expression
//
// expr ::= prefix-expr
Tree*
parse_expr(Parser& p) {
return parse_arrow_expr(p);
}
// Parse a definition expression.
//
// def-expr ::= 'def' name '=' expr
Tree*
parse_def(Parser& p) {
if (const Token* k = parse::accept(p, def_tok)) {
if (Tree* n = parse_name(p)) {
if (parse::expect(p, equal_tok)) {
if (Tree* e = parse_expr(p))
return new Def_tree(k, n, e);
else
parse::parse_error(p) << "expected 'expr' after '='";
}
} else {
parse::parse_error(p) << "expected 'name' after 'def'";
}
}
return nullptr;
}
// Parse a statement.
//
// stmt ::= def-stmt | expr-stmt
Tree*
parse_stmt(Parser& p) {
if (Tree* t = parse_def(p))
return t;
if (Tree* t = parse_expr(p))
return t;
return nullptr;
}
// Parse a program.
//
// program ::= stmt-list
// stmt-list ::= stmt ';' | stmt-list ';' stmt
//
Tree*
parse_program(Parser& p) {
Tree_seq* stmts = new Tree_seq();
while (not parse::end_of_stream(p)) {
// Parse the next statement...
if (Tree* s = parse_stmt(p))
stmts->push_back(s);
else
return nullptr;
// ... and it's trailing ';'
if (not parse::expect(p, semicolon_tok))
return nullptr;
}
return new Prog_tree(stmts);
}
// -------------------------------------------------------------------------- //
// Parser
// Parse a range of tokens.
Tree*
Parser::operator()(Token_iterator f, Token_iterator l) {
if (f == l)
return nullptr;
first = f;
last = l;
current = first;
use_diagnostics(diags);
return parse_program(*this);
}