-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.c
3737 lines (3084 loc) · 101 KB
/
parse.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 "parse.h"
#include "alloc.h"
#include "lex.h"
#include "log.h"
#include "program.h"
#include "util.h"
#include "var_table.h"
#include "vector.h"
#include <ctype.h>
#include <string.h>
#define EXP_PARSE(e, diag) \
do { \
if (!(e)) { \
BUG("failure during parsing: %s", diag); \
} \
} while (0)
struct parser {
struct arena_allocator *arena;
struct lexer *lexer;
struct var_table ty_table;
struct vector *diagnostics;
};
enum parser_create_result parser_create(struct program *program,
struct preproc *preproc,
struct parser **parser) {
struct parser *p = nonnull_malloc(sizeof(*p));
arena_allocator_create(&p->arena);
if (lexer_create(program, preproc, &p->lexer) != LEX_CREATE_RESULT_SUCCESS) {
err("failed to create lexer");
return PARSER_CREATE_RESULT_FAILURE;
}
p->ty_table = var_table_create(p->arena);
*parser = p;
return PARSER_CREATE_RESULT_SUCCESS;
}
void parser_free(struct parser **parser) {
lexer_free(&(*parser)->lexer);
arena_allocator_free(&(*parser)->arena);
(*parser)->arena = NULL;
free(*parser);
*parser = NULL;
}
enum ast_associativity {
AST_ASSOCIATIVITY_NONE,
AST_ASSOCIATIVITY_LEFT,
AST_ASSOCIATIVITY_RIGHT,
};
struct ast_op_info {
enum ast_binary_op_ty ty;
enum ast_associativity associativity;
unsigned precedence;
};
static struct ast_op_info op_info(enum ast_binary_op_ty ty) {
struct ast_op_info info = {.ty = ty};
switch (ty) {
// ternary
case AST_BINARY_OP_TY_LOGICAL_OR:
info.precedence = 1;
info.associativity = AST_ASSOCIATIVITY_LEFT;
break;
case AST_BINARY_OP_TY_LOGICAL_AND:
info.precedence = 2;
info.associativity = AST_ASSOCIATIVITY_LEFT;
break;
case AST_BINARY_OP_TY_OR:
info.precedence = 3;
info.associativity = AST_ASSOCIATIVITY_LEFT;
break;
case AST_BINARY_OP_TY_XOR:
info.precedence = 4;
info.associativity = AST_ASSOCIATIVITY_LEFT;
break;
case AST_BINARY_OP_TY_AND:
info.precedence = 5;
info.associativity = AST_ASSOCIATIVITY_LEFT;
break;
case AST_BINARY_OP_TY_EQ:
case AST_BINARY_OP_TY_NEQ:
info.precedence = 6;
info.associativity = AST_ASSOCIATIVITY_LEFT;
break;
case AST_BINARY_OP_TY_GT:
case AST_BINARY_OP_TY_GTEQ:
case AST_BINARY_OP_TY_LT:
case AST_BINARY_OP_TY_LTEQ:
info.precedence = 7;
info.associativity = AST_ASSOCIATIVITY_LEFT;
break;
case AST_BINARY_OP_TY_LSHIFT:
case AST_BINARY_OP_TY_RSHIFT:
info.precedence = 8;
info.associativity = AST_ASSOCIATIVITY_LEFT;
break;
case AST_BINARY_OP_TY_ADD:
case AST_BINARY_OP_TY_SUB:
info.precedence = 9;
info.associativity = AST_ASSOCIATIVITY_LEFT;
break;
case AST_BINARY_OP_TY_MUL:
case AST_BINARY_OP_TY_DIV:
case AST_BINARY_OP_TY_QUOT:
info.precedence = 10;
info.associativity = AST_ASSOCIATIVITY_LEFT;
break;
}
return info;
}
static bool op_info_for_token(const struct lex_token *token,
struct ast_op_info *info) {
switch (token->ty) {
case LEX_TOKEN_TY_OP_EQ:
*info = op_info(AST_BINARY_OP_TY_EQ);
return true;
case LEX_TOKEN_TY_OP_NEQ:
*info = op_info(AST_BINARY_OP_TY_NEQ);
return true;
case LEX_TOKEN_TY_OP_GT:
*info = op_info(AST_BINARY_OP_TY_GT);
return true;
case LEX_TOKEN_TY_OP_GTEQ:
*info = op_info(AST_BINARY_OP_TY_GTEQ);
return true;
case LEX_TOKEN_TY_OP_LT:
*info = op_info(AST_BINARY_OP_TY_LT);
return true;
case LEX_TOKEN_TY_OP_LTEQ:
*info = op_info(AST_BINARY_OP_TY_LTEQ);
return true;
case LEX_TOKEN_TY_OP_LSHIFT:
*info = op_info(AST_BINARY_OP_TY_LSHIFT);
return true;
case LEX_TOKEN_TY_OP_RSHIFT:
*info = op_info(AST_BINARY_OP_TY_RSHIFT);
return true;
case LEX_TOKEN_TY_OP_LOGICAL_AND:
*info = op_info(AST_BINARY_OP_TY_LOGICAL_AND);
return true;
case LEX_TOKEN_TY_OP_LOGICAL_OR:
*info = op_info(AST_BINARY_OP_TY_LOGICAL_OR);
return true;
case LEX_TOKEN_TY_OP_AND:
*info = op_info(AST_BINARY_OP_TY_AND);
return true;
case LEX_TOKEN_TY_OP_OR:
*info = op_info(AST_BINARY_OP_TY_OR);
return true;
case LEX_TOKEN_TY_OP_XOR:
*info = op_info(AST_BINARY_OP_TY_XOR);
return true;
case LEX_TOKEN_TY_OP_ADD:
*info = op_info(AST_BINARY_OP_TY_ADD);
return true;
case LEX_TOKEN_TY_OP_SUB:
*info = op_info(AST_BINARY_OP_TY_SUB);
return true;
case LEX_TOKEN_TY_OP_MUL:
*info = op_info(AST_BINARY_OP_TY_MUL);
return true;
case LEX_TOKEN_TY_OP_DIV:
*info = op_info(AST_BINARY_OP_TY_DIV);
return true;
case LEX_TOKEN_TY_OP_QUOT:
*info = op_info(AST_BINARY_OP_TY_QUOT);
return true;
default:
// not an op
return false;
}
}
static bool parse_token(struct parser *parser, enum lex_token_ty ty) {
struct lex_token token;
peek_token(parser->lexer, &token);
if (token.ty == ty) {
consume_token(parser->lexer, token);
return true;
}
return false;
}
static bool parse_identifier(struct parser *parser, struct lex_token *token) {
struct text_pos pos = get_position(parser->lexer);
peek_token(parser->lexer, token);
if (token->ty == LEX_TOKEN_TY_IDENTIFIER) {
consume_token(parser->lexer, *token);
return true;
}
backtrack(parser->lexer, pos);
return false;
}
static bool parse_type_qualifier(struct parser *parser,
enum ast_type_qualifier *qualifier) {
struct lex_token token;
peek_token(parser->lexer, &token);
if (token.ty == LEX_TOKEN_TY_KW_CONST) {
*qualifier = AST_TYPE_QUALIFIER_CONST;
} else if (token.ty == LEX_TOKEN_TY_KW_VOLATILE) {
*qualifier = AST_TYPE_QUALIFIER_VOLATILE;
} else if (token.ty == LEX_TOKEN_TY_KW_RESTRICT) {
*qualifier = AST_TYPE_QUALIFIER_RESTRICT;
} else {
return false;
}
consume_token(parser->lexer, token);
return true;
}
static bool parse_function_specifier(struct parser *parser,
enum ast_function_specifier *specifier) {
struct lex_token token;
peek_token(parser->lexer, &token);
if (token.ty == LEX_TOKEN_TY_KW_INLINE) {
*specifier = AST_FUNCTION_SPECIFIER_INLINE;
} else {
return false;
}
consume_token(parser->lexer, token);
return true;
}
static bool
parse_storage_class_specifier(struct parser *parser,
enum ast_storage_class_specifier *specifier) {
struct lex_token token;
peek_token(parser->lexer, &token);
if (token.ty == LEX_TOKEN_TY_KW_TYPEDEF) {
*specifier = AST_STORAGE_CLASS_SPECIFIER_TYPEDEF;
} else if (token.ty == LEX_TOKEN_TY_KW_EXTERN) {
*specifier = AST_STORAGE_CLASS_SPECIFIER_EXTERN;
} else if (token.ty == LEX_TOKEN_TY_KW_STATIC) {
*specifier = AST_STORAGE_CLASS_SPECIFIER_STATIC;
} else if (token.ty == LEX_TOKEN_TY_KW_AUTO) {
*specifier = AST_STORAGE_CLASS_SPECIFIER_AUTO;
} else if (token.ty == LEX_TOKEN_TY_KW_REGISTER) {
*specifier = AST_STORAGE_CLASS_SPECIFIER_REGISTER;
} else {
return false;
}
consume_token(parser->lexer, token);
return true;
}
static bool parse_type_specifier_kw(struct parser *parser,
enum ast_type_specifier_kw *wkt) {
struct lex_token token;
peek_token(parser->lexer, &token);
switch (token.ty) {
case LEX_TOKEN_TY_KW_VOID:
consume_token(parser->lexer, token);
*wkt = AST_TYPE_SPECIFIER_KW_VOID;
return true;
case LEX_TOKEN_TY_KW_CHAR:
consume_token(parser->lexer, token);
*wkt = AST_TYPE_SPECIFIER_KW_CHAR;
return true;
case LEX_TOKEN_TY_KW_SHORT:
consume_token(parser->lexer, token);
*wkt = AST_TYPE_SPECIFIER_KW_SHORT;
return true;
case LEX_TOKEN_TY_KW_INT:
consume_token(parser->lexer, token);
*wkt = AST_TYPE_SPECIFIER_KW_INT;
return true;
case LEX_TOKEN_TY_KW_LONG:
consume_token(parser->lexer, token);
*wkt = AST_TYPE_SPECIFIER_KW_LONG;
return true;
case LEX_TOKEN_TY_KW_FLOAT:
consume_token(parser->lexer, token);
*wkt = AST_TYPE_SPECIFIER_KW_FLOAT;
return true;
case LEX_TOKEN_TY_KW_DOUBLE:
consume_token(parser->lexer, token);
*wkt = AST_TYPE_SPECIFIER_KW_DOUBLE;
return true;
case LEX_TOKEN_TY_KW_SIGNED:
consume_token(parser->lexer, token);
*wkt = AST_TYPE_SPECIFIER_KW_SIGNED;
return true;
case LEX_TOKEN_TY_KW_UNSIGNED:
consume_token(parser->lexer, token);
*wkt = AST_TYPE_SPECIFIER_KW_UNSIGNED;
return true;
// case LEX_TOKEN_TY_KW_BOOL:
// consume_token(parser->lexer, token);
// *wkt = AST_TYPE_SPECIFIER_KW_BOOL;
// return true;
// case LEX_TOKEN_TY_KW_COMPLEX:
// consume_token(parser->lexer, token);
// *wkt = AST_TYPE_SPECIFIER_KW_COMPLEX;
// return true;
case LEX_TOKEN_TY_KW_HALF:
consume_token(parser->lexer, token);
*wkt = AST_TYPE_SPECIFIER_KW_HALF;
return true;
default:
return false;
}
}
static bool parse_expr(struct parser *parser, struct ast_expr *expr);
static bool parse_enumerator(struct parser *parser,
struct ast_enumerator *enumerator) {
if (!parse_identifier(parser, &enumerator->identifier)) {
return false;
}
if (parse_token(parser, LEX_TOKEN_TY_OP_ASSG)) {
enumerator->value = arena_alloc(parser->arena, sizeof(*enumerator->value));
EXP_PARSE(parse_expr(parser, enumerator->value),
"expected expression after = in enum body");
} else {
enumerator->value = NULL;
}
parse_token(parser, LEX_TOKEN_TY_COMMA);
return true;
}
static void parse_enumerator_list(struct parser *parser,
struct ast_enumerator_list *enumerator_list) {
struct vector *list = vector_create(sizeof(*enumerator_list->enumerators));
struct ast_enumerator enumerator;
while (parse_enumerator(parser, &enumerator)) {
parse_token(parser, LEX_TOKEN_TY_COMMA);
vector_push_back(list, &enumerator);
}
parse_token(parser, LEX_TOKEN_TY_COMMA);
enumerator_list->enumerators =
arena_alloc(parser->arena, vector_byte_size(list));
enumerator_list->num_enumerators = vector_length(list);
vector_copy_to(list, enumerator_list->enumerators);
vector_free(&list);
}
static bool parse_enum_specifier(struct parser *parser,
struct ast_enum_specifier *enum_specifier) {
struct text_pos pos = get_position(parser->lexer);
if (!parse_token(parser, LEX_TOKEN_TY_KW_ENUM)) {
return false;
}
struct lex_token identifier;
if (parse_identifier(parser, &identifier)) {
enum_specifier->identifier =
arena_alloc(parser->arena, sizeof(*enum_specifier->identifier));
*enum_specifier->identifier = identifier;
} else {
enum_specifier->identifier = NULL;
}
struct ast_enumerator_list enumerator_list;
if (parse_token(parser, LEX_TOKEN_TY_OPEN_BRACE)) {
parse_enumerator_list(parser, &enumerator_list);
EXP_PARSE(parse_token(parser, LEX_TOKEN_TY_CLOSE_BRACE),
"expected } after enumerator body");
enum_specifier->enumerator_list =
arena_alloc(parser->arena, sizeof(*enum_specifier->enumerator_list));
*enum_specifier->enumerator_list = enumerator_list;
} else {
enum_specifier->enumerator_list = NULL;
}
if (!enum_specifier->identifier && !enum_specifier->enumerator_list) {
backtrack(parser->lexer, pos);
return false;
}
return true;
}
static void parse_declaration_specifier_list(
struct parser *parser,
struct ast_declaration_specifier_list *specifier_list);
static bool parse_declarator(struct parser *parser,
struct ast_declarator *declarator);
static void
parse_declaration_list(struct parser *parser,
struct ast_declaration_list *declaration_list);
static bool parse_struct_or_union_specifier(
struct parser *parser,
struct ast_struct_or_union_specifier *struct_or_union_specifier) {
struct text_pos pos = get_position(parser->lexer);
enum ast_struct_or_union_specifier_ty ty;
if (parse_token(parser, LEX_TOKEN_TY_KW_STRUCT)) {
ty = AST_STRUCT_OR_UNION_SPECIFIER_TY_STRUCT;
} else if (parse_token(parser, LEX_TOKEN_TY_KW_UNION)) {
ty = AST_STRUCT_OR_UNION_SPECIFIER_TY_UNION;
} else {
return false;
}
struct lex_token identifier;
if (parse_identifier(parser, &identifier)) {
struct_or_union_specifier->identifier = arena_alloc(
parser->arena, sizeof(*struct_or_union_specifier->identifier));
*struct_or_union_specifier->identifier = identifier;
} else {
struct_or_union_specifier->identifier = NULL;
}
struct_or_union_specifier->ty = ty;
struct_or_union_specifier->decl_list = NULL;
if (parse_token(parser, LEX_TOKEN_TY_OPEN_BRACE)) {
struct_or_union_specifier->decl_list = arena_alloc(
parser->arena, sizeof(*struct_or_union_specifier->decl_list));
parse_declaration_list(parser, struct_or_union_specifier->decl_list);
EXP_PARSE(parse_token(parser, LEX_TOKEN_TY_CLOSE_BRACE),
"expected } after struct_or_union_declaration body");
}
if (!struct_or_union_specifier->identifier &&
!struct_or_union_specifier->decl_list) {
backtrack(parser->lexer, pos);
return false;
}
return true;
}
static bool parse_typedef_name(struct parser *parser,
struct lex_token *typedef_name) {
struct lex_token identifier;
peek_token(parser->lexer, &identifier);
if (identifier.ty != LEX_TOKEN_TY_IDENTIFIER) {
return false;
}
struct var_table_entry *entry = var_table_get_entry(
&parser->ty_table, identifier_str(parser, &identifier));
if (!entry) {
return false;
}
*typedef_name = identifier;
consume_token(parser->lexer, identifier);
return true;
}
enum type_specifier_mode {
TYPE_SPECIFIER_MODE_ALLOW_TYPEDEFS,
TYPE_SPECIFIER_MODE_DISALLOW_TYPEDEFS
};
static bool parse_type_specifier(struct parser *parser,
struct ast_type_specifier *type_specifier,
enum type_specifier_mode mode) {
if (parse_type_specifier_kw(parser, &type_specifier->type_specifier_kw)) {
type_specifier->ty = AST_TYPE_SPECIFIER_TY_KW;
return true;
}
if (parse_struct_or_union_specifier(
parser, &type_specifier->struct_or_union_specifier)) {
type_specifier->ty = AST_TYPE_SPECIFIER_STRUCT_OR_UNION;
return true;
}
if (parse_enum_specifier(parser, &type_specifier->enum_specifier)) {
type_specifier->ty = AST_TYPE_SPECIFIER_ENUM;
return true;
}
if (mode == TYPE_SPECIFIER_MODE_ALLOW_TYPEDEFS &&
parse_typedef_name(parser, &type_specifier->typedef_name)) {
type_specifier->ty = AST_TYPE_SPECIFIER_TYPEDEF_NAME;
return true;
}
return false;
}
static bool parse_decl_specifier(struct parser *parser,
struct ast_declaration_specifier *specifier,
enum type_specifier_mode mode) {
if (parse_storage_class_specifier(parser,
&specifier->storage_class_specifier)) {
specifier->ty = AST_DECL_SPECIFIER_TY_STORAGE_CLASS_SPECIFIER;
return true;
}
if (parse_function_specifier(parser, &specifier->function_specifier)) {
specifier->ty = AST_DECL_SPECIFIER_TY_FUNCTION_SPECIFIER;
return true;
}
if (parse_type_qualifier(parser, &specifier->type_qualifier)) {
specifier->ty = AST_DECL_SPECIFIER_TY_TYPE_QUALIFIER;
return true;
}
if (parse_type_specifier(parser, &specifier->type_specifier, mode)) {
specifier->ty = AST_DECL_SPECIFIER_TY_TYPE_SPECIFIER;
return true;
}
return false;
}
static void parse_declaration_specifier_list(
struct parser *parser,
struct ast_declaration_specifier_list *specifier_list) {
struct vector *list = vector_create(sizeof(*specifier_list->decl_specifiers));
// code like this will parse wrong
// ```
// typedef struct s s;
//
// struct s {
// struct s1 { } s;
// } s2;
// ```
// because it will take `struct s1 { }` as a type qualifier, and then `s` as a
// typedef name type qualifier so we do a hack if we have seen _any_ type
// specifiers so far, we do not look for typedef names anymore
enum type_specifier_mode mode = TYPE_SPECIFIER_MODE_ALLOW_TYPEDEFS;
struct ast_declaration_specifier specifier;
while (parse_decl_specifier(parser, &specifier, mode)) {
if (specifier.ty == AST_DECL_SPECIFIER_TY_TYPE_SPECIFIER) {
mode = TYPE_SPECIFIER_MODE_DISALLOW_TYPEDEFS;
}
vector_push_back(list, &specifier);
}
specifier_list->decl_specifiers =
arena_alloc(parser->arena, vector_byte_size(list));
specifier_list->num_decl_specifiers = vector_length(list);
vector_copy_to(list, specifier_list->decl_specifiers);
vector_free(&list);
}
static bool parse_designator(struct parser *parser,
struct ast_designator *designator) {
struct text_pos pos = get_position(parser->lexer);
if (parse_token(parser, LEX_TOKEN_TY_OPEN_SQUARE_BRACKET)) {
struct ast_expr expr;
EXP_PARSE(parse_expr(parser, &expr), "expression after [ in designator");
EXP_PARSE(parse_token(parser, LEX_TOKEN_TY_CLOSE_SQUARE_BRACKET),
"] after expression in designator");
designator->ty = AST_DESIGNATOR_TY_INDEX;
designator->index = arena_alloc(parser->arena, sizeof(*designator->index));
*designator->index = expr;
return true;
} else if (parse_token(parser, LEX_TOKEN_TY_DOT)) {
struct lex_token identifier;
EXP_PARSE(parse_identifier(parser, &identifier),
"identifier after dot in designator");
designator->ty = AST_DESIGNATOR_TY_FIELD;
designator->field = identifier;
return true;
}
backtrack(parser->lexer, pos);
return false;
}
static bool parse_designator_list(struct parser *parser,
struct ast_designator_list *designator_list) {
struct vector *list = vector_create(sizeof(*designator_list->designators));
struct ast_designator designator;
while (parse_designator(parser, &designator)) {
vector_push_back(list, &designator);
}
if (vector_empty(list)) {
vector_free(&list);
return false;
}
designator_list->designators =
arena_alloc(parser->arena, vector_byte_size(list));
designator_list->num_designators = vector_length(list);
vector_copy_to(list, designator_list->designators);
vector_free(&list);
return true;
}
static bool parse_init_list(struct parser *parser,
struct ast_init_list *init_list);
static bool parse_init(struct parser *parser, struct ast_init *init) {
if (parse_init_list(parser, &init->init_list)) {
init->ty = AST_INIT_TY_INIT_LIST;
return true;
} else if (parse_expr(parser, &init->expr)) {
init->ty = AST_INIT_TY_EXPR;
return true;
}
return false;
}
static bool parse_init_list_init(struct parser *parser,
struct ast_init_list_init *init_list) {
struct text_pos pos = get_position(parser->lexer);
*init_list =
(struct ast_init_list_init){.designator_list = NULL, .init = NULL};
struct ast_designator_list designator_list;
struct ast_init init;
if (parse_designator_list(parser, &designator_list)) {
EXP_PARSE(parse_token(parser, LEX_TOKEN_TY_OP_ASSG),
"expected = after designator in init list");
init_list->designator_list =
arena_alloc(parser->arena, sizeof(*init_list->designator_list));
*init_list->designator_list = designator_list;
EXP_PARSE(parse_init(parser, &init), "expected init in init list");
} else if (!parse_init(parser, &init)) {
backtrack(parser->lexer, pos);
return false;
}
parse_token(parser, LEX_TOKEN_TY_COMMA);
init_list->init = arena_alloc(parser->arena, sizeof(*init_list->init));
*init_list->init = init;
return true;
}
static bool parse_init_list(struct parser *parser,
struct ast_init_list *init_list) {
struct text_pos pos = get_position(parser->lexer);
if (!parse_token(parser, LEX_TOKEN_TY_OPEN_BRACE)) {
backtrack(parser->lexer, pos);
return false;
}
struct vector *inits = vector_create(sizeof(struct ast_init_list_init));
struct ast_init_list_init init;
while (parse_init_list_init(parser, &init)) {
vector_push_back(inits, &init);
parse_token(parser, LEX_TOKEN_TY_COMMA);
}
EXP_PARSE(parse_token(parser, LEX_TOKEN_TY_CLOSE_BRACE),
"expected } at end of init list");
init_list->inits = arena_alloc(parser->arena, vector_byte_size(inits));
init_list->num_inits = vector_length(inits);
vector_copy_to(inits, init_list->inits);
vector_free(&inits);
return true;
}
static bool parse_pointer(struct parser *parser, struct ast_pointer *pointer) {
if (!parse_token(parser, LEX_TOKEN_TY_OP_MUL)) {
return false;
}
parse_declaration_specifier_list(parser, &pointer->specifier_list);
return true;
}
static void parse_pointer_list(struct parser *parser,
struct ast_pointer_list *pointer_list) {
struct vector *list = vector_create(sizeof(*pointer_list->pointers));
struct ast_pointer pointer;
while (parse_pointer(parser, &pointer)) {
vector_push_back(list, &pointer);
}
pointer_list->pointers = arena_alloc(parser->arena, vector_byte_size(list));
pointer_list->num_pointers = vector_length(list);
vector_copy_to(list, pointer_list->pointers);
vector_free(&list);
}
static bool
parse_ast_array_declarator(struct parser *parser,
struct ast_array_declarator *array_declarator) {
if (!parse_token(parser, LEX_TOKEN_TY_OPEN_SQUARE_BRACKET)) {
return false;
}
parse_declaration_specifier_list(parser, &array_declarator->specifier_list);
enum ast_array_declarator_ty ty;
if (parse_token(parser, LEX_TOKEN_TY_OP_MUL)) {
ty = AST_ARRAY_DECLARATOR_TY_STAR;
} else {
bool is_static = parse_token(parser, LEX_TOKEN_TY_KW_STATIC);
is_static = is_static || parse_token(parser, LEX_TOKEN_TY_KW_STATIC);
struct ast_expr size;
if (is_static) {
EXP_PARSE(parse_expr(parser, &size),
"expected expr in static array type");
ty = AST_ARRAY_DECLARATOR_TY_STATIC_SIZED;
array_declarator->size =
arena_alloc(parser->arena, sizeof(*array_declarator->size));
*array_declarator->size = size;
} else if (parse_expr(parser, &size)) {
ty = AST_ARRAY_DECLARATOR_TY_SIZED;
array_declarator->size =
arena_alloc(parser->arena, sizeof(*array_declarator->size));
*array_declarator->size = size;
} else {
ty = AST_ARRAY_DECLARATOR_TY_UNSIZED;
}
}
EXP_PARSE(parse_token(parser, LEX_TOKEN_TY_CLOSE_SQUARE_BRACKET),
"expected ) after abstract declarator after");
array_declarator->ty = ty;
return true;
}
static bool parse_paramlist(struct parser *parser,
struct ast_paramlist *param_list);
static bool
parse_ast_func_declarator(struct parser *parser,
struct ast_func_declarator *func_declarator) {
struct ast_paramlist param_list;
if (!parse_paramlist(parser, ¶m_list)) {
return false;
}
func_declarator->param_list =
arena_alloc(parser->arena, sizeof(*func_declarator->param_list));
*func_declarator->param_list = param_list;
return true;
}
static bool
parse_abstract_declarator(struct parser *parser,
struct ast_abstract_declarator *abstract_declarator);
static bool parse_direct_abstract_declarator(
struct parser *parser,
struct ast_direct_abstract_declarator *direct_abstract_declarator) {
struct text_pos pos = get_position(parser->lexer);
struct ast_array_declarator array_declarator;
if (parse_ast_array_declarator(parser, &array_declarator)) {
direct_abstract_declarator->ty =
AST_DIRECT_ABSTRACT_DECLARATOR_TY_ARRAY_DECLARATOR;
direct_abstract_declarator->array_declarator = arena_alloc(
parser->arena, sizeof(*direct_abstract_declarator->array_declarator));
*direct_abstract_declarator->array_declarator = array_declarator;
return true;
}
struct ast_func_declarator func_declarator;
if (parse_ast_func_declarator(parser, &func_declarator)) {
direct_abstract_declarator->ty =
AST_DIRECT_ABSTRACT_DECLARATOR_TY_FUNC_DECLARATOR;
direct_abstract_declarator->func_declarator = arena_alloc(
parser->arena, sizeof(*direct_abstract_declarator->func_declarator));
*direct_abstract_declarator->func_declarator = func_declarator;
return true;
}
struct ast_abstract_declarator abstract_declarator;
if (parse_token(parser, LEX_TOKEN_TY_OPEN_BRACKET) &&
parse_abstract_declarator(parser, &abstract_declarator)) {
EXP_PARSE(parse_token(parser, LEX_TOKEN_TY_CLOSE_BRACKET),
"expected ) after abstract declarator after");
direct_abstract_declarator->ty =
AST_DIRECT_ABSTRACT_DECLARATOR_TY_PAREN_DECLARATOR;
direct_abstract_declarator->paren_declarator = arena_alloc(
parser->arena, sizeof(*direct_abstract_declarator->paren_declarator));
*direct_abstract_declarator->paren_declarator = abstract_declarator;
return true;
}
backtrack(parser->lexer, pos);
return false;
}
static void
parse_direct_abstract_declarator_list(struct parser *parser,
struct ast_direct_abstract_declarator_list
*direct_abstract_declarator_list) {
struct vector *list = vector_create(
sizeof(*direct_abstract_declarator_list->direct_abstract_declarators));
struct ast_direct_abstract_declarator direct_abstract_declarator;
while (
parse_direct_abstract_declarator(parser, &direct_abstract_declarator)) {
vector_push_back(list, &direct_abstract_declarator);
}
direct_abstract_declarator_list->direct_abstract_declarators =
arena_alloc(parser->arena, vector_byte_size(list));
direct_abstract_declarator_list->num_direct_abstract_declarators =
vector_length(list);
vector_copy_to(list,
direct_abstract_declarator_list->direct_abstract_declarators);
vector_free(&list);
}
static bool
parse_abstract_declarator(struct parser *parser,
struct ast_abstract_declarator *abstract_declarator) {
struct text_pos pos = get_position(parser->lexer);
parse_pointer_list(parser, &abstract_declarator->pointer_list);
parse_direct_abstract_declarator_list(
parser, &abstract_declarator->direct_abstract_declarator_list);
if (!abstract_declarator->pointer_list.num_pointers &&
!abstract_declarator->direct_abstract_declarator_list
.num_direct_abstract_declarators) {
backtrack(parser->lexer, pos);
return false;
}
return true;
}
static bool
parse_direct_declarator(struct parser *parser,
struct ast_direct_declarator *direct_declarator) {
struct text_pos pos = get_position(parser->lexer);
if (parse_identifier(parser, &direct_declarator->identifier)) {
direct_declarator->ty = AST_DIRECT_DECLARATOR_TY_IDENTIFIER;
return true;
}
struct ast_array_declarator array_declarator;
if (parse_ast_array_declarator(parser, &array_declarator)) {
direct_declarator->ty = AST_DIRECT_DECLARATOR_TY_ARRAY_DECLARATOR;
direct_declarator->array_declarator = arena_alloc(
parser->arena, sizeof(*direct_declarator->array_declarator));
*direct_declarator->array_declarator = array_declarator;
return true;
}
struct ast_func_declarator func_declarator;
if (parse_ast_func_declarator(parser, &func_declarator)) {
direct_declarator->ty = AST_DIRECT_DECLARATOR_TY_FUNC_DECLARATOR;
direct_declarator->func_declarator =
arena_alloc(parser->arena, sizeof(*direct_declarator->func_declarator));
*direct_declarator->func_declarator = func_declarator;
return true;
}
struct ast_declarator declarator;
if (parse_token(parser, LEX_TOKEN_TY_OPEN_BRACKET) &&
parse_declarator(parser, &declarator)) {
EXP_PARSE(parse_token(parser, LEX_TOKEN_TY_CLOSE_BRACKET),
"expected ) after declarator after");
direct_declarator->ty = AST_DIRECT_DECLARATOR_TY_PAREN_DECLARATOR;
direct_declarator->paren_declarator = arena_alloc(
parser->arena, sizeof(*direct_declarator->paren_declarator));
*direct_declarator->paren_declarator = declarator;
return true;
}
backtrack(parser->lexer, pos);
return false;
}
// TODO: both this and abstract_declarator_list should really be a recursive
// type, rather than list because you have to do hacks like check if you haven't
// yet seen a sub decl
static void parse_direct_declarator_list(
struct parser *parser,
struct ast_direct_declarator_list *direct_declarator_list) {
struct vector *list =
vector_create(sizeof(*direct_declarator_list->direct_declarators));
struct ast_direct_declarator direct_declarator;
while (parse_direct_declarator(parser, &direct_declarator)) {
if (vector_empty(list)) {
// first must be identifier or sub decl
if (direct_declarator.ty != AST_DIRECT_DECLARATOR_TY_IDENTIFIER &&
direct_declarator.ty != AST_DIRECT_DECLARATOR_TY_PAREN_DECLARATOR) {
break;
}
}
vector_push_back(list, &direct_declarator);
}
direct_declarator_list->direct_declarators =
arena_alloc(parser->arena, vector_byte_size(list));
direct_declarator_list->num_direct_declarators = vector_length(list);
vector_copy_to(list, direct_declarator_list->direct_declarators);
vector_free(&list);
}
static bool parse_declarator(struct parser *parser,
struct ast_declarator *declarator) {
struct text_pos pos = get_position(parser->lexer);
parse_pointer_list(parser, &declarator->pointer_list);
parse_direct_declarator_list(parser, &declarator->direct_declarator_list);
bool has_declarator =
declarator->direct_declarator_list.num_direct_declarators;
struct text_pos end_of_declarator = get_position(parser->lexer);
struct ast_expr expr;
if (parse_token(parser, LEX_TOKEN_TY_COLON) && parse_expr(parser, &expr)) {
declarator->bitfield_size =
arena_alloc(parser->arena, sizeof(*declarator->bitfield_size));
*declarator->bitfield_size = expr;
} else {
declarator->bitfield_size = NULL;
backtrack(parser->lexer, end_of_declarator);
}
if (!has_declarator && !declarator->bitfield_size) {
backtrack(parser->lexer, pos);
return false;
}
return true;
}
static bool parse_init_declarator(struct parser *parser,
struct ast_init_declarator *init_declarator) {
if (!parse_declarator(parser, &init_declarator->declarator)) {
return false;