-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathparser.c
2399 lines (1984 loc) · 59.4 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
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <config.h>
#include "token_t.h"
#include "parser_t.h"
#include <assert.h>
#include <stdio.h>
#include <stdarg.h>
#include "symbol_table_t.h"
#include "lexer.h"
#include "symbol.h"
#include "type_hash.h"
#include "ast_t.h"
#include "type_t.h"
#include "adt/array.h"
#include "adt/obst.h"
#include "adt/util.h"
#include "adt/error.h"
//#define ABORT_ON_ERROR
//#define PRINT_TOKENS
static expression_parse_function_t *expression_parsers;
static parse_statement_function *statement_parsers;
static parse_declaration_function *declaration_parsers;
static parse_attribute_function *attribute_parsers;
static unsigned char token_anchor_set[T_LAST_TOKEN];
static symbol_t *current_module_name;
static context_t *current_context;
static int error = 0;
token_t token;
module_t *modules;
static inline void *allocate_ast_zero(size_t size)
{
void *res = allocate_ast(size);
memset(res, 0, size);
return res;
}
static size_t get_entity_struct_size(entity_kind_t kind)
{
static const size_t sizes[] = {
[ENTITY_ERROR] = sizeof(entity_base_t),
[ENTITY_FUNCTION] = sizeof(function_entity_t),
[ENTITY_FUNCTION_PARAMETER] = sizeof(function_parameter_t),
[ENTITY_VARIABLE] = sizeof(variable_t),
[ENTITY_CONSTANT] = sizeof(constant_t),
[ENTITY_TYPE_VARIABLE] = sizeof(type_variable_t),
[ENTITY_TYPEALIAS] = sizeof(typealias_t),
[ENTITY_CONCEPT] = sizeof(concept_t),
[ENTITY_CONCEPT_FUNCTION] = sizeof(concept_function_t),
[ENTITY_LABEL] = sizeof(label_t)
};
assert(kind < sizeof(sizes)/sizeof(sizes[0]));
assert(sizes[kind] != 0);
return sizes[kind];
}
entity_t *allocate_entity(entity_kind_t kind)
{
size_t size = get_entity_struct_size(kind);
entity_t *entity = allocate_ast_zero(size);
entity->kind = kind;
return entity;
}
static size_t get_expression_struct_size(expression_kind_t kind)
{
static const size_t sizes[] = {
[EXPR_ERROR] = sizeof(expression_base_t),
[EXPR_INT_CONST] = sizeof(int_const_t),
[EXPR_FLOAT_CONST] = sizeof(float_const_t),
[EXPR_BOOL_CONST] = sizeof(bool_const_t),
[EXPR_STRING_CONST] = sizeof(string_const_t),
[EXPR_NULL_POINTER] = sizeof(expression_base_t),
[EXPR_REFERENCE] = sizeof(reference_expression_t),
[EXPR_CALL] = sizeof(call_expression_t),
[EXPR_SELECT] = sizeof(select_expression_t),
[EXPR_ARRAY_ACCESS] = sizeof(array_access_expression_t),
[EXPR_SIZEOF] = sizeof(sizeof_expression_t),
[EXPR_FUNC] = sizeof(func_expression_t),
};
if (kind >= EXPR_UNARY_FIRST && kind <= EXPR_UNARY_LAST) {
return sizeof(unary_expression_t);
}
if (kind >= EXPR_BINARY_FIRST && kind <= EXPR_BINARY_LAST) {
return sizeof(binary_expression_t);
}
assert(kind <= sizeof(sizes)/sizeof(sizes[0]));
assert(sizes[kind] != 0);
return sizes[kind];
}
expression_t *allocate_expression(expression_kind_t kind)
{
size_t size = get_expression_struct_size(kind);
expression_t *expression = allocate_ast_zero(size);
expression->kind = kind;
return expression;
}
static size_t get_statement_struct_size(statement_kind_t kind)
{
static const size_t sizes[] = {
[STATEMENT_ERROR] = sizeof(statement_base_t),
[STATEMENT_BLOCK] = sizeof(block_statement_t),
[STATEMENT_RETURN] = sizeof(return_statement_t),
[STATEMENT_DECLARATION] = sizeof(declaration_statement_t),
[STATEMENT_IF] = sizeof(if_statement_t),
[STATEMENT_EXPRESSION] = sizeof(expression_statement_t),
[STATEMENT_GOTO] = sizeof(goto_statement_t),
[STATEMENT_LABEL] = sizeof(label_statement_t),
};
assert(kind < sizeof(sizes)/sizeof(sizes[0]));
assert(sizes[kind] != 0);
return sizes[kind];
};
static statement_t *allocate_statement(statement_kind_t kind)
{
size_t size = get_statement_struct_size(kind);
statement_t *statement = allocate_ast_zero(size);
statement->kind = kind;
return statement;
}
static size_t get_type_struct_size(type_kind_t kind)
{
static const size_t sizes[] = {
[TYPE_ERROR] = sizeof(type_base_t),
[TYPE_VOID] = sizeof(type_base_t),
[TYPE_ATOMIC] = sizeof(atomic_type_t),
[TYPE_COMPOUND_STRUCT] = sizeof(compound_type_t),
[TYPE_COMPOUND_UNION] = sizeof(compound_type_t),
[TYPE_FUNCTION] = sizeof(function_type_t),
[TYPE_POINTER] = sizeof(pointer_type_t),
[TYPE_ARRAY] = sizeof(array_type_t),
[TYPE_TYPEOF] = sizeof(typeof_type_t),
[TYPE_REFERENCE] = sizeof(type_reference_t),
[TYPE_REFERENCE_TYPE_VARIABLE] = sizeof(type_reference_t),
[TYPE_BIND_TYPEVARIABLES] = sizeof(bind_typevariables_type_t)
};
assert(kind < sizeof(sizes)/sizeof(sizes[0]));
assert(sizes[kind] != 0);
return sizes[kind];
}
type_t *allocate_type(type_kind_t kind)
{
size_t size = get_type_struct_size(kind);
type_t *type = obstack_alloc(type_obst, size);
memset(type, 0, size);
type->kind = kind;
return type;
}
void next_token(void)
{
lexer_next_token(&token);
#ifdef PRINT_TOKENS
print_token(stderr, &token);
fprintf(stderr, "\n");
#endif
}
static void replace_token_type(token_type_t type)
{
token.type = type;
}
static inline void eat(token_type_t type)
{
assert(token.type == type);
next_token();
}
static void add_anchor_token(token_type_t token_type)
{
assert(token_type < T_LAST_TOKEN);
++token_anchor_set[token_type];
}
static void rem_anchor_token(token_type_t token_type)
{
assert(token_type < T_LAST_TOKEN);
assert(token_anchor_set[token_type] != 0);
--token_anchor_set[token_type];
}
static inline void parser_found_error(void)
{
error = 1;
#ifdef ABORT_ON_ERROR
abort();
#endif
}
void parser_print_error_prefix(void)
{
fputs(source_position.input_name, stderr);
fputc(':', stderr);
fprintf(stderr, "%d", source_position.linenr);
fputs(": error: ", stderr);
parser_found_error();
}
static void parse_error(const char *message)
{
parser_print_error_prefix();
fprintf(stderr, "parse error: %s\n", message);
}
static void parse_error_expected(const char *message, ...)
{
va_list args;
int first = 1;
if (message != NULL) {
parser_print_error_prefix();
fprintf(stderr, "%s\n", message);
}
parser_print_error_prefix();
fputs("Parse error: got ", stderr);
print_token(stderr, &token);
fputs(", expected ", stderr);
va_start(args, message);
token_type_t token_type = va_arg(args, token_type_t);
while (token_type != 0) {
if (first == 1) {
first = 0;
} else {
fprintf(stderr, ", ");
}
print_token_type(stderr, token_type);
token_type = va_arg(args, token_type_t);
}
va_end(args);
fprintf(stderr, "\n");
}
/**
* error recovery: skip a block and all contained sub-blocks
*/
static void maybe_eat_block(void)
{
if (token.type != T_INDENT)
return;
next_token();
unsigned indent = 1;
while (indent >= 1) {
if (token.type == T_INDENT) {
indent++;
} else if (token.type == T_DEDENT) {
indent--;
} else if (token.type == T_EOF) {
break;
}
next_token();
}
}
/**
* eats nested brace groups
*/
static void eat_until_matching_token(token_type_t type)
{
token_type_t end_token;
switch (type) {
case '(': end_token = ')'; break;
case '{': end_token = '}'; break;
case '[': end_token = ']'; break;
default: end_token = type; break;
}
unsigned parenthesis_count = 0;
unsigned brace_count = 0;
unsigned bracket_count = 0;
while (token.type != end_token ||
parenthesis_count != 0 ||
brace_count != 0 ||
bracket_count != 0) {
switch (token.type) {
case T_EOF: return;
case '(': ++parenthesis_count; break;
case '{': ++brace_count; break;
case '[': ++bracket_count; break;
case ')':
if (parenthesis_count > 0)
--parenthesis_count;
goto check_stop;
case '}':
if (brace_count > 0)
--brace_count;
goto check_stop;
case ']':
if (bracket_count > 0)
--bracket_count;
check_stop:
if (token.type == end_token &&
parenthesis_count == 0 &&
brace_count == 0 &&
bracket_count == 0)
return;
break;
default:
break;
}
next_token();
}
}
/**
* Eat input tokens until an anchor is found.
*/
static void eat_until_anchor(void)
{
while (token_anchor_set[token.type] == 0) {
if (token.type == '(' || token.type == '{' || token.type == '[')
eat_until_matching_token(token.type);
if (token.type == ':') {
next_token();
if (!token_anchor_set[token.type] == 0) {
maybe_eat_block();
}
} else {
next_token();
}
}
}
#define expect(expected, error_label) \
do { \
if (UNLIKELY(token.type != (expected))) { \
parse_error_expected(NULL, (expected), 0); \
add_anchor_token(expected); \
eat_until_anchor(); \
if (token.type == expected) \
next_token(); \
rem_anchor_token(expected); \
goto error_label; \
} \
next_token(); \
} while (0)
static void parse_function(function_t *function);
static statement_t *parse_block(void);
static void parse_parameter_declarations(function_type_t *function_type,
function_parameter_t **parameters);
static atomic_type_kind_t parse_unsigned_atomic_type(void)
{
switch (token.type) {
case T_byte:
next_token();
return ATOMIC_TYPE_UBYTE;
case T_short:
next_token();
return ATOMIC_TYPE_USHORT;
case T_long:
next_token();
if (token.type == T_long) {
next_token();
return ATOMIC_TYPE_ULONGLONG;
}
return ATOMIC_TYPE_ULONG;
case T_int:
next_token();
return ATOMIC_TYPE_UINT;
default:
parse_error_expected("couldn't parse type", T_byte, T_short, T_int,
T_long, 0);
return ATOMIC_TYPE_INVALID;
}
}
static atomic_type_kind_t parse_signed_atomic_type(void)
{
switch (token.type) {
case T_bool:
next_token();
return ATOMIC_TYPE_BOOL;
case T_byte:
next_token();
return ATOMIC_TYPE_BYTE;
case T_short:
next_token();
return ATOMIC_TYPE_SHORT;
case T_long:
next_token();
if (token.type == T_long) {
next_token();
return ATOMIC_TYPE_LONGLONG;
}
return ATOMIC_TYPE_LONG;
case T_int:
next_token();
return ATOMIC_TYPE_INT;
case T_float:
next_token();
return ATOMIC_TYPE_FLOAT;
case T_double:
next_token();
return ATOMIC_TYPE_DOUBLE;
default:
parse_error_expected("couldn't parse type", T_byte, T_short, T_int,
T_long, T_float, T_double, 0);
return ATOMIC_TYPE_INVALID;
}
}
static type_t *parse_atomic_type(void)
{
atomic_type_kind_t akind;
switch (token.type) {
case T_unsigned:
next_token();
akind = parse_unsigned_atomic_type();
break;
case T_signed:
next_token();
/* fallthrough */
default:
akind = parse_signed_atomic_type();
break;
}
type_t *type = allocate_type(TYPE_ATOMIC);
type->atomic.akind = akind;
type_t *result = typehash_insert(type);
if (result != type) {
obstack_free(type_obst, type);
}
return result;
}
static type_argument_t *parse_type_argument(void)
{
type_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
argument->type = parse_type();
return argument;
}
static type_argument_t *parse_type_arguments(void)
{
type_argument_t *first_argument = parse_type_argument();
type_argument_t *last_argument = first_argument;
while (token.type == ',') {
next_token();
type_argument_t *type_argument = parse_type_argument();
last_argument->next = type_argument;
last_argument = type_argument;
}
return first_argument;
}
static type_t *parse_typeof(void)
{
type_t *type = allocate_type(TYPE_TYPEOF);
eat(T_typeof);
expect('(', end_error);
add_anchor_token(')');
type->typeof.expression = parse_expression();
rem_anchor_token(')');
expect(')', end_error);
end_error:
return type;
}
static type_t *parse_type_ref(void)
{
assert(token.type == T_IDENTIFIER);
type_t *type = allocate_type(TYPE_REFERENCE);
type->reference.symbol = token.v.symbol;
type->reference.source_position = source_position;
next_token();
if (token.type == '<') {
next_token();
add_anchor_token('>');
type->reference.type_arguments = parse_type_arguments();
rem_anchor_token('>');
expect('>', end_error);
}
end_error:
return type;
}
static type_t *create_error_type(void)
{
return allocate_type(TYPE_ERROR);
}
static type_t *parse_function_type(void)
{
eat(T_func);
type_t *type = allocate_type(TYPE_FUNCTION);
parse_parameter_declarations(&type->function, NULL);
expect(':', end_error);
type->function.result_type = parse_type();
return type;
end_error:
return create_error_type();
}
static compound_entry_t *parse_compound_entries(void)
{
compound_entry_t *result = NULL;
compound_entry_t *last_entry = NULL;
while (token.type != T_DEDENT && token.type != T_EOF) {
compound_entry_t *entry = allocate_ast_zero(sizeof(entry[0]));
if (token.type != T_IDENTIFIER) {
parse_error_expected("Problem while parsing compound entry",
T_IDENTIFIER, 0);
eat_until_matching_token(T_NEWLINE);
next_token();
continue;
}
entry->symbol = token.v.symbol;
next_token();
expect(':', end_error);
entry->type = parse_type();
entry->attributes = parse_attributes();
if (last_entry == NULL) {
result = entry;
} else {
last_entry->next = entry;
}
last_entry = entry;
expect(T_NEWLINE, end_error);
}
end_error:
return result;
}
static type_t *parse_union_type(void)
{
eat(T_union);
type_t *type = allocate_type(TYPE_COMPOUND_UNION);
type->compound.attributes = parse_attributes();
expect(':', end_error);
expect(T_NEWLINE, end_error);
expect(T_INDENT, end_error);
add_anchor_token(T_DEDENT);
type->compound.entries = parse_compound_entries();
/* force end of statement */
rem_anchor_token(T_DEDENT);
assert(token.type == T_DEDENT);
replace_token_type(T_NEWLINE);
end_error:
return type;
}
static type_t *parse_struct_type(void)
{
eat(T_struct);
type_t *type = allocate_type(TYPE_COMPOUND_STRUCT);
type->compound.attributes = parse_attributes();
expect(':', end_error);
expect(T_NEWLINE, end_error);
expect(T_INDENT, end_error);
add_anchor_token(T_DEDENT);
type->compound.entries = parse_compound_entries();
/* force end of statement */
rem_anchor_token(T_DEDENT);
assert(token.type == T_DEDENT);
replace_token_type(T_NEWLINE);
end_error:
return type;
}
static type_t *make_pointer_type_no_hash(type_t *points_to)
{
type_t *type = allocate_type(TYPE_POINTER);
type->pointer.points_to = points_to;
return type;
}
static type_t *parse_brace_type(void)
{
eat('(');
add_anchor_token(')');
type_t *type = parse_type();
rem_anchor_token(')');
expect(')', end_error);
end_error:
return type;
}
type_t *parse_type(void)
{
type_t *type;
switch (token.type) {
case T_unsigned:
case T_signed:
case T_bool:
case T_int:
case T_long:
case T_byte:
case T_short:
case T_float:
case T_double:
type = parse_atomic_type();
break;
case T_IDENTIFIER:
type = parse_type_ref();
break;
case T_typeof:
type = parse_typeof();
break;
case T_void:
type = type_void;
next_token();
break;
case T_union:
type = parse_union_type();
break;
case T_struct:
type = parse_struct_type();
break;
case T_func:
type = parse_function_type();
break;
case '(':
type = parse_brace_type();
break;
default:
parser_print_error_prefix();
fprintf(stderr, "Token ");
print_token(stderr, &token);
fprintf(stderr, " doesn't start a type\n");
type = type_invalid;
break;
}
/* parse type modifiers */
while (true) {
switch (token.type) {
case '*': {
next_token();
type = make_pointer_type_no_hash(type);
break;
}
case '[': {
next_token();
add_anchor_token(']');
expression_t *size = parse_expression();
rem_anchor_token(']');
expect(']', end_error);
type_t *array_type = allocate_type(TYPE_ARRAY);
array_type->array.element_type = type;
array_type->array.size_expression = size;
type = array_type;
break;
}
default:
return type;
}
}
end_error:
return type;
}
static expression_t *parse_string_const(void)
{
expression_t *expression = allocate_expression(EXPR_STRING_CONST);
expression->string_const.value = token.v.string;
next_token();
return expression;
}
static expression_t *parse_int_const(void)
{
expression_t *expression = allocate_expression(EXPR_INT_CONST);
expression->int_const.value = token.v.intvalue;
next_token();
return expression;
}
static expression_t *parse_true(void)
{
eat(T_true);
expression_t *expression = allocate_expression(EXPR_BOOL_CONST);
expression->bool_const.value = true;
return expression;
}
static expression_t *parse_false(void)
{
eat(T_false);
expression_t *expression = allocate_expression(EXPR_BOOL_CONST);
expression->bool_const.value = false;
return expression;
}
static expression_t *parse_null(void)
{
eat(T_null);
expression_t *expression = allocate_expression(EXPR_NULL_POINTER);
expression->base.type = make_pointer_type(type_void);
return expression;
}
static expression_t *parse_func_expression(void)
{
eat(T_func);
expression_t *expression = allocate_expression(EXPR_FUNC);
parse_function(&expression->func.function);
return expression;
}
static expression_t *parse_reference(void)
{
expression_t *expression = allocate_expression(EXPR_REFERENCE);
expression->reference.symbol = token.v.symbol;
next_token();
if (token.type == T_TYPESTART) {
next_token();
add_anchor_token('>');
expression->reference.type_arguments = parse_type_arguments();
rem_anchor_token('>');
expect('>', end_error);
}
end_error:
return expression;
}
static expression_t *create_error_expression(void)
{
expression_t *expression = allocate_expression(EXPR_ERROR);
expression->base.type = create_error_type();
return expression;
}
static expression_t *parse_sizeof(void)
{
eat(T_sizeof);
expression_t *expression = allocate_expression(EXPR_SIZEOF);
if (token.type == '(') {
next_token();
type_t *type = allocate_type(TYPE_TYPEOF);
add_anchor_token(')');
type->typeof.expression = parse_expression();
rem_anchor_token(')');
expect(')', end_error);
expression->sizeofe.type = type;
} else {
expect('<', end_error);
add_anchor_token('>');
expression->sizeofe.type = parse_type();
rem_anchor_token('>');
expect('>', end_error);
}
return expression;
end_error:
return create_error_expression();
}
void register_statement_parser(parse_statement_function parser,
token_type_t token_type)
{
unsigned len = (unsigned) ARR_LEN(statement_parsers);
if (token_type >= len) {
ARR_RESIZE(parse_statement_function, statement_parsers, token_type + 1);
memset(& statement_parsers[len], 0,
(token_type - len + 1) * sizeof(statement_parsers[0]));
}
if (statement_parsers[token_type] != NULL) {
fprintf(stderr, "for token ");
print_token_type(stderr, token_type);
fprintf(stderr, "\n");
panic("Trying to register multiple statement parsers for 1 token");
}
statement_parsers[token_type] = parser;
}
void register_declaration_parser(parse_declaration_function parser,
token_type_t token_type)
{
unsigned len = (unsigned) ARR_LEN(declaration_parsers);
if (token_type >= len) {
ARR_RESIZE(parse_declaration_function, declaration_parsers, token_type + 1);
memset(& declaration_parsers[len], 0,
(token_type - len + 1) * sizeof(declaration_parsers[0]));
}
if (declaration_parsers[token_type] != NULL) {
fprintf(stderr, "for token ");
print_token_type(stderr, token_type);
fprintf(stderr, "\n");
panic("trying to register multiple namespace parsers for 1 token");
}
declaration_parsers[token_type] = parser;
}
void register_attribute_parser(parse_attribute_function parser,
token_type_t token_type)
{
unsigned len = (unsigned) ARR_LEN(attribute_parsers);
if (token_type >= len) {
ARR_RESIZE(parse_attribute_function, attribute_parsers, token_type + 1);
memset(& attribute_parsers[len], 0,
(token_type - len + 1) * sizeof(attribute_parsers[0]));
}
if (attribute_parsers[token_type] != NULL) {
fprintf(stderr, "for token ");
print_token_type(stderr, token_type);
fprintf(stderr, "\n");
panic("trying to register multiple namespace parsers for 1 token");
}
attribute_parsers[token_type] = parser;
}
static expression_parse_function_t *get_expression_parser_entry(token_type_t token_type)
{
unsigned len = (unsigned) ARR_LEN(expression_parsers);
if (token_type >= len) {
ARR_RESIZE(expression_parse_function_t, expression_parsers, token_type + 1);
memset(& expression_parsers[len], 0,
(token_type - len + 1) * sizeof(expression_parsers[0]));
}
return &expression_parsers[token_type];
}
void register_expression_parser(parse_expression_function parser,
token_type_t token_type)
{
expression_parse_function_t *entry
= get_expression_parser_entry(token_type);
if (entry->parser != NULL) {
fprintf(stderr, "for token ");
print_token_type(stderr, token_type);
fprintf(stderr, "\n");
panic("trying to register multiple expression parsers for a token");
}
entry->parser = parser;
}
void register_expression_infix_parser(parse_expression_infix_function parser,
token_type_t token_type,
unsigned precedence)
{
expression_parse_function_t *entry
= get_expression_parser_entry(token_type);
if (entry->infix_parser != NULL) {
fprintf(stderr, "for token ");
print_token_type(stderr, token_type);
fprintf(stderr, "\n");
panic("trying to register multiple infix expression parsers for a "
"token");
}
entry->infix_parser = parser;
entry->infix_precedence = precedence;
}
static expression_t *expected_expression_error(void)
{
parser_print_error_prefix();
fprintf(stderr, "expected expression, got token ");
print_token(stderr, & token);
fprintf(stderr, "\n");
return create_error_expression();
}
static expression_t *parse_parenthesized_expression(void)
{
eat('(');
add_anchor_token(')');
expression_t *result = parse_expression();
rem_anchor_token(')');
expect(')', end_error);
end_error:
return result;
}
static expression_t *parse_cast_expression(void)
{
eat(T_cast);
expression_t *expression = allocate_expression(EXPR_UNARY_CAST);
expect('<', end_error);
expression->base.type = parse_type();
expect('>', end_error);
expression->unary.value = parse_sub_expression(PREC_CAST);
end_error:
return expression;
}
static expression_t *parse_call_expression(expression_t *left)
{
expression_t *expression = allocate_expression(EXPR_CALL);
expression->call.function = left;
/* parse arguments */
eat('(');
add_anchor_token(')');
add_anchor_token(',');
if (token.type != ')') {
call_argument_t *last_argument = NULL;
while (true) {
call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
argument->expression = parse_expression();
if (last_argument == NULL) {
expression->call.arguments = argument;
} else {
last_argument->next = argument;
}
last_argument = argument;
if (token.type != ',')
break;
next_token();
}
}
rem_anchor_token(',');
rem_anchor_token(')');
expect(')', end_error);
end_error:
return expression;
}