-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.c
5207 lines (4279 loc) · 149 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 "ap_val.h"
#include "builtins.h"
#include "diagnostics.h"
#include "lex.h"
#include "log.h"
#include "program.h"
#include "util.h"
#include "var_table.h"
#include "vector.h"
#include <string.h>
enum parser_state {
PARSER_STATE_NORMAL,
PARSER_STATE_ERR,
};
struct parser {
struct arena_allocator *arena;
struct lexer *lexer;
struct var_table ty_table;
enum parser_state state;
enum parse_result_ty result_ty;
struct compiler_diagnostics *diagnostics;
};
// root error is given back to the first call that enters error state
// this prevents sub-errors clearing the error flag
typedef enum {
PARSER_ERR_NONE,
PARSER_ERR_ROOT,
PARSER_ERR_REENTRANT
} err_state;
enum parser_create_result parser_create(
struct program program, struct preproc *preproc,
enum compile_c_standard c_standard, enum compile_preproc_mode mode,
struct compiler_diagnostics *diagnostics, struct parser **parser) {
struct parser *p = nonnull_malloc(sizeof(*p));
arena_allocator_create("parser", &p->arena);
if (lexer_create(program, preproc, c_standard, mode, &p->lexer) !=
LEX_CREATE_RESULT_SUCCESS) {
err("failed to create lexer");
return PARSER_CREATE_RESULT_FAILURE;
}
p->result_ty = PARSE_RESULT_TY_SUCCESS;
p->ty_table = vt_create(p->arena);
p->diagnostics = diagnostics;
p->state = PARSER_STATE_NORMAL;
#define BUILTIN_TY(name) \
do { \
vt_create_entry(&p->ty_table, VAR_TABLE_NS_TYPEDEF, \
MK_USTR("__builtin_" #name)); \
} while (0);
#define BUILTIN_FN(...)
BUILTINS_LIST;
#undef BUILTIN_FN
#undef BUILTIN_TY
*parser = p;
return PARSER_CREATE_RESULT_SUCCESS;
}
static err_state parser_err(struct parser *parser) {
if (parser->state == PARSER_STATE_ERR) {
return PARSER_ERR_REENTRANT;
}
parser->state = PARSER_STATE_ERR;
return PARSER_ERR_ROOT;
}
static void parser_clear_err(struct parser *parser, err_state state) {
if (state == PARSER_ERR_NONE) {
return;
}
DEBUG_ASSERT(parser->state == PARSER_STATE_ERR,
"called when parser not in err state");
if (state == PARSER_ERR_ROOT) {
parser->state = PARSER_STATE_NORMAL;
}
}
void parser_free(struct parser **parser) {
lexer_free(&(*parser)->lexer);
vt_free(&(*parser)->ty_table);
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_MOD:
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_MOD:
*info = op_info(AST_BINARY_OP_TY_MOD);
return true;
default:
// not an op
return false;
}
}
// unlike `parse_token`, always ""succeeds"" (because we have enough context to
// know what token is next, so we must know what to do after) but generates a
// diagnostic if it fails to find it
static bool parse_expected_token(struct parser *parser, enum lex_token_ty ty,
struct text_pos start, const char *err,
struct text_span *span) {
struct lex_token token;
lex_peek_token(parser->lexer, &token);
if (span) {
*span = token.span;
}
if (token.ty == ty) {
lex_consume_token(parser->lexer, token);
return true;
}
struct text_pos end = token.span.end;
const char *prefix = "expected token ";
size_t pref_len = strlen(prefix);
size_t err_len = strlen(err);
char *msg = aralloc(parser->arena, pref_len + err_len + 1);
memcpy(msg, prefix, pref_len);
memcpy(msg + pref_len, err, err_len);
msg[pref_len + err_len] = '\0';
parser->result_ty = PARSE_RESULT_TY_FAILURE;
compiler_diagnostics_add(parser->diagnostics,
MK_PARSER_DIAGNOSTIC(EXPECTED_TOKEN, expected_token,
MK_TEXT_SPAN(start, end), end,
msg));
if (span) {
*span = MK_TEXT_SPAN(end, end);
}
return false;
}
static bool parse_expected_identifier(struct parser *parser,
struct lex_token *token,
struct text_pos start, const char *err) {
lex_peek_token(parser->lexer, token);
if (token->ty == LEX_TOKEN_TY_IDENTIFIER) {
lex_consume_token(parser->lexer, *token);
return true;
}
parser->result_ty = PARSE_RESULT_TY_FAILURE;
compiler_diagnostics_add(
parser->diagnostics,
MK_PARSER_DIAGNOSTIC(EXPECTED_TOKEN, expected_token,
MK_TEXT_SPAN(start, token->span.end),
token->span.end, err));
return false;
}
static bool parse_expr(struct parser *parser, struct ast_expr *expr);
static bool parse_expected_expr(struct parser *parser, struct ast_expr *expr,
const char *err) {
if (parse_expr(parser, expr)) {
return true;
}
struct lex_token token;
lex_peek_token(parser->lexer, &token);
parser->result_ty = PARSE_RESULT_TY_FAILURE;
compiler_diagnostics_add(parser->diagnostics,
MK_PARSER_DIAGNOSTIC(EXPECTED_EXPR, expected_expr,
token.span, token.span.start,
err));
return false;
}
static bool parse_token(struct parser *parser, enum lex_token_ty ty,
struct text_span *span) {
struct lex_token token;
lex_peek_token(parser->lexer, &token);
if (token.ty == ty) {
lex_consume_token(parser->lexer, token);
if (span) {
*span = token.span;
}
return true;
}
return false;
}
static bool parse_identifier(struct parser *parser, struct lex_token *token) {
struct lex_pos pos = lex_get_position(parser->lexer);
lex_peek_token(parser->lexer, token);
if (token->ty == LEX_TOKEN_TY_IDENTIFIER) {
lex_consume_token(parser->lexer, *token);
return true;
}
lex_backtrack(parser->lexer, pos);
return false;
}
static bool parse_type_qualifier(struct parser *parser,
enum ast_type_qualifier *qualifier,
struct text_span *span) {
struct lex_token token;
lex_peek_token(parser->lexer, &token);
*span = token.span;
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 if (token.ty == LEX_TOKEN_TY_KW_NONNULL) {
*qualifier = AST_TYPE_QUALIFIER_NONNULL;
} else if (token.ty == LEX_TOKEN_TY_KW_NULLABLE) {
*qualifier = AST_TYPE_QUALIFIER_NULLABLE;
} else {
return false;
}
lex_consume_token(parser->lexer, token);
return true;
}
static bool parse_function_specifier(struct parser *parser,
enum ast_function_specifier *specifier,
struct text_span *span) {
struct lex_token token;
lex_peek_token(parser->lexer, &token);
*span = token.span;
if (token.ty == LEX_TOKEN_TY_KW_INLINE) {
*specifier = AST_FUNCTION_SPECIFIER_INLINE;
} else if (token.ty == LEX_TOKEN_TY_KW_NORETURN) {
*specifier = AST_FUNCTION_SPECIFIER_NORETURN;
} else {
return false;
}
lex_consume_token(parser->lexer, token);
return true;
}
static bool
parse_storage_class_specifier(struct parser *parser,
enum ast_storage_class_specifier *specifier,
struct text_span *span) {
struct lex_token token;
lex_peek_token(parser->lexer, &token);
*span = token.span;
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;
}
lex_consume_token(parser->lexer, token);
return true;
}
static bool parse_type_specifier_kw(struct parser *parser,
enum ast_type_specifier_kw *wkt,
struct text_span *span) {
struct lex_token token;
lex_peek_token(parser->lexer, &token);
*span = token.span;
switch (token.ty) {
case LEX_TOKEN_TY_KW_VOID:
lex_consume_token(parser->lexer, token);
*wkt = AST_TYPE_SPECIFIER_KW_VOID;
return true;
case LEX_TOKEN_TY_KW_CHAR:
lex_consume_token(parser->lexer, token);
*wkt = AST_TYPE_SPECIFIER_KW_CHAR;
return true;
case LEX_TOKEN_TY_KW_SHORT:
lex_consume_token(parser->lexer, token);
*wkt = AST_TYPE_SPECIFIER_KW_SHORT;
return true;
case LEX_TOKEN_TY_KW_INT:
lex_consume_token(parser->lexer, token);
*wkt = AST_TYPE_SPECIFIER_KW_INT;
return true;
case LEX_TOKEN_TY_KW_BOOL:
lex_consume_token(parser->lexer, token);
*wkt = AST_TYPE_SPECIFIER_KW_BOOL;
return true;
case LEX_TOKEN_TY_KW_UINT128:
lex_consume_token(parser->lexer, token);
*wkt = AST_TYPE_SPECIFIER_KW_UINT128;
return true;
case LEX_TOKEN_TY_KW_LONG:
lex_consume_token(parser->lexer, token);
*wkt = AST_TYPE_SPECIFIER_KW_LONG;
return true;
case LEX_TOKEN_TY_KW_FLOAT:
lex_consume_token(parser->lexer, token);
*wkt = AST_TYPE_SPECIFIER_KW_FLOAT;
return true;
case LEX_TOKEN_TY_KW_DOUBLE:
lex_consume_token(parser->lexer, token);
*wkt = AST_TYPE_SPECIFIER_KW_DOUBLE;
return true;
case LEX_TOKEN_TY_KW_SIGNED:
lex_consume_token(parser->lexer, token);
*wkt = AST_TYPE_SPECIFIER_KW_SIGNED;
return true;
case LEX_TOKEN_TY_KW_UNSIGNED:
lex_consume_token(parser->lexer, token);
*wkt = AST_TYPE_SPECIFIER_KW_UNSIGNED;
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:
lex_consume_token(parser->lexer, token);
*wkt = AST_TYPE_SPECIFIER_KW_HALF;
return true;
default:
return false;
}
}
static bool parse_compoundexpr_raw(struct parser *parser,
struct ast_compoundexpr *compound_expr);
static bool parse_compoundexpr_as_expr(struct parser *parser,
struct ast_expr *expr);
static bool parse_attribute(struct parser *parser,
struct ast_attribute *attribute) {
struct lex_pos pos = lex_get_position(parser->lexer);
struct lex_token identifier;
lex_peek_token(parser->lexer, &identifier);
if (identifier.ty == LEX_TOKEN_TY_COMMA ||
identifier.ty == LEX_TOKEN_TY_CLOSE_BRACKET) {
attribute->ty = AST_ATTRIBUTE_TY_EMPTY;
// technically points to wrong char (the one ahead of it) but is zero length
// so shouldn't matter?
attribute->span =
MK_TEXT_SPAN(identifier.span.start, identifier.span.start);
return true;
}
parse_expected_identifier(parser, &identifier, identifier.span.start,
"expected attribute to start with identifier");
attribute->prefix = NULL;
struct lex_token next;
lex_peek_token(parser->lexer, &next);
if (next.ty == LEX_TOKEN_TY_COLON) {
lex_consume_token(parser->lexer, next);
parse_expected_token(parser, LEX_TOKEN_TY_COLON, identifier.span.start,
"'::' not ':' after attribute prefix", NULL);
attribute->prefix = aralloc(parser->arena, sizeof(*attribute->prefix));
*attribute->prefix = identifier;
parse_expected_identifier(parser, &identifier, identifier.span.start,
"expected attribute to start with identifier");
}
struct lex_token open;
lex_peek_token(parser->lexer, &open);
if (open.ty != LEX_TOKEN_TY_OPEN_BRACKET) {
attribute->ty = AST_ATTRIBUTE_TY_NAMED;
attribute->name = identifier;
attribute->span = identifier.span;
return true;
}
lex_consume_token(parser->lexer, open);
// reuse compoundexpr parsing, but intentionally a different type so we can
// support other attributes (e.g types, `__attribute__((foo(char *)))`)
struct ast_compoundexpr compoundexpr;
if (!parse_compoundexpr_raw(parser, &compoundexpr)) {
lex_backtrack(parser->lexer, pos);
return false;
}
attribute->ty = AST_ATTRIBUTE_TY_PARAMETERIZED;
attribute->name = identifier;
attribute->params = aralloc(parser->arena, sizeof(*attribute->params) *
compoundexpr.num_exprs);
attribute->num_params = compoundexpr.num_exprs;
for (size_t i = 0; i < compoundexpr.num_exprs; i++) {
attribute->params[i] =
(struct ast_attribute_param){.expr = &compoundexpr.exprs[i]};
}
struct text_span close;
parse_expected_token(parser, LEX_TOKEN_TY_CLOSE_BRACKET,
identifier.span.start, "`)` after attribute params",
&close);
attribute->span = MK_TEXT_SPAN(identifier.span.start, close.end);
return true;
}
static bool parse_attribute_list(struct parser *parser,
struct ast_attribute_list *attribute_list) {
struct lex_pos pos = lex_get_position(parser->lexer);
// copied from parse_compoundexpr. maybe we should unify
struct vector *attributes =
vector_create_in_arena(sizeof(struct ast_attribute), parser->arena);
bool first = true;
struct text_pos start = lex_cur_pos(parser->lexer);
struct text_pos end = start;
struct lex_token token;
struct ast_attribute sub_attribute;
do {
if (!parse_attribute(parser, &sub_attribute)) {
lex_backtrack(parser->lexer, pos);
*attribute_list = (struct ast_attribute_list){0};
return false;
}
if (first) {
first = false;
start = sub_attribute.span.start;
}
vector_push_back(attributes, &sub_attribute);
end = sub_attribute.span.end;
lex_peek_token(parser->lexer, &token);
} while (token.ty == LEX_TOKEN_TY_COMMA &&
/* hacky */ (lex_consume_token(parser->lexer, token), true));
attribute_list->attributes = vector_head(attributes);
attribute_list->num_attributes = vector_length(attributes);
attribute_list->span = MK_TEXT_SPAN(start, end);
return true;
}
static bool
parse_attribute_specifier(struct parser *parser,
struct ast_attribute_specifier *attribute_specifier) {
struct lex_pos pos = lex_get_position(parser->lexer);
struct text_span kw_span;
struct text_pos start = kw_span.start;
struct text_span end;
if (parse_token(parser, LEX_TOKEN_TY_KW_ATTRIBUTE, &kw_span)) {
parse_expected_token(parser, LEX_TOKEN_TY_OPEN_BRACKET, start,
"`((` after `__attribute__` keyword", NULL);
parse_expected_token(parser, LEX_TOKEN_TY_OPEN_BRACKET, start,
"`((` after `__attribute__` keyword", NULL);
parse_attribute_list(parser, &attribute_specifier->attribute_list);
parse_expected_token(parser, LEX_TOKEN_TY_CLOSE_BRACKET, start,
"`))` after attribute list", NULL);
parse_expected_token(parser, LEX_TOKEN_TY_CLOSE_BRACKET, start,
"`))` after attribute list", &end);
} else if (parse_token(parser, LEX_TOKEN_TY_OPEN_SQUARE_BRACKET, &kw_span) &&
parse_token(parser, LEX_TOKEN_TY_OPEN_SQUARE_BRACKET, NULL)) {
parse_attribute_list(parser, &attribute_specifier->attribute_list);
parse_expected_token(parser, LEX_TOKEN_TY_CLOSE_SQUARE_BRACKET, start,
"`]]` after attribute list", NULL);
parse_expected_token(parser, LEX_TOKEN_TY_CLOSE_SQUARE_BRACKET, start,
"`]]` after attribute list", &end);
} else {
lex_backtrack(parser->lexer, pos);
return false;
}
attribute_specifier->span = MK_TEXT_SPAN(kw_span.start, end.end);
return true;
}
static bool parse_enumerator(struct parser *parser,
struct ast_enumerator *enumerator) {
if (!parse_identifier(parser, &enumerator->identifier)) {
return false;
}
struct text_pos end;
if (parse_token(parser, LEX_TOKEN_TY_OP_ASSG, NULL)) {
enumerator->value = aralloc(parser->arena, sizeof(*enumerator->value));
parse_expected_expr(parser, enumerator->value,
"expected expression after = in enum body");
end = enumerator->value->span.end;
} else {
enumerator->value = NULL;
end = enumerator->identifier.span.end;
}
struct text_span comma;
(void)parse_token(parser, LEX_TOKEN_TY_COMMA, &comma);
enumerator->span = MK_TEXT_SPAN(enumerator->identifier.span.start, end);
return true;
}
static void parse_enumerator_list(struct parser *parser,
struct ast_enumerator_list *enumerator_list) {
struct vector *list = vector_create_in_arena(
sizeof(*enumerator_list->enumerators), parser->arena);
struct ast_enumerator enumerator;
bool first = true;
struct text_pos start = lex_cur_pos(parser->lexer);
struct text_pos end = start;
while (parse_enumerator(parser, &enumerator)) {
if (first) {
first = false;
start = enumerator.span.start;
}
end = enumerator.span.end;
struct text_span comma;
if (parse_token(parser, LEX_TOKEN_TY_COMMA, &comma)) {
end = comma.end;
}
vector_push_back(list, &enumerator);
}
struct text_span comma;
if (parse_token(parser, LEX_TOKEN_TY_COMMA, &comma)) {
end = comma.end;
}
enumerator_list->enumerators = vector_head(list);
enumerator_list->num_enumerators = vector_length(list);
enumerator_list->span = MK_TEXT_SPAN(start, end);
}
static bool parse_enum_specifier(struct parser *parser,
struct ast_enum_specifier *enum_specifier) {
struct lex_pos pos = lex_get_position(parser->lexer);
struct text_span enum_span;
if (!parse_token(parser, LEX_TOKEN_TY_KW_ENUM, &enum_span)) {
return false;
}
struct text_pos start = enum_span.start;
struct text_pos end = enum_span.end;
struct lex_token identifier;
if (parse_identifier(parser, &identifier)) {
end = identifier.span.end;
enum_specifier->identifier =
aralloc(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, NULL)) {
parse_enumerator_list(parser, &enumerator_list);
struct text_span span;
parse_expected_token(parser, LEX_TOKEN_TY_CLOSE_BRACE, enum_span.start,
"`}` after enumerator body", &span);
end = span.end;
enum_specifier->enumerator_list =
aralloc(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) {
lex_backtrack(parser->lexer, pos);
return false;
}
enum_specifier->span = MK_TEXT_SPAN(start, end);
return true;
}
enum type_specifier_mode {
TYPE_SPECIFIER_MODE_ALLOW_TYPEDEFS,
TYPE_SPECIFIER_MODE_DISALLOW_TYPEDEFS
};
static void parse_declaration_specifier_list(
struct parser *parser, enum type_specifier_mode mode,
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 lex_pos pos = lex_get_position(parser->lexer);
struct text_pos end;
enum ast_struct_or_union_specifier_ty ty;
struct text_span start_span;
if (parse_token(parser, LEX_TOKEN_TY_KW_STRUCT, &start_span)) {
ty = AST_STRUCT_OR_UNION_SPECIFIER_TY_STRUCT;
end = start_span.end;
} else if (parse_token(parser, LEX_TOKEN_TY_KW_UNION, &start_span)) {
ty = AST_STRUCT_OR_UNION_SPECIFIER_TY_UNION;
end = start_span.end;
} else {
return false;
}
struct lex_token identifier;
if (parse_identifier(parser, &identifier)) {
struct_or_union_specifier->identifier = aralloc(
parser->arena, sizeof(*struct_or_union_specifier->identifier));
*struct_or_union_specifier->identifier = identifier;
end = identifier.span.end;
} 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, NULL)) {
struct_or_union_specifier->decl_list = aralloc(
parser->arena, sizeof(*struct_or_union_specifier->decl_list));
parse_declaration_list(parser, struct_or_union_specifier->decl_list);
struct text_span brace;
parse_expected_token(parser, LEX_TOKEN_TY_CLOSE_BRACE, start_span.start,
"`}` after struct/union body", &brace);
end = brace.end;
}
if (!struct_or_union_specifier->identifier &&
!struct_or_union_specifier->decl_list) {
lex_backtrack(parser->lexer, pos);
return false;
}
struct_or_union_specifier->span = MK_TEXT_SPAN(start_span.start, end);
return true;
}
static bool parse_typedef_name(struct parser *parser,
struct lex_token *typedef_name) {
struct lex_token identifier;
lex_peek_token(parser->lexer, &identifier);
if (identifier.ty != LEX_TOKEN_TY_IDENTIFIER) {
return false;
}
ustr_t name = identifier_str(parser, &identifier);
struct var_table_entry *entry =
vt_get_entry(&parser->ty_table, VAR_TABLE_NS_TYPEDEF, name);
if (!entry) {
return false;
}
*typedef_name = identifier;
lex_consume_token(parser->lexer, identifier);
typedef_name->span = identifier.span;
return true;
}
enum parse_type_or_expr_mode {
PARSE_TYPE_OR_EXPR_MODE_NORMAL,
PARSE_TYPE_OR_EXPR_MODE_EXPR_NEEDS_PARENS,
};
static void parse_type_or_expr(struct parser *parser, struct text_span context,
enum parse_type_or_expr_mode mode,
struct ast_type_or_expr *type_or_expr);
static bool parse_typeof_specifier(struct parser *parser,
struct ast_type_specifier *type_specifier) {
struct lex_token token;
lex_peek_token(parser->lexer, &token);
// spec says its either `type ( expr )` or `typeof ( type ) `
// i assume `type` means `type-name`?
switch (token.ty) {
case LEX_TOKEN_TY_KW_TYPEOF:
type_specifier->ty = AST_TYPE_SPECIFIER_TYPEOF;
lex_consume_token(parser->lexer, token);
parse_type_or_expr(parser, token.span,
PARSE_TYPE_OR_EXPR_MODE_EXPR_NEEDS_PARENS,
&type_specifier->type_of);
type_specifier->span = type_specifier->type_of_unqual.span;
return true;
case LEX_TOKEN_TY_KW_TYPEOF_UNQUAL:
type_specifier->ty = AST_TYPE_SPECIFIER_TYPEOF_UNQUAL;
lex_consume_token(parser->lexer, token);
parse_type_or_expr(parser, token.span,
PARSE_TYPE_OR_EXPR_MODE_EXPR_NEEDS_PARENS,
&type_specifier->type_of_unqual);
type_specifier->span = type_specifier->type_of_unqual.span;
return true;
default:
return false;
}
}
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->span)) {
type_specifier->ty = AST_TYPE_SPECIFIER_TY_KW;
return true;
}
if (parse_typeof_specifier(parser, type_specifier)) {
return true;
}
if (parse_struct_or_union_specifier(
parser, &type_specifier->struct_or_union_specifier)) {
type_specifier->ty = AST_TYPE_SPECIFIER_STRUCT_OR_UNION;
type_specifier->span = type_specifier->struct_or_union_specifier.span;
return true;
}
if (parse_enum_specifier(parser, &type_specifier->enum_specifier)) {
type_specifier->ty = AST_TYPE_SPECIFIER_ENUM;
type_specifier->span = type_specifier->enum_specifier.span;
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;
type_specifier->span = type_specifier->typedef_name.span;
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->span)) {
specifier->ty = AST_DECL_SPECIFIER_TY_STORAGE_CLASS_SPECIFIER;
return true;
}
if (parse_function_specifier(parser, &specifier->function_specifier,
&specifier->span)) {
specifier->ty = AST_DECL_SPECIFIER_TY_FUNCTION_SPECIFIER;
return true;
}
if (parse_type_qualifier(parser, &specifier->type_qualifier,
&specifier->span)) {
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;
specifier->span = specifier->type_specifier.span;
return true;
}
if (parse_attribute_specifier(parser, &specifier->attribute_specifier)) {
specifier->ty = AST_DECL_SPECIFIER_TY_ATTRIBUTE_SPECIFIER;
specifier->span = specifier->attribute_specifier.span;
return true;
}
return false;
}
static void parse_declaration_specifier_list(
struct parser *parser, enum type_specifier_mode mode,
struct ast_declaration_specifier_list *specifier_list) {
struct vector *list = vector_create_in_arena(
sizeof(*specifier_list->decl_specifiers), parser->arena);
// 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
bool first = true;