source: mainline/uspace/app/sbi/src/parse.c

Last change on this file was 09ab0a9a, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix vertical spacing with new Ccheck revision.

  • Property mode set to 100644
File size: 37.1 KB
Line 
1/*
2 * Copyright (c) 2011 Jiri Svoboda
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @file Parser
30 *
31 * Consumes a sequence of lexical elements and produces a syntax tree (stree).
32 * Parsing primitives, module members, statements.
33 */
34
35#include <assert.h>
36#include <stdlib.h>
37#include "cspan.h"
38#include "debug.h"
39#include "lex.h"
40#include "list.h"
41#include "mytypes.h"
42#include "p_expr.h"
43#include "p_type.h"
44#include "stree.h"
45#include "strtab.h"
46#include "symbol.h"
47
48#include "parse.h"
49
50/*
51 * Module and CSI members
52 */
53static stree_csi_t *parse_csi(parse_t *parse, lclass_t dclass,
54 stree_csi_t *outer_csi);
55static stree_csimbr_t *parse_csimbr(parse_t *parse, stree_csi_t *outer_csi);
56
57static stree_ctor_t *parse_ctor(parse_t *parse, stree_csi_t *outer_csi);
58
59static stree_enum_t *parse_enum(parse_t *parse, stree_csi_t *outer_csi);
60static stree_embr_t *parse_embr(parse_t *parse, stree_enum_t *outer_enum);
61
62static stree_deleg_t *parse_deleg(parse_t *parse, stree_csi_t *outer_csi);
63static stree_fun_t *parse_fun(parse_t *parse, stree_csi_t *outer_csi);
64static stree_var_t *parse_var(parse_t *parse, stree_csi_t *outer_csi);
65static stree_prop_t *parse_prop(parse_t *parse, stree_csi_t *outer_csi);
66
67static void parse_symbol_attrs(parse_t *parse, stree_symbol_t *symbol);
68static stree_symbol_attr_t *parse_symbol_attr(parse_t *parse);
69
70static stree_proc_arg_t *parse_proc_arg(parse_t *parse);
71static stree_arg_attr_t *parse_arg_attr(parse_t *parse);
72static stree_fun_sig_t *parse_fun_sig(parse_t *parse);
73
74static void parse_prop_get(parse_t *parse, stree_prop_t *prop);
75static void parse_prop_set(parse_t *parse, stree_prop_t *prop);
76
77/*
78 * Statements
79 */
80static stree_block_t *parse_block(parse_t *parse);
81
82static stree_vdecl_t *parse_vdecl(parse_t *parse);
83static stree_if_t *parse_if(parse_t *parse);
84static stree_switch_t *parse_switch(parse_t *parse);
85static stree_while_t *parse_while(parse_t *parse);
86static stree_for_t *parse_for(parse_t *parse);
87static stree_raise_t *parse_raise(parse_t *parse);
88static stree_break_t *parse_break(parse_t *parse);
89static stree_return_t *parse_return(parse_t *parse);
90static stree_wef_t *parse_wef(parse_t *parse);
91static stree_exps_t *parse_exps(parse_t *parse);
92
93static stree_except_t *parse_except(parse_t *parse);
94
95/** Initialize parser object.
96 *
97 * Set up parser @a parse to use lexer @a lex for input and to store
98 * output (i.e. new declarations) to program @a prog. @a prog is not
99 * necessarily empty, the declarations being parsed are simply added
100 * to it.
101 *
102 * @param parse Parser object.
103 * @param prog Destination program stree.
104 * @param lex Input lexer.
105 */
106void parse_init(parse_t *parse, stree_program_t *prog, struct lex *lex)
107{
108 parse->program = prog;
109 parse->cur_mod = parse->program->module;
110 parse->lex = lex;
111
112 parse->error = b_false;
113 parse->error_bailout = b_false;
114
115 lex_next(parse->lex);
116}
117
118/** Parse module.
119 *
120 * Parse a program module.
121 *
122 * The input is read using the lexer associated with @a parse. The resulting
123 * declarations are added to existing declarations in the program associated
124 * with @a parse.
125 *
126 * If any parse error occurs, parse->error will @c b_true when this function
127 * returns. parse->error_bailout will be @c b_true if the error has not
128 * been recovered yet. Similar holds for other parsing functions in this
129 * module.
130 *
131 * @param parse Parser object.
132 */
133void parse_module(parse_t *parse)
134{
135 stree_csi_t *csi;
136 stree_enum_t *enum_d;
137 stree_modm_t *modm;
138
139 while (lcur_lc(parse) != lc_eof && !parse_is_error(parse)) {
140 switch (lcur_lc(parse)) {
141 case lc_class:
142 case lc_struct:
143 case lc_interface:
144 csi = parse_csi(parse, lcur_lc(parse), NULL);
145 modm = stree_modm_new(mc_csi);
146 modm->u.csi = csi;
147
148 list_append(&parse->cur_mod->members, modm);
149 break;
150 case lc_enum:
151 enum_d = parse_enum(parse, NULL);
152 modm = stree_modm_new(mc_enum);
153 modm->u.enum_d = enum_d;
154
155 list_append(&parse->cur_mod->members, modm);
156 break;
157 default:
158 lunexpected_error(parse);
159 lex_next(parse->lex);
160 break;
161 }
162
163 }
164}
165
166/** Parse class, struct or interface declaration.
167 *
168 * @param parse Parser object.
169 * @param dclass What to parse: @c lc_class, @c lc_struct or @c lc_csi.
170 * @param outer_csi CSI containing this declaration or @c NULL if global.
171 * @return New syntax tree node.
172 */
173static stree_csi_t *parse_csi(parse_t *parse, lclass_t dclass,
174 stree_csi_t *outer_csi)
175{
176 stree_csi_t *csi;
177 csi_class_t cc;
178 stree_csimbr_t *csimbr;
179 stree_symbol_t *symbol;
180 stree_ident_t *targ_name;
181 stree_targ_t *targ;
182 stree_texpr_t *pref;
183
184 switch (dclass) {
185 case lc_class:
186 cc = csi_class;
187 break;
188 case lc_struct:
189 cc = csi_struct;
190 break;
191 case lc_interface:
192 cc = csi_interface;
193 break;
194 default:
195 assert(b_false);
196 }
197
198 lskip(parse);
199
200 csi = stree_csi_new(cc);
201 csi->name = parse_ident(parse);
202
203 list_init(&csi->targ);
204
205 while (lcur_lc(parse) == lc_slash) {
206 lskip(parse);
207 targ_name = parse_ident(parse);
208
209 targ = stree_targ_new();
210 targ->name = targ_name;
211
212 list_append(&csi->targ, targ);
213 }
214
215 symbol = stree_symbol_new(sc_csi);
216 symbol->u.csi = csi;
217 symbol->outer_csi = outer_csi;
218 csi->symbol = symbol;
219
220#ifdef DEBUG_PARSE_TRACE
221 printf("parse_csi: csi=%p, csi->name = %p (%s)\n", csi, csi->name,
222 strtab_get_str(csi->name->sid));
223#endif
224 if (lcur_lc(parse) == lc_colon) {
225 /* Inheritance list */
226 lskip(parse);
227
228 while (b_true) {
229 pref = parse_texpr(parse);
230 if (parse_is_error(parse))
231 break;
232
233 list_append(&csi->inherit, pref);
234 if (lcur_lc(parse) != lc_plus)
235 break;
236
237 lskip(parse);
238 }
239 }
240
241 lmatch(parse, lc_is);
242 list_init(&csi->members);
243
244 /* Parse class, struct or interface members. */
245 while (lcur_lc(parse) != lc_end && !parse_is_error(parse)) {
246 csimbr = parse_csimbr(parse, csi);
247 if (csimbr == NULL)
248 continue;
249
250 list_append(&csi->members, csimbr);
251 }
252
253 lmatch(parse, lc_end);
254
255 if (outer_csi != NULL) {
256 switch (outer_csi->cc) {
257 case csi_class:
258 case csi_struct:
259 break;
260 case csi_interface:
261 cspan_print(csi->name->cspan);
262 printf(" Error: CSI declared inside interface.\n");
263 parse_note_error(parse);
264 /* XXX Free csi */
265 return NULL;
266 }
267 }
268
269 return csi;
270}
271
272/** Parse class, struct or interface member.
273 *
274 * @param parse Parser object.
275 * @param outer_csi CSI containing this declaration.
276 * @return New syntax tree node. In case of parse error,
277 * @c NULL may (but need not) be returned.
278 */
279static stree_csimbr_t *parse_csimbr(parse_t *parse, stree_csi_t *outer_csi)
280{
281 stree_csimbr_t *csimbr;
282
283 stree_csi_t *csi;
284 stree_ctor_t *ctor;
285 stree_deleg_t *deleg;
286 stree_enum_t *enum_d;
287 stree_fun_t *fun;
288 stree_var_t *var;
289 stree_prop_t *prop;
290
291 csimbr = NULL;
292
293 switch (lcur_lc(parse)) {
294 case lc_class:
295 case lc_struct:
296 case lc_interface:
297 csi = parse_csi(parse, lcur_lc(parse), outer_csi);
298 if (csi != NULL) {
299 csimbr = stree_csimbr_new(csimbr_csi);
300 csimbr->u.csi = csi;
301 }
302 break;
303 case lc_new:
304 ctor = parse_ctor(parse, outer_csi);
305 if (ctor != NULL) {
306 csimbr = stree_csimbr_new(csimbr_ctor);
307 csimbr->u.ctor = ctor;
308 }
309 break;
310 case lc_deleg:
311 deleg = parse_deleg(parse, outer_csi);
312 if (deleg != NULL) {
313 csimbr = stree_csimbr_new(csimbr_deleg);
314 csimbr->u.deleg = deleg;
315 }
316 break;
317 case lc_enum:
318 enum_d = parse_enum(parse, outer_csi);
319 if (enum_d != NULL) {
320 csimbr = stree_csimbr_new(csimbr_enum);
321 csimbr->u.enum_d = enum_d;
322 }
323 break;
324 case lc_fun:
325 fun = parse_fun(parse, outer_csi);
326 csimbr = stree_csimbr_new(csimbr_fun);
327 csimbr->u.fun = fun;
328 break;
329 case lc_var:
330 var = parse_var(parse, outer_csi);
331 if (var != NULL) {
332 csimbr = stree_csimbr_new(csimbr_var);
333 csimbr->u.var = var;
334 }
335 break;
336 case lc_prop:
337 prop = parse_prop(parse, outer_csi);
338 csimbr = stree_csimbr_new(csimbr_prop);
339 csimbr->u.prop = prop;
340 break;
341 default:
342 lunexpected_error(parse);
343 lex_next(parse->lex);
344 break;
345 }
346
347 return csimbr;
348}
349
350/** Parse constructor.
351 *
352 * @param parse Parser object.
353 * @param outer_csi CSI containing this declaration or @c NULL if global.
354 * @return New syntax tree node.
355 */
356static stree_ctor_t *parse_ctor(parse_t *parse, stree_csi_t *outer_csi)
357{
358 stree_ctor_t *ctor;
359 stree_symbol_t *symbol;
360 cspan_t *cspan;
361
362 ctor = stree_ctor_new();
363 symbol = stree_symbol_new(sc_ctor);
364
365 symbol->u.ctor = ctor;
366 symbol->outer_csi = outer_csi;
367 ctor->symbol = symbol;
368
369 lmatch(parse, lc_new);
370 cspan = lprev_span(parse);
371
372 /* Fake identifier. */
373 ctor->name = stree_ident_new();
374 ctor->name->sid = strtab_get_sid(CTOR_IDENT);
375 ctor->name->cspan = lprev_span(parse);
376
377#ifdef DEBUG_PARSE_TRACE
378 printf("Parsing constructor of CSI '");
379 symbol_print_fqn(csi_to_symbol(outer_csi));
380 printf("'.\n");
381#endif
382 ctor->sig = parse_fun_sig(parse);
383 if (ctor->sig->rtype != NULL) {
384 cspan_print(cspan);
385 printf(" Error: Constructor of CSI '");
386 symbol_print_fqn(csi_to_symbol(outer_csi));
387 printf("' has a return type.\n");
388 parse_note_error(parse);
389 }
390
391 /* Parse attributes. */
392 parse_symbol_attrs(parse, symbol);
393
394 ctor->proc = stree_proc_new();
395 ctor->proc->outer_symbol = symbol;
396
397 if (lcur_lc(parse) == lc_scolon) {
398 lskip(parse);
399
400 /* This constructor has no body. */
401 cspan_print(cspan);
402 printf(" Error: Constructor of CSI '");
403 symbol_print_fqn(csi_to_symbol(outer_csi));
404 printf("' has no body.\n");
405 parse_note_error(parse);
406
407 ctor->proc->body = NULL;
408 } else {
409 lmatch(parse, lc_is);
410 ctor->proc->body = parse_block(parse);
411 lmatch(parse, lc_end);
412 }
413
414 switch (outer_csi->cc) {
415 case csi_class:
416 case csi_struct:
417 break;
418 case csi_interface:
419 cspan_print(ctor->name->cspan);
420 printf(" Error: Constructor declared inside interface.\n");
421 parse_note_error(parse);
422 /* XXX Free ctor */
423 return NULL;
424 }
425
426 return ctor;
427}
428
429/** Parse @c enum declaration.
430 *
431 * @param parse Parser object.
432 * @param outer_csi CSI containing this declaration or @c NULL if global.
433 * @return New syntax tree node.
434 */
435static stree_enum_t *parse_enum(parse_t *parse, stree_csi_t *outer_csi)
436{
437 stree_enum_t *enum_d;
438 stree_symbol_t *symbol;
439 stree_embr_t *embr;
440
441 enum_d = stree_enum_new();
442 symbol = stree_symbol_new(sc_enum);
443
444 symbol->u.enum_d = enum_d;
445 symbol->outer_csi = outer_csi;
446 enum_d->symbol = symbol;
447
448 lmatch(parse, lc_enum);
449 enum_d->name = parse_ident(parse);
450 list_init(&enum_d->members);
451
452#ifdef DEBUG_PARSE_TRACE
453 printf("Parse enum '%s'.\n", strtab_get_str(enum_d->name->sid));
454#endif
455 lmatch(parse, lc_is);
456
457 /* Parse enum members. */
458 while (lcur_lc(parse) != lc_end && !parse_is_error(parse)) {
459 embr = parse_embr(parse, enum_d);
460 if (embr == NULL)
461 break;
462
463 list_append(&enum_d->members, embr);
464 }
465
466 if (list_is_empty(&enum_d->members)) {
467 cspan_print(enum_d->name->cspan);
468 printf("Error: Enum type '%s' has no members.\n",
469 strtab_get_str(enum_d->name->sid));
470 parse_note_error(parse);
471 }
472
473 lmatch(parse, lc_end);
474
475 if (outer_csi != NULL) {
476 switch (outer_csi->cc) {
477 case csi_class:
478 case csi_struct:
479 break;
480 case csi_interface:
481 cspan_print(enum_d->name->cspan);
482 printf(" Error: Enum declared inside interface.\n");
483 parse_note_error(parse);
484 /* XXX Free enum */
485 return NULL;
486 }
487 }
488
489 return enum_d;
490}
491
492/** Parse enum member.
493 *
494 * @param parse Parser object.
495 * @param outer_enum Enum containing this declaration.
496 * @return New syntax tree node. In case of parse error,
497 * @c NULL may (but need not) be returned.
498 */
499static stree_embr_t *parse_embr(parse_t *parse, stree_enum_t *outer_enum)
500{
501 stree_embr_t *embr;
502
503 embr = stree_embr_new();
504 embr->outer_enum = outer_enum;
505 embr->name = parse_ident(parse);
506
507 lmatch(parse, lc_scolon);
508
509 return embr;
510}
511
512/** Parse delegate.
513 *
514 * @param parse Parser object.
515 * @param outer_csi CSI containing this declaration or @c NULL if global.
516 * @return New syntax tree node.
517 */
518static stree_deleg_t *parse_deleg(parse_t *parse, stree_csi_t *outer_csi)
519{
520 stree_deleg_t *deleg;
521 stree_symbol_t *symbol;
522
523 deleg = stree_deleg_new();
524 symbol = stree_symbol_new(sc_deleg);
525
526 symbol->u.deleg = deleg;
527 symbol->outer_csi = outer_csi;
528 deleg->symbol = symbol;
529
530 lmatch(parse, lc_deleg);
531 deleg->name = parse_ident(parse);
532
533#ifdef DEBUG_PARSE_TRACE
534 printf("Parsing delegate '%s'.\n", strtab_get_str(deleg->name->sid));
535#endif
536
537 deleg->sig = parse_fun_sig(parse);
538
539 /* Parse attributes. */
540 parse_symbol_attrs(parse, symbol);
541
542 lmatch(parse, lc_scolon);
543
544 switch (outer_csi->cc) {
545 case csi_class:
546 case csi_struct:
547 break;
548 case csi_interface:
549 cspan_print(deleg->name->cspan);
550 printf(" Error: Delegate declared inside interface.\n");
551 parse_note_error(parse);
552 /* XXX Free deleg */
553 return NULL;
554 }
555
556 return deleg;
557}
558
559/** Parse member function.
560 *
561 * @param parse Parser object.
562 * @param outer_csi CSI containing this declaration or @c NULL if global.
563 * @return New syntax tree node.
564 */
565static stree_fun_t *parse_fun(parse_t *parse, stree_csi_t *outer_csi)
566{
567 stree_fun_t *fun;
568 stree_symbol_t *symbol;
569 bool_t body_expected;
570
571 fun = stree_fun_new();
572 symbol = stree_symbol_new(sc_fun);
573
574 symbol->u.fun = fun;
575 symbol->outer_csi = outer_csi;
576 fun->symbol = symbol;
577
578 lmatch(parse, lc_fun);
579 fun->name = parse_ident(parse);
580
581#ifdef DEBUG_PARSE_TRACE
582 printf("Parsing function '%s'.\n", strtab_get_str(fun->name->sid));
583#endif
584 fun->sig = parse_fun_sig(parse);
585
586 /* Parse attributes. */
587 parse_symbol_attrs(parse, symbol);
588
589 body_expected = !stree_symbol_has_attr(symbol, sac_builtin) &&
590 (outer_csi->cc != csi_interface);
591
592 fun->proc = stree_proc_new();
593 fun->proc->outer_symbol = symbol;
594
595 if (lcur_lc(parse) == lc_scolon) {
596 lskip(parse);
597
598 /* Body not present */
599 if (body_expected) {
600 cspan_print(fun->name->cspan);
601 printf(" Error: Function '");
602 symbol_print_fqn(symbol);
603 printf("' should have a body.\n");
604 parse_note_error(parse);
605 }
606
607 fun->proc->body = NULL;
608 } else {
609 lmatch(parse, lc_is);
610 fun->proc->body = parse_block(parse);
611 lmatch(parse, lc_end);
612
613 /* Body present */
614 if (!body_expected) {
615 cspan_print(fun->name->cspan);
616 printf(" Error: Function declaration '");
617 symbol_print_fqn(symbol);
618 printf("' should not have a body.\n");
619 parse_note_error(parse);
620 }
621 }
622
623 return fun;
624}
625
626/** Parse member variable.
627 *
628 * @param parse Parser object.
629 * @param outer_csi CSI containing this declaration or @c NULL if global.
630 * @return New syntax tree node.
631 */
632static stree_var_t *parse_var(parse_t *parse, stree_csi_t *outer_csi)
633{
634 stree_var_t *var;
635 stree_symbol_t *symbol;
636
637 var = stree_var_new();
638 symbol = stree_symbol_new(sc_var);
639 symbol->u.var = var;
640 symbol->outer_csi = outer_csi;
641 var->symbol = symbol;
642
643 lmatch(parse, lc_var);
644 var->name = parse_ident(parse);
645 lmatch(parse, lc_colon);
646 var->type = parse_texpr(parse);
647
648 parse_symbol_attrs(parse, symbol);
649
650 lmatch(parse, lc_scolon);
651
652 switch (outer_csi->cc) {
653 case csi_class:
654 case csi_struct:
655 break;
656 case csi_interface:
657 cspan_print(var->name->cspan);
658 printf(" Error: Variable declared inside interface.\n");
659 parse_note_error(parse);
660 /* XXX Free var */
661 return NULL;
662 }
663
664 return var;
665}
666
667/** Parse member property.
668 *
669 * @param parse Parser object.
670 * @param outer_csi CSI containing this declaration or @c NULL if global.
671 * @return New syntax tree node.
672 */
673static stree_prop_t *parse_prop(parse_t *parse, stree_csi_t *outer_csi)
674{
675 stree_prop_t *prop;
676 stree_symbol_t *symbol;
677
678 stree_ident_t *ident;
679 stree_proc_arg_t *arg;
680
681 prop = stree_prop_new();
682 list_init(&prop->args);
683
684 symbol = stree_symbol_new(sc_prop);
685 symbol->u.prop = prop;
686 symbol->outer_csi = outer_csi;
687 prop->symbol = symbol;
688
689 lmatch(parse, lc_prop);
690
691 if (lcur_lc(parse) == lc_self) {
692 /* Indexed property set */
693
694 /* Use some name that is impossible as identifier. */
695 ident = stree_ident_new();
696 ident->sid = strtab_get_sid(INDEXER_IDENT);
697 prop->name = ident;
698
699 lskip(parse);
700 lmatch(parse, lc_lsbr);
701
702 /* Parse formal parameters. */
703 while (!parse_is_error(parse)) {
704 arg = parse_proc_arg(parse);
705 if (stree_arg_has_attr(arg, aac_packed)) {
706 prop->varg = arg;
707 break;
708 } else {
709 list_append(&prop->args, arg);
710 }
711
712 if (lcur_lc(parse) == lc_rsbr)
713 break;
714
715 lmatch(parse, lc_scolon);
716 }
717
718 lmatch(parse, lc_rsbr);
719 } else {
720 /* Named property */
721 prop->name = parse_ident(parse);
722 }
723
724 lmatch(parse, lc_colon);
725 prop->type = parse_texpr(parse);
726
727 /* Parse attributes. */
728 parse_symbol_attrs(parse, symbol);
729
730 lmatch(parse, lc_is);
731
732 while (lcur_lc(parse) != lc_end && !parse_is_error(parse)) {
733 switch (lcur_lc(parse)) {
734 case lc_get:
735 parse_prop_get(parse, prop);
736 break;
737 case lc_set:
738 parse_prop_set(parse, prop);
739 break;
740 default:
741 lunexpected_error(parse);
742 }
743 }
744
745 lmatch(parse, lc_end);
746
747 return prop;
748}
749
750/** Parse symbol attributes.
751 *
752 * Parse list of attributes and add them to @a symbol.
753 *
754 * @param parse Parser object
755 * @param symbol Symbol to add these attributes to
756 */
757static void parse_symbol_attrs(parse_t *parse, stree_symbol_t *symbol)
758{
759 stree_symbol_attr_t *attr;
760
761 /* Parse attributes. */
762 while (lcur_lc(parse) == lc_comma && !parse_is_error(parse)) {
763 lskip(parse);
764 attr = parse_symbol_attr(parse);
765 list_append(&symbol->attr, attr);
766 }
767}
768
769/** Parse symbol attribute.
770 *
771 * @param parse Parser object
772 * @return New syntax tree node
773 */
774static stree_symbol_attr_t *parse_symbol_attr(parse_t *parse)
775{
776 stree_symbol_attr_t *attr;
777 symbol_attr_class_t sac;
778
779 /* Make compiler happy. */
780 sac = 0;
781
782 switch (lcur_lc(parse)) {
783 case lc_builtin:
784 sac = sac_builtin;
785 break;
786 case lc_static:
787 sac = sac_static;
788 break;
789 default:
790 cspan_print(lcur_span(parse));
791 printf(" Error: Unexpected attribute '");
792 lem_print(lcur(parse));
793 printf("'.\n");
794 parse_note_error(parse);
795 break;
796 }
797
798 lskip(parse);
799
800 attr = stree_symbol_attr_new(sac);
801 return attr;
802}
803
804/** Parse formal function argument.
805 *
806 * @param parse Parser object.
807 * @return New syntax tree node.
808 */
809static stree_proc_arg_t *parse_proc_arg(parse_t *parse)
810{
811 stree_proc_arg_t *arg;
812 stree_arg_attr_t *attr;
813
814 arg = stree_proc_arg_new();
815 arg->name = parse_ident(parse);
816 lmatch(parse, lc_colon);
817 arg->type = parse_texpr(parse);
818
819#ifdef DEBUG_PARSE_TRACE
820 printf("Parse procedure argument.\n");
821#endif
822 list_init(&arg->attr);
823
824 /* Parse attributes. */
825 while (lcur_lc(parse) == lc_comma && !parse_is_error(parse)) {
826 lskip(parse);
827 attr = parse_arg_attr(parse);
828 list_append(&arg->attr, attr);
829 }
830
831 return arg;
832}
833
834/** Parse argument attribute.
835 *
836 * @param parse Parser object.
837 * @return New syntax tree node.
838 */
839static stree_arg_attr_t *parse_arg_attr(parse_t *parse)
840{
841 stree_arg_attr_t *attr;
842
843 if (lcur_lc(parse) != lc_packed) {
844 cspan_print(lcur_span(parse));
845 printf(" Error: Unexpected attribute '");
846 lem_print(lcur(parse));
847 printf("'.\n");
848 parse_note_error(parse);
849 }
850
851 lskip(parse);
852
853 attr = stree_arg_attr_new(aac_packed);
854 return attr;
855}
856
857/** Parse function signature.
858 *
859 * @param parse Parser object.
860 * @return New syntax tree node.
861 */
862static stree_fun_sig_t *parse_fun_sig(parse_t *parse)
863{
864 stree_fun_sig_t *sig;
865 stree_proc_arg_t *arg;
866
867 sig = stree_fun_sig_new();
868
869 lmatch(parse, lc_lparen);
870
871#ifdef DEBUG_PARSE_TRACE
872 printf("Parsing function signature.\n");
873#endif
874
875 list_init(&sig->args);
876
877 if (lcur_lc(parse) != lc_rparen) {
878
879 /* Parse formal parameters. */
880 while (!parse_is_error(parse)) {
881 arg = parse_proc_arg(parse);
882
883 if (stree_arg_has_attr(arg, aac_packed)) {
884 sig->varg = arg;
885 break;
886 } else {
887 list_append(&sig->args, arg);
888 }
889
890 if (lcur_lc(parse) == lc_rparen)
891 break;
892
893 lmatch(parse, lc_scolon);
894 }
895 }
896
897 lmatch(parse, lc_rparen);
898
899 if (lcur_lc(parse) == lc_colon) {
900 lskip(parse);
901 sig->rtype = parse_texpr(parse);
902 } else {
903 sig->rtype = NULL;
904 }
905
906 return sig;
907}
908
909/** Parse member property getter.
910 *
911 * @param parse Parser object.
912 * @param prop Property containing this declaration.
913 */
914static void parse_prop_get(parse_t *parse, stree_prop_t *prop)
915{
916 cspan_t *cspan;
917 stree_block_t *block;
918 stree_proc_t *getter;
919 bool_t body_expected;
920
921 body_expected = (prop->symbol->outer_csi->cc != csi_interface);
922
923 lskip(parse);
924 cspan = lprev_span(parse);
925
926 if (prop->getter != NULL) {
927 cspan_print(cspan);
928 printf(" Error: Duplicate getter.\n");
929 parse_note_error(parse);
930 return;
931 }
932
933 if (lcur_lc(parse) == lc_scolon) {
934 /* Body not present */
935 lskip(parse);
936 block = NULL;
937
938 if (body_expected) {
939 cspan_print(prop->name->cspan);
940 printf(" Error: Property '");
941 symbol_print_fqn(prop->symbol);
942 printf("' getter should have "
943 "a body.\n");
944 parse_note_error(parse);
945 }
946 } else {
947 /* Body present */
948 lmatch(parse, lc_is);
949 block = parse_block(parse);
950 lmatch(parse, lc_end);
951
952 if (!body_expected) {
953 cspan_print(prop->name->cspan);
954 printf(" Error: Property '");
955 symbol_print_fqn(prop->symbol);
956 printf("' getter declaration should "
957 "not have a body.\n");
958 parse_note_error(parse);
959
960 /* XXX Free block */
961 block = NULL;
962 }
963 }
964
965 /* Create getter procedure */
966 getter = stree_proc_new();
967 getter->body = block;
968 getter->outer_symbol = prop->symbol;
969
970 /* Store getter in property. */
971 prop->getter = getter;
972}
973
974/** Parse member property setter.
975 *
976 * @param parse Parser object.
977 * @param prop Property containing this declaration.
978 */
979static void parse_prop_set(parse_t *parse, stree_prop_t *prop)
980{
981 cspan_t *cspan;
982 stree_block_t *block;
983 stree_proc_t *setter;
984 bool_t body_expected;
985
986 body_expected = (prop->symbol->outer_csi->cc != csi_interface);
987
988 lskip(parse);
989 cspan = lprev_span(parse);
990
991 if (prop->setter != NULL) {
992 cspan_print(cspan);
993 printf(" Error: Duplicate setter.\n");
994 parse_note_error(parse);
995 return;
996 }
997
998 prop->setter_arg = stree_proc_arg_new();
999 prop->setter_arg->name = parse_ident(parse);
1000 prop->setter_arg->type = prop->type;
1001
1002 if (lcur_lc(parse) == lc_scolon) {
1003 /* Body not present */
1004 lskip(parse);
1005
1006 block = NULL;
1007
1008 if (body_expected) {
1009 cspan_print(prop->name->cspan);
1010 printf(" Error: Property '");
1011 symbol_print_fqn(prop->symbol);
1012 printf("' setter should have "
1013 "a body.\n");
1014 parse_note_error(parse);
1015 }
1016 } else {
1017 /* Body present */
1018 lmatch(parse, lc_is);
1019 block = parse_block(parse);
1020 lmatch(parse, lc_end);
1021
1022 if (!body_expected) {
1023 cspan_print(prop->name->cspan);
1024 printf(" Error: Property '");
1025 symbol_print_fqn(prop->symbol);
1026 printf("' setter declaration should "
1027 "not have a body.\n");
1028 parse_note_error(parse);
1029 }
1030 }
1031
1032 /* Create setter procedure */
1033 setter = stree_proc_new();
1034 setter->body = block;
1035 setter->outer_symbol = prop->symbol;
1036
1037 /* Store setter in property. */
1038 prop->setter = setter;
1039}
1040
1041/** Parse statement block.
1042 *
1043 * @param parse Parser object.
1044 * @return New syntax tree node.
1045 */
1046static stree_block_t *parse_block(parse_t *parse)
1047{
1048 stree_block_t *block;
1049 stree_stat_t *stat;
1050
1051 block = stree_block_new();
1052 list_init(&block->stats);
1053
1054 /* Avoid peeking if there is an error condition. */
1055 if (parse_is_error(parse))
1056 return block;
1057
1058 while (terminates_block(lcur_lc(parse)) != b_true &&
1059 !parse_is_error(parse)) {
1060
1061 stat = parse_stat(parse);
1062 list_append(&block->stats, stat);
1063 }
1064
1065 return block;
1066}
1067
1068/** Parse statement.
1069 *
1070 * @param parse Parser object.
1071 * @return New syntax tree node.
1072 */
1073stree_stat_t *parse_stat(parse_t *parse)
1074{
1075 stree_stat_t *stat;
1076
1077 stree_vdecl_t *vdecl_s;
1078 stree_if_t *if_s;
1079 stree_switch_t *switch_s;
1080 stree_while_t *while_s;
1081 stree_for_t *for_s;
1082 stree_raise_t *raise_s;
1083 stree_break_t *break_s;
1084 stree_return_t *return_s;
1085 stree_wef_t *wef_s;
1086 stree_exps_t *exp_s;
1087
1088#ifdef DEBUG_PARSE_TRACE
1089 printf("Parse statement.\n");
1090#endif
1091 switch (lcur_lc(parse)) {
1092 case lc_var:
1093 vdecl_s = parse_vdecl(parse);
1094 stat = stree_stat_new(st_vdecl);
1095 stat->u.vdecl_s = vdecl_s;
1096 break;
1097 case lc_if:
1098 if_s = parse_if(parse);
1099 stat = stree_stat_new(st_if);
1100 stat->u.if_s = if_s;
1101 break;
1102 case lc_switch:
1103 switch_s = parse_switch(parse);
1104 stat = stree_stat_new(st_switch);
1105 stat->u.switch_s = switch_s;
1106 break;
1107 case lc_while:
1108 while_s = parse_while(parse);
1109 stat = stree_stat_new(st_while);
1110 stat->u.while_s = while_s;
1111 break;
1112 case lc_for:
1113 for_s = parse_for(parse);
1114 stat = stree_stat_new(st_for);
1115 stat->u.for_s = for_s;
1116 break;
1117 case lc_raise:
1118 raise_s = parse_raise(parse);
1119 stat = stree_stat_new(st_raise);
1120 stat->u.raise_s = raise_s;
1121 break;
1122 case lc_break:
1123 break_s = parse_break(parse);
1124 stat = stree_stat_new(st_break);
1125 stat->u.break_s = break_s;
1126 break;
1127 case lc_return:
1128 return_s = parse_return(parse);
1129 stat = stree_stat_new(st_return);
1130 stat->u.return_s = return_s;
1131 break;
1132 case lc_do:
1133 case lc_with:
1134 wef_s = parse_wef(parse);
1135 stat = stree_stat_new(st_wef);
1136 stat->u.wef_s = wef_s;
1137 break;
1138 default:
1139 exp_s = parse_exps(parse);
1140 stat = stree_stat_new(st_exps);
1141 stat->u.exp_s = exp_s;
1142 break;
1143 }
1144
1145#ifdef DEBUG_PARSE_TRACE
1146 printf("Parsed statement %p\n", stat);
1147#endif
1148 return stat;
1149}
1150
1151/** Parse variable declaration statement.
1152 *
1153 * @param parse Parser object.
1154 * @return New syntax tree node.
1155 */
1156static stree_vdecl_t *parse_vdecl(parse_t *parse)
1157{
1158 stree_vdecl_t *vdecl;
1159
1160 vdecl = stree_vdecl_new();
1161
1162 lmatch(parse, lc_var);
1163 vdecl->name = parse_ident(parse);
1164 lmatch(parse, lc_colon);
1165 vdecl->type = parse_texpr(parse);
1166
1167 if (lcur_lc(parse) == lc_assign) {
1168 lskip(parse);
1169 (void) parse_expr(parse);
1170 }
1171
1172 lmatch(parse, lc_scolon);
1173
1174#ifdef DEBUG_PARSE_TRACE
1175 printf("Parsed vdecl for '%s'\n", strtab_get_str(vdecl->name->sid));
1176 printf("vdecl = %p, vdecl->name = %p, sid=%d\n",
1177 vdecl, vdecl->name, vdecl->name->sid);
1178#endif
1179 return vdecl;
1180}
1181
1182/** Parse @c if statement.
1183 *
1184 * @param parse Parser object.
1185 * @return New syntax tree node.
1186 */
1187static stree_if_t *parse_if(parse_t *parse)
1188{
1189 stree_if_t *if_s;
1190 stree_if_clause_t *if_c;
1191
1192#ifdef DEBUG_PARSE_TRACE
1193 printf("Parse 'if' statement.\n");
1194#endif
1195 if_s = stree_if_new();
1196 list_init(&if_s->if_clauses);
1197
1198 /* Parse @c if clause. */
1199 lmatch(parse, lc_if);
1200
1201 if_c = stree_if_clause_new();
1202 if_c->cond = parse_expr(parse);
1203 lmatch(parse, lc_then);
1204 if_c->block = parse_block(parse);
1205
1206 list_append(&if_s->if_clauses, if_c);
1207
1208 /* Parse @c elif clauses. */
1209 while (lcur_lc(parse) == lc_elif) {
1210 lskip(parse);
1211 if_c = stree_if_clause_new();
1212 if_c->cond = parse_expr(parse);
1213 lmatch(parse, lc_then);
1214 if_c->block = parse_block(parse);
1215
1216 list_append(&if_s->if_clauses, if_c);
1217 }
1218
1219 /* Parse @c else clause. */
1220 if (lcur_lc(parse) == lc_else) {
1221 lskip(parse);
1222 if_s->else_block = parse_block(parse);
1223 } else {
1224 if_s->else_block = NULL;
1225 }
1226
1227 lmatch(parse, lc_end);
1228 return if_s;
1229}
1230
1231/** Parse @c switch statement.
1232 *
1233 * @param parse Parser object.
1234 * @return New syntax tree node.
1235 */
1236static stree_switch_t *parse_switch(parse_t *parse)
1237{
1238 stree_switch_t *switch_s;
1239 stree_when_t *when_c;
1240 stree_expr_t *expr;
1241
1242#ifdef DEBUG_PARSE_TRACE
1243 printf("Parse 'switch' statement.\n");
1244#endif
1245 lmatch(parse, lc_switch);
1246
1247 switch_s = stree_switch_new();
1248 list_init(&switch_s->when_clauses);
1249
1250 switch_s->expr = parse_expr(parse);
1251 lmatch(parse, lc_is);
1252
1253 /* Parse @c when clauses. */
1254 while (lcur_lc(parse) == lc_when) {
1255 lskip(parse);
1256 when_c = stree_when_new();
1257 list_init(&when_c->exprs);
1258 while (b_true) {
1259 expr = parse_expr(parse);
1260 list_append(&when_c->exprs, expr);
1261 if (lcur_lc(parse) != lc_comma)
1262 break;
1263 lskip(parse);
1264 }
1265
1266 lmatch(parse, lc_do);
1267 when_c->block = parse_block(parse);
1268
1269 list_append(&switch_s->when_clauses, when_c);
1270 }
1271
1272 /* Parse @c else clause. */
1273 if (lcur_lc(parse) == lc_else) {
1274 lskip(parse);
1275 lmatch(parse, lc_do);
1276 switch_s->else_block = parse_block(parse);
1277 } else {
1278 switch_s->else_block = NULL;
1279 }
1280
1281 lmatch(parse, lc_end);
1282 return switch_s;
1283}
1284
1285/** Parse @c while statement.
1286 *
1287 * @param parse Parser object.
1288 */
1289static stree_while_t *parse_while(parse_t *parse)
1290{
1291 stree_while_t *while_s;
1292
1293#ifdef DEBUG_PARSE_TRACE
1294 printf("Parse 'while' statement.\n");
1295#endif
1296 while_s = stree_while_new();
1297
1298 lmatch(parse, lc_while);
1299 while_s->cond = parse_expr(parse);
1300 lmatch(parse, lc_do);
1301 while_s->body = parse_block(parse);
1302 lmatch(parse, lc_end);
1303
1304 return while_s;
1305}
1306
1307/** Parse @c for statement.
1308 *
1309 * @param parse Parser object.
1310 * @return New syntax tree node.
1311 */
1312static stree_for_t *parse_for(parse_t *parse)
1313{
1314 stree_for_t *for_s;
1315
1316#ifdef DEBUG_PARSE_TRACE
1317 printf("Parse 'for' statement.\n");
1318#endif
1319 for_s = stree_for_new();
1320
1321 lmatch(parse, lc_for);
1322 lmatch(parse, lc_ident);
1323 lmatch(parse, lc_colon);
1324 (void) parse_texpr(parse);
1325 lmatch(parse, lc_in);
1326 (void) parse_expr(parse);
1327 lmatch(parse, lc_do);
1328 for_s->body = parse_block(parse);
1329 lmatch(parse, lc_end);
1330
1331 return for_s;
1332}
1333
1334/** Parse @c raise statement.
1335 *
1336 * @param parse Parser object.
1337 */
1338static stree_raise_t *parse_raise(parse_t *parse)
1339{
1340 stree_raise_t *raise_s;
1341
1342#ifdef DEBUG_PARSE_TRACE
1343 printf("Parse 'raise' statement.\n");
1344#endif
1345 raise_s = stree_raise_new();
1346 lmatch(parse, lc_raise);
1347 raise_s->expr = parse_expr(parse);
1348 lmatch(parse, lc_scolon);
1349
1350 return raise_s;
1351}
1352
1353/** Parse @c break statement.
1354 *
1355 * @param parse Parser object.
1356 * @return New syntax tree node.
1357 */
1358static stree_break_t *parse_break(parse_t *parse)
1359{
1360 stree_break_t *break_s;
1361
1362#ifdef DEBUG_PARSE_TRACE
1363 printf("Parse 'break' statement.\n");
1364#endif
1365 break_s = stree_break_new();
1366
1367 lmatch(parse, lc_break);
1368 lmatch(parse, lc_scolon);
1369
1370 return break_s;
1371}
1372
1373/** Parse @c return statement.
1374 *
1375 * @param parse Parser object.
1376 * @return New syntax tree node.
1377 */
1378static stree_return_t *parse_return(parse_t *parse)
1379{
1380 stree_return_t *return_s;
1381
1382#ifdef DEBUG_PARSE_TRACE
1383 printf("Parse 'return' statement.\n");
1384#endif
1385 return_s = stree_return_new();
1386
1387 lmatch(parse, lc_return);
1388
1389 if (lcur_lc(parse) != lc_scolon)
1390 return_s->expr = parse_expr(parse);
1391
1392 lmatch(parse, lc_scolon);
1393
1394 return return_s;
1395}
1396
1397/** Parse @c with-except-finally statement.
1398 *
1399 * @param parse Parser object.
1400 * @return New syntax tree node.
1401 */
1402static stree_wef_t *parse_wef(parse_t *parse)
1403{
1404 stree_wef_t *wef_s;
1405 stree_except_t *except_c;
1406
1407#ifdef DEBUG_PARSE_TRACE
1408 printf("Parse WEF statement.\n");
1409#endif
1410 wef_s = stree_wef_new();
1411 list_init(&wef_s->except_clauses);
1412
1413 if (lcur_lc(parse) == lc_with) {
1414 lmatch(parse, lc_with);
1415 lmatch(parse, lc_ident);
1416 lmatch(parse, lc_colon);
1417 (void) parse_texpr(parse);
1418 lmatch(parse, lc_assign);
1419 (void) parse_expr(parse);
1420 }
1421
1422 lmatch(parse, lc_do);
1423 wef_s->with_block = parse_block(parse);
1424
1425 while (lcur_lc(parse) == lc_except && !parse_is_error(parse)) {
1426 except_c = parse_except(parse);
1427 list_append(&wef_s->except_clauses, except_c);
1428 }
1429
1430 if (lcur_lc(parse) == lc_finally) {
1431 lmatch(parse, lc_finally);
1432 lmatch(parse, lc_do);
1433 wef_s->finally_block = parse_block(parse);
1434 } else {
1435 wef_s->finally_block = NULL;
1436 }
1437
1438 lmatch(parse, lc_end);
1439
1440 return wef_s;
1441}
1442
1443/** Parse expression statement.
1444 *
1445 * @param parse Parser object.
1446 * @return New syntax tree node.
1447 */
1448static stree_exps_t *parse_exps(parse_t *parse)
1449{
1450 stree_expr_t *expr;
1451 stree_exps_t *exps;
1452
1453#ifdef DEBUG_PARSE_TRACE
1454 printf("Parse expression statement.\n");
1455#endif
1456 expr = parse_expr(parse);
1457 lmatch(parse, lc_scolon);
1458
1459 exps = stree_exps_new();
1460 exps->expr = expr;
1461
1462 return exps;
1463}
1464
1465/** Parse @c except clause.
1466 *
1467 * @param parse Parser object.
1468 * @return New syntax tree node.
1469 */
1470static stree_except_t *parse_except(parse_t *parse)
1471{
1472 stree_except_t *except_c;
1473
1474#ifdef DEBUG_PARSE_TRACE
1475 printf("Parse 'except' statement.\n");
1476#endif
1477 except_c = stree_except_new();
1478
1479 lmatch(parse, lc_except);
1480 except_c->evar = parse_ident(parse);
1481 lmatch(parse, lc_colon);
1482 except_c->etype = parse_texpr(parse);
1483 lmatch(parse, lc_do);
1484
1485 except_c->block = parse_block(parse);
1486
1487 return except_c;
1488}
1489
1490/** Parse identifier.
1491 *
1492 * @param parse Parser object.
1493 * @return New syntax tree node.
1494 */
1495stree_ident_t *parse_ident(parse_t *parse)
1496{
1497 stree_ident_t *ident;
1498
1499#ifdef DEBUG_PARSE_TRACE
1500 printf("Parse identifier.\n");
1501#endif
1502 lcheck(parse, lc_ident);
1503 ident = stree_ident_new();
1504 ident->sid = lcur(parse)->u.ident.sid;
1505 ident->cspan = lcur_span(parse);
1506 lskip(parse);
1507
1508 return ident;
1509}
1510
1511/** Signal a parse error, start bailing out from parser.
1512 *
1513 * @param parse Parser object.
1514 */
1515void parse_raise_error(parse_t *parse)
1516{
1517 parse->error = b_true;
1518 parse->error_bailout = b_true;
1519}
1520
1521/** Note a parse error that has been immediately recovered.
1522 *
1523 * @param parse Parser object.
1524 */
1525void parse_note_error(parse_t *parse)
1526{
1527 parse->error = b_true;
1528}
1529
1530/** Check if we are currently bailing out of parser due to a parse error.
1531 *
1532 * @param parse Parser object.
1533 */
1534bool_t parse_is_error(parse_t *parse)
1535{
1536 return parse->error_bailout;
1537}
1538
1539/** Recover from parse error bailout.
1540 *
1541 * Still remember that there was an error, but stop bailing out.
1542 *
1543 * @param parse Parser object.
1544 */
1545void parse_recover_error(parse_t *parse)
1546{
1547 assert(parse->error == b_true);
1548 assert(parse->error_bailout == b_true);
1549
1550 parse->error_bailout = b_false;
1551}
1552
1553/** Return current lem.
1554 *
1555 * @param parse Parser object.
1556 * @return Pointer to current lem. Only valid until the lexing
1557 * position is advanced.
1558 */
1559lem_t *lcur(parse_t *parse)
1560{
1561#ifdef DEBUG_LPARSE_TRACE
1562 printf("lcur()\n");
1563#endif
1564 return lex_get_current(parse->lex);
1565}
1566
1567/** Return current lem lclass.
1568 *
1569 * @param parse Parser object
1570 * @return Lclass of the current lem
1571 */
1572lclass_t lcur_lc(parse_t *parse)
1573{
1574 lem_t *lem;
1575
1576 /*
1577 * This allows us to skip error checking in many places. If there is an
1578 * active error, lcur_lc() returns lc_invalid without reading input.
1579 *
1580 * Without this measure we would have to check for error all the time
1581 * or risk requiring extra input from the user (in interactive mode)
1582 * before actually bailing out from the parser.
1583 */
1584 if (parse_is_error(parse))
1585 return lc_invalid;
1586
1587 lem = lcur(parse);
1588 return lem->lclass;
1589}
1590
1591/** Return coordinate span of current lem.
1592 *
1593 * @param parse Parser object
1594 * @return Coordinate span of current lem or @c NULL if a
1595 * parse error is active
1596 */
1597cspan_t *lcur_span(parse_t *parse)
1598{
1599 lem_t *lem;
1600
1601 if (parse_is_error(parse))
1602 return NULL;
1603
1604 lem = lcur(parse);
1605 return lem->cspan;
1606}
1607
1608/** Return coordinate span of previous lem.
1609 *
1610 * @param parse Parser object
1611 * @return Coordinate span of previous lem or @c NULL if
1612 * parse error is active or previous lem is not
1613 * available.
1614 */
1615cspan_t *lprev_span(parse_t *parse)
1616{
1617 lem_t *lem;
1618
1619 if (parse_is_error(parse))
1620 return NULL;
1621
1622 lem = lex_peek_prev(parse->lex);
1623 if (lem == NULL)
1624 return NULL;
1625
1626 return lem->cspan;
1627}
1628
1629/** Skip to next lem.
1630 *
1631 * @param parse Parser object.
1632 */
1633void lskip(parse_t *parse)
1634{
1635#ifdef DEBUG_LPARSE_TRACE
1636 printf("lskip()\n");
1637#endif
1638 lex_next(parse->lex);
1639}
1640
1641/** Verify that lclass of current lem is @a lc.
1642 *
1643 * If a lem of different lclass is found, a parse error is raised and
1644 * a message is printed.
1645 *
1646 * @param parse Parser object.
1647 * @param lc Expected lclass.
1648 */
1649void lcheck(parse_t *parse, lclass_t lc)
1650{
1651#ifdef DEBUG_LPARSE_TRACE
1652 printf("lcheck(");
1653 lclass_print(lc);
1654 printf(")\n");
1655#endif
1656 if (lcur(parse)->lclass != lc) {
1657 lem_print_coords(lcur(parse));
1658 printf(" Error: expected '");
1659 lclass_print(lc);
1660 printf("', got '");
1661 lem_print(lcur(parse));
1662 printf("'.\n");
1663 parse_raise_error(parse);
1664 }
1665}
1666
1667/** Verify that lclass of current lem is @a lc and go to next lem.
1668 *
1669 * If a lem of different lclass is found, a parse error is raised and
1670 * a message is printed.
1671 *
1672 * @param parse Parser object.
1673 * @param lc Expected lclass.
1674 */
1675void lmatch(parse_t *parse, lclass_t lc)
1676{
1677#ifdef DEBUG_LPARSE_TRACE
1678 printf("lmatch(");
1679 lclass_print(lc);
1680 printf(")\n");
1681#endif
1682 /*
1683 * This allows us to skip error checking in many places. If there is an
1684 * active error, lmatch() does nothing (similar to parse_block(), etc.
1685 *
1686 * Without this measure we would have to check for error all the time
1687 * or risk requiring extra input from the user (in interactive mode)
1688 * before actually bailing out from the parser.
1689 */
1690 if (parse_is_error(parse))
1691 return;
1692
1693 lcheck(parse, lc);
1694 lskip(parse);
1695}
1696
1697/** Raise and display generic parsing error.
1698 *
1699 * @param parse Parser object.
1700 */
1701void lunexpected_error(parse_t *parse)
1702{
1703 lem_print_coords(lcur(parse));
1704 printf(" Error: unexpected token '");
1705 lem_print(lcur(parse));
1706 printf("'.\n");
1707 parse_raise_error(parse);
1708}
1709
1710/** Determine whether @a lclass is in follow(block).
1711 *
1712 * Tests whether @a lclass belongs to the follow(block) set, i.e. if it is
1713 * lclass of a lem that can follow a block in the program.
1714 *
1715 * @param lclass Lclass.
1716 */
1717bool_t terminates_block(lclass_t lclass)
1718{
1719 switch (lclass) {
1720 case lc_elif:
1721 case lc_else:
1722 case lc_end:
1723 case lc_except:
1724 case lc_finally:
1725 case lc_when:
1726 return b_true;
1727 default:
1728 return b_false;
1729 }
1730}
Note: See TracBrowser for help on using the repository browser.