-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.c
719 lines (652 loc) · 19.1 KB
/
parser.c
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
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
/*
Copyright 2022 Shota Minami
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "array.h"
#include "compiler.h"
#include "context.h"
#include "context_fwd.h"
#include "mppl_syntax.h"
#include "report.h"
#include "source.h"
#include "string.h"
#include "syntax_kind.h"
#include "syntax_tree.h"
#include "utility.h"
typedef struct Parser Parser;
struct Parser {
unsigned long offset;
const Source *source;
Ctx *ctx;
LexStatus status;
SyntaxBuilder *builder;
const String *token;
SyntaxKind token_kind;
BITSET(expected, SYNTAX_EOF_TOKEN + 1);
Array *errors;
int alive;
unsigned long breakable;
};
static const String *token(Parser *self)
{
if (!self->token) {
while (1) {
LexedToken lexed;
LexStatus status = mpplc_lex(self->source, self->offset, &lexed);
const String *token = ctx_string(self->ctx, self->source->text + lexed.offset, lexed.length);
if (syntax_kind_is_token(lexed.kind)) {
self->status = status;
self->token = token;
self->token_kind = lexed.kind;
break;
} else {
syntax_builder_trivia(self->builder, lexed.kind, token, 1);
self->offset += lexed.length;
}
}
}
return self->token;
}
static void bump(Parser *self)
{
if (token(self)) {
bitset_clear(self->expected);
syntax_builder_token(self->builder, self->token_kind, self->token);
self->offset += string_length(self->token);
self->token = NULL;
}
}
static int check_any(Parser *self, const SyntaxKind *kinds, unsigned long count)
{
if (!self->alive) {
return 0;
} else {
unsigned long i;
for (i = 0; i < count; ++i) {
bitset_set(self->expected, kinds[i]);
}
for (i = 0; i < count; ++i) {
if (token(self) && self->token_kind == kinds[i]) {
return 1;
}
}
return 0;
}
}
static int check(Parser *self, SyntaxKind kind)
{
return check_any(self, &kind, 1);
}
static int eat_any(Parser *self, const SyntaxKind *kinds, unsigned long count)
{
if (!self->alive) {
return 0;
} else if (check_any(self, kinds, count)) {
bump(self);
return 1;
} else {
return 0;
}
}
static int eat(Parser *self, SyntaxKind kind)
{
return eat_any(self, &kind, 1);
}
static const char *SYNTAX_DISPLAY_STRING[] = {
"",
"identifier",
"integer",
"string",
"`+`",
"`-`",
"`*`",
"`=`",
"`<>`",
"`<`",
"`<=`",
"`>`",
"`>=`",
"`(`",
"`)`",
"`[`",
"`]`",
"`:=`",
"`.`",
"`,`",
"`:`",
"`;`",
"`program`",
"`var`",
"`array`",
"`of`",
"`begin`",
"`end`",
"`if`",
"`then`",
"`else`",
"`procedure`",
"`return`",
"`call`",
"`while`",
"`do`",
"`not`",
"`or`",
"`div`",
"`and`",
"`char`",
"`integer`",
"`boolean`",
"`read`",
"`write`",
"`readln`",
"`writeln`",
"`true`",
"`false`",
"`break`",
"EOF",
};
static void error_unexpected(Parser *self)
{
char expected[1024];
unsigned long cursor = 0;
SyntaxKind kind;
Report *report;
for (kind = 0; kind <= SYNTAX_EOF_TOKEN; ++kind) {
if (bitset_get(self->expected, kind)) {
if (cursor > 0) {
if (bitset_count(self->expected) > 1) {
cursor += sprintf(expected + cursor, ", ");
} else {
cursor += sprintf(expected + cursor, " and ");
}
}
cursor += sprintf(expected + cursor, "%s", SYNTAX_DISPLAY_STRING[kind]);
bitset_reset(self->expected, kind);
}
}
if (token(self) && self->token_kind == SYNTAX_BAD_TOKEN) {
switch (self->status) {
case LEX_ERROR_STRAY_CHAR: {
if (is_graphic(string_data(token(self))[0])) {
report = report_new(REPORT_KIND_ERROR, self->offset, "stray `%s` in program", string_data(token(self)));
} else {
report = report_new(REPORT_KIND_ERROR, self->offset, "stray `\\x%X` in program", (unsigned char) string_data(token(self))[0]);
}
report_annotation(report, self->offset, self->offset + string_length(token(self)), "expected %s", expected);
self->alive = 0;
break;
}
case LEX_ERROR_NONGRAPHIC_CHAR: {
report = report_new(REPORT_KIND_ERROR, self->offset, "non-graphic character in string");
report_annotation(report, self->offset, self->offset + string_length(token(self)), NULL);
break;
}
case LEX_ERROR_UNTERMINATED_STRING: {
report = report_new(REPORT_KIND_ERROR, self->offset, "string is unterminated");
report_annotation(report, self->offset, self->offset + string_length(token(self)), NULL);
self->alive = 0;
break;
}
case LEX_ERROR_UNTERMINATED_COMMENT: {
report = report_new(REPORT_KIND_ERROR, self->offset, "comment is unterminated");
report_annotation(report, self->offset, self->offset + string_length(token(self)), NULL);
self->alive = 0;
break;
}
case LEX_ERROR_TOO_BIG_NUMBER: {
report = report_new(REPORT_KIND_ERROR, self->offset, "number is too big");
report_annotation(report, self->offset, self->offset + string_length(token(self)), "value should be less than or equal to 32768");
break;
}
default:
unreachable();
}
} else {
report = report_new(REPORT_KIND_ERROR, self->offset, "expected %s, found `%s`", expected, string_data(token(self)));
report_annotation(report, self->offset, self->offset + string_length(token(self)), "expected %s", expected);
self->alive = 0;
}
array_push(self->errors, &report);
bump(self);
}
static int expect_any(Parser *self, const SyntaxKind *kinds, unsigned long count)
{
if (!self->alive) {
syntax_builder_null(self->builder);
return 0;
} else if (eat_any(self, kinds, count)) {
return 1;
} else {
error_unexpected(self);
return 0;
}
}
static int expect(Parser *self, SyntaxKind kind)
{
return expect_any(self, &kind, 1);
}
static void expect_semi(Parser *self)
{
unsigned long offset = self->offset;
if (!eat(self, SYNTAX_SEMI_TOKEN) && self->alive) {
Report *report = report_new(REPORT_KIND_ERROR, offset, "semicolon is missing");
report_annotation(report, offset, offset + 1, "insert `;` here");
array_push(self->errors, &report);
self->alive = 0;
bump(self);
}
}
static const SyntaxKind FIRST_STD_TYPE[] = { SYNTAX_INTEGER_KW, SYNTAX_BOOLEAN_KW, SYNTAX_CHAR_KW };
static void parse_std_type(Parser *self)
{
expect_any(self, FIRST_STD_TYPE, sizeof(FIRST_STD_TYPE) / sizeof(SyntaxKind));
}
static void parse_array_type(Parser *self)
{
syntax_builder_start_tree(self->builder);
expect(self, SYNTAX_ARRAY_KW);
expect(self, SYNTAX_LBRACKET_TOKEN);
expect(self, SYNTAX_NUMBER_LIT);
expect(self, SYNTAX_RBRACKET_TOKEN);
expect(self, SYNTAX_OF_KW);
parse_std_type(self);
syntax_builder_end_tree(self->builder, SYNTAX_ARRAY_TYPE);
}
static void parse_type(Parser *self)
{
if (check_any(self, FIRST_STD_TYPE, sizeof(FIRST_STD_TYPE) / sizeof(SyntaxKind))) {
parse_std_type(self);
} else {
parse_array_type(self);
}
}
static void parse_factor(Parser *self);
static void parse_expr(Parser *self);
static void parse_var(Parser *self)
{
syntax_builder_start_tree(self->builder);
expect(self, SYNTAX_IDENT_TOKEN);
if (eat(self, SYNTAX_LBRACKET_TOKEN)) {
parse_expr(self);
expect(self, SYNTAX_RBRACKET_TOKEN);
syntax_builder_end_tree(self->builder, SYNTAX_INDEXED_VAR);
} else {
syntax_builder_end_tree(self->builder, SYNTAX_ENTIRE_VAR);
}
}
static void parse_paren_expr(Parser *self)
{
syntax_builder_start_tree(self->builder);
expect(self, SYNTAX_LPAREN_TOKEN);
parse_expr(self);
expect(self, SYNTAX_RPAREN_TOKEN);
syntax_builder_end_tree(self->builder, SYNTAX_PAREN_EXPR);
}
static void parse_not_expr(Parser *self)
{
syntax_builder_start_tree(self->builder);
expect(self, SYNTAX_NOT_KW);
parse_factor(self);
syntax_builder_end_tree(self->builder, SYNTAX_NOT_EXPR);
}
static void parse_cast_expr(Parser *self)
{
syntax_builder_start_tree(self->builder);
parse_std_type(self);
expect(self, SYNTAX_LPAREN_TOKEN);
parse_expr(self);
expect(self, SYNTAX_RPAREN_TOKEN);
syntax_builder_end_tree(self->builder, SYNTAX_CAST_EXPR);
}
static const SyntaxKind FIRST_CONST[] = { SYNTAX_NUMBER_LIT, SYNTAX_TRUE_KW, SYNTAX_FALSE_KW, SYNTAX_STRING_LIT };
static void parse_factor(Parser *self)
{
if (check(self, SYNTAX_IDENT_TOKEN)) {
parse_var(self);
} else if (check(self, SYNTAX_LPAREN_TOKEN)) {
parse_paren_expr(self);
} else if (check(self, SYNTAX_NOT_KW)) {
parse_not_expr(self);
} else if (check_any(self, FIRST_STD_TYPE, sizeof(FIRST_STD_TYPE) / sizeof(SyntaxKind))) {
parse_cast_expr(self);
} else {
expect_any(self, FIRST_CONST, sizeof(FIRST_CONST) / sizeof(SyntaxKind));
}
}
static const SyntaxKind FIRST_MULTI_OP[] = { SYNTAX_STAR_TOKEN, SYNTAX_DIV_KW, SYNTAX_AND_KW };
static void parse_term(Parser *self)
{
unsigned long checkpoint = syntax_builder_checkpoint(self->builder);
parse_factor(self);
while (eat_any(self, FIRST_MULTI_OP, sizeof(FIRST_MULTI_OP) / sizeof(SyntaxKind))) {
syntax_builder_start_tree_at(self->builder, checkpoint);
parse_factor(self);
syntax_builder_end_tree(self->builder, SYNTAX_BINARY_EXPR);
}
}
static const SyntaxKind FIRST_ADD_OP[] = { SYNTAX_PLUS_TOKEN, SYNTAX_MINUS_TOKEN, SYNTAX_OR_KW };
static void parse_simple_expr(Parser *self)
{
unsigned long checkpoint = syntax_builder_checkpoint(self->builder);
if (check_any(self, FIRST_ADD_OP, sizeof(FIRST_ADD_OP) / sizeof(SyntaxKind))) {
syntax_builder_null(self->builder);
} else {
parse_term(self);
}
while (eat_any(self, FIRST_ADD_OP, sizeof(FIRST_ADD_OP) / sizeof(SyntaxKind))) {
syntax_builder_start_tree_at(self->builder, checkpoint);
parse_term(self);
syntax_builder_end_tree(self->builder, SYNTAX_BINARY_EXPR);
}
}
static const SyntaxKind FIRST_RELAT_OP[] = {
SYNTAX_EQUAL_TOKEN,
SYNTAX_NOTEQ_TOKEN,
SYNTAX_LESS_TOKEN,
SYNTAX_LESSEQ_TOKEN,
SYNTAX_GREATER_TOKEN,
SYNTAX_GREATEREQ_TOKEN,
};
static void parse_expr(Parser *self)
{
unsigned long checkpoint = syntax_builder_checkpoint(self->builder);
parse_simple_expr(self);
while (eat_any(self, FIRST_RELAT_OP, sizeof(FIRST_RELAT_OP) / sizeof(SyntaxKind))) {
syntax_builder_start_tree_at(self->builder, checkpoint);
parse_simple_expr(self);
syntax_builder_end_tree(self->builder, SYNTAX_BINARY_EXPR);
}
}
static void parse_stmt(Parser *self);
static void parse_assign_stmt(Parser *self)
{
syntax_builder_start_tree(self->builder);
parse_var(self);
expect(self, SYNTAX_ASSIGN_TOKEN);
parse_expr(self);
syntax_builder_end_tree(self->builder, SYNTAX_ASSIGN_STMT);
}
static void parse_if_stmt(Parser *self)
{
syntax_builder_start_tree(self->builder);
expect(self, SYNTAX_IF_KW);
parse_expr(self);
expect(self, SYNTAX_THEN_KW);
parse_stmt(self);
if (eat(self, SYNTAX_ELSE_KW)) {
parse_stmt(self);
} else {
syntax_builder_null(self->builder);
syntax_builder_null(self->builder);
}
syntax_builder_end_tree(self->builder, SYNTAX_IF_STMT);
}
static void parse_while_stmt(Parser *self)
{
++self->breakable;
syntax_builder_start_tree(self->builder);
expect(self, SYNTAX_WHILE_KW);
parse_expr(self);
expect(self, SYNTAX_DO_KW);
parse_stmt(self);
syntax_builder_end_tree(self->builder, SYNTAX_WHILE_STMT);
--self->breakable;
}
static void parse_break_stmt(Parser *self)
{
syntax_builder_start_tree(self->builder);
if (check(self, SYNTAX_BREAK_KW)) {
if (!self->breakable && self->alive) {
Report *report = report_new(REPORT_KIND_ERROR, self->offset, "`break` is outside of a loop");
report_annotation(report, self->offset, self->offset + string_length(token(self)),
"`break` should be enclosed by a loop");
array_push(self->errors, &report);
}
bump(self);
} else {
error_unexpected(self);
}
syntax_builder_end_tree(self->builder, SYNTAX_BREAK_STMT);
}
static void parse_act_param_list(Parser *self)
{
syntax_builder_start_tree(self->builder);
expect(self, SYNTAX_LPAREN_TOKEN);
do {
parse_expr(self);
} while (eat(self, SYNTAX_COMMA_TOKEN));
expect(self, SYNTAX_RPAREN_TOKEN);
syntax_builder_end_tree(self->builder, SYNTAX_ACT_PARAM_LIST);
}
static void parse_call_stmt(Parser *self)
{
syntax_builder_start_tree(self->builder);
expect(self, SYNTAX_CALL_KW);
expect(self, SYNTAX_IDENT_TOKEN);
if (check(self, SYNTAX_LPAREN_TOKEN)) {
parse_act_param_list(self);
} else {
syntax_builder_null(self->builder);
}
syntax_builder_end_tree(self->builder, SYNTAX_CALL_STMT);
}
static void parse_return_stmt(Parser *self)
{
syntax_builder_start_tree(self->builder);
expect(self, SYNTAX_RETURN_KW);
syntax_builder_end_tree(self->builder, SYNTAX_RETURN_STMT);
}
static void parse_input_list(Parser *self)
{
syntax_builder_start_tree(self->builder);
expect(self, SYNTAX_LPAREN_TOKEN);
do {
parse_var(self);
} while (eat(self, SYNTAX_COMMA_TOKEN));
expect(self, SYNTAX_RPAREN_TOKEN);
syntax_builder_end_tree(self->builder, SYTANX_INPUT_LIST);
}
static const SyntaxKind FIRST_INPUT_STMT[] = { SYNTAX_READ_KW, SYNTAX_READLN_KW };
static void parse_input_stmt(Parser *self)
{
syntax_builder_start_tree(self->builder);
expect_any(self, FIRST_INPUT_STMT, sizeof(FIRST_INPUT_STMT) / sizeof(SyntaxKind));
if (check(self, SYNTAX_LPAREN_TOKEN)) {
parse_input_list(self);
} else {
syntax_builder_null(self->builder);
}
syntax_builder_end_tree(self->builder, SYNTAX_INPUT_STMT);
}
static void parse_output_value(Parser *self)
{
syntax_builder_start_tree(self->builder);
parse_expr(self);
if (eat(self, SYNTAX_COLON_TOKEN)) {
expect(self, SYNTAX_NUMBER_LIT);
} else {
syntax_builder_null(self->builder);
syntax_builder_null(self->builder);
}
syntax_builder_end_tree(self->builder, SYNTAX_OUTPUT_VALUE);
}
static void parse_output_list(Parser *self)
{
syntax_builder_start_tree(self->builder);
expect(self, SYNTAX_LPAREN_TOKEN);
do {
parse_output_value(self);
} while (eat(self, SYNTAX_COMMA_TOKEN));
expect(self, SYNTAX_RPAREN_TOKEN);
syntax_builder_end_tree(self->builder, SYNTAX_OUTPUT_LIST);
}
static const SyntaxKind FIRST_OUTPUT_STMT[] = { SYNTAX_WRITE_KW, SYNTAX_WRITELN_KW };
static void parse_output_stmt(Parser *self)
{
syntax_builder_start_tree(self->builder);
expect_any(self, FIRST_OUTPUT_STMT, sizeof(FIRST_OUTPUT_STMT) / sizeof(SyntaxKind));
if (check(self, SYNTAX_LPAREN_TOKEN)) {
parse_output_list(self);
} else {
syntax_builder_null(self->builder);
}
syntax_builder_end_tree(self->builder, SYNTAX_OUTPUT_STMT);
}
static void parse_comp_stmt(Parser *self)
{
syntax_builder_start_tree(self->builder);
expect(self, SYNTAX_BEGIN_KW);
do {
parse_stmt(self);
} while (eat(self, SYNTAX_SEMI_TOKEN));
expect(self, SYNTAX_END_KW);
syntax_builder_end_tree(self->builder, SYNTAX_COMP_STMT);
}
static void parse_stmt(Parser *self)
{
if (check(self, SYNTAX_IDENT_TOKEN)) {
parse_assign_stmt(self);
} else if (check(self, SYNTAX_IF_KW)) {
parse_if_stmt(self);
} else if (check(self, SYNTAX_WHILE_KW)) {
parse_while_stmt(self);
} else if (check(self, SYNTAX_BREAK_KW)) {
parse_break_stmt(self);
} else if (check(self, SYNTAX_CALL_KW)) {
parse_call_stmt(self);
} else if (check(self, SYNTAX_RETURN_KW)) {
parse_return_stmt(self);
} else if (check_any(self, FIRST_INPUT_STMT, sizeof(FIRST_INPUT_STMT) / sizeof(SyntaxKind))) {
parse_input_stmt(self);
} else if (check_any(self, FIRST_OUTPUT_STMT, sizeof(FIRST_OUTPUT_STMT) / sizeof(SyntaxKind))) {
parse_output_stmt(self);
} else if (check(self, SYNTAX_BEGIN_KW)) {
parse_comp_stmt(self);
} else {
syntax_builder_null(self->builder);
}
}
static void parse_var_decl(Parser *self)
{
syntax_builder_start_tree(self->builder);
do {
expect(self, SYNTAX_IDENT_TOKEN);
} while (eat(self, SYNTAX_COMMA_TOKEN));
expect(self, SYNTAX_COLON_TOKEN);
parse_type(self);
syntax_builder_end_tree(self->builder, SYNTAX_VAR_DECL);
}
static void parse_var_decl_part(Parser *self)
{
syntax_builder_start_tree(self->builder);
expect(self, SYNTAX_VAR_KW);
do {
parse_var_decl(self);
expect_semi(self);
} while (check(self, SYNTAX_IDENT_TOKEN));
syntax_builder_end_tree(self->builder, SYNTAX_VAR_DECL_PART);
}
static void parse_fml_param_sec(Parser *self)
{
syntax_builder_start_tree(self->builder);
do {
expect(self, SYNTAX_IDENT_TOKEN);
} while (eat(self, SYNTAX_COMMA_TOKEN));
expect(self, SYNTAX_COLON_TOKEN);
parse_type(self);
syntax_builder_end_tree(self->builder, SYNTAX_FML_PARAM_SEC);
}
static void parse_fml_param_list(Parser *self)
{
syntax_builder_start_tree(self->builder);
expect(self, SYNTAX_LPAREN_TOKEN);
do {
parse_fml_param_sec(self);
} while (eat(self, SYNTAX_SEMI_TOKEN));
expect(self, SYNTAX_RPAREN_TOKEN);
syntax_builder_end_tree(self->builder, SYNTAX_FML_PARAM_LIST);
}
static void parse_proc_decl(Parser *self)
{
syntax_builder_start_tree(self->builder);
expect(self, SYNTAX_PROCEDURE_KW);
expect(self, SYNTAX_IDENT_TOKEN);
if (check(self, SYNTAX_LPAREN_TOKEN)) {
parse_fml_param_list(self);
} else {
syntax_builder_null(self->builder);
}
expect_semi(self);
if (check(self, SYNTAX_VAR_KW)) {
parse_var_decl_part(self);
} else {
syntax_builder_null(self->builder);
}
parse_comp_stmt(self);
expect_semi(self);
syntax_builder_end_tree(self->builder, SYNTAX_PROC_DECL);
}
static void parse_program(Parser *self)
{
syntax_builder_start_tree(self->builder);
expect(self, SYNTAX_PROGRAM_KW);
expect(self, SYNTAX_IDENT_TOKEN);
expect_semi(self);
while (1) {
if (check(self, SYNTAX_VAR_KW)) {
parse_var_decl_part(self);
} else if (check(self, SYNTAX_PROCEDURE_KW)) {
parse_proc_decl(self);
} else {
break;
}
}
parse_comp_stmt(self);
expect(self, SYNTAX_DOT_TOKEN);
expect(self, SYNTAX_EOF_TOKEN);
syntax_builder_end_tree(self->builder, SYNTAX_PROGRAM);
}
int mpplc_parse(const Source *source, Ctx *ctx, MpplProgram **syntax)
{
Parser self;
int result;
self.offset = 0;
self.source = source;
self.ctx = ctx;
self.status = LEX_OK;
self.builder = syntax_builder_new();
self.token = NULL;
self.token_kind = SYNTAX_BAD_TOKEN;
bitset_clear(self.expected);
self.errors = array_new(sizeof(Report *));
self.alive = 1;
self.breakable = 0;
parse_program(&self);
*syntax = (MpplProgram *) syntax_builder_build(self.builder);
{
unsigned long i;
for (i = 0; i < array_count(self.errors); ++i) {
report_emit(*(Report **) array_at(self.errors, i), source);
}
fflush(stdout);
}
result = !array_count(self.errors);
if (!result) {
mppl_unref(*syntax);
*syntax = NULL;
}
array_free(self.errors);
return result;
}