-
Notifications
You must be signed in to change notification settings - Fork 234
Expand file tree
/
Copy pathparser.c
More file actions
3962 lines (3299 loc) · 140 KB
/
parser.c
File metadata and controls
3962 lines (3299 loc) · 140 KB
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 "rbs/parser.h"
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include "rbs/defines.h"
#include "rbs/string.h"
#include "rbs/util/rbs_unescape.h"
#include "rbs/util/rbs_buffer.h"
#include "rbs/util/rbs_assert.h"
#define INTERN(str) \
rbs_constant_pool_insert_constant( \
RBS_GLOBAL_CONSTANT_POOL, \
(const uint8_t *) str, \
strlen(str) \
)
#define INTERN_TOKEN(parser, tok) \
rbs_constant_pool_insert_shared_with_encoding( \
&parser->constant_pool, \
(const uint8_t *) rbs_peek_token(parser->rbs_lexer_t, tok), \
rbs_token_bytes(tok), \
(void *) parser->rbs_lexer_t->encoding \
)
#define KEYWORD_CASES \
case kBOOL: \
case kBOT: \
case kCLASS: \
case kFALSE: \
case kINSTANCE: \
case kINTERFACE: \
case kNIL: \
case kSELF: \
case kSINGLETON: \
case kTOP: \
case kTRUE: \
case kVOID: \
case kTYPE: \
case kUNCHECKED: \
case kIN: \
case kOUT: \
case kEND: \
case kDEF: \
case kINCLUDE: \
case kEXTEND: \
case kPREPEND: \
case kALIAS: \
case kMODULE: \
case kATTRREADER: \
case kATTRWRITER: \
case kATTRACCESSOR: \
case kPUBLIC: \
case kPRIVATE: \
case kUNTYPED: \
case kUSE: \
case kAS: \
case k__TODO__: \
case kSKIP: \
case kRETURN: \
/* nop */
#define CHECK_PARSE(call) \
if (!call) { \
return false; \
}
#define ASSERT_TOKEN(parser, expected_type) \
if (parser->current_token.type != expected_type) { \
rbs_parser_set_error(parser, parser->current_token, true, "expected a token `%s`", rbs_token_type_str(expected_type)); \
return false; \
}
#define ADVANCE_ASSERT(parser, expected_type) \
do { \
rbs_parser_advance(parser); \
ASSERT_TOKEN(parser, expected_type) \
} while (0);
#define RESET_TABLE_P(table) (table->size == 0)
#define ALLOCATOR() parser->allocator
typedef struct {
rbs_node_list_t *required_positionals;
rbs_node_list_t *optional_positionals;
rbs_node_t *rest_positionals;
rbs_node_list_t *trailing_positionals;
rbs_hash_t *required_keywords;
rbs_hash_t *optional_keywords;
rbs_node_t *rest_keywords;
} method_params;
/**
* id_table represents a set of RBS constant IDs.
* This is used to manage the set of bound variables.
* */
typedef struct id_table {
size_t size;
size_t count;
rbs_constant_id_t *ids;
struct id_table *next;
} id_table;
static bool rbs_is_untyped_params(method_params *params) {
return params->required_positionals == NULL;
}
/**
* Returns RBS::Location object of `current_token` of a parser parser.
*
* @param parser
* @return New RBS::Location object.
* */
static rbs_location_t *rbs_location_current_token(rbs_parser_t *parser) {
return rbs_location_new(ALLOCATOR(), parser->current_token.range);
}
static bool parse_optional(rbs_parser_t *parser, rbs_node_t **optional, bool void_allowed, bool self_allowed);
static bool parse_simple(rbs_parser_t *parser, rbs_node_t **type, bool void_allowed, bool self_allowed);
/**
* @returns A borrowed copy of the current token, which does *not* need to be freed.
*/
static rbs_string_t rbs_parser_peek_current_token(rbs_parser_t *parser) {
rbs_range_t rg = parser->current_token.range;
const char *start = parser->rbs_lexer_t->string.start + rg.start.byte_pos;
size_t length = rg.end.byte_pos - rg.start.byte_pos;
return rbs_string_new(start, start + length);
}
static rbs_constant_id_t rbs_constant_pool_insert_string(rbs_constant_pool_t *self, rbs_string_t string) {
return rbs_constant_pool_insert_shared(self, (const uint8_t *) string.start, rbs_string_len(string));
}
typedef enum {
CLASS_NAME = 1,
INTERFACE_NAME = 2,
ALIAS_NAME = 4
} TypeNameKind;
static void parser_advance_no_gap(rbs_parser_t *parser) {
if (parser->current_token.range.end.byte_pos == parser->next_token.range.start.byte_pos) {
rbs_parser_advance(parser);
} else {
rbs_parser_set_error(parser, parser->next_token, true, "unexpected token");
}
}
/*
type_name ::= {`::`} (tUIDENT `::`)* <tXIDENT>
| {(tUIDENT `::`)*} <tXIDENT>
| {<tXIDENT>}
*/
NODISCARD
static bool parse_type_name(rbs_parser_t *parser, TypeNameKind kind, rbs_range_t *rg, rbs_type_name_t **type_name) {
bool absolute = false;
if (rg) {
rg->start = parser->current_token.range.start;
}
if (parser->current_token.type == pCOLON2) {
absolute = true;
parser_advance_no_gap(parser);
}
rbs_node_list_t *path = rbs_node_list_new(ALLOCATOR());
while (
parser->current_token.type == tUIDENT && parser->next_token.type == pCOLON2 && parser->current_token.range.end.byte_pos == parser->next_token.range.start.byte_pos && parser->next_token.range.end.byte_pos == parser->next_token2.range.start.byte_pos
) {
rbs_constant_id_t symbol_value = INTERN_TOKEN(parser, parser->current_token);
rbs_location_t *symbolLoc = rbs_location_new(ALLOCATOR(), parser->next_token.range);
rbs_ast_symbol_t *symbol = rbs_ast_symbol_new(ALLOCATOR(), symbolLoc, &parser->constant_pool, symbol_value);
rbs_node_list_append(path, (rbs_node_t *) symbol);
rbs_parser_advance(parser);
rbs_parser_advance(parser);
}
rbs_range_t namespace_range = {
.start = rg->start,
.end = parser->current_token.range.end
};
rbs_location_t *loc = rbs_location_new(ALLOCATOR(), namespace_range);
rbs_namespace_t *namespace = rbs_namespace_new(ALLOCATOR(), loc, path, absolute);
switch (parser->current_token.type) {
case tLIDENT:
case kSKIP:
case kRETURN:
if (kind & ALIAS_NAME) goto success;
goto error_handling;
case tULIDENT:
if (kind & INTERFACE_NAME) goto success;
goto error_handling;
case tUIDENT:
if (kind & CLASS_NAME) goto success;
goto error_handling;
default:
goto error_handling;
}
success: {
if (rg) {
rg->end = parser->current_token.range.end;
}
rbs_location_t *symbolLoc = rbs_location_current_token(parser);
rbs_constant_id_t name = INTERN_TOKEN(parser, parser->current_token);
rbs_ast_symbol_t *symbol = rbs_ast_symbol_new(ALLOCATOR(), symbolLoc, &parser->constant_pool, name);
*type_name = rbs_type_name_new(ALLOCATOR(), rbs_location_new(ALLOCATOR(), *rg), namespace, symbol);
return true;
}
error_handling: {
const char *ids = NULL;
if (kind & ALIAS_NAME) {
ids = "alias name";
}
if (kind & INTERFACE_NAME) {
ids = "interface name";
}
if (kind & CLASS_NAME) {
ids = "class/module/constant name";
}
rbs_assert(ids != NULL, "Unknown kind of type: %i", kind);
rbs_parser_set_error(parser, parser->current_token, true, "expected one of %s", ids);
return false;
}
}
/*
type_list ::= {} type `,` ... <`,`> eol
| {} type `,` ... `,` <type> eol
*/
NODISCARD
static bool parse_type_list(rbs_parser_t *parser, enum RBSTokenType eol, rbs_node_list_t *types, bool void_allowed, bool self_allowed) {
while (true) {
rbs_node_t *type;
CHECK_PARSE(rbs_parse_type(parser, &type, void_allowed, self_allowed));
rbs_node_list_append(types, type);
if (parser->next_token.type == pCOMMA) {
rbs_parser_advance(parser);
if (parser->next_token.type == eol) {
break;
}
} else {
if (parser->next_token.type == eol) {
break;
} else {
rbs_parser_set_error(parser, parser->next_token, true, "comma delimited type list is expected");
return false;
}
}
}
return true;
}
/*
type_list_with_commas ::= {} type `,` ... <`,`> eol
| {} type `,` ... `,` <type> eol
*/
NODISCARD
static bool parse_type_list_with_commas(rbs_parser_t *parser, enum RBSTokenType eol, rbs_node_list_t *types, rbs_location_list_t *comma_locations, bool void_allowed, bool self_allowed) {
while (true) {
rbs_node_t *type;
CHECK_PARSE(rbs_parse_type(parser, &type, void_allowed, self_allowed));
rbs_node_list_append(types, type);
if (parser->next_token.type == pCOMMA) {
rbs_location_t *comma_loc = rbs_location_new(ALLOCATOR(), parser->next_token.range);
rbs_location_list_append(comma_locations, comma_loc);
rbs_parser_advance(parser);
if (parser->next_token.type == eol) {
// Handle trailing comma - for type applications, this is an error
if (eol == pRBRACKET) {
rbs_parser_set_error(parser, parser->next_token, true, "unexpected trailing comma");
return false;
}
break;
}
} else {
if (parser->next_token.type == eol) {
break;
} else {
rbs_parser_set_error(parser, parser->next_token, true, "comma delimited type list is expected");
return false;
}
}
}
return true;
}
static bool is_keyword_token(enum RBSTokenType type) {
switch (type) {
case tLIDENT:
case tUIDENT:
case tULIDENT:
case tULLIDENT:
case tQIDENT:
case tBANGIDENT:
KEYWORD_CASES
return true;
default:
return false;
}
}
/*
function_param ::= {} <type>
| {} type <param>
*/
NODISCARD
static bool parse_function_param(rbs_parser_t *parser, rbs_types_function_param_t **function_param, bool self_allowed) {
rbs_range_t type_range;
type_range.start = parser->next_token.range.start;
rbs_node_t *type;
CHECK_PARSE(rbs_parse_type(parser, &type, false, self_allowed));
type_range.end = parser->current_token.range.end;
if (parser->next_token.type == pCOMMA || parser->next_token.type == pRPAREN) {
rbs_range_t param_range = type_range;
rbs_location_t *loc = rbs_location_new(ALLOCATOR(), param_range);
rbs_loc_alloc_children(ALLOCATOR(), loc, 1);
rbs_loc_add_optional_child(loc, INTERN("name"), NULL_RANGE);
*function_param = rbs_types_function_param_new(ALLOCATOR(), loc, type, NULL);
return true;
} else {
rbs_range_t name_range = parser->next_token.range;
rbs_parser_advance(parser);
rbs_range_t param_range = {
.start = type_range.start,
.end = name_range.end,
};
if (!is_keyword_token(parser->current_token.type)) {
rbs_parser_set_error(parser, parser->current_token, true, "unexpected token for function parameter name");
return false;
}
rbs_string_t unquoted_str = rbs_unquote_string(ALLOCATOR(), rbs_parser_peek_current_token(parser));
rbs_location_t *symbolLoc = rbs_location_current_token(parser);
rbs_constant_id_t constant_id = rbs_constant_pool_insert_string(&parser->constant_pool, unquoted_str);
rbs_ast_symbol_t *name = rbs_ast_symbol_new(ALLOCATOR(), symbolLoc, &parser->constant_pool, constant_id);
rbs_location_t *loc = rbs_location_new(ALLOCATOR(), param_range);
rbs_loc_alloc_children(ALLOCATOR(), loc, 1);
rbs_loc_add_optional_child(loc, INTERN("name"), name_range);
*function_param = rbs_types_function_param_new(ALLOCATOR(), loc, type, name);
return true;
}
}
static rbs_constant_id_t intern_token_start_end(rbs_parser_t *parser, rbs_token_t start_token, rbs_token_t end_token) {
return rbs_constant_pool_insert_shared_with_encoding(
&parser->constant_pool,
(const uint8_t *) rbs_peek_token(parser->rbs_lexer_t, start_token),
end_token.range.end.byte_pos - start_token.range.start.byte_pos,
parser->rbs_lexer_t->encoding
);
}
/*
keyword_key ::= {} <keyword> `:`
| {} keyword <`?`> `:`
*/
NODISCARD
static bool parse_keyword_key(rbs_parser_t *parser, rbs_ast_symbol_t **key) {
rbs_parser_advance(parser);
rbs_location_t *symbolLoc = rbs_location_current_token(parser);
if (parser->next_token.type == pQUESTION) {
*key = rbs_ast_symbol_new(
ALLOCATOR(),
symbolLoc,
&parser->constant_pool,
intern_token_start_end(parser, parser->current_token, parser->next_token)
);
rbs_parser_advance(parser);
} else {
*key = rbs_ast_symbol_new(ALLOCATOR(), symbolLoc, &parser->constant_pool, INTERN_TOKEN(parser, parser->current_token));
}
return true;
}
/*
keyword ::= {} keyword `:` <function_param>
*/
NODISCARD
static bool parse_keyword(rbs_parser_t *parser, rbs_hash_t *keywords, rbs_hash_t *memo, bool self_allowed) {
rbs_ast_symbol_t *key = NULL;
CHECK_PARSE(parse_keyword_key(parser, &key));
if (rbs_hash_find(memo, (rbs_node_t *) key)) {
rbs_parser_set_error(parser, parser->current_token, true, "duplicated keyword argument");
return false;
} else {
rbs_location_t *loc = rbs_location_current_token(parser);
rbs_hash_set(memo, (rbs_node_t *) key, (rbs_node_t *) rbs_ast_bool_new(ALLOCATOR(), loc, true));
}
ADVANCE_ASSERT(parser, pCOLON);
rbs_types_function_param_t *param = NULL;
CHECK_PARSE(parse_function_param(parser, ¶m, self_allowed));
rbs_hash_set(keywords, (rbs_node_t *) key, (rbs_node_t *) param);
return true;
}
/*
Returns true if keyword is given.
is_keyword === {} KEYWORD `:`
*/
static bool is_keyword(rbs_parser_t *parser) {
if (is_keyword_token(parser->next_token.type)) {
if (parser->next_token2.type == pCOLON && parser->next_token.range.end.byte_pos == parser->next_token2.range.start.byte_pos) {
return true;
}
if (parser->next_token2.type == pQUESTION && parser->next_token3.type == pCOLON && parser->next_token.range.end.byte_pos == parser->next_token2.range.start.byte_pos && parser->next_token2.range.end.byte_pos == parser->next_token3.range.start.byte_pos) {
return true;
}
}
return false;
}
/**
* Advance token if _next_ token is `type`.
* Ensures one token advance and `parser->current_token.type == type`, or current token not changed.
*
* @returns true if token advances, false otherwise.
**/
static bool parser_advance_if(rbs_parser_t *parser, enum RBSTokenType type) {
if (parser->next_token.type == type) {
rbs_parser_advance(parser);
return true;
} else {
return false;
}
}
/*
params ::= {} `)`
| {} `?` `)` -- Untyped function params (assign params.required = nil)
| <required_params> `)`
| <required_params> `,` `)`
required_params ::= {} function_param `,` <required_params>
| {} <function_param>
| {} <optional_params>
optional_params ::= {} `?` function_param `,` <optional_params>
| {} `?` <function_param>
| {} <rest_params>
rest_params ::= {} `*` function_param `,` <trailing_params>
| {} `*` <function_param>
| {} <trailing_params>
trailing_params ::= {} function_param `,` <trailing_params>
| {} <function_param>
| {} <keywords>
keywords ::= {} required_keyword `,` <keywords>
| {} `?` optional_keyword `,` <keywords>
| {} `**` function_param `,` <keywords>
| {} <required_keyword>
| {} `?` <optional_keyword>
| {} `**` <function_param>
*/
NODISCARD
static bool parse_params(rbs_parser_t *parser, method_params *params, bool self_allowed) {
if (parser->next_token.type == pQUESTION && parser->next_token2.type == pRPAREN) {
params->required_positionals = NULL;
rbs_parser_advance(parser);
return true;
}
if (parser->next_token.type == pRPAREN) {
return true;
}
rbs_hash_t *memo = rbs_hash_new(ALLOCATOR());
while (true) {
switch (parser->next_token.type) {
case pQUESTION:
goto PARSE_OPTIONAL_PARAMS;
case pSTAR:
goto PARSE_REST_PARAM;
case pSTAR2:
goto PARSE_KEYWORDS;
case pRPAREN:
goto EOP;
default:
if (is_keyword(parser)) {
goto PARSE_KEYWORDS;
}
rbs_types_function_param_t *param = NULL;
CHECK_PARSE(parse_function_param(parser, ¶m, self_allowed));
rbs_node_list_append(params->required_positionals, (rbs_node_t *) param);
break;
}
if (!parser_advance_if(parser, pCOMMA)) {
goto EOP;
}
}
PARSE_OPTIONAL_PARAMS:
while (true) {
switch (parser->next_token.type) {
case pQUESTION:
rbs_parser_advance(parser);
if (is_keyword(parser)) {
CHECK_PARSE(parse_keyword(parser, params->optional_keywords, memo, self_allowed));
parser_advance_if(parser, pCOMMA);
goto PARSE_KEYWORDS;
}
rbs_types_function_param_t *param = NULL;
CHECK_PARSE(parse_function_param(parser, ¶m, self_allowed));
rbs_node_list_append(params->optional_positionals, (rbs_node_t *) param);
break;
default:
goto PARSE_REST_PARAM;
}
if (!parser_advance_if(parser, pCOMMA)) {
goto EOP;
}
}
PARSE_REST_PARAM:
if (parser->next_token.type == pSTAR) {
rbs_parser_advance(parser);
rbs_types_function_param_t *param = NULL;
CHECK_PARSE(parse_function_param(parser, ¶m, self_allowed));
params->rest_positionals = (rbs_node_t *) param;
if (!parser_advance_if(parser, pCOMMA)) {
goto EOP;
}
}
goto PARSE_TRAILING_PARAMS;
PARSE_TRAILING_PARAMS:
while (true) {
switch (parser->next_token.type) {
case pQUESTION:
goto PARSE_KEYWORDS;
case pSTAR:
goto EOP;
case pSTAR2:
goto PARSE_KEYWORDS;
case pRPAREN:
goto EOP;
default:
if (is_keyword(parser)) {
goto PARSE_KEYWORDS;
}
rbs_types_function_param_t *param = NULL;
CHECK_PARSE(parse_function_param(parser, ¶m, self_allowed));
rbs_node_list_append(params->trailing_positionals, (rbs_node_t *) param);
break;
}
if (!parser_advance_if(parser, pCOMMA)) {
goto EOP;
}
}
PARSE_KEYWORDS:
while (true) {
switch (parser->next_token.type) {
case pQUESTION:
rbs_parser_advance(parser);
if (is_keyword(parser)) {
CHECK_PARSE(parse_keyword(parser, params->optional_keywords, memo, self_allowed));
} else {
rbs_parser_set_error(parser, parser->next_token, true, "optional keyword argument type is expected");
return false;
}
break;
case pSTAR2:
rbs_parser_advance(parser);
rbs_types_function_param_t *param = NULL;
CHECK_PARSE(parse_function_param(parser, ¶m, self_allowed));
params->rest_keywords = (rbs_node_t *) param;
break;
case tUIDENT:
case tLIDENT:
case tQIDENT:
case tULIDENT:
case tULLIDENT:
case tBANGIDENT:
KEYWORD_CASES
if (is_keyword(parser)) {
CHECK_PARSE(parse_keyword(parser, params->required_keywords, memo, self_allowed));
} else {
rbs_parser_set_error(parser, parser->next_token, true, "required keyword argument type is expected");
return false;
}
break;
default:
goto EOP;
}
if (!parser_advance_if(parser, pCOMMA)) {
goto EOP;
}
}
EOP:
if (parser->next_token.type != pRPAREN) {
rbs_parser_set_error(parser, parser->next_token, true, "unexpected token for method type parameters");
return false;
}
return true;
}
/*
optional ::= {} <simple_type>
| {} simple_type <`?`>
*/
NODISCARD
static bool parse_optional(rbs_parser_t *parser, rbs_node_t **optional, bool void_allowed, bool self_allowed) {
rbs_range_t rg;
rg.start = parser->next_token.range.start;
rbs_node_t *type = NULL;
CHECK_PARSE(parse_simple(parser, &type, void_allowed, self_allowed));
if (parser->next_token.type == pQUESTION) {
if (void_allowed && type->type == RBS_TYPES_BASES_VOID) {
rbs_parser_set_error(parser, parser->current_token, true, "void type is not allowed here");
return false;
}
rbs_parser_advance(parser);
rg.end = parser->current_token.range.end;
rbs_location_t *location = rbs_location_new(ALLOCATOR(), rg);
*optional = (rbs_node_t *) rbs_types_optional_new(ALLOCATOR(), location, type);
} else {
*optional = type;
}
return true;
}
static void initialize_method_params(method_params *params, rbs_allocator_t *allocator) {
*params = (method_params) {
.required_positionals = rbs_node_list_new(allocator),
.optional_positionals = rbs_node_list_new(allocator),
.rest_positionals = NULL,
.trailing_positionals = rbs_node_list_new(allocator),
.required_keywords = rbs_hash_new(allocator),
.optional_keywords = rbs_hash_new(allocator),
.rest_keywords = NULL,
};
}
/*
self_type_binding ::= {} <>
| {} `[` `self` `:` type <`]`>
*/
NODISCARD
static bool parse_self_type_binding(rbs_parser_t *parser, rbs_node_t **self_type, bool self_allowed) {
if (parser->next_token.type == pLBRACKET) {
rbs_parser_advance(parser);
ADVANCE_ASSERT(parser, kSELF);
ADVANCE_ASSERT(parser, pCOLON);
rbs_node_t *type;
CHECK_PARSE(rbs_parse_type(parser, &type, false, self_allowed));
ADVANCE_ASSERT(parser, pRBRACKET);
*self_type = type;
}
return true;
}
typedef struct {
rbs_node_t *function;
rbs_types_block_t *block;
rbs_node_t *function_self_type;
} parse_function_result;
/*
function ::= {} `(` params `)` self_type_binding? `{` `(` params `)` self_type_binding? `->` optional `}` `->` <optional>
| {} `(` params `)` self_type_binding? `->` <optional>
| {} self_type_binding? `{` `(` params `)` self_type_binding? `->` optional `}` `->` <optional>
| {} self_type_binding? `{` self_type_binding `->` optional `}` `->` <optional>
| {} self_type_binding? `->` <optional>
*/
NODISCARD
static bool parse_function(rbs_parser_t *parser, bool accept_type_binding, parse_function_result **result, bool self_allowed) {
rbs_node_t *function = NULL;
rbs_types_block_t *block = NULL;
rbs_node_t *function_self_type = NULL;
rbs_range_t function_range;
function_range.start = parser->current_token.range.start;
method_params params;
initialize_method_params(¶ms, ALLOCATOR());
if (parser->next_token.type == pLPAREN) {
rbs_parser_advance(parser);
CHECK_PARSE(parse_params(parser, ¶ms, self_allowed));
ADVANCE_ASSERT(parser, pRPAREN);
}
// Passing NULL to function_self_type means the function itself doesn't accept self type binding. (== method type)
if (accept_type_binding) {
CHECK_PARSE(parse_self_type_binding(parser, &function_self_type, self_allowed));
} else {
if (rbs_is_untyped_params(¶ms)) {
if (parser->next_token.type != pARROW) {
rbs_parser_set_error(parser, parser->next_token2, true, "A method type with untyped method parameter cannot have block");
return false;
}
}
}
bool required = true;
rbs_range_t block_range;
if (parser->next_token.type == pQUESTION && parser->next_token2.type == pLBRACE) {
// Optional block
block_range.start = parser->next_token.range.start;
required = false;
rbs_parser_advance(parser);
} else if (parser->next_token.type == pLBRACE) {
block_range.start = parser->next_token.range.start;
}
if (parser->next_token.type == pLBRACE) {
rbs_parser_advance(parser);
method_params block_params;
initialize_method_params(&block_params, ALLOCATOR());
if (parser->next_token.type == pLPAREN) {
rbs_parser_advance(parser);
CHECK_PARSE(parse_params(parser, &block_params, self_allowed));
ADVANCE_ASSERT(parser, pRPAREN);
}
rbs_node_t *self_type = NULL;
CHECK_PARSE(parse_self_type_binding(parser, &self_type, self_allowed));
ADVANCE_ASSERT(parser, pARROW);
rbs_node_t *block_return_type = NULL;
CHECK_PARSE(parse_optional(parser, &block_return_type, true, self_allowed));
ADVANCE_ASSERT(parser, pRBRACE);
block_range.end = parser->current_token.range.end;
rbs_location_t *loc = rbs_location_new(ALLOCATOR(), block_range);
rbs_node_t *block_function = NULL;
if (rbs_is_untyped_params(&block_params)) {
block_function = (rbs_node_t *) rbs_types_untyped_function_new(ALLOCATOR(), loc, block_return_type);
} else {
block_function = (rbs_node_t *) rbs_types_function_new(
ALLOCATOR(),
loc,
block_params.required_positionals,
block_params.optional_positionals,
block_params.rest_positionals,
block_params.trailing_positionals,
block_params.required_keywords,
block_params.optional_keywords,
block_params.rest_keywords,
block_return_type
);
}
block = rbs_types_block_new(ALLOCATOR(), loc, block_function, required, self_type);
}
ADVANCE_ASSERT(parser, pARROW);
rbs_node_t *type = NULL;
CHECK_PARSE(parse_optional(parser, &type, true, self_allowed));
function_range.end = parser->current_token.range.end;
rbs_location_t *loc = rbs_location_new(ALLOCATOR(), function_range);
if (rbs_is_untyped_params(¶ms)) {
function = (rbs_node_t *) rbs_types_untyped_function_new(ALLOCATOR(), loc, type);
} else {
function = (rbs_node_t *) rbs_types_function_new(
ALLOCATOR(),
loc,
params.required_positionals,
params.optional_positionals,
params.rest_positionals,
params.trailing_positionals,
params.required_keywords,
params.optional_keywords,
params.rest_keywords,
type
);
}
(*result)->function = function;
(*result)->block = block;
(*result)->function_self_type = function_self_type;
return true;
}
/*
proc_type ::= {`^`} <function>
*/
NODISCARD
static bool parse_proc_type(rbs_parser_t *parser, rbs_types_proc_t **proc, bool self_allowed) {
rbs_position_t start = parser->current_token.range.start;
parse_function_result *result = rbs_allocator_alloc(ALLOCATOR(), parse_function_result);
CHECK_PARSE(parse_function(parser, true, &result, self_allowed));
rbs_position_t end = parser->current_token.range.end;
rbs_location_t *loc = rbs_location_new(ALLOCATOR(), (rbs_range_t) { .start = start, .end = end });
*proc = rbs_types_proc_new(ALLOCATOR(), loc, result->function, result->block, result->function_self_type);
return true;
}
static void check_key_duplication(rbs_parser_t *parser, rbs_hash_t *fields, rbs_node_t *key) {
if (rbs_hash_find(fields, ((rbs_node_t *) key))) {
rbs_parser_set_error(parser, parser->current_token, true, "duplicated record key");
}
}
/**
* ... `{` ... `}` ...
* > >
* */
/*
record_attributes ::= {`{`} record_attribute... <record_attribute> `}`
record_attribute ::= {} keyword_token `:` <type>
| {} literal_type `=>` <type>
*/
NODISCARD
static bool parse_record_attributes(rbs_parser_t *parser, rbs_hash_t **fields, bool self_allowed) {
*fields = rbs_hash_new(ALLOCATOR());
if (parser->next_token.type == pRBRACE) return true;
while (true) {
rbs_ast_symbol_t *key = NULL;
bool required = true;
if (parser->next_token.type == pQUESTION) {
// { ?foo: type } syntax
required = false;
rbs_parser_advance(parser);
}
if (is_keyword(parser)) {
// { foo: type } syntax
CHECK_PARSE(parse_keyword_key(parser, &key));
check_key_duplication(parser, *fields, (rbs_node_t *) key);
ADVANCE_ASSERT(parser, pCOLON);
} else {
// { key => type } syntax
switch (parser->next_token.type) {
case tSYMBOL:
case tSQSYMBOL:
case tDQSYMBOL:
case tSQSTRING:
case tDQSTRING:
case tINTEGER:
case kTRUE:
case kFALSE: {
rbs_node_t *type = NULL;
CHECK_PARSE(parse_simple(parser, &type, false, self_allowed));
key = (rbs_ast_symbol_t *) ((rbs_types_literal_t *) type)->literal;
break;
}
default:
rbs_parser_set_error(parser, parser->next_token, true, "unexpected record key token");
return false;
}
check_key_duplication(parser, *fields, (rbs_node_t *) key);
ADVANCE_ASSERT(parser, pFATARROW);
}
rbs_range_t field_range;
field_range.start = parser->current_token.range.end;
rbs_node_t *type;
CHECK_PARSE(rbs_parse_type(parser, &type, false, self_allowed));
field_range.end = parser->current_token.range.end;
rbs_location_t *loc = rbs_location_new(ALLOCATOR(), field_range);
rbs_hash_set(*fields, (rbs_node_t *) key, (rbs_node_t *) rbs_types_record_field_type_new(ALLOCATOR(), loc, type, required));
if (parser_advance_if(parser, pCOMMA)) {
if (parser->next_token.type == pRBRACE) {
break;
}
} else {
break;
}
}
return true;
}
/*
symbol ::= {<tSYMBOL>}
*/
NODISCARD
static bool parse_symbol(rbs_parser_t *parser, rbs_location_t *location, rbs_types_literal_t **symbol) {
size_t offset_bytes = parser->rbs_lexer_t->encoding->char_width((const uint8_t *) ":", (size_t) 1);
size_t bytes = rbs_token_bytes(parser->current_token) - offset_bytes;
rbs_ast_symbol_t *literal;
switch (parser->current_token.type) {
case tSYMBOL: {
rbs_location_t *symbolLoc = rbs_location_current_token(parser);
char *buffer = rbs_peek_token(parser->rbs_lexer_t, parser->current_token);
rbs_constant_id_t constant_id = rbs_constant_pool_insert_shared(
&parser->constant_pool,
(const uint8_t *) buffer + offset_bytes,
bytes
);
literal = rbs_ast_symbol_new(ALLOCATOR(), symbolLoc, &parser->constant_pool, constant_id);
break;
}
case tDQSYMBOL:
case tSQSYMBOL: {
rbs_location_t *symbolLoc = rbs_location_current_token(parser);
rbs_string_t current_token = rbs_parser_peek_current_token(parser);
rbs_string_t symbol = rbs_string_new(current_token.start + offset_bytes, current_token.end);
rbs_string_t unquoted_symbol = rbs_unquote_string(ALLOCATOR(), symbol);
rbs_constant_id_t constant_id = rbs_constant_pool_insert_string(&parser->constant_pool, unquoted_symbol);
literal = rbs_ast_symbol_new(ALLOCATOR(), symbolLoc, &parser->constant_pool, constant_id);
break;
}
default:
rbs_parser_set_error(parser, parser->current_token, false, "Unexpected error");
return false;
}
*symbol = rbs_types_literal_new(ALLOCATOR(), location, (rbs_node_t *) literal);
return true;
}
/*
instance_type ::= {type_name} <type_args>
type_args ::= {} <> /empty/
| {} `[` type_list <`]`>
*/
NODISCARD
static bool parse_instance_type(rbs_parser_t *parser, bool parse_alias, rbs_node_t **type) {
TypeNameKind expected_kind = INTERFACE_NAME | CLASS_NAME;
if (parse_alias) {