rbs 3.9.2 → 4.0.0.dev.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (115) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby.yml +1 -1
  3. data/.github/workflows/windows.yml +1 -1
  4. data/CHANGELOG.md +0 -13
  5. data/Rakefile +28 -21
  6. data/Steepfile +1 -0
  7. data/config.yml +232 -62
  8. data/ext/rbs_extension/ast_translation.c +1149 -0
  9. data/ext/rbs_extension/ast_translation.h +30 -0
  10. data/{src/constants.c → ext/rbs_extension/class_constants.c} +15 -1
  11. data/{include/rbs/constants.h → ext/rbs_extension/class_constants.h} +10 -1
  12. data/ext/rbs_extension/extconf.rb +3 -1
  13. data/ext/rbs_extension/{location.c → legacy_location.c} +25 -34
  14. data/ext/rbs_extension/legacy_location.h +40 -0
  15. data/ext/rbs_extension/main.c +402 -8
  16. data/ext/rbs_extension/rbs_extension.h +3 -21
  17. data/ext/rbs_extension/rbs_string_bridging.c +9 -0
  18. data/ext/rbs_extension/rbs_string_bridging.h +20 -0
  19. data/include/rbs/ast.h +748 -0
  20. data/include/rbs/defines.h +60 -0
  21. data/{ext/rbs_extension → include/rbs}/lexer.h +40 -32
  22. data/include/rbs/location.h +59 -0
  23. data/include/rbs/parser.h +151 -0
  24. data/include/rbs/string.h +49 -0
  25. data/include/rbs/util/rbs_allocator.h +38 -0
  26. data/include/rbs/util/rbs_assert.h +9 -0
  27. data/include/rbs/util/rbs_buffer.h +83 -0
  28. data/include/rbs/util/rbs_constant_pool.h +3 -64
  29. data/include/rbs/util/rbs_encoding.h +280 -0
  30. data/include/rbs/util/rbs_unescape.h +23 -0
  31. data/include/rbs.h +1 -2
  32. data/lib/rbs/annotate/formatter.rb +3 -13
  33. data/lib/rbs/annotate/rdoc_annotator.rb +3 -1
  34. data/lib/rbs/annotate/rdoc_source.rb +1 -1
  35. data/lib/rbs/ast/ruby/annotations.rb +119 -0
  36. data/lib/rbs/ast/ruby/comment_block.rb +221 -0
  37. data/lib/rbs/ast/ruby/declarations.rb +86 -0
  38. data/lib/rbs/ast/ruby/helpers/constant_helper.rb +24 -0
  39. data/lib/rbs/ast/ruby/helpers/location_helper.rb +15 -0
  40. data/lib/rbs/ast/ruby/members.rb +213 -0
  41. data/lib/rbs/buffer.rb +104 -24
  42. data/lib/rbs/cli/validate.rb +39 -34
  43. data/lib/rbs/cli.rb +4 -5
  44. data/lib/rbs/definition.rb +6 -1
  45. data/lib/rbs/definition_builder/ancestor_builder.rb +63 -60
  46. data/lib/rbs/definition_builder/method_builder.rb +45 -30
  47. data/lib/rbs/definition_builder.rb +44 -9
  48. data/lib/rbs/environment/class_entry.rb +69 -0
  49. data/lib/rbs/environment/module_entry.rb +66 -0
  50. data/lib/rbs/environment.rb +185 -154
  51. data/lib/rbs/environment_loader.rb +2 -2
  52. data/lib/rbs/errors.rb +4 -3
  53. data/lib/rbs/inline_parser/comment_association.rb +117 -0
  54. data/lib/rbs/inline_parser.rb +206 -0
  55. data/lib/rbs/location_aux.rb +35 -3
  56. data/lib/rbs/parser_aux.rb +11 -1
  57. data/lib/rbs/prototype/runtime.rb +2 -2
  58. data/lib/rbs/source.rb +99 -0
  59. data/lib/rbs/subtractor.rb +4 -3
  60. data/lib/rbs/version.rb +1 -1
  61. data/lib/rbs.rb +12 -0
  62. data/lib/rdoc/discover.rb +1 -1
  63. data/lib/rdoc_plugin/parser.rb +2 -2
  64. data/rbs.gemspec +1 -0
  65. data/sig/ancestor_builder.rbs +1 -1
  66. data/sig/annotate/formatter.rbs +2 -2
  67. data/sig/annotate/rdoc_annotater.rbs +1 -1
  68. data/sig/ast/ruby/annotations.rbs +110 -0
  69. data/sig/ast/ruby/comment_block.rbs +119 -0
  70. data/sig/ast/ruby/declarations.rbs +60 -0
  71. data/sig/ast/ruby/helpers/constant_helper.rbs +11 -0
  72. data/sig/ast/ruby/helpers/location_helper.rbs +15 -0
  73. data/sig/ast/ruby/members.rbs +72 -0
  74. data/sig/buffer.rbs +63 -5
  75. data/sig/definition.rbs +1 -0
  76. data/sig/definition_builder.rbs +1 -1
  77. data/sig/environment/class_entry.rbs +50 -0
  78. data/sig/environment/module_entry.rbs +50 -0
  79. data/sig/environment.rbs +22 -76
  80. data/sig/errors.rbs +13 -6
  81. data/sig/inline_parser/comment_association.rbs +71 -0
  82. data/sig/inline_parser.rbs +87 -0
  83. data/sig/location.rbs +32 -7
  84. data/sig/method_builder.rbs +7 -4
  85. data/sig/parser.rbs +16 -0
  86. data/sig/source.rbs +48 -0
  87. data/src/ast.c +1345 -0
  88. data/src/lexer.c +2867 -0
  89. data/src/lexer.re +151 -0
  90. data/{ext/rbs_extension → src}/lexstate.c +58 -42
  91. data/src/location.c +71 -0
  92. data/src/parser.c +3739 -0
  93. data/src/string.c +89 -0
  94. data/src/util/rbs_allocator.c +149 -0
  95. data/src/util/rbs_assert.c +19 -0
  96. data/src/util/rbs_buffer.c +54 -0
  97. data/src/util/rbs_constant_pool.c +13 -81
  98. data/src/util/rbs_encoding.c +5273 -0
  99. data/src/util/rbs_unescape.c +130 -0
  100. data/stdlib/rdoc/0/code_object.rbs +2 -2
  101. data/stdlib/rdoc/0/comment.rbs +2 -0
  102. data/stdlib/rdoc/0/options.rbs +76 -0
  103. data/stdlib/rdoc/0/rdoc.rbs +6 -4
  104. data/stdlib/rdoc/0/store.rbs +1 -1
  105. metadata +70 -17
  106. data/ext/rbs_extension/lexer.c +0 -2728
  107. data/ext/rbs_extension/lexer.re +0 -147
  108. data/ext/rbs_extension/location.h +0 -85
  109. data/ext/rbs_extension/parser.c +0 -2982
  110. data/ext/rbs_extension/parser.h +0 -18
  111. data/ext/rbs_extension/parserstate.c +0 -411
  112. data/ext/rbs_extension/parserstate.h +0 -163
  113. data/ext/rbs_extension/unescape.c +0 -32
  114. data/include/rbs/ruby_objs.h +0 -72
  115. data/src/ruby_objs.c +0 -799
data/src/parser.c ADDED
@@ -0,0 +1,3739 @@
1
+ #include "rbs/parser.h"
2
+
3
+ #include <stdlib.h>
4
+ #include <stdarg.h>
5
+ #include <stdio.h>
6
+ #include <ctype.h>
7
+ #include <string.h>
8
+
9
+ #include "rbs/defines.h"
10
+ #include "rbs/string.h"
11
+ #include "rbs/util/rbs_unescape.h"
12
+ #include "rbs/util/rbs_buffer.h"
13
+ #include "rbs/util/rbs_assert.h"
14
+
15
+ #define INTERN(str) \
16
+ rbs_constant_pool_insert_constant( \
17
+ RBS_GLOBAL_CONSTANT_POOL, \
18
+ (const uint8_t *) str, \
19
+ strlen(str) \
20
+ )
21
+
22
+ #define INTERN_TOKEN(parser, tok) \
23
+ rbs_constant_pool_insert_shared_with_encoding( \
24
+ &parser->constant_pool, \
25
+ (const uint8_t *) rbs_peek_token(parser->rbs_lexer_t, tok),\
26
+ rbs_token_bytes(tok), \
27
+ (void *) parser->rbs_lexer_t->encoding \
28
+ )
29
+
30
+ #define KEYWORD_CASES \
31
+ case kBOOL:\
32
+ case kBOT: \
33
+ case kCLASS: \
34
+ case kFALSE: \
35
+ case kINSTANCE: \
36
+ case kINTERFACE: \
37
+ case kNIL: \
38
+ case kSELF: \
39
+ case kSINGLETON: \
40
+ case kTOP: \
41
+ case kTRUE: \
42
+ case kVOID: \
43
+ case kTYPE: \
44
+ case kUNCHECKED: \
45
+ case kIN: \
46
+ case kOUT: \
47
+ case kEND: \
48
+ case kDEF: \
49
+ case kINCLUDE: \
50
+ case kEXTEND: \
51
+ case kPREPEND: \
52
+ case kALIAS: \
53
+ case kMODULE: \
54
+ case kATTRREADER: \
55
+ case kATTRWRITER: \
56
+ case kATTRACCESSOR: \
57
+ case kPUBLIC: \
58
+ case kPRIVATE: \
59
+ case kUNTYPED: \
60
+ case kUSE: \
61
+ case kAS: \
62
+ case k__TODO__: \
63
+ case kSKIP: \
64
+ case kRETURN: \
65
+ /* nop */
66
+
67
+ #define CHECK_PARSE(call) \
68
+ if (!call) { \
69
+ return false; \
70
+ }
71
+
72
+ #define ASSERT_TOKEN(parser, expected_type) \
73
+ if (parser->current_token.type != expected_type) { \
74
+ rbs_parser_set_error(parser, parser->current_token, true, "expected a token `%s`", rbs_token_type_str(expected_type)); \
75
+ return false; \
76
+ }
77
+
78
+ #define ADVANCE_ASSERT(parser, expected_type) do {\
79
+ rbs_parser_advance(parser); \
80
+ ASSERT_TOKEN(parser, expected_type) \
81
+ } while(0);
82
+
83
+ #define RESET_TABLE_P(table) (table->size == 0)
84
+
85
+ #define ALLOCATOR() parser->allocator
86
+
87
+ typedef struct {
88
+ rbs_node_list_t *required_positionals;
89
+ rbs_node_list_t *optional_positionals;
90
+ rbs_node_t *rest_positionals;
91
+ rbs_node_list_t *trailing_positionals;
92
+ rbs_hash_t *required_keywords;
93
+ rbs_hash_t *optional_keywords;
94
+ rbs_node_t *rest_keywords;
95
+ } method_params;
96
+
97
+ /**
98
+ * id_table represents a set of RBS constant IDs.
99
+ * This is used to manage the set of bound variables.
100
+ * */
101
+ typedef struct id_table {
102
+ size_t size;
103
+ size_t count;
104
+ rbs_constant_id_t *ids;
105
+ struct id_table *next;
106
+ } id_table;
107
+
108
+ static bool rbs_is_untyped_params(method_params *params) {
109
+ return params->required_positionals == NULL;
110
+ }
111
+
112
+ /**
113
+ * Returns RBS::Location object of `current_token` of a parser parser.
114
+ *
115
+ * @param parser
116
+ * @return New RBS::Location object.
117
+ * */
118
+ static rbs_location_t *rbs_location_current_token(rbs_parser_t *parser) {
119
+ return rbs_location_new(ALLOCATOR(), parser->current_token.range);
120
+ }
121
+
122
+ static bool parse_optional(rbs_parser_t *parser, rbs_node_t **optional);
123
+ static bool parse_simple(rbs_parser_t *parser, rbs_node_t **type);
124
+
125
+ /**
126
+ * @returns A borrowed copy of the current token, which does *not* need to be freed.
127
+ */
128
+ static rbs_string_t rbs_parser_peek_current_token(rbs_parser_t *parser) {
129
+ rbs_range_t rg = parser->current_token.range;
130
+
131
+ const char *start = parser->rbs_lexer_t->string.start + rg.start.byte_pos;
132
+ size_t length = rg.end.byte_pos - rg.start.byte_pos;
133
+
134
+ return rbs_string_new(start, start + length);
135
+ }
136
+
137
+ static rbs_constant_id_t rbs_constant_pool_insert_string(rbs_constant_pool_t *self, rbs_string_t string) {
138
+ return rbs_constant_pool_insert_shared(self, (const uint8_t *) string.start, rbs_string_len(string));
139
+ }
140
+
141
+ typedef enum {
142
+ CLASS_NAME = 1,
143
+ INTERFACE_NAME = 2,
144
+ ALIAS_NAME = 4
145
+ } TypeNameKind;
146
+
147
+ static void parser_advance_no_gap(rbs_parser_t *parser) {
148
+ if (parser->current_token.range.end.byte_pos == parser->next_token.range.start.byte_pos) {
149
+ rbs_parser_advance(parser);
150
+ } else {
151
+ rbs_parser_set_error(parser, parser->next_token, true, "unexpected token");
152
+ }
153
+ }
154
+
155
+ /*
156
+ type_name ::= {`::`} (tUIDENT `::`)* <tXIDENT>
157
+ | {(tUIDENT `::`)*} <tXIDENT>
158
+ | {<tXIDENT>}
159
+ */
160
+ NODISCARD
161
+ static bool parse_type_name(rbs_parser_t *parser, TypeNameKind kind, rbs_range_t *rg, rbs_type_name_t **type_name) {
162
+ bool absolute = false;
163
+
164
+ if (rg) {
165
+ rg->start = parser->current_token.range.start;
166
+ }
167
+
168
+ if (parser->current_token.type == pCOLON2) {
169
+ absolute = true;
170
+ parser_advance_no_gap(parser);
171
+ }
172
+
173
+ rbs_node_list_t *path = rbs_node_list_new(ALLOCATOR());
174
+
175
+ while (
176
+ parser->current_token.type == tUIDENT
177
+ && parser->next_token.type == pCOLON2
178
+ && parser->current_token.range.end.byte_pos == parser->next_token.range.start.byte_pos
179
+ && parser->next_token.range.end.byte_pos == parser->next_token2.range.start.byte_pos
180
+ ) {
181
+ rbs_constant_id_t symbol_value = INTERN_TOKEN(parser, parser->current_token);
182
+ rbs_location_t *symbolLoc = rbs_location_new(ALLOCATOR(), parser->next_token.range);
183
+ rbs_ast_symbol_t *symbol = rbs_ast_symbol_new(ALLOCATOR(), symbolLoc, &parser->constant_pool, symbol_value);
184
+ rbs_node_list_append(path, (rbs_node_t *)symbol);
185
+
186
+ rbs_parser_advance(parser);
187
+ rbs_parser_advance(parser);
188
+ }
189
+
190
+ rbs_range_t namespace_range = {
191
+ .start = rg->start,
192
+ .end = parser->current_token.range.end
193
+ };
194
+ rbs_location_t *loc = rbs_location_new(ALLOCATOR(), namespace_range);
195
+ rbs_namespace_t *namespace = rbs_namespace_new(ALLOCATOR(), loc, path, absolute);
196
+
197
+ switch (parser->current_token.type) {
198
+ case tLIDENT:
199
+ case kSKIP:
200
+ case kRETURN:
201
+ if (kind & ALIAS_NAME) goto success;
202
+ goto error_handling;
203
+ case tULIDENT:
204
+ if (kind & INTERFACE_NAME) goto success;
205
+ goto error_handling;
206
+ case tUIDENT:
207
+ if (kind & CLASS_NAME) goto success;
208
+ goto error_handling;
209
+ default:
210
+ goto error_handling;
211
+ }
212
+
213
+ success: {
214
+ if (rg) {
215
+ rg->end = parser->current_token.range.end;
216
+ }
217
+
218
+ rbs_location_t *symbolLoc = rbs_location_current_token(parser);
219
+ rbs_constant_id_t name = INTERN_TOKEN(parser, parser->current_token);
220
+ rbs_ast_symbol_t *symbol = rbs_ast_symbol_new(ALLOCATOR(), symbolLoc, &parser->constant_pool, name);
221
+ *type_name = rbs_type_name_new(ALLOCATOR(), rbs_location_new(ALLOCATOR(), *rg), namespace, symbol);
222
+ return true;
223
+ }
224
+
225
+ error_handling: {
226
+ const char *ids = NULL;
227
+ if (kind & ALIAS_NAME) {
228
+ ids = "alias name";
229
+ }
230
+ if (kind & INTERFACE_NAME) {
231
+ ids = "interface name";
232
+ }
233
+ if (kind & CLASS_NAME) {
234
+ ids = "class/module/constant name";
235
+ }
236
+
237
+ rbs_assert(ids != NULL, "Unknown kind of type: %i", kind);
238
+
239
+ rbs_parser_set_error(parser, parser->current_token, true, "expected one of %s", ids);
240
+ return false;
241
+ }
242
+ }
243
+
244
+ /*
245
+ type_list ::= {} type `,` ... <`,`> eol
246
+ | {} type `,` ... `,` <type> eol
247
+ */
248
+ NODISCARD
249
+ static bool parse_type_list(rbs_parser_t *parser, enum RBSTokenType eol, rbs_node_list_t *types) {
250
+ while (true) {
251
+ rbs_node_t *type;
252
+ CHECK_PARSE(rbs_parse_type(parser, &type));
253
+ rbs_node_list_append(types, type);
254
+
255
+ if (parser->next_token.type == pCOMMA) {
256
+ rbs_parser_advance(parser);
257
+
258
+ if (parser->next_token.type == eol) {
259
+ break;
260
+ }
261
+ } else {
262
+ if (parser->next_token.type == eol) {
263
+ break;
264
+ } else {
265
+ rbs_parser_set_error(parser, parser->next_token, true, "comma delimited type list is expected");
266
+ return false;
267
+ }
268
+ }
269
+ }
270
+
271
+ return true;
272
+ }
273
+
274
+ static bool is_keyword_token(enum RBSTokenType type) {
275
+ switch (type)
276
+ {
277
+ case tLIDENT:
278
+ case tUIDENT:
279
+ case tULIDENT:
280
+ case tULLIDENT:
281
+ case tQIDENT:
282
+ case tBANGIDENT:
283
+ KEYWORD_CASES
284
+ return true;
285
+ default:
286
+ return false;
287
+ }
288
+ }
289
+
290
+ /*
291
+ function_param ::= {} <type>
292
+ | {} type <param>
293
+ */
294
+ NODISCARD
295
+ static bool parse_function_param(rbs_parser_t *parser, rbs_types_function_param_t **function_param) {
296
+ rbs_range_t type_range;
297
+ type_range.start = parser->next_token.range.start;
298
+ rbs_node_t *type;
299
+ CHECK_PARSE(rbs_parse_type(parser, &type));
300
+ type_range.end = parser->current_token.range.end;
301
+
302
+ if (parser->next_token.type == pCOMMA || parser->next_token.type == pRPAREN) {
303
+ rbs_range_t param_range = type_range;
304
+
305
+ rbs_location_t *loc = rbs_location_new(ALLOCATOR(), param_range);
306
+ rbs_loc_alloc_children(ALLOCATOR(), loc, 1);
307
+ rbs_loc_add_optional_child(loc, INTERN("name"), NULL_RANGE);
308
+
309
+ *function_param = rbs_types_function_param_new(ALLOCATOR(), loc, type, NULL);
310
+ return true;
311
+ } else {
312
+ rbs_range_t name_range = parser->next_token.range;
313
+
314
+ rbs_parser_advance(parser);
315
+
316
+ rbs_range_t param_range = {
317
+ .start = type_range.start,
318
+ .end = name_range.end,
319
+ };
320
+
321
+ if (!is_keyword_token(parser->current_token.type)) {
322
+ rbs_parser_set_error(parser, parser->current_token, true, "unexpected token for function parameter name");
323
+ return false;
324
+ }
325
+
326
+ rbs_string_t unquoted_str = rbs_unquote_string(ALLOCATOR(), rbs_parser_peek_current_token(parser));
327
+ rbs_location_t *symbolLoc = rbs_location_current_token(parser);
328
+ rbs_constant_id_t constant_id = rbs_constant_pool_insert_string(&parser->constant_pool, unquoted_str);
329
+ rbs_ast_symbol_t *name = rbs_ast_symbol_new(ALLOCATOR(), symbolLoc, &parser->constant_pool, constant_id);
330
+
331
+ rbs_location_t *loc = rbs_location_new(ALLOCATOR(), param_range);
332
+ rbs_loc_alloc_children(ALLOCATOR(), loc, 1);
333
+ rbs_loc_add_optional_child(loc, INTERN("name"), name_range);
334
+
335
+ *function_param = rbs_types_function_param_new(ALLOCATOR(), loc, type, name);
336
+ return true;
337
+ }
338
+ }
339
+
340
+ static rbs_constant_id_t intern_token_start_end(rbs_parser_t *parser, rbs_token_t start_token, rbs_token_t end_token) {
341
+ return rbs_constant_pool_insert_shared_with_encoding(
342
+ &parser->constant_pool,
343
+ (const uint8_t *) rbs_peek_token(parser->rbs_lexer_t, start_token),
344
+ end_token.range.end.byte_pos - start_token.range.start.byte_pos,
345
+ parser->rbs_lexer_t->encoding
346
+ );
347
+ }
348
+
349
+ /*
350
+ keyword_key ::= {} <keyword> `:`
351
+ | {} keyword <`?`> `:`
352
+ */
353
+ NODISCARD
354
+ static bool parse_keyword_key(rbs_parser_t *parser, rbs_ast_symbol_t **key) {
355
+ rbs_parser_advance(parser);
356
+
357
+ rbs_location_t *symbolLoc = rbs_location_current_token(parser);
358
+
359
+ if (parser->next_token.type == pQUESTION) {
360
+ *key = rbs_ast_symbol_new(
361
+ ALLOCATOR(),
362
+ symbolLoc,
363
+ &parser->constant_pool,
364
+ intern_token_start_end(parser, parser->current_token, parser->next_token)
365
+ );
366
+ rbs_parser_advance(parser);
367
+ } else {
368
+ *key = rbs_ast_symbol_new(ALLOCATOR(), symbolLoc, &parser->constant_pool, INTERN_TOKEN(parser, parser->current_token));
369
+ }
370
+
371
+ return true;
372
+ }
373
+
374
+ /*
375
+ keyword ::= {} keyword `:` <function_param>
376
+ */
377
+ NODISCARD
378
+ static bool parse_keyword(rbs_parser_t *parser, rbs_hash_t *keywords, rbs_hash_t *memo) {
379
+ rbs_ast_symbol_t *key = NULL;
380
+ CHECK_PARSE(parse_keyword_key(parser, &key));
381
+
382
+ if (rbs_hash_find(memo, (rbs_node_t *) key)) {
383
+ rbs_parser_set_error(parser, parser->current_token, true, "duplicated keyword argument");
384
+ return false;
385
+ } else {
386
+ rbs_location_t *loc = rbs_location_current_token(parser);
387
+ rbs_hash_set(memo, (rbs_node_t *) key, (rbs_node_t *) rbs_ast_bool_new(ALLOCATOR(), loc, true));
388
+ }
389
+
390
+ ADVANCE_ASSERT(parser, pCOLON);
391
+ rbs_types_function_param_t *param = NULL;
392
+ CHECK_PARSE(parse_function_param(parser, &param));
393
+
394
+ rbs_hash_set(keywords, (rbs_node_t *) key, (rbs_node_t *) param);
395
+
396
+ return true;
397
+ }
398
+
399
+ /*
400
+ Returns true if keyword is given.
401
+
402
+ is_keyword === {} KEYWORD `:`
403
+ */
404
+ static bool is_keyword(rbs_parser_t *parser) {
405
+ if (is_keyword_token(parser->next_token.type)) {
406
+ if (parser->next_token2.type == pCOLON && parser->next_token.range.end.byte_pos == parser->next_token2.range.start.byte_pos) {
407
+ return true;
408
+ }
409
+
410
+ if (parser->next_token2.type == pQUESTION
411
+ && parser->next_token3.type == pCOLON
412
+ && parser->next_token.range.end.byte_pos == parser->next_token2.range.start.byte_pos
413
+ && parser->next_token2.range.end.byte_pos == parser->next_token3.range.start.byte_pos) {
414
+ return true;
415
+ }
416
+ }
417
+
418
+ return false;
419
+ }
420
+
421
+ /**
422
+ * Advance token if _next_ token is `type`.
423
+ * Ensures one token advance and `parser->current_token.type == type`, or current token not changed.
424
+ *
425
+ * @returns true if token advances, false otherwise.
426
+ **/
427
+ static bool parser_advance_if(rbs_parser_t *parser, enum RBSTokenType type) {
428
+ if (parser->next_token.type == type) {
429
+ rbs_parser_advance(parser);
430
+ return true;
431
+ } else {
432
+ return false;
433
+ }
434
+ }
435
+
436
+ /*
437
+ params ::= {} `)`
438
+ | {} `?` `)` -- Untyped function params (assign params.required = nil)
439
+ | <required_params> `)`
440
+ | <required_params> `,` `)`
441
+
442
+ required_params ::= {} function_param `,` <required_params>
443
+ | {} <function_param>
444
+ | {} <optional_params>
445
+
446
+ optional_params ::= {} `?` function_param `,` <optional_params>
447
+ | {} `?` <function_param>
448
+ | {} <rest_params>
449
+
450
+ rest_params ::= {} `*` function_param `,` <trailing_params>
451
+ | {} `*` <function_param>
452
+ | {} <trailing_params>
453
+
454
+ trailing_params ::= {} function_param `,` <trailing_params>
455
+ | {} <function_param>
456
+ | {} <keywords>
457
+
458
+ keywords ::= {} required_keyword `,` <keywords>
459
+ | {} `?` optional_keyword `,` <keywords>
460
+ | {} `**` function_param `,` <keywords>
461
+ | {} <required_keyword>
462
+ | {} `?` <optional_keyword>
463
+ | {} `**` <function_param>
464
+ */
465
+ NODISCARD
466
+ static bool parse_params(rbs_parser_t *parser, method_params *params) {
467
+ if (parser->next_token.type == pQUESTION && parser->next_token2.type == pRPAREN) {
468
+ params->required_positionals = NULL;
469
+ rbs_parser_advance(parser);
470
+ return true;
471
+ }
472
+ if (parser->next_token.type == pRPAREN) {
473
+ return true;
474
+ }
475
+
476
+ rbs_hash_t *memo = rbs_hash_new(ALLOCATOR());
477
+
478
+ while (true) {
479
+ switch (parser->next_token.type) {
480
+ case pQUESTION:
481
+ goto PARSE_OPTIONAL_PARAMS;
482
+ case pSTAR:
483
+ goto PARSE_REST_PARAM;
484
+ case pSTAR2:
485
+ goto PARSE_KEYWORDS;
486
+ case pRPAREN:
487
+ goto EOP;
488
+
489
+ default:
490
+ if (is_keyword(parser)) {
491
+ goto PARSE_KEYWORDS;
492
+ }
493
+
494
+ rbs_types_function_param_t *param = NULL;
495
+ CHECK_PARSE(parse_function_param(parser, &param));
496
+ rbs_node_list_append(params->required_positionals, (rbs_node_t *)param);
497
+
498
+ break;
499
+ }
500
+
501
+ if (!parser_advance_if(parser, pCOMMA)) {
502
+ goto EOP;
503
+ }
504
+ }
505
+
506
+ PARSE_OPTIONAL_PARAMS:
507
+ while (true) {
508
+ switch (parser->next_token.type) {
509
+ case pQUESTION:
510
+ rbs_parser_advance(parser);
511
+
512
+ if (is_keyword(parser)) {
513
+ CHECK_PARSE(parse_keyword(parser, params->optional_keywords, memo));
514
+ parser_advance_if(parser, pCOMMA);
515
+ goto PARSE_KEYWORDS;
516
+ }
517
+
518
+ rbs_types_function_param_t *param = NULL;
519
+ CHECK_PARSE(parse_function_param(parser, &param));
520
+ rbs_node_list_append(params->optional_positionals, (rbs_node_t *)param);
521
+
522
+ break;
523
+ default:
524
+ goto PARSE_REST_PARAM;
525
+ }
526
+
527
+ if (!parser_advance_if(parser, pCOMMA)) {
528
+ goto EOP;
529
+ }
530
+ }
531
+
532
+ PARSE_REST_PARAM:
533
+ if (parser->next_token.type == pSTAR) {
534
+ rbs_parser_advance(parser);
535
+ rbs_types_function_param_t *param = NULL;
536
+ CHECK_PARSE(parse_function_param(parser, &param));
537
+ params->rest_positionals = (rbs_node_t *) param;
538
+
539
+ if (!parser_advance_if(parser, pCOMMA)) {
540
+ goto EOP;
541
+ }
542
+ }
543
+ goto PARSE_TRAILING_PARAMS;
544
+
545
+ PARSE_TRAILING_PARAMS:
546
+ while (true) {
547
+ switch (parser->next_token.type) {
548
+ case pQUESTION:
549
+ goto PARSE_KEYWORDS;
550
+ case pSTAR:
551
+ goto EOP;
552
+ case pSTAR2:
553
+ goto PARSE_KEYWORDS;
554
+ case pRPAREN:
555
+ goto EOP;
556
+
557
+ default:
558
+ if (is_keyword(parser)) {
559
+ goto PARSE_KEYWORDS;
560
+ }
561
+
562
+ rbs_types_function_param_t *param = NULL;
563
+ CHECK_PARSE(parse_function_param(parser, &param));
564
+ rbs_node_list_append(params->trailing_positionals, (rbs_node_t *)param);
565
+
566
+ break;
567
+ }
568
+
569
+ if (!parser_advance_if(parser, pCOMMA)) {
570
+ goto EOP;
571
+ }
572
+ }
573
+
574
+ PARSE_KEYWORDS:
575
+ while (true) {
576
+ switch (parser->next_token.type) {
577
+ case pQUESTION:
578
+ rbs_parser_advance(parser);
579
+ if (is_keyword(parser)) {
580
+ CHECK_PARSE(parse_keyword(parser, params->optional_keywords, memo));
581
+ } else {
582
+ rbs_parser_set_error(parser, parser->next_token, true, "optional keyword argument type is expected");
583
+ return false;
584
+ }
585
+ break;
586
+
587
+ case pSTAR2:
588
+ rbs_parser_advance(parser);
589
+ rbs_types_function_param_t *param = NULL;
590
+ CHECK_PARSE(parse_function_param(parser, &param));
591
+ params->rest_keywords = (rbs_node_t *) param;
592
+ break;
593
+
594
+ case tUIDENT:
595
+ case tLIDENT:
596
+ case tQIDENT:
597
+ case tULIDENT:
598
+ case tULLIDENT:
599
+ case tBANGIDENT:
600
+ KEYWORD_CASES
601
+ if (is_keyword(parser)) {
602
+ CHECK_PARSE(parse_keyword(parser, params->required_keywords, memo));
603
+ } else {
604
+ rbs_parser_set_error(parser, parser->next_token, true, "required keyword argument type is expected");
605
+ return false;
606
+ }
607
+ break;
608
+
609
+ default:
610
+ goto EOP;
611
+ }
612
+
613
+ if (!parser_advance_if(parser, pCOMMA)) {
614
+ goto EOP;
615
+ }
616
+ }
617
+
618
+ EOP:
619
+ if (parser->next_token.type != pRPAREN) {
620
+ rbs_parser_set_error(parser, parser->next_token, true, "unexpected token for method type parameters");
621
+ return false;
622
+ }
623
+
624
+ return true;
625
+ }
626
+
627
+ /*
628
+ optional ::= {} <simple_type>
629
+ | {} simple_type <`?`>
630
+ */
631
+ NODISCARD
632
+ static bool parse_optional(rbs_parser_t *parser, rbs_node_t **optional) {
633
+ rbs_range_t rg;
634
+ rg.start = parser->next_token.range.start;
635
+
636
+ rbs_node_t *type = NULL;
637
+ CHECK_PARSE(parse_simple(parser, &type));
638
+
639
+ if (parser->next_token.type == pQUESTION) {
640
+ rbs_parser_advance(parser);
641
+ rg.end = parser->current_token.range.end;
642
+ rbs_location_t *location = rbs_location_new(ALLOCATOR(), rg);
643
+ *optional = (rbs_node_t *) rbs_types_optional_new(ALLOCATOR(), location, type);
644
+ } else {
645
+ *optional = type;
646
+ }
647
+
648
+ return true;
649
+ }
650
+
651
+ static void initialize_method_params(method_params *params, rbs_allocator_t *allocator) {
652
+ *params = (method_params) {
653
+ .required_positionals = rbs_node_list_new(allocator),
654
+ .optional_positionals = rbs_node_list_new(allocator),
655
+ .rest_positionals = NULL,
656
+ .trailing_positionals = rbs_node_list_new(allocator),
657
+ .required_keywords = rbs_hash_new(allocator),
658
+ .optional_keywords = rbs_hash_new(allocator),
659
+ .rest_keywords = NULL,
660
+ };
661
+ }
662
+
663
+ /*
664
+ self_type_binding ::= {} <>
665
+ | {} `[` `self` `:` type <`]`>
666
+ */
667
+ NODISCARD
668
+ static bool parse_self_type_binding(rbs_parser_t *parser, rbs_node_t **self_type) {
669
+ if (parser->next_token.type == pLBRACKET) {
670
+ rbs_parser_advance(parser);
671
+ ADVANCE_ASSERT(parser, kSELF);
672
+ ADVANCE_ASSERT(parser, pCOLON);
673
+ rbs_node_t *type;
674
+ CHECK_PARSE(rbs_parse_type(parser, &type));
675
+ ADVANCE_ASSERT(parser, pRBRACKET);
676
+ *self_type = type;
677
+ }
678
+
679
+ return true;
680
+ }
681
+
682
+ typedef struct {
683
+ rbs_node_t *function;
684
+ rbs_types_block_t *block;
685
+ rbs_node_t *function_self_type;
686
+ } parse_function_result;
687
+
688
+ /*
689
+ function ::= {} `(` params `)` self_type_binding? `{` `(` params `)` self_type_binding? `->` optional `}` `->` <optional>
690
+ | {} `(` params `)` self_type_binding? `->` <optional>
691
+ | {} self_type_binding? `{` `(` params `)` self_type_binding? `->` optional `}` `->` <optional>
692
+ | {} self_type_binding? `{` self_type_binding `->` optional `}` `->` <optional>
693
+ | {} self_type_binding? `->` <optional>
694
+ */
695
+ NODISCARD
696
+ static bool parse_function(rbs_parser_t *parser, bool accept_type_binding, parse_function_result **result) {
697
+ rbs_node_t *function = NULL;
698
+ rbs_types_block_t *block = NULL;
699
+ rbs_node_t *function_self_type = NULL;
700
+ rbs_range_t function_range;
701
+ function_range.start = parser->current_token.range.start;
702
+
703
+ method_params params;
704
+ initialize_method_params(&params, ALLOCATOR());
705
+
706
+ if (parser->next_token.type == pLPAREN) {
707
+ rbs_parser_advance(parser);
708
+ CHECK_PARSE(parse_params(parser, &params));
709
+ ADVANCE_ASSERT(parser, pRPAREN);
710
+ }
711
+
712
+ // Passing NULL to function_self_type means the function itself doesn't accept self type binding. (== method type)
713
+ if (accept_type_binding) {
714
+ CHECK_PARSE(parse_self_type_binding(parser, &function_self_type));
715
+ } else {
716
+ if (rbs_is_untyped_params(&params)) {
717
+ if (parser->next_token.type != pARROW) {
718
+ rbs_parser_set_error(parser, parser->next_token2, true, "A method type with untyped method parameter cannot have block");
719
+ return false;
720
+ }
721
+ }
722
+ }
723
+
724
+ bool required = true;
725
+ if (parser->next_token.type == pQUESTION && parser->next_token2.type == pLBRACE) {
726
+ // Optional block
727
+ required = false;
728
+ rbs_parser_advance(parser);
729
+ }
730
+ if (parser->next_token.type == pLBRACE) {
731
+ rbs_parser_advance(parser);
732
+
733
+ method_params block_params;
734
+ initialize_method_params(&block_params, ALLOCATOR());
735
+
736
+ if (parser->next_token.type == pLPAREN) {
737
+ rbs_parser_advance(parser);
738
+ CHECK_PARSE(parse_params(parser, &block_params));
739
+ ADVANCE_ASSERT(parser, pRPAREN);
740
+ }
741
+
742
+ rbs_node_t *self_type = NULL;
743
+ CHECK_PARSE(parse_self_type_binding(parser, &self_type));
744
+
745
+ ADVANCE_ASSERT(parser, pARROW);
746
+ rbs_node_t *block_return_type = NULL;
747
+ CHECK_PARSE(parse_optional(parser, &block_return_type));
748
+
749
+ rbs_node_t *block_function = NULL;
750
+ function_range.end = parser->current_token.range.end;
751
+ rbs_location_t *loc = rbs_location_new(ALLOCATOR(), function_range);
752
+ if (rbs_is_untyped_params(&block_params)) {
753
+ block_function = (rbs_node_t *) rbs_types_untyped_function_new(ALLOCATOR(), loc, block_return_type);
754
+ } else {
755
+ block_function = (rbs_node_t *) rbs_types_function_new(
756
+ ALLOCATOR(),
757
+ loc,
758
+ block_params.required_positionals,
759
+ block_params.optional_positionals,
760
+ block_params.rest_positionals,
761
+ block_params.trailing_positionals,
762
+ block_params.required_keywords,
763
+ block_params.optional_keywords,
764
+ block_params.rest_keywords,
765
+ block_return_type
766
+ );
767
+ }
768
+
769
+ block = rbs_types_block_new(ALLOCATOR(), loc, block_function, required, self_type);
770
+
771
+ ADVANCE_ASSERT(parser, pRBRACE);
772
+ }
773
+
774
+ ADVANCE_ASSERT(parser, pARROW);
775
+ rbs_node_t *type = NULL;
776
+ CHECK_PARSE(parse_optional(parser, &type));
777
+
778
+ function_range.end = parser->current_token.range.end;
779
+ rbs_location_t *loc = rbs_location_new(ALLOCATOR(), function_range);
780
+ if (rbs_is_untyped_params(&params)) {
781
+ function = (rbs_node_t *) rbs_types_untyped_function_new(ALLOCATOR(), loc, type);
782
+ } else {
783
+ function = (rbs_node_t *) rbs_types_function_new(
784
+ ALLOCATOR(),
785
+ loc,
786
+ params.required_positionals,
787
+ params.optional_positionals,
788
+ params.rest_positionals,
789
+ params.trailing_positionals,
790
+ params.required_keywords,
791
+ params.optional_keywords,
792
+ params.rest_keywords,
793
+ type
794
+ );
795
+ }
796
+
797
+ (*result)->function = function;
798
+ (*result)->block = block;
799
+ (*result)->function_self_type = function_self_type;
800
+ return true;
801
+ }
802
+
803
+ /*
804
+ proc_type ::= {`^`} <function>
805
+ */
806
+ NODISCARD
807
+ static bool parse_proc_type(rbs_parser_t *parser, rbs_types_proc_t **proc) {
808
+ rbs_position_t start = parser->current_token.range.start;
809
+ parse_function_result *result = rbs_allocator_alloc(ALLOCATOR(), parse_function_result);
810
+ CHECK_PARSE(parse_function(parser, true, &result));
811
+
812
+ rbs_position_t end = parser->current_token.range.end;
813
+ rbs_location_t *loc = rbs_location_new(ALLOCATOR(), (rbs_range_t) { .start = start, .end = end });
814
+ *proc = rbs_types_proc_new(ALLOCATOR(), loc, result->function, result->block, result->function_self_type);
815
+ return true;
816
+ }
817
+
818
+ static void check_key_duplication(rbs_parser_t *parser, rbs_hash_t *fields, rbs_node_t *key) {
819
+ if (rbs_hash_find(fields, ((rbs_node_t *) key))) {
820
+ rbs_parser_set_error(parser, parser->current_token, true, "duplicated record key");
821
+ }
822
+ }
823
+
824
+ /**
825
+ * ... `{` ... `}` ...
826
+ * > >
827
+ * */
828
+ /*
829
+ record_attributes ::= {`{`} record_attribute... <record_attribute> `}`
830
+
831
+ record_attribute ::= {} keyword_token `:` <type>
832
+ | {} literal_type `=>` <type>
833
+ */
834
+ NODISCARD
835
+ static bool parse_record_attributes(rbs_parser_t *parser, rbs_hash_t **fields) {
836
+ *fields = rbs_hash_new(ALLOCATOR());
837
+
838
+ if (parser->next_token.type == pRBRACE) return true;
839
+
840
+ while (true) {
841
+ rbs_ast_symbol_t *key = NULL;
842
+ bool required = true;
843
+
844
+ if (parser->next_token.type == pQUESTION) {
845
+ // { ?foo: type } syntax
846
+ required = false;
847
+ rbs_parser_advance(parser);
848
+ }
849
+
850
+ if (is_keyword(parser)) {
851
+ // { foo: type } syntax
852
+ CHECK_PARSE(parse_keyword_key(parser, &key));
853
+
854
+ check_key_duplication(parser, *fields, (rbs_node_t *) key);
855
+ ADVANCE_ASSERT(parser, pCOLON);
856
+ } else {
857
+ // { key => type } syntax
858
+ switch (parser->next_token.type) {
859
+ case tSYMBOL:
860
+ case tSQSYMBOL:
861
+ case tDQSYMBOL:
862
+ case tSQSTRING:
863
+ case tDQSTRING:
864
+ case tINTEGER:
865
+ case kTRUE:
866
+ case kFALSE: {
867
+ rbs_node_t *type = NULL;
868
+ CHECK_PARSE(parse_simple(parser, &type));
869
+
870
+ key = (rbs_ast_symbol_t *) ((rbs_types_literal_t *) type)->literal;
871
+ break;
872
+ }
873
+ default:
874
+ rbs_parser_set_error(parser, parser->next_token, true, "unexpected record key token");
875
+ return false;
876
+ }
877
+ check_key_duplication(parser, *fields, (rbs_node_t *) key);
878
+ ADVANCE_ASSERT(parser, pFATARROW);
879
+ }
880
+
881
+ rbs_range_t field_range;
882
+ field_range.start = parser->current_token.range.end;
883
+
884
+ rbs_node_t *type;
885
+ CHECK_PARSE(rbs_parse_type(parser, &type));
886
+
887
+ field_range.end = parser->current_token.range.end;
888
+ rbs_location_t *loc = rbs_location_new(ALLOCATOR(), field_range);
889
+ rbs_hash_set(*fields, (rbs_node_t *) key, (rbs_node_t *) rbs_types_record_field_type_new(ALLOCATOR(), loc, type, required));
890
+
891
+ if (parser_advance_if(parser, pCOMMA)) {
892
+ if (parser->next_token.type == pRBRACE) {
893
+ break;
894
+ }
895
+ } else {
896
+ break;
897
+ }
898
+ }
899
+ return true;
900
+ }
901
+
902
+ /*
903
+ symbol ::= {<tSYMBOL>}
904
+ */
905
+ NODISCARD
906
+ static bool parse_symbol(rbs_parser_t *parser, rbs_location_t *location, rbs_types_literal_t **symbol) {
907
+ size_t offset_bytes = parser->rbs_lexer_t->encoding->char_width((const uint8_t *) ":", (size_t) 1);
908
+ size_t bytes = rbs_token_bytes(parser->current_token) - offset_bytes;
909
+
910
+ rbs_ast_symbol_t *literal;
911
+
912
+ switch (parser->current_token.type)
913
+ {
914
+ case tSYMBOL: {
915
+ rbs_location_t *symbolLoc = rbs_location_current_token(parser);
916
+
917
+ char *buffer = rbs_peek_token(parser->rbs_lexer_t, parser->current_token);
918
+ rbs_constant_id_t constant_id = rbs_constant_pool_insert_shared(
919
+ &parser->constant_pool,
920
+ (const uint8_t *) buffer+offset_bytes,
921
+ bytes
922
+ );
923
+ literal = rbs_ast_symbol_new(ALLOCATOR(), symbolLoc, &parser->constant_pool, constant_id);
924
+ break;
925
+ }
926
+ case tDQSYMBOL:
927
+ case tSQSYMBOL: {
928
+ rbs_location_t *symbolLoc = rbs_location_current_token(parser);
929
+ rbs_string_t current_token = rbs_parser_peek_current_token(parser);
930
+
931
+ rbs_string_t symbol = rbs_string_new(current_token.start + offset_bytes, current_token.end);
932
+
933
+ rbs_string_t unquoted_symbol = rbs_unquote_string(ALLOCATOR(), symbol);
934
+
935
+ rbs_constant_id_t constant_id = rbs_constant_pool_insert_string(&parser->constant_pool, unquoted_symbol);
936
+
937
+ literal = rbs_ast_symbol_new(ALLOCATOR(), symbolLoc, &parser->constant_pool, constant_id);
938
+ break;
939
+ }
940
+ default:
941
+ rbs_parser_set_error(parser, parser->current_token, false, "Unexpected error");
942
+ return false;
943
+ }
944
+
945
+ *symbol = rbs_types_literal_new(ALLOCATOR(), location, (rbs_node_t *) literal);
946
+ return true;
947
+ }
948
+
949
+ /*
950
+ instance_type ::= {type_name} <type_args>
951
+
952
+ type_args ::= {} <> /empty/
953
+ | {} `[` type_list <`]`>
954
+ */
955
+ NODISCARD
956
+ static bool parse_instance_type(rbs_parser_t *parser, bool parse_alias, rbs_node_t **type) {
957
+ TypeNameKind expected_kind = INTERFACE_NAME | CLASS_NAME;
958
+ if (parse_alias) {
959
+ expected_kind |= ALIAS_NAME;
960
+ }
961
+
962
+ rbs_range_t name_range;
963
+ rbs_type_name_t *type_name = NULL;
964
+ CHECK_PARSE(parse_type_name(parser, expected_kind, &name_range, &type_name));
965
+
966
+ rbs_node_list_t *types = rbs_node_list_new(ALLOCATOR());
967
+
968
+ TypeNameKind kind;
969
+ switch (parser->current_token.type) {
970
+ case tUIDENT: {
971
+ kind = CLASS_NAME;
972
+ break;
973
+ }
974
+ case tULIDENT: {
975
+ kind = INTERFACE_NAME;
976
+ break;
977
+ }
978
+ case kSKIP:
979
+ case kRETURN:
980
+ case tLIDENT: {
981
+ kind = ALIAS_NAME;
982
+ break;
983
+ }
984
+ default:
985
+ rbs_parser_set_error(parser, parser->current_token, false, "unexpected token for type name");
986
+ return false;
987
+ }
988
+
989
+ rbs_range_t args_range;
990
+ if (parser->next_token.type == pLBRACKET) {
991
+ rbs_parser_advance(parser);
992
+ args_range.start = parser->current_token.range.start;
993
+ CHECK_PARSE(parse_type_list(parser, pRBRACKET, types));
994
+ ADVANCE_ASSERT(parser, pRBRACKET);
995
+ args_range.end = parser->current_token.range.end;
996
+ } else {
997
+ args_range = NULL_RANGE;
998
+ }
999
+
1000
+ rbs_range_t type_range = {
1001
+ .start = name_range.start,
1002
+ .end = rbs_nonnull_pos_or(args_range.end, name_range.end),
1003
+ };
1004
+
1005
+ rbs_location_t *loc = rbs_location_new(ALLOCATOR(), type_range);
1006
+ rbs_loc_alloc_children(ALLOCATOR(), loc, 2);
1007
+ rbs_loc_add_required_child(loc, INTERN("name"), name_range);
1008
+ rbs_loc_add_optional_child(loc, INTERN("args"), args_range);
1009
+
1010
+ if (kind == CLASS_NAME) {
1011
+ *type = (rbs_node_t *) rbs_types_class_instance_new(ALLOCATOR(), loc, type_name, types);
1012
+ } else if (kind == INTERFACE_NAME) {
1013
+ *type = (rbs_node_t *) rbs_types_interface_new(ALLOCATOR(), loc, type_name, types);
1014
+ } else if (kind == ALIAS_NAME) {
1015
+ *type = (rbs_node_t *) rbs_types_alias_new(ALLOCATOR(), loc, type_name, types);
1016
+ }
1017
+
1018
+ return true;
1019
+ }
1020
+
1021
+ /*
1022
+ singleton_type ::= {`singleton`} `(` type_name <`)`>
1023
+ */
1024
+ NODISCARD
1025
+ static bool parse_singleton_type(rbs_parser_t *parser, rbs_types_class_singleton_t **singleton) {
1026
+ ASSERT_TOKEN(parser, kSINGLETON);
1027
+
1028
+ rbs_range_t type_range;
1029
+ type_range.start = parser->current_token.range.start;
1030
+ ADVANCE_ASSERT(parser, pLPAREN);
1031
+ rbs_parser_advance(parser);
1032
+
1033
+ rbs_range_t name_range;
1034
+ rbs_type_name_t *type_name = NULL;
1035
+ CHECK_PARSE(parse_type_name(parser, CLASS_NAME, &name_range, &type_name));
1036
+
1037
+ ADVANCE_ASSERT(parser, pRPAREN);
1038
+ type_range.end = parser->current_token.range.end;
1039
+
1040
+ rbs_location_t *loc = rbs_location_new(ALLOCATOR(), type_range);
1041
+ rbs_loc_alloc_children(ALLOCATOR(), loc, 1);
1042
+ rbs_loc_add_required_child(loc, INTERN("name"), name_range);
1043
+
1044
+ *singleton = rbs_types_class_singleton_new(ALLOCATOR(), loc, type_name);
1045
+ return true;
1046
+ }
1047
+
1048
+ /**
1049
+ * Returns true if given type variable is recorded in the table.
1050
+ * If not found, it goes one table up, if it's not a reset table.
1051
+ * Or returns false, if it's a reset table.
1052
+ * */
1053
+ static bool parser_typevar_member(rbs_parser_t *parser, rbs_constant_id_t id) {
1054
+ id_table *table = parser->vars;
1055
+
1056
+ while (table && !RESET_TABLE_P(table)) {
1057
+ for (size_t i = 0; i < table->count; i++) {
1058
+ if (table->ids[i] == id) {
1059
+ return true;
1060
+ }
1061
+ }
1062
+
1063
+ table = table->next;
1064
+ }
1065
+
1066
+ return false;
1067
+ }
1068
+
1069
+ /*
1070
+ simple ::= {} `(` type <`)`>
1071
+ | {} <base type>
1072
+ | {} <type_name>
1073
+ | {} class_instance `[` type_list <`]`>
1074
+ | {} `singleton` `(` type_name <`)`>
1075
+ | {} `[` type_list <`]`>
1076
+ | {} `{` record_attributes <`}`>
1077
+ | {} `^` <function>
1078
+ */
1079
+ NODISCARD
1080
+ static bool parse_simple(rbs_parser_t *parser, rbs_node_t **type) {
1081
+ rbs_parser_advance(parser);
1082
+
1083
+ switch (parser->current_token.type) {
1084
+ case pLPAREN: {
1085
+ rbs_node_t *lparen_type;
1086
+ CHECK_PARSE(rbs_parse_type(parser, &lparen_type));
1087
+ ADVANCE_ASSERT(parser, pRPAREN);
1088
+ *type = lparen_type;
1089
+ return true;
1090
+ }
1091
+ case kBOOL: {
1092
+ rbs_location_t *loc = rbs_location_current_token(parser);
1093
+ *type = (rbs_node_t *) rbs_types_bases_bool_new(ALLOCATOR(), loc);
1094
+ return true;
1095
+ }
1096
+ case kBOT: {
1097
+ rbs_location_t *loc = rbs_location_current_token(parser);
1098
+ *type = (rbs_node_t *) rbs_types_bases_bottom_new(ALLOCATOR(), loc);
1099
+ return true;
1100
+ }
1101
+ case kCLASS: {
1102
+ rbs_location_t *loc = rbs_location_current_token(parser);
1103
+ *type = (rbs_node_t *) rbs_types_bases_class_new(ALLOCATOR(), loc);
1104
+ return true;
1105
+ }
1106
+ case kINSTANCE: {
1107
+ rbs_location_t *loc = rbs_location_current_token(parser);
1108
+ *type = (rbs_node_t *) rbs_types_bases_instance_new(ALLOCATOR(), loc);
1109
+ return true;
1110
+ }
1111
+ case kNIL: {
1112
+ rbs_location_t *loc = rbs_location_current_token(parser);
1113
+ *type = (rbs_node_t *) rbs_types_bases_nil_new(ALLOCATOR(), loc);
1114
+ return true;
1115
+ }
1116
+ case kSELF: {
1117
+ rbs_location_t *loc = rbs_location_current_token(parser);
1118
+ *type = (rbs_node_t *) rbs_types_bases_self_new(ALLOCATOR(), loc);
1119
+ return true;
1120
+ }
1121
+ case kTOP: {
1122
+ rbs_location_t *loc = rbs_location_current_token(parser);
1123
+ *type = (rbs_node_t *) rbs_types_bases_top_new(ALLOCATOR(), loc);
1124
+ return true;
1125
+ }
1126
+ case kVOID: {
1127
+ rbs_location_t *loc = rbs_location_current_token(parser);
1128
+ *type = (rbs_node_t *) rbs_types_bases_void_new(ALLOCATOR(), loc);
1129
+ return true;
1130
+ }
1131
+ case kUNTYPED: {
1132
+ rbs_location_t *loc = rbs_location_current_token(parser);
1133
+ *type = (rbs_node_t *) rbs_types_bases_any_new(ALLOCATOR(), loc, false);
1134
+ return true;
1135
+ }
1136
+ case k__TODO__: {
1137
+ rbs_location_t *loc = rbs_location_current_token(parser);
1138
+ *type = (rbs_node_t *) rbs_types_bases_any_new(ALLOCATOR(), loc, true);
1139
+ return true;
1140
+ }
1141
+ case tINTEGER: {
1142
+ rbs_location_t *loc = rbs_location_current_token(parser);
1143
+
1144
+ rbs_string_t string = rbs_parser_peek_current_token(parser);
1145
+ rbs_string_t stripped_string = rbs_string_strip_whitespace( &string);
1146
+
1147
+ rbs_node_t *literal = (rbs_node_t *) rbs_ast_integer_new(ALLOCATOR(), loc, stripped_string);
1148
+ *type = (rbs_node_t *) rbs_types_literal_new(ALLOCATOR(), loc, literal);
1149
+ return true;
1150
+ }
1151
+ case kTRUE: {
1152
+ rbs_location_t *loc = rbs_location_current_token(parser);
1153
+ *type = (rbs_node_t *) rbs_types_literal_new(ALLOCATOR(), loc, (rbs_node_t *) rbs_ast_bool_new(ALLOCATOR(), loc, true));
1154
+ return true;
1155
+ }
1156
+ case kFALSE: {
1157
+ rbs_location_t *loc = rbs_location_current_token(parser);
1158
+ *type = (rbs_node_t *) rbs_types_literal_new(ALLOCATOR(), loc, (rbs_node_t *) rbs_ast_bool_new(ALLOCATOR(), loc, false));
1159
+ return true;
1160
+ }
1161
+ case tSQSTRING:
1162
+ case tDQSTRING: {
1163
+ rbs_location_t *loc = rbs_location_current_token(parser);
1164
+
1165
+ rbs_string_t unquoted_str = rbs_unquote_string(ALLOCATOR(), rbs_parser_peek_current_token(parser));
1166
+ rbs_node_t *literal = (rbs_node_t *) rbs_ast_string_new(ALLOCATOR(), loc, unquoted_str);
1167
+ *type = (rbs_node_t *) rbs_types_literal_new(ALLOCATOR(), loc, literal);
1168
+ return true;
1169
+ }
1170
+ case tSYMBOL:
1171
+ case tSQSYMBOL:
1172
+ case tDQSYMBOL: {
1173
+ rbs_location_t *loc = rbs_location_current_token(parser);
1174
+ rbs_types_literal_t *literal = NULL;
1175
+ CHECK_PARSE(parse_symbol(parser, loc, &literal));
1176
+ *type = (rbs_node_t *) literal;
1177
+ return true;
1178
+ }
1179
+ case tUIDENT: {
1180
+ const char *name_str = rbs_peek_token(parser->rbs_lexer_t, parser->current_token);
1181
+ size_t name_len = rbs_token_bytes(parser->current_token);
1182
+
1183
+ rbs_constant_id_t name = rbs_constant_pool_find(&parser->constant_pool, (const uint8_t *) name_str, name_len);
1184
+
1185
+ if (parser_typevar_member(parser, name)) {
1186
+ rbs_location_t *loc = rbs_location_current_token(parser);
1187
+ rbs_ast_symbol_t *symbol = rbs_ast_symbol_new(ALLOCATOR(), loc, &parser->constant_pool, name);
1188
+ *type = (rbs_node_t *) rbs_types_variable_new(ALLOCATOR(), loc, symbol);
1189
+ return true;
1190
+ }
1191
+
1192
+ RBS_FALLTHROUGH // for type name
1193
+ }
1194
+ case tULIDENT:
1195
+ case tLIDENT:
1196
+ case kSKIP:
1197
+ case kRETURN:
1198
+ case pCOLON2: {
1199
+ rbs_node_t *instance_type = NULL;
1200
+ CHECK_PARSE(parse_instance_type(parser, true, &instance_type));
1201
+ *type = instance_type;
1202
+ return true;
1203
+ }
1204
+ case kSINGLETON: {
1205
+ rbs_types_class_singleton_t *singleton = NULL;
1206
+ CHECK_PARSE(parse_singleton_type(parser, &singleton));
1207
+ *type = (rbs_node_t *) singleton;
1208
+ return true;
1209
+ }
1210
+ case pLBRACKET: {
1211
+ rbs_range_t rg;
1212
+ rg.start = parser->current_token.range.start;
1213
+ rbs_node_list_t *types = rbs_node_list_new(ALLOCATOR());
1214
+ if (parser->next_token.type != pRBRACKET) {
1215
+ CHECK_PARSE(parse_type_list(parser, pRBRACKET, types));
1216
+ }
1217
+ ADVANCE_ASSERT(parser, pRBRACKET);
1218
+ rg.end = parser->current_token.range.end;
1219
+
1220
+ rbs_location_t *loc = rbs_location_new(ALLOCATOR(), rg);
1221
+ *type = (rbs_node_t *) rbs_types_tuple_new(ALLOCATOR(), loc, types);
1222
+ return true;
1223
+ }
1224
+ case pAREF_OPR: {
1225
+ rbs_location_t *loc = rbs_location_current_token(parser);
1226
+ rbs_node_list_t *types = rbs_node_list_new(ALLOCATOR());
1227
+ *type = (rbs_node_t *) rbs_types_tuple_new(ALLOCATOR(), loc, types);
1228
+ return true;
1229
+ }
1230
+ case pLBRACE: {
1231
+ rbs_position_t start = parser->current_token.range.start;
1232
+ rbs_hash_t *fields = NULL;
1233
+ CHECK_PARSE(parse_record_attributes(parser, &fields));
1234
+ ADVANCE_ASSERT(parser, pRBRACE);
1235
+ rbs_position_t end = parser->current_token.range.end;
1236
+ rbs_location_t *loc = rbs_location_new(ALLOCATOR(), (rbs_range_t) { .start = start, .end = end });
1237
+ *type = (rbs_node_t *) rbs_types_record_new(ALLOCATOR(), loc, fields);
1238
+ return true;
1239
+ }
1240
+ case pHAT: {
1241
+ rbs_types_proc_t *value = NULL;
1242
+ CHECK_PARSE(parse_proc_type(parser, &value));
1243
+ *type = (rbs_node_t *) value;
1244
+ return true;
1245
+ }
1246
+ default:
1247
+ rbs_parser_set_error(parser, parser->current_token, true, "unexpected token for simple type");
1248
+ return false;
1249
+ }
1250
+ }
1251
+
1252
+ /*
1253
+ intersection ::= {} optional `&` ... '&' <optional>
1254
+ | {} <optional>
1255
+ */
1256
+ NODISCARD
1257
+ static bool parse_intersection(rbs_parser_t *parser, rbs_node_t **type) {
1258
+ rbs_range_t rg;
1259
+ rg.start = parser->next_token.range.start;
1260
+
1261
+ rbs_node_t *optional = NULL;
1262
+ CHECK_PARSE(parse_optional(parser, &optional));
1263
+ *type = optional;
1264
+
1265
+ rbs_node_list_t *intersection_types = rbs_node_list_new(ALLOCATOR());
1266
+
1267
+ rbs_node_list_append(intersection_types, optional);
1268
+ while (parser->next_token.type == pAMP) {
1269
+ rbs_parser_advance(parser);
1270
+ rbs_node_t *type = NULL;
1271
+ CHECK_PARSE(parse_optional(parser, &type));
1272
+ rbs_node_list_append(intersection_types, type);
1273
+ }
1274
+
1275
+ rg.end = parser->current_token.range.end;
1276
+
1277
+ if (intersection_types->length > 1) {
1278
+ rbs_location_t *location = rbs_location_new(ALLOCATOR(), rg);
1279
+ *type = (rbs_node_t *) rbs_types_intersection_new(ALLOCATOR(), location, intersection_types);
1280
+ }
1281
+
1282
+ return true;
1283
+ }
1284
+
1285
+ /*
1286
+ union ::= {} intersection '|' ... '|' <intersection>
1287
+ | {} <intersection>
1288
+ */
1289
+ bool rbs_parse_type(rbs_parser_t *parser, rbs_node_t **type) {
1290
+ rbs_range_t rg;
1291
+ rg.start = parser->next_token.range.start;
1292
+ rbs_node_list_t *union_types = rbs_node_list_new(ALLOCATOR());
1293
+
1294
+ CHECK_PARSE(parse_intersection(parser, type));
1295
+
1296
+ rbs_node_list_append(union_types, *type);
1297
+
1298
+ while (parser->next_token.type == pBAR) {
1299
+ rbs_parser_advance(parser);
1300
+ rbs_node_t *intersection = NULL;
1301
+ CHECK_PARSE(parse_intersection(parser, &intersection));
1302
+ rbs_node_list_append(union_types, intersection);
1303
+ }
1304
+
1305
+ rg.end = parser->current_token.range.end;
1306
+
1307
+ if (union_types->length > 1) {
1308
+ rbs_location_t *location = rbs_location_new(ALLOCATOR(), rg);
1309
+ *type = (rbs_node_t *) rbs_types_union_new(ALLOCATOR(), location, union_types);
1310
+ }
1311
+
1312
+ return true;
1313
+ }
1314
+
1315
+ /*
1316
+ type_params ::= {} `[` type_param `,` ... <`]`>
1317
+ | {<>}
1318
+
1319
+ type_param ::= kUNCHECKED? (kIN|kOUT|) tUIDENT upper_bound? default_type? (module_type_params == true)
1320
+
1321
+ type_param ::= tUIDENT upper_bound? default_type? (module_type_params == false)
1322
+ */
1323
+ NODISCARD
1324
+ static bool parse_type_params(rbs_parser_t *parser, rbs_range_t *rg, bool module_type_params, rbs_node_list_t **params) {
1325
+ *params = rbs_node_list_new(ALLOCATOR());
1326
+
1327
+ bool required_param_allowed = true;
1328
+
1329
+ if (parser->next_token.type == pLBRACKET) {
1330
+ rbs_parser_advance(parser);
1331
+
1332
+ rg->start = parser->current_token.range.start;
1333
+
1334
+ while (true) {
1335
+ bool unchecked = false;
1336
+ rbs_keyword_t *variance = rbs_keyword_new(ALLOCATOR(), rbs_location_current_token(parser), INTERN("invariant"));
1337
+ rbs_node_t *upper_bound = NULL;
1338
+ rbs_node_t *default_type = NULL;
1339
+
1340
+ rbs_range_t param_range;
1341
+ param_range.start = parser->next_token.range.start;
1342
+
1343
+ rbs_range_t unchecked_range = NULL_RANGE;
1344
+ rbs_range_t variance_range = NULL_RANGE;
1345
+ if (module_type_params) {
1346
+ if (parser->next_token.type == kUNCHECKED) {
1347
+ unchecked = true;
1348
+ rbs_parser_advance(parser);
1349
+ unchecked_range = parser->current_token.range;
1350
+ }
1351
+
1352
+ if (parser->next_token.type == kIN || parser->next_token.type == kOUT) {
1353
+ switch (parser->next_token.type) {
1354
+ case kIN:
1355
+ variance = rbs_keyword_new(ALLOCATOR(), rbs_location_current_token(parser), INTERN("contravariant"));
1356
+ break;
1357
+ case kOUT:
1358
+ variance = rbs_keyword_new(ALLOCATOR(), rbs_location_current_token(parser), INTERN("covariant"));
1359
+ break;
1360
+ default:
1361
+ rbs_parser_set_error(parser, parser->current_token, false, "Unexpected error");
1362
+ return false;
1363
+ }
1364
+
1365
+ rbs_parser_advance(parser);
1366
+ variance_range = parser->current_token.range;
1367
+ }
1368
+ }
1369
+
1370
+ ADVANCE_ASSERT(parser, tUIDENT);
1371
+ rbs_range_t name_range = parser->current_token.range;
1372
+
1373
+ rbs_string_t string = rbs_parser_peek_current_token(parser);
1374
+ rbs_location_t *nameSymbolLoc = rbs_location_current_token(parser);
1375
+ rbs_constant_id_t id = rbs_constant_pool_insert_string(&parser->constant_pool, string);
1376
+ rbs_ast_symbol_t *name = rbs_ast_symbol_new(ALLOCATOR(), nameSymbolLoc, &parser->constant_pool, id);
1377
+
1378
+ CHECK_PARSE(rbs_parser_insert_typevar(parser, id));
1379
+
1380
+ rbs_range_t upper_bound_range = NULL_RANGE;
1381
+ if (parser->next_token.type == pLT) {
1382
+ rbs_parser_advance(parser);
1383
+ upper_bound_range.start = parser->current_token.range.start;
1384
+ CHECK_PARSE(rbs_parse_type(parser, &upper_bound));
1385
+ upper_bound_range.end = parser->current_token.range.end;
1386
+ }
1387
+
1388
+ rbs_range_t default_type_range = NULL_RANGE;
1389
+ if (module_type_params) {
1390
+ if (parser->next_token.type == pEQ) {
1391
+ rbs_parser_advance(parser);
1392
+
1393
+ default_type_range.start = parser->current_token.range.start;
1394
+ CHECK_PARSE(rbs_parse_type(parser, &default_type));
1395
+ default_type_range.end = parser->current_token.range.end;
1396
+
1397
+ required_param_allowed = false;
1398
+ } else {
1399
+ if (!required_param_allowed) {
1400
+ rbs_parser_set_error(parser, parser->current_token, true, "required type parameter is not allowed after optional type parameter");
1401
+ return false;
1402
+ }
1403
+ }
1404
+ }
1405
+
1406
+ param_range.end = parser->current_token.range.end;
1407
+
1408
+ rbs_location_t *loc = rbs_location_new(ALLOCATOR(), param_range);
1409
+ rbs_loc_alloc_children(ALLOCATOR(), loc, 5);
1410
+ rbs_loc_add_required_child(loc, INTERN("name"), name_range);
1411
+ rbs_loc_add_optional_child(loc, INTERN("variance"), variance_range);
1412
+ rbs_loc_add_optional_child(loc, INTERN("unchecked"), unchecked_range);
1413
+ rbs_loc_add_optional_child(loc, INTERN("upper_bound"), upper_bound_range);
1414
+ rbs_loc_add_optional_child(loc, INTERN("default"), default_type_range);
1415
+
1416
+ rbs_ast_type_param_t *param = rbs_ast_type_param_new(ALLOCATOR(), loc, name, variance, upper_bound, default_type, unchecked);
1417
+
1418
+ rbs_node_list_append(*params, (rbs_node_t *) param);
1419
+
1420
+ if (parser->next_token.type == pCOMMA) {
1421
+ rbs_parser_advance(parser);
1422
+ }
1423
+
1424
+ if (parser->next_token.type == pRBRACKET) {
1425
+ break;
1426
+ }
1427
+ }
1428
+
1429
+ ADVANCE_ASSERT(parser, pRBRACKET);
1430
+ rg->end = parser->current_token.range.end;
1431
+ } else {
1432
+ *rg = NULL_RANGE;
1433
+ }
1434
+
1435
+ return true;
1436
+ }
1437
+
1438
+ NODISCARD
1439
+ static bool parser_pop_typevar_table(rbs_parser_t *parser) {
1440
+ id_table *table;
1441
+
1442
+ if (parser->vars) {
1443
+ table = parser->vars;
1444
+ parser->vars = table->next;
1445
+ } else {
1446
+ rbs_parser_set_error(parser, parser->current_token, false, "Cannot pop empty table");
1447
+ return false;
1448
+ }
1449
+
1450
+ if (parser->vars && RESET_TABLE_P(parser->vars)) {
1451
+ table = parser->vars;
1452
+ parser->vars = table->next;
1453
+ }
1454
+
1455
+ return true;
1456
+ }
1457
+
1458
+ /*
1459
+ method_type ::= {} type_params <function>
1460
+ */
1461
+ // TODO: Should this be NODISCARD?
1462
+ bool rbs_parse_method_type(rbs_parser_t *parser, rbs_method_type_t **method_type) {
1463
+ rbs_parser_push_typevar_table(parser, false);
1464
+
1465
+ rbs_range_t rg;
1466
+ rg.start = parser->next_token.range.start;
1467
+
1468
+ rbs_range_t params_range = NULL_RANGE;
1469
+ rbs_node_list_t *type_params;
1470
+ CHECK_PARSE(parse_type_params(parser, &params_range, false, &type_params));
1471
+
1472
+ rbs_range_t type_range;
1473
+ type_range.start = parser->next_token.range.start;
1474
+
1475
+ parse_function_result *result = rbs_allocator_alloc(ALLOCATOR(), parse_function_result);
1476
+ CHECK_PARSE(parse_function(parser, false, &result));
1477
+
1478
+ rg.end = parser->current_token.range.end;
1479
+ type_range.end = rg.end;
1480
+
1481
+ CHECK_PARSE(parser_pop_typevar_table(parser));
1482
+
1483
+ rbs_location_t *loc = rbs_location_new(ALLOCATOR(), rg);
1484
+ rbs_loc_alloc_children(ALLOCATOR(), loc, 2);
1485
+ rbs_loc_add_required_child(loc, INTERN("type"), type_range);
1486
+ rbs_loc_add_optional_child(loc, INTERN("type_params"), params_range);
1487
+
1488
+ *method_type = rbs_method_type_new(ALLOCATOR(), loc, type_params, result->function, result->block);
1489
+ return true;
1490
+ }
1491
+
1492
+ /*
1493
+ global_decl ::= {tGIDENT} `:` <type>
1494
+ */
1495
+ NODISCARD
1496
+ static bool parse_global_decl(rbs_parser_t *parser, rbs_node_list_t *annotations, rbs_ast_declarations_global_t **global) {
1497
+ rbs_range_t decl_range;
1498
+ decl_range.start = parser->current_token.range.start;
1499
+
1500
+ rbs_ast_comment_t *comment = rbs_parser_get_comment(parser, decl_range.start.line);
1501
+
1502
+ rbs_range_t name_range = parser->current_token.range;
1503
+ rbs_location_t *symbolLoc = rbs_location_new(ALLOCATOR(), name_range);
1504
+
1505
+ rbs_ast_symbol_t *type_name = rbs_ast_symbol_new(ALLOCATOR(), symbolLoc, &parser->constant_pool, INTERN_TOKEN(parser, parser->current_token));
1506
+
1507
+ ADVANCE_ASSERT(parser, pCOLON);
1508
+ rbs_range_t colon_range = parser->current_token.range;
1509
+
1510
+ rbs_node_t *type;
1511
+ CHECK_PARSE(rbs_parse_type(parser, &type));
1512
+ decl_range.end = parser->current_token.range.end;
1513
+
1514
+ rbs_location_t *loc = rbs_location_new(ALLOCATOR(), decl_range);
1515
+ rbs_loc_alloc_children(ALLOCATOR(), loc, 2);
1516
+ rbs_loc_add_required_child(loc, INTERN("name"), name_range);
1517
+ rbs_loc_add_required_child(loc, INTERN("colon"), colon_range);
1518
+
1519
+ *global = rbs_ast_declarations_global_new(ALLOCATOR(), loc, type_name, type, comment, annotations);
1520
+ return true;
1521
+ }
1522
+
1523
+ /*
1524
+ const_decl ::= {const_name} `:` <type>
1525
+ */
1526
+ NODISCARD
1527
+ static bool parse_const_decl(rbs_parser_t *parser, rbs_node_list_t *annotations, rbs_ast_declarations_constant_t **constant) {
1528
+ rbs_range_t decl_range;
1529
+
1530
+ decl_range.start = parser->current_token.range.start;
1531
+ rbs_ast_comment_t *comment = rbs_parser_get_comment(parser, decl_range.start.line);
1532
+
1533
+ rbs_range_t name_range;
1534
+ rbs_type_name_t *type_name = NULL;
1535
+ CHECK_PARSE(parse_type_name(parser, CLASS_NAME, &name_range, &type_name));
1536
+
1537
+ ADVANCE_ASSERT(parser, pCOLON);
1538
+ rbs_range_t colon_range = parser->current_token.range;
1539
+
1540
+ rbs_node_t *type;
1541
+ CHECK_PARSE(rbs_parse_type(parser, &type));
1542
+
1543
+ decl_range.end = parser->current_token.range.end;
1544
+
1545
+ rbs_location_t *loc = rbs_location_new(ALLOCATOR(), decl_range);
1546
+ rbs_loc_alloc_children(ALLOCATOR(), loc, 2);
1547
+ rbs_loc_add_required_child(loc, INTERN("name"), name_range);
1548
+ rbs_loc_add_required_child(loc, INTERN("colon"), colon_range);
1549
+
1550
+ *constant = rbs_ast_declarations_constant_new(ALLOCATOR(), loc, type_name, type, comment, annotations);
1551
+ return true;
1552
+ }
1553
+
1554
+ /*
1555
+ type_decl ::= {kTYPE} alias_name `=` <type>
1556
+ */
1557
+ NODISCARD
1558
+ static bool parse_type_decl(rbs_parser_t *parser, rbs_position_t comment_pos, rbs_node_list_t *annotations, rbs_ast_declarations_type_alias_t **typealias) {
1559
+ rbs_parser_push_typevar_table(parser, true);
1560
+
1561
+ rbs_range_t decl_range;
1562
+ decl_range.start = parser->current_token.range.start;
1563
+ comment_pos = rbs_nonnull_pos_or(comment_pos, decl_range.start);
1564
+
1565
+ rbs_range_t keyword_range = parser->current_token.range;
1566
+
1567
+ rbs_parser_advance(parser);
1568
+
1569
+ rbs_range_t name_range;
1570
+ rbs_type_name_t *type_name = NULL;
1571
+ CHECK_PARSE(parse_type_name(parser, ALIAS_NAME, &name_range, &type_name));
1572
+
1573
+ rbs_range_t params_range;
1574
+ rbs_node_list_t *type_params;
1575
+ CHECK_PARSE(parse_type_params(parser, &params_range, true, &type_params));
1576
+
1577
+ ADVANCE_ASSERT(parser, pEQ);
1578
+ rbs_range_t eq_range = parser->current_token.range;
1579
+
1580
+ rbs_node_t *type;
1581
+ CHECK_PARSE(rbs_parse_type(parser, &type));
1582
+
1583
+ decl_range.end = parser->current_token.range.end;
1584
+
1585
+ rbs_location_t *loc = rbs_location_new(ALLOCATOR(), decl_range);
1586
+ rbs_loc_alloc_children(ALLOCATOR(), loc, 4);
1587
+ rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range);
1588
+ rbs_loc_add_required_child(loc, INTERN("name"), name_range);
1589
+ rbs_loc_add_optional_child(loc, INTERN("type_params"), params_range);
1590
+ rbs_loc_add_required_child(loc, INTERN("eq"), eq_range);
1591
+
1592
+ CHECK_PARSE(parser_pop_typevar_table(parser));
1593
+
1594
+ rbs_ast_comment_t *comment = rbs_parser_get_comment(parser, comment_pos.line);
1595
+
1596
+ *typealias = rbs_ast_declarations_type_alias_new(ALLOCATOR(), loc, type_name, type_params, type, annotations, comment);
1597
+ return true;
1598
+ }
1599
+
1600
+ /*
1601
+ annotation ::= {<tANNOTATION>}
1602
+ */
1603
+ NODISCARD
1604
+ static bool parse_annotation(rbs_parser_t *parser, rbs_ast_annotation_t **annotation) {
1605
+ rbs_range_t rg = parser->current_token.range;
1606
+
1607
+ size_t offset_bytes =
1608
+ parser->rbs_lexer_t->encoding->char_width((const uint8_t *) "%", (size_t) 1) +
1609
+ parser->rbs_lexer_t->encoding->char_width((const uint8_t *) "a", (size_t) 1);
1610
+
1611
+ rbs_string_t str = rbs_string_new(
1612
+ parser->rbs_lexer_t->string.start + rg.start.byte_pos + offset_bytes,
1613
+ parser->rbs_lexer_t->string.end
1614
+ );
1615
+ unsigned int open_char = rbs_utf8_string_to_codepoint(str);
1616
+
1617
+ unsigned int close_char;
1618
+
1619
+ switch (open_char) {
1620
+ case '{':
1621
+ close_char = '}';
1622
+ break;
1623
+ case '(':
1624
+ close_char = ')';
1625
+ break;
1626
+ case '[':
1627
+ close_char = ']';
1628
+ break;
1629
+ case '<':
1630
+ close_char = '>';
1631
+ break;
1632
+ case '|':
1633
+ close_char = '|';
1634
+ break;
1635
+ default:
1636
+ rbs_parser_set_error(parser, parser->current_token, false, "Unexpected error");
1637
+ return false;
1638
+ }
1639
+
1640
+ size_t open_bytes = parser->rbs_lexer_t->encoding->char_width((const uint8_t *) &open_char, (size_t) 1);
1641
+ size_t close_bytes = parser->rbs_lexer_t->encoding->char_width((const uint8_t *) &close_char, (size_t) 1);
1642
+
1643
+ rbs_string_t current_token = rbs_parser_peek_current_token(parser);
1644
+ size_t total_offset = offset_bytes + open_bytes;
1645
+
1646
+ rbs_string_t annotation_str = rbs_string_new(
1647
+ current_token.start + total_offset,
1648
+ current_token.end - close_bytes
1649
+ );
1650
+
1651
+ rbs_string_t stripped_annotation_str = rbs_string_strip_whitespace(&annotation_str);
1652
+
1653
+ *annotation = rbs_ast_annotation_new(ALLOCATOR(), rbs_location_new(ALLOCATOR(), rg), stripped_annotation_str);
1654
+ return true;
1655
+ }
1656
+
1657
+ /*
1658
+ annotations ::= {} annotation ... <annotation>
1659
+ | {<>}
1660
+ */
1661
+ NODISCARD
1662
+ static bool parse_annotations(rbs_parser_t *parser, rbs_node_list_t *annotations, rbs_position_t *annot_pos) {
1663
+ *annot_pos = NullPosition;
1664
+
1665
+ while (true) {
1666
+ if (parser->next_token.type == tANNOTATION) {
1667
+ rbs_parser_advance(parser);
1668
+
1669
+ if (rbs_null_position_p((*annot_pos))) {
1670
+ *annot_pos = parser->current_token.range.start;
1671
+ }
1672
+
1673
+ rbs_ast_annotation_t *annotation = NULL;
1674
+ CHECK_PARSE(parse_annotation(parser, &annotation));
1675
+ rbs_node_list_append(annotations, (rbs_node_t *) annotation);
1676
+ } else {
1677
+ break;
1678
+ }
1679
+ }
1680
+
1681
+ return true;
1682
+ }
1683
+
1684
+ /*
1685
+ method_name ::= {} <IDENT | keyword>
1686
+ | {} (IDENT | keyword)~<`?`>
1687
+ */
1688
+ NODISCARD
1689
+ static bool parse_method_name(rbs_parser_t *parser, rbs_range_t *range, rbs_ast_symbol_t **symbol) {
1690
+ rbs_parser_advance(parser);
1691
+
1692
+ switch (parser->current_token.type)
1693
+ {
1694
+ case tUIDENT:
1695
+ case tLIDENT:
1696
+ case tULIDENT:
1697
+ case tULLIDENT:
1698
+ KEYWORD_CASES
1699
+ if (parser->next_token.type == pQUESTION && parser->current_token.range.end.byte_pos == parser->next_token.range.start.byte_pos) {
1700
+ range->start = parser->current_token.range.start;
1701
+ range->end = parser->next_token.range.end;
1702
+ rbs_parser_advance(parser);
1703
+
1704
+ rbs_constant_id_t constant_id = rbs_constant_pool_insert_shared_with_encoding(
1705
+ &parser->constant_pool,
1706
+ (const uint8_t *) parser->rbs_lexer_t->string.start + range->start.byte_pos,
1707
+ range->end.byte_pos - range->start.byte_pos,
1708
+ parser->rbs_lexer_t->encoding
1709
+ );
1710
+
1711
+ rbs_location_t *symbolLoc = rbs_location_new(ALLOCATOR(), *range);
1712
+ *symbol = rbs_ast_symbol_new(ALLOCATOR(), symbolLoc, &parser->constant_pool, constant_id);
1713
+ } else {
1714
+ *range = parser->current_token.range;
1715
+ rbs_location_t *symbolLoc = rbs_location_new(ALLOCATOR(), *range);
1716
+ *symbol = rbs_ast_symbol_new(ALLOCATOR(), symbolLoc, &parser->constant_pool, INTERN_TOKEN(parser, parser->current_token));
1717
+ }
1718
+ return true;
1719
+
1720
+ case tBANGIDENT:
1721
+ case tEQIDENT: {
1722
+ *range = parser->current_token.range;
1723
+ rbs_location_t *symbolLoc = rbs_location_new(ALLOCATOR(), *range);
1724
+ *symbol = rbs_ast_symbol_new(ALLOCATOR(), symbolLoc, &parser->constant_pool, INTERN_TOKEN(parser, parser->current_token));
1725
+ return true;
1726
+ }
1727
+ case tQIDENT: {
1728
+ rbs_string_t string = rbs_parser_peek_current_token(parser);
1729
+ rbs_string_t unquoted_str = rbs_unquote_string(ALLOCATOR(), string);
1730
+ rbs_constant_id_t constant_id = rbs_constant_pool_insert_string(&parser->constant_pool, unquoted_str);
1731
+ rbs_location_t *symbolLoc = rbs_location_current_token(parser);
1732
+ *symbol = rbs_ast_symbol_new(ALLOCATOR(), symbolLoc, &parser->constant_pool, constant_id);
1733
+ return true;
1734
+ }
1735
+
1736
+ case pBAR:
1737
+ case pHAT:
1738
+ case pAMP:
1739
+ case pSTAR:
1740
+ case pSTAR2:
1741
+ case pLT:
1742
+ case pAREF_OPR:
1743
+ case tOPERATOR: {
1744
+ *range = parser->current_token.range;
1745
+ rbs_location_t *symbolLoc = rbs_location_new(ALLOCATOR(), *range);
1746
+ *symbol = rbs_ast_symbol_new(ALLOCATOR(), symbolLoc, &parser->constant_pool, INTERN_TOKEN(parser, parser->current_token));
1747
+ return true;
1748
+ }
1749
+
1750
+ default:
1751
+ rbs_parser_set_error(parser, parser->current_token, true, "unexpected token for method name");
1752
+ return false;
1753
+ }
1754
+ }
1755
+
1756
+ typedef enum {
1757
+ INSTANCE_KIND,
1758
+ SINGLETON_KIND,
1759
+ INSTANCE_SINGLETON_KIND
1760
+ } InstanceSingletonKind;
1761
+
1762
+ /*
1763
+ instance_singleton_kind ::= {<>}
1764
+ | {} kSELF <`.`>
1765
+ | {} kSELF~`?` <`.`>
1766
+
1767
+ @param allow_selfq `true` to accept `self?` kind.
1768
+ */
1769
+ static InstanceSingletonKind parse_instance_singleton_kind(rbs_parser_t *parser, bool allow_selfq, rbs_range_t *rg) {
1770
+ InstanceSingletonKind kind = INSTANCE_KIND;
1771
+
1772
+ if (parser->next_token.type == kSELF) {
1773
+ rbs_range_t self_range = parser->next_token.range;
1774
+
1775
+ if (parser->next_token2.type == pDOT) {
1776
+ rbs_parser_advance(parser);
1777
+ rbs_parser_advance(parser);
1778
+ kind = SINGLETON_KIND;
1779
+ } else if (
1780
+ parser->next_token2.type == pQUESTION
1781
+ && parser->next_token.range.end.char_pos == parser->next_token2.range.start.char_pos
1782
+ && parser->next_token3.type == pDOT
1783
+ && allow_selfq) {
1784
+ rbs_parser_advance(parser);
1785
+ rbs_parser_advance(parser);
1786
+ rbs_parser_advance(parser);
1787
+ kind = INSTANCE_SINGLETON_KIND;
1788
+ }
1789
+
1790
+ *rg = (rbs_range_t) {
1791
+ .start = self_range.start,
1792
+ .end = parser->current_token.range.end,
1793
+ };
1794
+ } else {
1795
+ *rg = NULL_RANGE;
1796
+ }
1797
+
1798
+ return kind;
1799
+ }
1800
+
1801
+ /**
1802
+ * def_member ::= {kDEF} method_name `:` <method_types>
1803
+ * | {kPRIVATE} kDEF method_name `:` <method_types>
1804
+ * | {kPUBLIC} kDEF method_name `:` <method_types>
1805
+ *
1806
+ * method_types ::= {} <method_type>
1807
+ * | {} <`...`>
1808
+ * | {} method_type `|` <method_types>
1809
+ *
1810
+ * @param instance_only `true` to reject singleton method definition.
1811
+ * @param accept_overload `true` to accept overloading (...) definition.
1812
+ * */
1813
+ NODISCARD
1814
+ static bool parse_member_def(rbs_parser_t *parser, bool instance_only, bool accept_overload, rbs_position_t comment_pos, rbs_node_list_t *annotations, rbs_ast_members_method_definition_t **method_definition) {
1815
+ rbs_range_t member_range;
1816
+ member_range.start = parser->current_token.range.start;
1817
+ comment_pos = rbs_nonnull_pos_or(comment_pos, member_range.start);
1818
+
1819
+ rbs_ast_comment_t *comment = rbs_parser_get_comment(parser, comment_pos.line);
1820
+
1821
+ rbs_range_t visibility_range;
1822
+ rbs_keyword_t *visibility;
1823
+ switch (parser->current_token.type)
1824
+ {
1825
+ case kPRIVATE: {
1826
+ visibility_range = parser->current_token.range;
1827
+ visibility = rbs_keyword_new(ALLOCATOR(), rbs_location_new(ALLOCATOR(), visibility_range), INTERN("private"));
1828
+ member_range.start = visibility_range.start;
1829
+ rbs_parser_advance(parser);
1830
+ break;
1831
+ }
1832
+ case kPUBLIC: {
1833
+ visibility_range = parser->current_token.range;
1834
+ visibility = rbs_keyword_new(ALLOCATOR(), rbs_location_new(ALLOCATOR(), visibility_range), INTERN("public"));
1835
+ member_range.start = visibility_range.start;
1836
+ rbs_parser_advance(parser);
1837
+ break;
1838
+ }
1839
+ default:
1840
+ visibility_range = NULL_RANGE;
1841
+ visibility = NULL;
1842
+ break;
1843
+ }
1844
+
1845
+ rbs_range_t keyword_range = parser->current_token.range;
1846
+
1847
+ rbs_range_t kind_range;
1848
+ InstanceSingletonKind kind;
1849
+ if (instance_only) {
1850
+ kind_range = NULL_RANGE;
1851
+ kind = INSTANCE_KIND;
1852
+ } else {
1853
+ kind = parse_instance_singleton_kind(parser, visibility == NULL, &kind_range);
1854
+ }
1855
+
1856
+ rbs_range_t name_range;
1857
+ rbs_ast_symbol_t *name = NULL;
1858
+ CHECK_PARSE(parse_method_name(parser, &name_range, &name));
1859
+
1860
+ #define SELF_ID rbs_constant_pool_insert_constant(&parser->constant_pool, (const unsigned char *) "self?", strlen("self?"))
1861
+
1862
+ if (parser->next_token.type == pDOT && name->constant_id == SELF_ID) {
1863
+ rbs_parser_set_error(parser, parser->next_token, true, "`self?` method cannot have visibility");
1864
+ return false;
1865
+ } else {
1866
+ ADVANCE_ASSERT(parser, pCOLON);
1867
+ }
1868
+
1869
+ rbs_parser_push_typevar_table(parser, kind != INSTANCE_KIND);
1870
+
1871
+ rbs_node_list_t *overloads = rbs_node_list_new(ALLOCATOR());
1872
+ bool overloading = false;
1873
+ rbs_range_t overloading_range = NULL_RANGE;
1874
+ bool loop = true;
1875
+ while (loop) {
1876
+ rbs_node_list_t *annotations = rbs_node_list_new(ALLOCATOR());
1877
+ rbs_position_t overload_annot_pos = NullPosition;
1878
+
1879
+ rbs_range_t overload_range;
1880
+ overload_range.start = parser->current_token.range.start;
1881
+
1882
+ if (parser->next_token.type == tANNOTATION) {
1883
+ CHECK_PARSE(parse_annotations(parser, annotations, &overload_annot_pos));
1884
+ }
1885
+
1886
+ switch (parser->next_token.type) {
1887
+ case pLPAREN:
1888
+ case pARROW:
1889
+ case pLBRACE:
1890
+ case pLBRACKET:
1891
+ case pQUESTION:
1892
+ {
1893
+ rbs_method_type_t *method_type = NULL;
1894
+ CHECK_PARSE(rbs_parse_method_type(parser, &method_type));
1895
+
1896
+ overload_range.end = parser->current_token.range.end;
1897
+ rbs_location_t *loc = rbs_location_new(ALLOCATOR(), overload_range);
1898
+ rbs_node_t *overload = (rbs_node_t *) rbs_ast_members_method_definition_overload_new(ALLOCATOR(), loc, annotations, (rbs_node_t *) method_type);
1899
+ rbs_node_list_append(overloads, overload);
1900
+ member_range.end = parser->current_token.range.end;
1901
+ break;
1902
+ }
1903
+
1904
+ case pDOT3:
1905
+ if (accept_overload) {
1906
+ overloading = true;
1907
+ rbs_parser_advance(parser);
1908
+ loop = false;
1909
+ overloading_range = parser->current_token.range;
1910
+ member_range.end = overloading_range.end;
1911
+ break;
1912
+ } else {
1913
+ rbs_parser_set_error(parser, parser->next_token, true, "unexpected overloading method definition");
1914
+ return false;
1915
+ }
1916
+
1917
+ default:
1918
+ rbs_parser_set_error(parser, parser->next_token, true, "unexpected token for method type");
1919
+ return false;
1920
+ }
1921
+
1922
+ if (parser->next_token.type == pBAR) {
1923
+ rbs_parser_advance(parser);
1924
+ } else {
1925
+ loop = false;
1926
+ }
1927
+ }
1928
+
1929
+ CHECK_PARSE(parser_pop_typevar_table(parser));
1930
+
1931
+ rbs_keyword_t *k;
1932
+ switch (kind) {
1933
+ case INSTANCE_KIND: {
1934
+ k = rbs_keyword_new(ALLOCATOR(), rbs_location_current_token(parser), INTERN("instance"));
1935
+ break;
1936
+ }
1937
+ case SINGLETON_KIND: {
1938
+ k = rbs_keyword_new(ALLOCATOR(), rbs_location_current_token(parser), INTERN("singleton"));
1939
+ break;
1940
+ }
1941
+ case INSTANCE_SINGLETON_KIND: {
1942
+ k = rbs_keyword_new(ALLOCATOR(), rbs_location_current_token(parser), INTERN("singleton_instance"));
1943
+ break;
1944
+ }
1945
+ default:
1946
+ rbs_parser_set_error(parser, parser->current_token, false, "Unexpected error");
1947
+ return false;
1948
+ }
1949
+
1950
+ rbs_location_t *loc = rbs_location_new(ALLOCATOR(), member_range);
1951
+ rbs_loc_alloc_children(ALLOCATOR(), loc, 5);
1952
+ rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range);
1953
+ rbs_loc_add_required_child(loc, INTERN("name"), name_range);
1954
+ rbs_loc_add_optional_child(loc, INTERN("kind"), kind_range);
1955
+ rbs_loc_add_optional_child(loc, INTERN("overloading"), overloading_range);
1956
+ rbs_loc_add_optional_child(loc, INTERN("visibility"), visibility_range);
1957
+
1958
+ *method_definition = rbs_ast_members_method_definition_new(ALLOCATOR(), loc, name, k, overloads, annotations, comment, overloading, visibility);
1959
+ return true;
1960
+ }
1961
+
1962
+ /**
1963
+ * class_instance_name ::= {} <class_name>
1964
+ * | {} class_name `[` type args <`]`>
1965
+ *
1966
+ * @param kind
1967
+ * */
1968
+ NODISCARD
1969
+ static bool class_instance_name(rbs_parser_t *parser, TypeNameKind kind, rbs_node_list_t *args, rbs_range_t *name_range, rbs_range_t *args_range, rbs_type_name_t **name) {
1970
+ rbs_parser_advance(parser);
1971
+
1972
+ rbs_type_name_t *type_name = NULL;
1973
+ CHECK_PARSE(parse_type_name(parser, kind, name_range, &type_name));
1974
+ *name = type_name;
1975
+
1976
+ if (parser->next_token.type == pLBRACKET) {
1977
+ rbs_parser_advance(parser);
1978
+ args_range->start = parser->current_token.range.start;
1979
+ CHECK_PARSE(parse_type_list(parser, pRBRACKET, args));
1980
+ ADVANCE_ASSERT(parser, pRBRACKET);
1981
+ args_range->end = parser->current_token.range.end;
1982
+ } else {
1983
+ *args_range = NULL_RANGE;
1984
+ }
1985
+
1986
+ return true;
1987
+ }
1988
+
1989
+ /**
1990
+ * mixin_member ::= {kINCLUDE} <class_instance_name>
1991
+ * | {kPREPEND} <class_instance_name>
1992
+ * | {kEXTEND} <class_instance_name>
1993
+ *
1994
+ * @param from_interface `true` when the member is in an interface.
1995
+ * */
1996
+ NODISCARD
1997
+ static bool parse_mixin_member(rbs_parser_t *parser, bool from_interface, rbs_position_t comment_pos, rbs_node_list_t *annotations, rbs_node_t **mixin_member) {
1998
+ rbs_range_t member_range;
1999
+ member_range.start = parser->current_token.range.start;
2000
+ comment_pos = rbs_nonnull_pos_or(comment_pos, member_range.start);
2001
+
2002
+ enum RBSTokenType type = parser->current_token.type;
2003
+ rbs_range_t keyword_range = parser->current_token.range;
2004
+
2005
+ bool reset_typevar_scope;
2006
+ switch (type)
2007
+ {
2008
+ case kINCLUDE:
2009
+ reset_typevar_scope = false;
2010
+ break;
2011
+ case kEXTEND:
2012
+ reset_typevar_scope = true;
2013
+ break;
2014
+ case kPREPEND:
2015
+ reset_typevar_scope = false;
2016
+ break;
2017
+ default:
2018
+ rbs_parser_set_error(parser, parser->current_token, false, "Unexpected error");
2019
+ return false;
2020
+ }
2021
+
2022
+ if (from_interface) {
2023
+ if (parser->current_token.type != kINCLUDE) {
2024
+ rbs_parser_set_error(parser, parser->current_token, true, "unexpected mixin in interface declaration");
2025
+ return false;
2026
+ }
2027
+ }
2028
+
2029
+ rbs_parser_push_typevar_table(parser, reset_typevar_scope);
2030
+
2031
+ rbs_node_list_t *args = rbs_node_list_new(ALLOCATOR());
2032
+ rbs_range_t name_range;
2033
+ rbs_range_t args_range = NULL_RANGE;
2034
+ rbs_type_name_t *name = NULL;
2035
+ CHECK_PARSE(class_instance_name(
2036
+ parser,
2037
+ from_interface ? INTERFACE_NAME : (INTERFACE_NAME | CLASS_NAME),
2038
+ args, &name_range, &args_range, &name
2039
+ ));
2040
+
2041
+ CHECK_PARSE(parser_pop_typevar_table(parser));
2042
+
2043
+ member_range.end = parser->current_token.range.end;
2044
+
2045
+ rbs_location_t *loc = rbs_location_new(ALLOCATOR(), member_range);
2046
+ rbs_loc_alloc_children(ALLOCATOR(), loc, 3);
2047
+ rbs_loc_add_required_child(loc, INTERN("name"), name_range);
2048
+ rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range);
2049
+ rbs_loc_add_optional_child(loc, INTERN("args"), args_range);
2050
+
2051
+ rbs_ast_comment_t *comment = rbs_parser_get_comment(parser, comment_pos.line);
2052
+ switch (type)
2053
+ {
2054
+ case kINCLUDE:
2055
+ *mixin_member = (rbs_node_t *) rbs_ast_members_include_new(ALLOCATOR(), loc, name, args, annotations, comment);
2056
+ return true;
2057
+ case kEXTEND:
2058
+ *mixin_member = (rbs_node_t *) rbs_ast_members_extend_new(ALLOCATOR(), loc, name, args, annotations, comment);
2059
+ return true;
2060
+ case kPREPEND:
2061
+ *mixin_member = (rbs_node_t *) rbs_ast_members_prepend_new(ALLOCATOR(), loc, name, args, annotations, comment);
2062
+ return true;
2063
+ default:
2064
+ rbs_parser_set_error(parser, parser->current_token, false, "Unexpected error");
2065
+ return false;
2066
+ }
2067
+ }
2068
+
2069
+ /**
2070
+ * @code
2071
+ * alias_member ::= {kALIAS} method_name <method_name>
2072
+ * | {kALIAS} kSELF `.` method_name kSELF `.` <method_name>
2073
+ * @endcode
2074
+ *
2075
+ * @param[in] instance_only `true` to reject `self.` alias.
2076
+ * */
2077
+ NODISCARD
2078
+ static bool parse_alias_member(rbs_parser_t *parser, bool instance_only, rbs_position_t comment_pos, rbs_node_list_t *annotations, rbs_ast_members_alias_t **alias_member) {
2079
+ rbs_range_t member_range;
2080
+ member_range.start = parser->current_token.range.start;
2081
+ rbs_range_t keyword_range = parser->current_token.range;
2082
+
2083
+ comment_pos = rbs_nonnull_pos_or(comment_pos, member_range.start);
2084
+ rbs_ast_comment_t *comment = rbs_parser_get_comment(parser, comment_pos.line);
2085
+
2086
+ rbs_keyword_t *kind;
2087
+ rbs_ast_symbol_t *new_name, *old_name;
2088
+ rbs_range_t new_kind_range, old_kind_range, new_name_range, old_name_range;
2089
+
2090
+ if (!instance_only && parser->next_token.type == kSELF) {
2091
+ kind = rbs_keyword_new(ALLOCATOR(), rbs_location_current_token(parser), INTERN("singleton"));
2092
+
2093
+ new_kind_range.start = parser->next_token.range.start;
2094
+ new_kind_range.end = parser->next_token2.range.end;
2095
+ ADVANCE_ASSERT(parser, kSELF);
2096
+ ADVANCE_ASSERT(parser, pDOT);
2097
+ CHECK_PARSE(parse_method_name(parser, &new_name_range, &new_name));
2098
+
2099
+ old_kind_range.start = parser->next_token.range.start;
2100
+ old_kind_range.end = parser->next_token2.range.end;
2101
+ ADVANCE_ASSERT(parser, kSELF);
2102
+ ADVANCE_ASSERT(parser, pDOT);
2103
+ CHECK_PARSE(parse_method_name(parser, &old_name_range, &old_name));
2104
+ } else {
2105
+ kind = rbs_keyword_new(ALLOCATOR(), rbs_location_current_token(parser), INTERN("instance"));
2106
+ CHECK_PARSE(parse_method_name(parser, &new_name_range, &new_name));
2107
+ CHECK_PARSE(parse_method_name(parser, &old_name_range, &old_name));
2108
+ new_kind_range = NULL_RANGE;
2109
+ old_kind_range = NULL_RANGE;
2110
+ }
2111
+
2112
+ member_range.end = parser->current_token.range.end;
2113
+ rbs_location_t *loc = rbs_location_new(ALLOCATOR(), member_range);
2114
+ rbs_loc_alloc_children(ALLOCATOR(), loc, 5);
2115
+ rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range);
2116
+ rbs_loc_add_required_child(loc, INTERN("new_name"), new_name_range);
2117
+ rbs_loc_add_required_child(loc, INTERN("old_name"), old_name_range);
2118
+ rbs_loc_add_optional_child(loc, INTERN("new_kind"), new_kind_range);
2119
+ rbs_loc_add_optional_child(loc, INTERN("old_kind"), old_kind_range);
2120
+
2121
+ *alias_member = rbs_ast_members_alias_new(ALLOCATOR(), loc, new_name, old_name, kind, annotations, comment);
2122
+ return true;
2123
+ }
2124
+
2125
+ /*
2126
+ variable_member ::= {tAIDENT} `:` <type>
2127
+ | {kSELF} `.` tAIDENT `:` <type>
2128
+ | {tA2IDENT} `:` <type>
2129
+ */
2130
+ NODISCARD
2131
+ static bool parse_variable_member(rbs_parser_t *parser, rbs_position_t comment_pos, rbs_node_list_t *annotations, rbs_node_t **variable_member) {
2132
+ if (annotations->length > 0) {
2133
+ rbs_parser_set_error(parser, parser->current_token, true, "annotation cannot be given to variable members");
2134
+ return false;
2135
+ }
2136
+
2137
+ rbs_range_t member_range;
2138
+ member_range.start = parser->current_token.range.start;
2139
+ comment_pos = rbs_nonnull_pos_or(comment_pos, member_range.start);
2140
+ rbs_ast_comment_t *comment = rbs_parser_get_comment(parser, comment_pos.line);
2141
+
2142
+ switch (parser->current_token.type)
2143
+ {
2144
+ case tAIDENT:
2145
+ case kATRBS: {
2146
+ rbs_range_t name_range = parser->current_token.range;
2147
+ rbs_location_t *symbolLoc = rbs_location_new(ALLOCATOR(), name_range);
2148
+ rbs_ast_symbol_t *name = rbs_ast_symbol_new(ALLOCATOR(), symbolLoc, &parser->constant_pool, INTERN_TOKEN(parser, parser->current_token));
2149
+
2150
+ ADVANCE_ASSERT(parser, pCOLON);
2151
+ rbs_range_t colon_range = parser->current_token.range;
2152
+
2153
+ rbs_node_t *type;
2154
+ CHECK_PARSE(rbs_parse_type(parser, &type));
2155
+ member_range.end = parser->current_token.range.end;
2156
+
2157
+ rbs_location_t *loc = rbs_location_new(ALLOCATOR(), member_range);
2158
+ rbs_loc_alloc_children(ALLOCATOR(), loc, 3);
2159
+ rbs_loc_add_required_child(loc, INTERN("name"), name_range);
2160
+ rbs_loc_add_required_child(loc, INTERN("colon"), colon_range);
2161
+ rbs_loc_add_optional_child(loc, INTERN("kind"), NULL_RANGE);
2162
+
2163
+ *variable_member = (rbs_node_t *)rbs_ast_members_instance_variable_new(ALLOCATOR(), loc, name, type, comment);
2164
+ return true;
2165
+ }
2166
+ case tA2IDENT: {
2167
+ rbs_range_t name_range = parser->current_token.range;
2168
+ rbs_location_t *symbolLoc = rbs_location_new(ALLOCATOR(), name_range);
2169
+ rbs_ast_symbol_t *name = rbs_ast_symbol_new(ALLOCATOR(), symbolLoc, &parser->constant_pool, INTERN_TOKEN(parser, parser->current_token));
2170
+
2171
+ ADVANCE_ASSERT(parser, pCOLON);
2172
+ rbs_range_t colon_range = parser->current_token.range;
2173
+
2174
+ rbs_parser_push_typevar_table(parser, true);
2175
+
2176
+ rbs_node_t *type;
2177
+ CHECK_PARSE(rbs_parse_type(parser, &type));
2178
+
2179
+ CHECK_PARSE(parser_pop_typevar_table(parser));
2180
+
2181
+ member_range.end = parser->current_token.range.end;
2182
+
2183
+ rbs_location_t *loc = rbs_location_new(ALLOCATOR(), member_range);
2184
+ rbs_loc_alloc_children(ALLOCATOR(), loc, 3);
2185
+ rbs_loc_add_required_child(loc, INTERN("name"), name_range);
2186
+ rbs_loc_add_required_child(loc, INTERN("colon"), colon_range);
2187
+ rbs_loc_add_optional_child(loc, INTERN("kind"), NULL_RANGE);
2188
+
2189
+ *variable_member = (rbs_node_t *) rbs_ast_members_class_variable_new(ALLOCATOR(), loc, name, type, comment);
2190
+ return true;
2191
+ }
2192
+ case kSELF: {
2193
+ rbs_range_t kind_range = {
2194
+ .start = parser->current_token.range.start,
2195
+ .end = parser->next_token.range.end
2196
+ };
2197
+
2198
+ ADVANCE_ASSERT(parser, pDOT);
2199
+ if (parser->next_token.type == tAIDENT || parser->next_token.type == kATRBS) {
2200
+ rbs_parser_advance(parser);
2201
+ } else {
2202
+ rbs_parser_set_error(parser, parser->current_token, false, "Unexpected error");
2203
+ return false;
2204
+ }
2205
+
2206
+ rbs_range_t name_range = parser->current_token.range;
2207
+ rbs_location_t *symbolLoc = rbs_location_new(ALLOCATOR(), name_range);
2208
+ rbs_ast_symbol_t *name = rbs_ast_symbol_new(ALLOCATOR(), symbolLoc, &parser->constant_pool, INTERN_TOKEN(parser, parser->current_token));
2209
+
2210
+ ADVANCE_ASSERT(parser, pCOLON);
2211
+ rbs_range_t colon_range = parser->current_token.range;
2212
+
2213
+ rbs_parser_push_typevar_table(parser, true);
2214
+
2215
+ rbs_node_t *type;
2216
+ CHECK_PARSE(rbs_parse_type(parser, &type));
2217
+
2218
+ CHECK_PARSE(parser_pop_typevar_table(parser));
2219
+
2220
+ member_range.end = parser->current_token.range.end;
2221
+
2222
+ rbs_location_t *loc = rbs_location_new(ALLOCATOR(), member_range);
2223
+ rbs_loc_alloc_children(ALLOCATOR(), loc, 3);
2224
+ rbs_loc_add_required_child(loc, INTERN("name"), name_range);
2225
+ rbs_loc_add_required_child(loc, INTERN("colon"), colon_range);
2226
+ rbs_loc_add_optional_child(loc, INTERN("kind"), kind_range);
2227
+
2228
+ *variable_member = (rbs_node_t *)rbs_ast_members_class_instance_variable_new(ALLOCATOR(), loc, name, type, comment);
2229
+ return true;
2230
+ }
2231
+ default:
2232
+ rbs_parser_set_error(parser, parser->current_token, false, "Unexpected error");
2233
+ return false;
2234
+ }
2235
+ }
2236
+
2237
+ /*
2238
+ visibility_member ::= {<`public`>}
2239
+ | {<`private`>}
2240
+ */
2241
+ NODISCARD
2242
+ static bool parse_visibility_member(rbs_parser_t *parser, rbs_node_list_t *annotations, rbs_node_t **visibility_member) {
2243
+ if (annotations->length > 0) {
2244
+ rbs_parser_set_error(parser, parser->current_token, true, "annotation cannot be given to visibility members");
2245
+ return false;
2246
+ }
2247
+
2248
+ rbs_location_t *location = rbs_location_current_token(parser);
2249
+
2250
+ switch (parser->current_token.type)
2251
+ {
2252
+ case kPUBLIC: {
2253
+ *visibility_member = (rbs_node_t *) rbs_ast_members_public_new(ALLOCATOR(), location);
2254
+ return true;
2255
+ }
2256
+ case kPRIVATE: {
2257
+ *visibility_member = (rbs_node_t *) rbs_ast_members_private_new(ALLOCATOR(), location);
2258
+ return true;
2259
+ }
2260
+ default:
2261
+ rbs_parser_set_error(parser, parser->current_token, false, "Unexpected error");
2262
+ return false;
2263
+ }
2264
+ }
2265
+
2266
+ /*
2267
+ attribute_member ::= {attr_keyword} attr_name attr_var `:` <type>
2268
+ | {visibility} attr_keyword attr_name attr_var `:` <type>
2269
+ | {attr_keyword} `self` `.` attr_name attr_var `:` <type>
2270
+ | {visibility} attr_keyword `self` `.` attr_name attr_var `:` <type>
2271
+
2272
+ attr_keyword ::= `attr_reader` | `attr_writer` | `attr_accessor`
2273
+
2274
+ visibility ::= `public` | `private`
2275
+
2276
+ attr_var ::= # empty
2277
+ | `(` tAIDENT `)` # Ivar name
2278
+ | `(` `)` # No variable
2279
+ */
2280
+ NODISCARD
2281
+ static bool parse_attribute_member(rbs_parser_t *parser, rbs_position_t comment_pos, rbs_node_list_t *annotations, rbs_node_t **attribute_member) {
2282
+ rbs_range_t member_range;
2283
+
2284
+ member_range.start = parser->current_token.range.start;
2285
+ comment_pos = rbs_nonnull_pos_or(comment_pos, member_range.start);
2286
+ rbs_ast_comment_t *comment = rbs_parser_get_comment(parser, comment_pos.line);
2287
+
2288
+ rbs_range_t visibility_range;
2289
+ rbs_keyword_t *visibility;
2290
+ switch (parser->current_token.type)
2291
+ {
2292
+ case kPRIVATE: {
2293
+ visibility_range = parser->current_token.range;
2294
+ visibility = rbs_keyword_new(ALLOCATOR(), rbs_location_new(ALLOCATOR(), visibility_range), INTERN("private"));
2295
+ rbs_parser_advance(parser);
2296
+ break;
2297
+ }
2298
+ case kPUBLIC: {
2299
+ visibility_range = parser->current_token.range;
2300
+ visibility = rbs_keyword_new(ALLOCATOR(), rbs_location_new(ALLOCATOR(), visibility_range), INTERN("public"));
2301
+ rbs_parser_advance(parser);
2302
+ break;
2303
+ }
2304
+ default:
2305
+ visibility = NULL;
2306
+ visibility_range = NULL_RANGE;
2307
+ break;
2308
+ }
2309
+
2310
+ enum RBSTokenType attr_type = parser->current_token.type;
2311
+ rbs_range_t keyword_range = parser->current_token.range;
2312
+
2313
+ rbs_range_t kind_range;
2314
+ InstanceSingletonKind is_kind = parse_instance_singleton_kind(parser, false, &kind_range);
2315
+
2316
+ rbs_keyword_t *kind = rbs_keyword_new(
2317
+ ALLOCATOR(),
2318
+ rbs_location_new(ALLOCATOR(), keyword_range),
2319
+ INTERN(((is_kind == INSTANCE_KIND) ? "instance" : "singleton"))
2320
+ );
2321
+
2322
+ rbs_range_t name_range;
2323
+ rbs_ast_symbol_t *attr_name;
2324
+ CHECK_PARSE(parse_method_name(parser, &name_range, &attr_name));
2325
+
2326
+ rbs_node_t *ivar_name; // rbs_ast_symbol_t, NULL or rbs_ast_bool_new(ALLOCATOR(), false)
2327
+ rbs_range_t ivar_range, ivar_name_range;
2328
+ if (parser->next_token.type == pLPAREN) {
2329
+ ADVANCE_ASSERT(parser, pLPAREN);
2330
+ ivar_range.start = parser->current_token.range.start;
2331
+
2332
+ if (parser_advance_if(parser, tAIDENT) || parser_advance_if(parser, kATRBS)) {
2333
+ rbs_location_t *symbolLoc = rbs_location_current_token(parser);
2334
+ ivar_name = (rbs_node_t *) rbs_ast_symbol_new(ALLOCATOR(), symbolLoc, &parser->constant_pool, INTERN_TOKEN(parser, parser->current_token));
2335
+ ivar_name_range = parser->current_token.range;
2336
+ } else {
2337
+ rbs_range_t false_range = {
2338
+ .start = parser->current_token.range.start,
2339
+ .end = parser->current_token.range.end
2340
+ };
2341
+ ivar_name = (rbs_node_t *) rbs_ast_bool_new(ALLOCATOR(), rbs_location_new(ALLOCATOR(), false_range), false);
2342
+ ivar_name_range = NULL_RANGE;
2343
+ }
2344
+
2345
+ ADVANCE_ASSERT(parser, pRPAREN);
2346
+ ivar_range.end = parser->current_token.range.end;
2347
+ } else {
2348
+ ivar_range = NULL_RANGE;
2349
+ ivar_name = NULL;
2350
+ ivar_name_range = NULL_RANGE;
2351
+ }
2352
+
2353
+ ADVANCE_ASSERT(parser, pCOLON);
2354
+ rbs_range_t colon_range = parser->current_token.range;
2355
+
2356
+ rbs_parser_push_typevar_table(parser, is_kind == SINGLETON_KIND);
2357
+
2358
+ rbs_node_t *type;
2359
+ CHECK_PARSE(rbs_parse_type(parser, &type));
2360
+
2361
+ CHECK_PARSE(parser_pop_typevar_table(parser));
2362
+
2363
+ member_range.end = parser->current_token.range.end;
2364
+
2365
+ rbs_location_t *loc = rbs_location_new(ALLOCATOR(), member_range);
2366
+ rbs_loc_alloc_children(ALLOCATOR(), loc, 7);
2367
+ rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range);
2368
+ rbs_loc_add_required_child(loc, INTERN("name"), name_range);
2369
+ rbs_loc_add_required_child(loc, INTERN("colon"), colon_range);
2370
+ rbs_loc_add_optional_child(loc, INTERN("kind"), kind_range);
2371
+ rbs_loc_add_optional_child(loc, INTERN("ivar"), ivar_range);
2372
+ rbs_loc_add_optional_child(loc, INTERN("ivar_name"), ivar_name_range);
2373
+ rbs_loc_add_optional_child(loc, INTERN("visibility"), visibility_range);
2374
+
2375
+ switch (attr_type)
2376
+ {
2377
+ case kATTRREADER:
2378
+ *attribute_member = (rbs_node_t *) rbs_ast_members_attr_reader_new(ALLOCATOR(), loc, attr_name, type, ivar_name, kind, annotations, comment, visibility);
2379
+ return true;
2380
+ case kATTRWRITER:
2381
+ *attribute_member = (rbs_node_t *) rbs_ast_members_attr_writer_new(ALLOCATOR(), loc, attr_name, type, ivar_name, kind, annotations, comment, visibility);
2382
+ return true;
2383
+ case kATTRACCESSOR:
2384
+ *attribute_member = (rbs_node_t *) rbs_ast_members_attr_accessor_new(ALLOCATOR(), loc, attr_name, type, ivar_name, kind, annotations, comment, visibility);
2385
+ return true;
2386
+ default:
2387
+ rbs_parser_set_error(parser, parser->current_token, false, "Unexpected error");
2388
+ return false;
2389
+ }
2390
+ }
2391
+
2392
+ /*
2393
+ interface_members ::= {} ...<interface_member> kEND
2394
+
2395
+ interface_member ::= def_member (instance method only && no overloading)
2396
+ | mixin_member (interface only)
2397
+ | alias_member (instance only)
2398
+ */
2399
+ NODISCARD
2400
+ static bool parse_interface_members(rbs_parser_t *parser, rbs_node_list_t **members) {
2401
+ *members = rbs_node_list_new(ALLOCATOR());
2402
+
2403
+ while (parser->next_token.type != kEND) {
2404
+ rbs_node_list_t *annotations = rbs_node_list_new(ALLOCATOR());
2405
+ rbs_position_t annot_pos = NullPosition;
2406
+
2407
+ CHECK_PARSE(parse_annotations(parser, annotations, &annot_pos));
2408
+ rbs_parser_advance(parser);
2409
+
2410
+ rbs_node_t *member;
2411
+ switch (parser->current_token.type) {
2412
+ case kDEF: {
2413
+ rbs_ast_members_method_definition_t *method_definition = NULL;
2414
+ CHECK_PARSE(parse_member_def(parser, true, true, annot_pos, annotations, &method_definition));
2415
+ member = (rbs_node_t *) method_definition;
2416
+ break;
2417
+ }
2418
+
2419
+ case kINCLUDE:
2420
+ case kEXTEND:
2421
+ case kPREPEND: {
2422
+ CHECK_PARSE(parse_mixin_member(parser, true, annot_pos, annotations, &member));
2423
+ break;
2424
+ }
2425
+
2426
+ case kALIAS: {
2427
+ rbs_ast_members_alias_t *alias_member = NULL;
2428
+ CHECK_PARSE(parse_alias_member(parser, true, annot_pos, annotations, &alias_member));
2429
+ member = (rbs_node_t *) alias_member;
2430
+ break;
2431
+ }
2432
+
2433
+ default:
2434
+ rbs_parser_set_error(parser, parser->current_token, true, "unexpected token for interface declaration member");
2435
+ return false;
2436
+ }
2437
+
2438
+ rbs_node_list_append(*members, member);
2439
+ }
2440
+
2441
+ return true;
2442
+ }
2443
+
2444
+ /*
2445
+ interface_decl ::= {`interface`} interface_name module_type_params interface_members <kEND>
2446
+ */
2447
+ NODISCARD
2448
+ static bool parse_interface_decl(rbs_parser_t *parser, rbs_position_t comment_pos, rbs_node_list_t *annotations, rbs_ast_declarations_interface_t **interface_decl) {
2449
+ rbs_parser_push_typevar_table(parser, true);
2450
+
2451
+ rbs_range_t member_range;
2452
+ member_range.start = parser->current_token.range.start;
2453
+ comment_pos = rbs_nonnull_pos_or(comment_pos, member_range.start);
2454
+
2455
+ rbs_range_t keyword_range = parser->current_token.range;
2456
+
2457
+ rbs_parser_advance(parser);
2458
+
2459
+ rbs_range_t name_range;
2460
+ rbs_type_name_t *name = NULL;
2461
+ CHECK_PARSE(parse_type_name(parser, INTERFACE_NAME, &name_range, &name));
2462
+
2463
+ rbs_range_t type_params_range;
2464
+ rbs_node_list_t *type_params;
2465
+ CHECK_PARSE(parse_type_params(parser, &type_params_range, true, &type_params));
2466
+
2467
+ rbs_node_list_t *members = NULL;
2468
+ CHECK_PARSE(parse_interface_members(parser, &members));
2469
+
2470
+ ADVANCE_ASSERT(parser, kEND);
2471
+ rbs_range_t end_range = parser->current_token.range;
2472
+ member_range.end = end_range.end;
2473
+
2474
+ CHECK_PARSE(parser_pop_typevar_table(parser));
2475
+
2476
+ rbs_location_t *loc = rbs_location_new(ALLOCATOR(), member_range);
2477
+ rbs_loc_alloc_children(ALLOCATOR(), loc, 4);
2478
+ rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range);
2479
+ rbs_loc_add_required_child(loc, INTERN("name"), name_range);
2480
+ rbs_loc_add_required_child(loc, INTERN("end"), end_range);
2481
+ rbs_loc_add_optional_child(loc, INTERN("type_params"), type_params_range);
2482
+
2483
+ rbs_ast_comment_t *comment = rbs_parser_get_comment(parser, comment_pos.line);
2484
+
2485
+ *interface_decl = rbs_ast_declarations_interface_new(ALLOCATOR(), loc, name, type_params, members, annotations, comment);
2486
+ return true;
2487
+ }
2488
+
2489
+ /*
2490
+ module_self_types ::= {`:`} module_self_type `,` ... `,` <module_self_type>
2491
+
2492
+ module_self_type ::= <module_name>
2493
+ | module_name `[` type_list <`]`>
2494
+ */
2495
+ NODISCARD
2496
+ static bool parse_module_self_types(rbs_parser_t *parser, rbs_node_list_t *array) {
2497
+ while (true) {
2498
+ rbs_parser_advance(parser);
2499
+
2500
+ rbs_range_t self_range;
2501
+ self_range.start = parser->current_token.range.start;
2502
+
2503
+ rbs_range_t name_range;
2504
+ rbs_type_name_t *module_name = NULL;
2505
+ CHECK_PARSE(parse_type_name(parser, CLASS_NAME | INTERFACE_NAME, &name_range, &module_name));
2506
+ self_range.end = name_range.end;
2507
+
2508
+ rbs_node_list_t *args = rbs_node_list_new(ALLOCATOR());
2509
+ rbs_range_t args_range = NULL_RANGE;
2510
+ if (parser->next_token.type == pLBRACKET) {
2511
+ rbs_parser_advance(parser);
2512
+ args_range.start = parser->current_token.range.start;
2513
+ CHECK_PARSE(parse_type_list(parser, pRBRACKET, args));
2514
+ rbs_parser_advance(parser);
2515
+ self_range.end = args_range.end = parser->current_token.range.end;
2516
+ }
2517
+
2518
+ rbs_location_t *loc = rbs_location_new(ALLOCATOR(), self_range);
2519
+ rbs_loc_alloc_children(ALLOCATOR(), loc, 2);
2520
+ rbs_loc_add_required_child(loc, INTERN("name"), name_range);
2521
+ rbs_loc_add_optional_child(loc, INTERN("args"), args_range);
2522
+
2523
+ rbs_ast_declarations_module_self_t *self_type = rbs_ast_declarations_module_self_new(ALLOCATOR(), loc, module_name, args);
2524
+ rbs_node_list_append(array, (rbs_node_t *)self_type);
2525
+
2526
+ if (parser->next_token.type == pCOMMA) {
2527
+ rbs_parser_advance(parser);
2528
+ } else {
2529
+ break;
2530
+ }
2531
+ }
2532
+
2533
+ return true;
2534
+ }
2535
+
2536
+ NODISCARD
2537
+ static bool parse_nested_decl(rbs_parser_t *parser, const char *nested_in, rbs_position_t annot_pos, rbs_node_list_t *annotations, rbs_node_t **decl);
2538
+
2539
+ /*
2540
+ module_members ::= {} ...<module_member> kEND
2541
+
2542
+ module_member ::= def_member
2543
+ | variable_member
2544
+ | mixin_member
2545
+ | alias_member
2546
+ | attribute_member
2547
+ | `public`
2548
+ | `private`
2549
+ */
2550
+ NODISCARD
2551
+ static bool parse_module_members(rbs_parser_t *parser, rbs_node_list_t **members) {
2552
+ *members = rbs_node_list_new(ALLOCATOR());
2553
+
2554
+ while (parser->next_token.type != kEND) {
2555
+ rbs_node_list_t *annotations = rbs_node_list_new(ALLOCATOR());
2556
+ rbs_position_t annot_pos;
2557
+ CHECK_PARSE(parse_annotations(parser, annotations, &annot_pos));
2558
+
2559
+ rbs_parser_advance(parser);
2560
+
2561
+ rbs_node_t *member;
2562
+ switch (parser->current_token.type)
2563
+ {
2564
+ case kDEF: {
2565
+ rbs_ast_members_method_definition_t *method_definition;
2566
+ CHECK_PARSE(parse_member_def(parser, false, true, annot_pos, annotations, &method_definition));
2567
+ member = (rbs_node_t *) method_definition;
2568
+ break;
2569
+ }
2570
+
2571
+ case kINCLUDE:
2572
+ case kEXTEND:
2573
+ case kPREPEND: {
2574
+ CHECK_PARSE(parse_mixin_member(parser, false, annot_pos, annotations, &member));
2575
+ break;
2576
+ }
2577
+ case kALIAS: {
2578
+ rbs_ast_members_alias_t *alias_member = NULL;
2579
+ CHECK_PARSE(parse_alias_member(parser, false, annot_pos, annotations, &alias_member));
2580
+ member = (rbs_node_t *) alias_member;
2581
+ break;
2582
+ }
2583
+ case tAIDENT:
2584
+ case tA2IDENT:
2585
+ case kATRBS:
2586
+ case kSELF: {
2587
+ CHECK_PARSE(parse_variable_member(parser, annot_pos, annotations, &member));
2588
+ break;
2589
+ }
2590
+
2591
+ case kATTRREADER:
2592
+ case kATTRWRITER:
2593
+ case kATTRACCESSOR: {
2594
+ CHECK_PARSE(parse_attribute_member(parser, annot_pos, annotations, &member));
2595
+ break;
2596
+ }
2597
+
2598
+ case kPUBLIC:
2599
+ case kPRIVATE:
2600
+ if (parser->next_token.range.start.line == parser->current_token.range.start.line) {
2601
+ switch (parser->next_token.type)
2602
+ {
2603
+ case kDEF: {
2604
+ rbs_ast_members_method_definition_t *method_definition = NULL;
2605
+ CHECK_PARSE(parse_member_def(parser, false, true, annot_pos, annotations, &method_definition));
2606
+ member = (rbs_node_t *) method_definition;
2607
+ break;
2608
+ }
2609
+ case kATTRREADER:
2610
+ case kATTRWRITER:
2611
+ case kATTRACCESSOR: {
2612
+ CHECK_PARSE(parse_attribute_member(parser, annot_pos, annotations, &member));
2613
+ break;
2614
+ }
2615
+ default:
2616
+ rbs_parser_set_error(parser, parser->next_token, true, "method or attribute definition is expected after visibility modifier");
2617
+ return false;
2618
+ }
2619
+ } else {
2620
+ CHECK_PARSE(parse_visibility_member(parser, annotations, &member));
2621
+ }
2622
+ break;
2623
+
2624
+ default:
2625
+ CHECK_PARSE(parse_nested_decl(parser, "module", annot_pos, annotations, &member));
2626
+ break;
2627
+ }
2628
+
2629
+ rbs_node_list_append(*members, member);
2630
+ }
2631
+
2632
+ return true;
2633
+ }
2634
+
2635
+ /*
2636
+ module_decl ::= {module_name} module_type_params module_members <kEND>
2637
+ | {module_name} module_name module_type_params `:` module_self_types module_members <kEND>
2638
+ */
2639
+ NODISCARD
2640
+ static bool parse_module_decl0(rbs_parser_t *parser, rbs_range_t keyword_range, rbs_type_name_t *module_name, rbs_range_t name_range, rbs_ast_comment_t *comment, rbs_node_list_t *annotations, rbs_ast_declarations_module_t **module_decl) {
2641
+ rbs_parser_push_typevar_table(parser, true);
2642
+
2643
+ rbs_range_t decl_range;
2644
+ decl_range.start = keyword_range.start;
2645
+
2646
+ rbs_range_t type_params_range;
2647
+ rbs_node_list_t *type_params;
2648
+ CHECK_PARSE(parse_type_params(parser, &type_params_range, true, &type_params));
2649
+
2650
+ rbs_node_list_t *self_types = rbs_node_list_new(ALLOCATOR());
2651
+ rbs_range_t colon_range;
2652
+ rbs_range_t self_types_range;
2653
+ if (parser->next_token.type == pCOLON) {
2654
+ rbs_parser_advance(parser);
2655
+ colon_range = parser->current_token.range;
2656
+ self_types_range.start = parser->next_token.range.start;
2657
+ CHECK_PARSE(parse_module_self_types(parser, self_types));
2658
+ self_types_range.end = parser->current_token.range.end;
2659
+ } else {
2660
+ colon_range = NULL_RANGE;
2661
+ self_types_range = NULL_RANGE;
2662
+ }
2663
+
2664
+ rbs_node_list_t *members = NULL;
2665
+ CHECK_PARSE(parse_module_members(parser, &members));
2666
+
2667
+ ADVANCE_ASSERT(parser, kEND);
2668
+ rbs_range_t end_range = parser->current_token.range;
2669
+ decl_range.end = parser->current_token.range.end;
2670
+
2671
+ rbs_location_t *loc = rbs_location_new(ALLOCATOR(), decl_range);
2672
+ rbs_loc_alloc_children(ALLOCATOR(), loc, 6);
2673
+ rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range);
2674
+ rbs_loc_add_required_child(loc, INTERN("name"), name_range);
2675
+ rbs_loc_add_required_child(loc, INTERN("end"), end_range);
2676
+ rbs_loc_add_optional_child(loc, INTERN("type_params"), type_params_range);
2677
+ rbs_loc_add_optional_child(loc, INTERN("colon"), colon_range);
2678
+ rbs_loc_add_optional_child(loc, INTERN("self_types"), self_types_range);
2679
+
2680
+ CHECK_PARSE(parser_pop_typevar_table(parser));
2681
+
2682
+ *module_decl = rbs_ast_declarations_module_new(ALLOCATOR(), loc, module_name, type_params, self_types, members, annotations, comment);
2683
+ return true;
2684
+ }
2685
+
2686
+ /*
2687
+ module_decl ::= {`module`} module_name `=` old_module_name <kEND>
2688
+ | {`module`} module_name module_decl0 <kEND>
2689
+
2690
+ */
2691
+ NODISCARD
2692
+ static bool parse_module_decl(rbs_parser_t *parser, rbs_position_t comment_pos, rbs_node_list_t *annotations, rbs_node_t **module_decl) {
2693
+ rbs_range_t keyword_range = parser->current_token.range;
2694
+
2695
+ comment_pos = rbs_nonnull_pos_or(comment_pos, parser->current_token.range.start);
2696
+ rbs_ast_comment_t *comment = rbs_parser_get_comment(parser, comment_pos.line);
2697
+
2698
+ rbs_parser_advance(parser);
2699
+
2700
+ rbs_range_t module_name_range;
2701
+ rbs_type_name_t *module_name;
2702
+ CHECK_PARSE(parse_type_name(parser, CLASS_NAME, &module_name_range, &module_name));
2703
+
2704
+ if (parser->next_token.type == pEQ) {
2705
+ rbs_range_t eq_range = parser->next_token.range;
2706
+ rbs_parser_advance(parser);
2707
+ rbs_parser_advance(parser);
2708
+
2709
+ rbs_range_t old_name_range;
2710
+ rbs_type_name_t *old_name = NULL;
2711
+ CHECK_PARSE(parse_type_name(parser, CLASS_NAME, &old_name_range, &old_name));
2712
+
2713
+ rbs_range_t decl_range = {
2714
+ .start = keyword_range.start,
2715
+ .end = old_name_range.end
2716
+ };
2717
+
2718
+ rbs_location_t *loc = rbs_location_new(ALLOCATOR(), decl_range);
2719
+ rbs_loc_alloc_children(ALLOCATOR(), loc, 4);
2720
+ rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range);
2721
+ rbs_loc_add_required_child(loc, INTERN("new_name"), module_name_range);
2722
+ rbs_loc_add_required_child(loc, INTERN("eq"), eq_range);
2723
+ rbs_loc_add_optional_child(loc, INTERN("old_name"), old_name_range);
2724
+
2725
+ *module_decl = (rbs_node_t *) rbs_ast_declarations_module_alias_new(ALLOCATOR(), loc, module_name, old_name, comment, annotations);
2726
+ } else {
2727
+ rbs_ast_declarations_module_t *module_decl0 = NULL;
2728
+ CHECK_PARSE(parse_module_decl0(parser, keyword_range, module_name, module_name_range, comment, annotations, &module_decl0));
2729
+ *module_decl = (rbs_node_t *) module_decl0;
2730
+ }
2731
+
2732
+ return true;
2733
+ }
2734
+
2735
+ /*
2736
+ class_decl_super ::= {} `<` <class_instance_name>
2737
+ | {<>}
2738
+ */
2739
+ NODISCARD
2740
+ static bool parse_class_decl_super(rbs_parser_t *parser, rbs_range_t *lt_range, rbs_ast_declarations_class_super_t **super) {
2741
+ if (parser_advance_if(parser, pLT)) {
2742
+ *lt_range = parser->current_token.range;
2743
+
2744
+ rbs_range_t super_range;
2745
+ super_range.start = parser->next_token.range.start;
2746
+
2747
+ rbs_node_list_t *args = rbs_node_list_new(ALLOCATOR());
2748
+ rbs_type_name_t *name = NULL;
2749
+ rbs_range_t name_range, args_range;
2750
+ CHECK_PARSE(class_instance_name(parser, CLASS_NAME, args, &name_range, &args_range, &name));
2751
+
2752
+ super_range.end = parser->current_token.range.end;
2753
+
2754
+ rbs_location_t *loc = rbs_location_new(ALLOCATOR(), super_range);
2755
+ rbs_loc_alloc_children(ALLOCATOR(), loc, 2);
2756
+ rbs_loc_add_required_child(loc, INTERN("name"), name_range);
2757
+ rbs_loc_add_optional_child(loc, INTERN("args"), args_range);
2758
+
2759
+
2760
+ *super = rbs_ast_declarations_class_super_new(ALLOCATOR(), loc, name, args);
2761
+ } else {
2762
+ *lt_range = NULL_RANGE;
2763
+ }
2764
+
2765
+ return true;
2766
+ }
2767
+
2768
+ /*
2769
+ class_decl ::= {class_name} type_params class_decl_super class_members <`end`>
2770
+ */
2771
+ NODISCARD
2772
+ static bool parse_class_decl0(rbs_parser_t *parser, rbs_range_t keyword_range, rbs_type_name_t *name, rbs_range_t name_range, rbs_ast_comment_t *comment, rbs_node_list_t *annotations, rbs_ast_declarations_class_t **class_decl) {
2773
+ rbs_parser_push_typevar_table(parser, true);
2774
+
2775
+ rbs_range_t decl_range;
2776
+ decl_range.start = keyword_range.start;
2777
+
2778
+ rbs_range_t type_params_range;
2779
+ rbs_node_list_t *type_params;
2780
+ CHECK_PARSE(parse_type_params(parser, &type_params_range, true, &type_params));
2781
+
2782
+ rbs_range_t lt_range;
2783
+ rbs_ast_declarations_class_super_t *super = NULL;
2784
+ CHECK_PARSE(parse_class_decl_super(parser, &lt_range, &super));
2785
+
2786
+ rbs_node_list_t *members = NULL;
2787
+ CHECK_PARSE(parse_module_members(parser, &members));
2788
+
2789
+ ADVANCE_ASSERT(parser, kEND);
2790
+
2791
+ rbs_range_t end_range = parser->current_token.range;
2792
+
2793
+ decl_range.end = end_range.end;
2794
+
2795
+ CHECK_PARSE(parser_pop_typevar_table(parser));
2796
+
2797
+ rbs_location_t *loc = rbs_location_new(ALLOCATOR(), decl_range);
2798
+ rbs_loc_alloc_children(ALLOCATOR(), loc, 5);
2799
+ rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range);
2800
+ rbs_loc_add_required_child(loc, INTERN("name"), name_range);
2801
+ rbs_loc_add_required_child(loc, INTERN("end"), end_range);
2802
+ rbs_loc_add_optional_child(loc, INTERN("type_params"), type_params_range);
2803
+ rbs_loc_add_optional_child(loc, INTERN("lt"), lt_range);
2804
+
2805
+ *class_decl = rbs_ast_declarations_class_new(ALLOCATOR(), loc, name, type_params, super, members, annotations, comment);
2806
+ return true;
2807
+ }
2808
+
2809
+ /*
2810
+ class_decl ::= {`class`} class_name `=` <class_name>
2811
+ | {`class`} class_name <class_decl0>
2812
+ */
2813
+ NODISCARD
2814
+ static bool parse_class_decl(rbs_parser_t *parser, rbs_position_t comment_pos, rbs_node_list_t *annotations, rbs_node_t **class_decl) {
2815
+ rbs_range_t keyword_range = parser->current_token.range;
2816
+
2817
+ comment_pos = rbs_nonnull_pos_or(comment_pos, parser->current_token.range.start);
2818
+ rbs_ast_comment_t *comment = rbs_parser_get_comment(parser, comment_pos.line);
2819
+
2820
+ rbs_parser_advance(parser);
2821
+ rbs_range_t class_name_range;
2822
+ rbs_type_name_t *class_name = NULL;
2823
+ CHECK_PARSE(parse_type_name(parser, CLASS_NAME, &class_name_range, &class_name));
2824
+
2825
+ if (parser->next_token.type == pEQ) {
2826
+ rbs_range_t eq_range = parser->next_token.range;
2827
+ rbs_parser_advance(parser);
2828
+ rbs_parser_advance(parser);
2829
+
2830
+ rbs_range_t old_name_range;
2831
+ rbs_type_name_t *old_name = NULL;
2832
+ CHECK_PARSE(parse_type_name(parser, CLASS_NAME, &old_name_range, &old_name));
2833
+
2834
+ rbs_range_t decl_range = {
2835
+ .start = keyword_range.start,
2836
+ .end = old_name_range.end,
2837
+ };
2838
+
2839
+ rbs_location_t *loc = rbs_location_new(ALLOCATOR(), decl_range);
2840
+ rbs_loc_alloc_children(ALLOCATOR(), loc, 4);
2841
+ rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range);
2842
+ rbs_loc_add_required_child(loc, INTERN("new_name"), class_name_range);
2843
+ rbs_loc_add_required_child(loc, INTERN("eq"), eq_range);
2844
+ rbs_loc_add_optional_child(loc, INTERN("old_name"), old_name_range);
2845
+
2846
+ *class_decl = (rbs_node_t *) rbs_ast_declarations_class_alias_new(ALLOCATOR(), loc, class_name, old_name, comment, annotations);
2847
+ } else {
2848
+ rbs_ast_declarations_class_t *class_decl0 = NULL;
2849
+ CHECK_PARSE(parse_class_decl0(parser, keyword_range, class_name, class_name_range, comment, annotations, &class_decl0));
2850
+ *class_decl = (rbs_node_t *) class_decl0;
2851
+ }
2852
+
2853
+ return true;
2854
+ }
2855
+
2856
+ /*
2857
+ nested_decl ::= {<const_decl>}
2858
+ | {<class_decl>}
2859
+ | {<interface_decl>}
2860
+ | {<module_decl>}
2861
+ | {<class_decl>}
2862
+ */
2863
+ NODISCARD
2864
+ static bool parse_nested_decl(rbs_parser_t *parser, const char *nested_in, rbs_position_t annot_pos, rbs_node_list_t *annotations, rbs_node_t **decl) {
2865
+ rbs_parser_push_typevar_table(parser, true);
2866
+
2867
+ switch (parser->current_token.type) {
2868
+ case tUIDENT:
2869
+ case pCOLON2: {
2870
+ rbs_ast_declarations_constant_t *constant = NULL;
2871
+ CHECK_PARSE(parse_const_decl(parser, annotations, &constant));
2872
+ *decl = (rbs_node_t *) constant;
2873
+ break;
2874
+ }
2875
+ case tGIDENT: {
2876
+ rbs_ast_declarations_global_t *global = NULL;
2877
+ CHECK_PARSE(parse_global_decl(parser, annotations, &global));
2878
+ *decl = (rbs_node_t *) global;
2879
+ break;
2880
+ }
2881
+ case kTYPE: {
2882
+ rbs_ast_declarations_type_alias_t *typealias = NULL;
2883
+ CHECK_PARSE(parse_type_decl(parser, annot_pos, annotations, &typealias));
2884
+ *decl = (rbs_node_t *) typealias;
2885
+ break;
2886
+ }
2887
+ case kINTERFACE: {
2888
+ rbs_ast_declarations_interface_t *interface_decl = NULL;
2889
+ CHECK_PARSE(parse_interface_decl(parser, annot_pos, annotations, &interface_decl));
2890
+ *decl = (rbs_node_t *) interface_decl;
2891
+ break;
2892
+ }
2893
+ case kMODULE: {
2894
+ rbs_node_t *module_decl = NULL;
2895
+ CHECK_PARSE(parse_module_decl(parser, annot_pos, annotations, &module_decl));
2896
+ *decl = module_decl;
2897
+ break;
2898
+ }
2899
+ case kCLASS: {
2900
+ rbs_node_t *class_decl = NULL;
2901
+ CHECK_PARSE(parse_class_decl(parser, annot_pos, annotations, &class_decl));
2902
+ *decl = class_decl;
2903
+ break;
2904
+ }
2905
+ default:
2906
+ rbs_parser_set_error(parser, parser->current_token, true, "unexpected token for class/module declaration member");
2907
+ return false;
2908
+ }
2909
+
2910
+ CHECK_PARSE(parser_pop_typevar_table(parser));
2911
+
2912
+ return true;
2913
+ }
2914
+
2915
+ NODISCARD
2916
+ static bool parse_decl(rbs_parser_t *parser, rbs_node_t **decl) {
2917
+ rbs_node_list_t *annotations = rbs_node_list_new(ALLOCATOR());
2918
+ rbs_position_t annot_pos = NullPosition;
2919
+
2920
+ CHECK_PARSE(parse_annotations(parser, annotations, &annot_pos));
2921
+ rbs_parser_advance(parser);
2922
+
2923
+ switch (parser->current_token.type) {
2924
+ case tUIDENT:
2925
+ case pCOLON2: {
2926
+ rbs_ast_declarations_constant_t *constant = NULL;
2927
+ CHECK_PARSE(parse_const_decl(parser, annotations, &constant));
2928
+ *decl = (rbs_node_t *) constant;
2929
+ return true;
2930
+ }
2931
+ case tGIDENT: {
2932
+ rbs_ast_declarations_global_t *global = NULL;
2933
+ CHECK_PARSE(parse_global_decl(parser, annotations, &global));
2934
+ *decl = (rbs_node_t *) global;
2935
+ return true;
2936
+ }
2937
+ case kTYPE: {
2938
+ rbs_ast_declarations_type_alias_t *typealias = NULL;
2939
+ CHECK_PARSE(parse_type_decl(parser, annot_pos, annotations, &typealias));
2940
+ *decl = (rbs_node_t *) typealias;
2941
+ return true;
2942
+ }
2943
+ case kINTERFACE: {
2944
+ rbs_ast_declarations_interface_t *interface_decl = NULL;
2945
+ CHECK_PARSE(parse_interface_decl(parser, annot_pos, annotations, &interface_decl));
2946
+ *decl = (rbs_node_t *) interface_decl;
2947
+ return true;
2948
+ }
2949
+ case kMODULE: {
2950
+ rbs_node_t *module_decl = NULL;
2951
+ CHECK_PARSE(parse_module_decl(parser, annot_pos, annotations, &module_decl));
2952
+ *decl = module_decl;
2953
+ return true;
2954
+ }
2955
+ case kCLASS: {
2956
+ rbs_node_t *class_decl = NULL;
2957
+ CHECK_PARSE(parse_class_decl(parser, annot_pos, annotations, &class_decl));
2958
+ *decl = class_decl;
2959
+ return true;
2960
+ }
2961
+ default:
2962
+ rbs_parser_set_error(parser, parser->current_token, true, "cannot start a declaration");
2963
+ return false;
2964
+ }
2965
+ }
2966
+
2967
+ /*
2968
+ namespace ::= {} (`::`)? (`tUIDENT` `::`)* `tUIDENT` <`::`>
2969
+ | {} <> (empty -- returns empty namespace)
2970
+ */
2971
+ NODISCARD
2972
+ static bool parse_namespace(rbs_parser_t *parser, rbs_range_t *rg, rbs_namespace_t **namespace) {
2973
+ bool is_absolute = false;
2974
+
2975
+ if (parser->next_token.type == pCOLON2) {
2976
+ *rg = (rbs_range_t) {
2977
+ .start = parser->next_token.range.start,
2978
+ .end = parser->next_token.range.end,
2979
+ };
2980
+ is_absolute = true;
2981
+
2982
+ rbs_parser_advance(parser);
2983
+ }
2984
+
2985
+ rbs_node_list_t *path = rbs_node_list_new(ALLOCATOR());
2986
+
2987
+ while (true) {
2988
+ if (parser->next_token.type == tUIDENT && parser->next_token2.type == pCOLON2) {
2989
+ rbs_location_t *symbolLoc = rbs_location_new(ALLOCATOR(), parser->next_token.range);
2990
+ rbs_ast_symbol_t *symbol = rbs_ast_symbol_new(ALLOCATOR(), symbolLoc, &parser->constant_pool, INTERN_TOKEN(parser, parser->next_token));
2991
+ rbs_node_list_append(path, (rbs_node_t *)symbol);
2992
+ if (rbs_null_position_p(rg->start)) {
2993
+ rg->start = parser->next_token.range.start;
2994
+ }
2995
+ rg->end = parser->next_token2.range.end;
2996
+ rbs_parser_advance(parser);
2997
+ rbs_parser_advance(parser);
2998
+ } else {
2999
+ break;
3000
+ }
3001
+ }
3002
+
3003
+ *namespace = rbs_namespace_new(ALLOCATOR(), rbs_location_new(ALLOCATOR(), *rg), path, is_absolute);
3004
+ return true;
3005
+ }
3006
+
3007
+ /*
3008
+ use_clauses ::= {} use_clause `,` ... `,` <use_clause>
3009
+
3010
+ use_clause ::= {} namespace <tUIDENT>
3011
+ | {} namespace tUIDENT `as` <tUIDENT>
3012
+ | {} namespace <tSTAR>
3013
+ */
3014
+ NODISCARD
3015
+ static bool parse_use_clauses(rbs_parser_t *parser, rbs_node_list_t *clauses) {
3016
+ while (true) {
3017
+ rbs_range_t namespace_range = NULL_RANGE;
3018
+ rbs_namespace_t *namespace = NULL;
3019
+ CHECK_PARSE(parse_namespace(parser, &namespace_range, &namespace));
3020
+
3021
+ switch (parser->next_token.type)
3022
+ {
3023
+ case tLIDENT:
3024
+ case tULIDENT:
3025
+ case tUIDENT: {
3026
+ rbs_parser_advance(parser);
3027
+
3028
+ enum RBSTokenType ident_type = parser->current_token.type;
3029
+
3030
+ rbs_range_t type_name_range = rbs_null_range_p(namespace_range)
3031
+ ? parser->current_token.range
3032
+ : (rbs_range_t) { .start = namespace_range.start, .end = parser->current_token.range.end };
3033
+
3034
+ rbs_location_t *symbolLoc = rbs_location_current_token(parser);
3035
+ rbs_ast_symbol_t *symbol = rbs_ast_symbol_new(ALLOCATOR(), symbolLoc, &parser->constant_pool, INTERN_TOKEN(parser, parser->current_token));
3036
+ rbs_type_name_t *type_name = rbs_type_name_new(ALLOCATOR(), rbs_location_new(ALLOCATOR(), type_name_range),namespace, symbol);
3037
+
3038
+ rbs_range_t keyword_range = NULL_RANGE;
3039
+ rbs_range_t new_name_range = NULL_RANGE;
3040
+ rbs_ast_symbol_t *new_name = NULL;
3041
+ rbs_range_t clause_range = type_name_range;
3042
+ if (parser->next_token.type == kAS) {
3043
+ rbs_parser_advance(parser);
3044
+ keyword_range = parser->current_token.range;
3045
+
3046
+ if (ident_type == tUIDENT) ADVANCE_ASSERT(parser, tUIDENT);
3047
+ if (ident_type == tLIDENT) ADVANCE_ASSERT(parser, tLIDENT);
3048
+ if (ident_type == tULIDENT) ADVANCE_ASSERT(parser, tULIDENT);
3049
+
3050
+ rbs_location_t *symbolLoc = rbs_location_new(ALLOCATOR(), new_name_range);
3051
+ new_name = rbs_ast_symbol_new(ALLOCATOR(), symbolLoc, &parser->constant_pool, INTERN_TOKEN(parser, parser->current_token));
3052
+ new_name_range = parser->current_token.range;
3053
+ clause_range.end = new_name_range.end;
3054
+ }
3055
+
3056
+ rbs_location_t *loc = rbs_location_new(ALLOCATOR(), clause_range);
3057
+ rbs_loc_alloc_children(ALLOCATOR(), loc, 3);
3058
+ rbs_loc_add_required_child(loc, INTERN("type_name"), type_name_range);
3059
+ rbs_loc_add_optional_child(loc, INTERN("keyword"), keyword_range);
3060
+ rbs_loc_add_optional_child(loc, INTERN("new_name"), new_name_range);
3061
+
3062
+ rbs_ast_directives_use_single_clause_t *clause = rbs_ast_directives_use_single_clause_new(ALLOCATOR(), loc, type_name, new_name);
3063
+ rbs_node_list_append(clauses, (rbs_node_t *)clause);
3064
+
3065
+ break;
3066
+ }
3067
+ case pSTAR:
3068
+ {
3069
+ rbs_range_t clause_range = namespace_range;
3070
+ rbs_parser_advance(parser);
3071
+
3072
+ rbs_range_t star_range = parser->current_token.range;
3073
+ clause_range.end = star_range.end;
3074
+
3075
+ rbs_location_t *loc = rbs_location_new(ALLOCATOR(), clause_range);
3076
+ rbs_loc_alloc_children(ALLOCATOR(), loc, 2);
3077
+ rbs_loc_add_required_child(loc, INTERN("namespace"), namespace_range);
3078
+ rbs_loc_add_required_child(loc, INTERN("star"), star_range);
3079
+
3080
+ rbs_ast_directives_use_wildcard_clause_t *clause = rbs_ast_directives_use_wildcard_clause_new(ALLOCATOR(), loc, namespace);
3081
+ rbs_node_list_append(clauses, (rbs_node_t *)clause);
3082
+
3083
+ break;
3084
+ }
3085
+ default:
3086
+ rbs_parser_set_error(parser, parser->next_token, true, "use clause is expected");
3087
+ return false;
3088
+ }
3089
+
3090
+ if (parser->next_token.type == pCOMMA) {
3091
+ rbs_parser_advance(parser);
3092
+ } else {
3093
+ break;
3094
+ }
3095
+ }
3096
+
3097
+ return true;
3098
+ }
3099
+
3100
+ /*
3101
+ use_directive ::= {} `use` <clauses>
3102
+ */
3103
+ NODISCARD
3104
+ static bool parse_use_directive(rbs_parser_t *parser, rbs_ast_directives_use_t **use_directive) {
3105
+ if (parser->next_token.type == kUSE) {
3106
+ rbs_parser_advance(parser);
3107
+
3108
+ rbs_range_t keyword_range = parser->current_token.range;
3109
+
3110
+ rbs_node_list_t *clauses = rbs_node_list_new(ALLOCATOR());
3111
+ CHECK_PARSE(parse_use_clauses(parser, clauses));
3112
+
3113
+ rbs_range_t directive_range = keyword_range;
3114
+ directive_range.end = parser->current_token.range.end;
3115
+
3116
+ rbs_location_t *loc = rbs_location_new(ALLOCATOR(), directive_range);
3117
+ rbs_loc_alloc_children(ALLOCATOR(), loc, 1);
3118
+ rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range);
3119
+
3120
+ *use_directive = rbs_ast_directives_use_new(ALLOCATOR(), loc, clauses);
3121
+ }
3122
+
3123
+ return true;
3124
+ }
3125
+
3126
+ static rbs_ast_comment_t *parse_comment_lines(rbs_parser_t *parser, rbs_comment_t *com) {
3127
+ size_t hash_bytes = parser->rbs_lexer_t->encoding->char_width((const uint8_t *) "#", (size_t) 1);
3128
+ size_t space_bytes = parser->rbs_lexer_t->encoding->char_width((const uint8_t *) " ", (size_t) 1);
3129
+
3130
+ rbs_buffer_t rbs_buffer;
3131
+ rbs_buffer_init(ALLOCATOR(), &rbs_buffer);
3132
+
3133
+ for (size_t i = 0; i < com->line_count; i++) {
3134
+ rbs_token_t tok = com->tokens[i];
3135
+
3136
+ const char *comment_start = parser->rbs_lexer_t->string.start + tok.range.start.byte_pos + hash_bytes;
3137
+ size_t comment_bytes = RBS_RANGE_BYTES(tok.range) - hash_bytes;
3138
+
3139
+ rbs_string_t str = rbs_string_new(
3140
+ comment_start,
3141
+ parser->rbs_lexer_t->string.end
3142
+ );
3143
+ unsigned char c = rbs_utf8_string_to_codepoint(str);
3144
+
3145
+ if (c == ' ') {
3146
+ comment_start += space_bytes;
3147
+ comment_bytes -= space_bytes;
3148
+ }
3149
+
3150
+ rbs_buffer_append_string(ALLOCATOR(), &rbs_buffer, comment_start, comment_bytes);
3151
+ rbs_buffer_append_cstr(ALLOCATOR(), &rbs_buffer, "\n");
3152
+ }
3153
+
3154
+ return rbs_ast_comment_new(
3155
+ ALLOCATOR(),
3156
+ rbs_location_new(ALLOCATOR(), (rbs_range_t) { .start = com->start, .end = com->end }),
3157
+ rbs_buffer_to_string(&rbs_buffer)
3158
+ );
3159
+ }
3160
+
3161
+ static rbs_comment_t *comment_get_comment(rbs_comment_t *com, int line) {
3162
+ if (com == NULL) {
3163
+ return NULL;
3164
+ }
3165
+
3166
+ if (com->end.line < line) {
3167
+ return NULL;
3168
+ }
3169
+
3170
+ if (com->end.line == line) {
3171
+ return com;
3172
+ }
3173
+
3174
+ return comment_get_comment(com->next_comment, line);
3175
+ }
3176
+
3177
+ static void comment_insert_new_line(rbs_allocator_t *allocator, rbs_comment_t *com, rbs_token_t comment_token) {
3178
+ if (com->line_count == 0) {
3179
+ com->start = comment_token.range.start;
3180
+ }
3181
+
3182
+ if (com->line_count == com->line_size) {
3183
+ com->line_size += 10;
3184
+
3185
+ if (com->tokens) {
3186
+ rbs_token_t *p = com->tokens;
3187
+ com->tokens = rbs_allocator_calloc(allocator, com->line_size, rbs_token_t);
3188
+ memcpy(com->tokens, p, sizeof(rbs_token_t) * com->line_count);
3189
+ } else {
3190
+ com->tokens = rbs_allocator_calloc(allocator, com->line_size, rbs_token_t);
3191
+ }
3192
+ }
3193
+
3194
+ com->tokens[com->line_count++] = comment_token;
3195
+ com->end = comment_token.range.end;
3196
+ }
3197
+
3198
+ static rbs_comment_t *alloc_comment(rbs_allocator_t *allocator, rbs_token_t comment_token, rbs_comment_t *last_comment) {
3199
+ rbs_comment_t *new_comment = rbs_allocator_alloc(allocator, rbs_comment_t);
3200
+
3201
+ *new_comment = (rbs_comment_t) {
3202
+ .start = comment_token.range.start,
3203
+ .end = comment_token.range.end,
3204
+
3205
+ .line_size = 0,
3206
+ .line_count = 0,
3207
+ .tokens = NULL,
3208
+
3209
+ .next_comment = last_comment,
3210
+ };
3211
+
3212
+ comment_insert_new_line(allocator, new_comment, comment_token);
3213
+
3214
+ return new_comment;
3215
+ }
3216
+
3217
+ /**
3218
+ * Insert new comment line token.
3219
+ * */
3220
+ static void insert_comment_line(rbs_parser_t *parser, rbs_token_t tok) {
3221
+ int prev_line = tok.range.start.line - 1;
3222
+
3223
+ rbs_comment_t *com = comment_get_comment(parser->last_comment, prev_line);
3224
+
3225
+ if (com) {
3226
+ comment_insert_new_line(ALLOCATOR(), com, tok);
3227
+ } else {
3228
+ parser->last_comment = alloc_comment(ALLOCATOR(), tok, parser->last_comment);
3229
+ }
3230
+ }
3231
+
3232
+ bool rbs_parse_signature(rbs_parser_t *parser, rbs_signature_t **signature) {
3233
+ rbs_range_t signature_range;
3234
+ signature_range.start = parser->current_token.range.start;
3235
+
3236
+ rbs_node_list_t *dirs = rbs_node_list_new(ALLOCATOR());
3237
+ rbs_node_list_t *decls = rbs_node_list_new(ALLOCATOR());
3238
+
3239
+ while (parser->next_token.type == kUSE) {
3240
+ rbs_ast_directives_use_t *use_node;
3241
+ CHECK_PARSE(parse_use_directive(parser, &use_node));
3242
+
3243
+ if (use_node == NULL) {
3244
+ rbs_node_list_append(dirs, NULL);
3245
+ } else {
3246
+ rbs_node_list_append(dirs, (rbs_node_t *)use_node);
3247
+ }
3248
+ }
3249
+
3250
+ while (parser->next_token.type != pEOF) {
3251
+ rbs_node_t *decl = NULL;
3252
+ CHECK_PARSE(parse_decl(parser, &decl));
3253
+ rbs_node_list_append(decls, decl);
3254
+ }
3255
+
3256
+ signature_range.end = parser->current_token.range.end;
3257
+ *signature = rbs_signature_new(ALLOCATOR(), rbs_location_new(ALLOCATOR(), signature_range), dirs, decls);
3258
+ return true;
3259
+ }
3260
+
3261
+ id_table *alloc_empty_table(rbs_allocator_t *allocator) {
3262
+ id_table *table = rbs_allocator_alloc(allocator, id_table);
3263
+
3264
+ *table = (id_table) {
3265
+ .size = 10,
3266
+ .count = 0,
3267
+ .ids = rbs_allocator_calloc(allocator, 10, rbs_constant_id_t),
3268
+ .next = NULL,
3269
+ };
3270
+
3271
+ return table;
3272
+ }
3273
+
3274
+ id_table *alloc_reset_table(rbs_allocator_t *allocator) {
3275
+ id_table *table = rbs_allocator_alloc(allocator, id_table);
3276
+
3277
+ *table = (id_table) {
3278
+ .size = 0,
3279
+ .count = 0,
3280
+ .ids = NULL,
3281
+ .next = NULL,
3282
+ };
3283
+
3284
+ return table;
3285
+ }
3286
+
3287
+ void rbs_parser_push_typevar_table(rbs_parser_t *parser, bool reset) {
3288
+ if (reset) {
3289
+ id_table *table = alloc_reset_table(ALLOCATOR());
3290
+ table->next = parser->vars;
3291
+ parser->vars = table;
3292
+ }
3293
+
3294
+ id_table *table = alloc_empty_table(ALLOCATOR());
3295
+ table->next = parser->vars;
3296
+ parser->vars = table;
3297
+ }
3298
+
3299
+ NODISCARD
3300
+ bool rbs_parser_insert_typevar(rbs_parser_t *parser, rbs_constant_id_t id) {
3301
+ id_table *table = parser->vars;
3302
+
3303
+ if (RESET_TABLE_P(table)) {
3304
+ rbs_parser_set_error(parser, parser->current_token, false, "Cannot insert to reset table");
3305
+ return false;
3306
+ }
3307
+
3308
+ if (table->size == table->count) {
3309
+ // expand
3310
+ rbs_constant_id_t *ptr = table->ids;
3311
+ table->size += 10;
3312
+ table->ids = rbs_allocator_calloc(ALLOCATOR(), table->size, rbs_constant_id_t);
3313
+ memcpy(table->ids, ptr, sizeof(rbs_constant_id_t) * table->count);
3314
+ }
3315
+
3316
+ table->ids[table->count++] = id;
3317
+
3318
+ return true;
3319
+ }
3320
+
3321
+ void rbs_parser_print(rbs_parser_t *parser) {
3322
+ printf(" current_token = %s (%d...%d)\n", rbs_token_type_str(parser->current_token.type), parser->current_token.range.start.char_pos, parser->current_token.range.end.char_pos);
3323
+ printf(" next_token = %s (%d...%d)\n", rbs_token_type_str(parser->next_token.type), parser->next_token.range.start.char_pos, parser->next_token.range.end.char_pos);
3324
+ printf(" next_token2 = %s (%d...%d)\n", rbs_token_type_str(parser->next_token2.type), parser->next_token2.range.start.char_pos, parser->next_token2.range.end.char_pos);
3325
+ printf(" next_token3 = %s (%d...%d)\n", rbs_token_type_str(parser->next_token3.type), parser->next_token3.range.start.char_pos, parser->next_token3.range.end.char_pos);
3326
+ }
3327
+
3328
+ void rbs_parser_advance(rbs_parser_t *parser) {
3329
+ parser->current_token = parser->next_token;
3330
+ parser->next_token = parser->next_token2;
3331
+ parser->next_token2 = parser->next_token3;
3332
+
3333
+ while (true) {
3334
+ if (parser->next_token3.type == pEOF) {
3335
+ break;
3336
+ }
3337
+
3338
+ parser->next_token3 = rbs_lexer_next_token(parser->rbs_lexer_t);
3339
+
3340
+ if (parser->next_token3.type == tCOMMENT) {
3341
+ // skip
3342
+ } else if (parser->next_token3.type == tLINECOMMENT) {
3343
+ insert_comment_line(parser, parser->next_token3);
3344
+ } else if (parser->next_token3.type == tTRIVIA) {
3345
+ //skip
3346
+ } else {
3347
+ break;
3348
+ }
3349
+ }
3350
+ }
3351
+
3352
+ void rbs_print_token(rbs_token_t tok) {
3353
+ printf(
3354
+ "%s char=%d...%d\n",
3355
+ rbs_token_type_str(tok.type),
3356
+ tok.range.start.char_pos,
3357
+ tok.range.end.char_pos
3358
+ );
3359
+ }
3360
+
3361
+ rbs_ast_comment_t *rbs_parser_get_comment(rbs_parser_t *parser, int subject_line) {
3362
+ int comment_line = subject_line - 1;
3363
+
3364
+ rbs_comment_t *com = comment_get_comment(parser->last_comment, comment_line);
3365
+
3366
+ if (com) {
3367
+ return parse_comment_lines(parser, com);
3368
+ } else {
3369
+ return NULL;
3370
+ }
3371
+ }
3372
+
3373
+ rbs_lexer_t *rbs_lexer_new(rbs_allocator_t *allocator, rbs_string_t string, const rbs_encoding_t *encoding, int start_pos, int end_pos) {
3374
+ rbs_lexer_t *lexer = rbs_allocator_alloc(allocator, rbs_lexer_t);
3375
+
3376
+ rbs_position_t start_position = (rbs_position_t) {
3377
+ .byte_pos = 0,
3378
+ .char_pos = 0,
3379
+ .line = 1,
3380
+ .column = 0,
3381
+ };
3382
+
3383
+ *lexer = (rbs_lexer_t) {
3384
+ .string = string,
3385
+ .start_pos = start_pos,
3386
+ .end_pos = end_pos,
3387
+ .current = start_position,
3388
+ .start = { 0 },
3389
+ .first_token_of_line = false,
3390
+ .last_char = 0,
3391
+ .encoding = encoding,
3392
+ };
3393
+
3394
+ rbs_skipn(lexer, start_pos);
3395
+ lexer->start = lexer->current;
3396
+ lexer->first_token_of_line = lexer->current.column == 0;
3397
+
3398
+ return lexer;
3399
+ }
3400
+
3401
+ rbs_parser_t *rbs_parser_new(rbs_string_t string, const rbs_encoding_t *encoding, int start_pos, int end_pos) {
3402
+ rbs_allocator_t *allocator = rbs_allocator_init();
3403
+
3404
+ rbs_lexer_t *lexer = rbs_lexer_new(allocator, string, encoding, start_pos, end_pos);
3405
+ rbs_parser_t *parser = rbs_allocator_alloc(allocator, rbs_parser_t);
3406
+
3407
+ *parser = (rbs_parser_t) {
3408
+ .rbs_lexer_t = lexer,
3409
+
3410
+ .current_token = NullToken,
3411
+ .next_token = NullToken,
3412
+ .next_token2 = NullToken,
3413
+ .next_token3 = NullToken,
3414
+
3415
+ .vars = NULL,
3416
+ .last_comment = NULL,
3417
+
3418
+ .constant_pool = {},
3419
+ .allocator = allocator,
3420
+ .error = NULL,
3421
+ };
3422
+
3423
+ // The parser's constant pool is mainly used for storing the names of type variables, which usually aren't many.
3424
+ // Below are some statistics gathered from the current test suite. We can see that 56% of parsers never add to their
3425
+ // constant pool at all. The initial capacity needs to be a power of 2. Picking 2 means that we won't need to realloc
3426
+ // in 85% of cases.
3427
+ //
3428
+ // TODO: recalculate these statistics based on a real world codebase, rather than the test suite.
3429
+ //
3430
+ // | Size | Count | Cumulative | % Coverage |
3431
+ // |------|-------|------------|------------|
3432
+ // | 0 | 7,862 | 7,862 | 56% |
3433
+ // | 1 | 3,196 | 11,058 | 79% |
3434
+ // | 2 | 778 | 12,719 | 85% |
3435
+ // | 3 | 883 | 11,941 | 91% |
3436
+ // | 4 | 478 | 13,197 | 95% |
3437
+ // | 5 | 316 | 13,513 | 97% |
3438
+ // | 6 | 288 | 13,801 | 99% |
3439
+ // | 7 | 144 | 13,945 | 100% |
3440
+ const size_t initial_pool_capacity = 2;
3441
+ rbs_constant_pool_init(&parser->constant_pool, initial_pool_capacity);
3442
+
3443
+ rbs_parser_advance(parser);
3444
+ rbs_parser_advance(parser);
3445
+ rbs_parser_advance(parser);
3446
+
3447
+ return parser;
3448
+ }
3449
+
3450
+ void rbs_parser_free(rbs_parser_t *parser) {
3451
+ rbs_constant_pool_free(&parser->constant_pool);
3452
+ rbs_allocator_free(ALLOCATOR());
3453
+ }
3454
+
3455
+ void rbs_parser_set_error(rbs_parser_t *parser, rbs_token_t tok, bool syntax_error, const char *fmt, ...) {
3456
+ if (parser->error) {
3457
+ return;
3458
+ }
3459
+
3460
+ va_list args;
3461
+
3462
+ va_start(args, fmt);
3463
+ int length = vsnprintf(NULL, 0, fmt, args);
3464
+ va_end(args);
3465
+
3466
+ char *message = rbs_allocator_alloc_many(ALLOCATOR(), length + 1, char);
3467
+
3468
+ va_start(args, fmt);
3469
+ vsnprintf(message, length + 1, fmt, args);
3470
+ va_end(args);
3471
+
3472
+ parser->error = rbs_allocator_alloc(ALLOCATOR(), rbs_error_t);
3473
+ parser->error->token = tok;
3474
+ parser->error->message = message;
3475
+ parser->error->syntax_error = syntax_error;
3476
+ }
3477
+
3478
+ /*
3479
+ parse_method_overload ::= {} annotations <method_type>
3480
+ */
3481
+ NODISCARD
3482
+ static bool parse_method_overload(rbs_parser_t *parser, rbs_node_list_t *annotations, rbs_method_type_t **method_type) {
3483
+ rbs_position_t pos = NullPosition;
3484
+
3485
+ if (!parse_annotations(parser, annotations, &pos)) {
3486
+ return false;
3487
+ }
3488
+
3489
+ return rbs_parse_method_type(parser, method_type);
3490
+ }
3491
+
3492
+ /*
3493
+ inline_method_overloads ::= {} <overload> -- returns true
3494
+ | {} overload `|` ... `|` overload -- returns true
3495
+ | {<>} -- returns false
3496
+ */
3497
+ NODISCARD
3498
+ static bool parse_inline_method_overloads(rbs_parser_t *parser, rbs_node_list_t *overloads, rbs_location_list_t *bar_locations) {
3499
+ while (true) {
3500
+ rbs_node_list_t *annotations = rbs_node_list_new(ALLOCATOR());
3501
+ rbs_method_type_t *method_type = NULL;
3502
+
3503
+ if (!parse_method_overload(parser, annotations, &method_type)) {
3504
+ return false;
3505
+ }
3506
+
3507
+ rbs_location_t *location = rbs_location_new(ALLOCATOR(), parser->current_token.range);
3508
+ rbs_ast_members_method_definition_overload_t *overload = rbs_ast_members_method_definition_overload_new(
3509
+ ALLOCATOR(),
3510
+ location,
3511
+ annotations,
3512
+ (rbs_node_t *) method_type
3513
+ );
3514
+ rbs_node_list_append(overloads, (rbs_node_t *) overload);
3515
+
3516
+ if (parser->next_token.type == pBAR) {
3517
+ rbs_location_t *bar_location = rbs_location_new(ALLOCATOR(), parser->next_token.range);
3518
+
3519
+ rbs_parser_advance(parser);
3520
+
3521
+ rbs_location_list_append(bar_locations, bar_location);
3522
+
3523
+ continue;
3524
+ }
3525
+
3526
+ return true;
3527
+ }
3528
+ }
3529
+
3530
+ NODISCARD
3531
+ static bool parse_inline_comment(rbs_parser_t *parser, rbs_location_t **comment) {
3532
+ if (parser->next_token.type != tINLINECOMMENT) {
3533
+ *comment = NULL;
3534
+ return true;
3535
+ }
3536
+
3537
+ rbs_range_t comment_range = parser->next_token.range;
3538
+ rbs_parser_advance(parser);
3539
+
3540
+ *comment = rbs_location_new(ALLOCATOR(), comment_range);
3541
+ return true;
3542
+ }
3543
+
3544
+ NODISCARD
3545
+ static bool parse_inline_leading_annotation(rbs_parser_t *parser, rbs_ast_ruby_annotations_t **annotation) {
3546
+ switch (parser->next_token.type) {
3547
+ case pCOLON: {
3548
+ rbs_range_t colon_range = parser->next_token.range;
3549
+ rbs_parser_advance(parser);
3550
+
3551
+ rbs_node_list_t *annotations = rbs_node_list_new(ALLOCATOR());
3552
+ rbs_method_type_t *method_type = NULL;
3553
+
3554
+ if (!parse_method_overload(parser, annotations, &method_type)) {
3555
+ return false;
3556
+ }
3557
+
3558
+ rbs_range_t full_range = {
3559
+ .start = colon_range.start,
3560
+ .end = parser->current_token.range.end
3561
+ };
3562
+
3563
+ rbs_location_t *full_loc = rbs_location_new(ALLOCATOR(), full_range);
3564
+ rbs_location_t *colon_loc = rbs_location_new(ALLOCATOR(), colon_range);
3565
+
3566
+ *annotation = (rbs_ast_ruby_annotations_t *) rbs_ast_ruby_annotations_colon_method_type_annotation_new(
3567
+ ALLOCATOR(),
3568
+ full_loc,
3569
+ colon_loc,
3570
+ annotations,
3571
+ (rbs_node_t *) method_type
3572
+ );
3573
+ return true;
3574
+ }
3575
+ case kATRBS: {
3576
+ rbs_range_t rbs_range = parser->next_token.range;
3577
+ rbs_parser_advance(parser);
3578
+
3579
+ switch (parser->next_token.type) {
3580
+ case pLPAREN:
3581
+ case pLBRACKET:
3582
+ case pLBRACE:
3583
+ case tANNOTATION: {
3584
+ rbs_node_list_t *overloads = rbs_node_list_new(ALLOCATOR());
3585
+ rbs_location_list_t *bar_locations = rbs_location_list_new(ALLOCATOR());
3586
+
3587
+ if (!parse_inline_method_overloads(parser, overloads, bar_locations)) {
3588
+ return false;
3589
+ }
3590
+
3591
+ rbs_range_t full_range = {
3592
+ .start = rbs_range.start,
3593
+ .end = parser->current_token.range.end
3594
+ };
3595
+
3596
+ rbs_location_t *full_loc = rbs_location_new(ALLOCATOR(), full_range);
3597
+ rbs_location_t *rbs_loc = rbs_location_new(ALLOCATOR(), rbs_range);
3598
+
3599
+ *annotation = (rbs_ast_ruby_annotations_t *) rbs_ast_ruby_annotations_method_types_annotation_new(
3600
+ ALLOCATOR(),
3601
+ full_loc,
3602
+ rbs_loc,
3603
+ overloads,
3604
+ bar_locations
3605
+ );
3606
+ return true;
3607
+ }
3608
+ case kSKIP: {
3609
+ rbs_parser_advance(parser);
3610
+
3611
+ rbs_range_t skip_range = parser->current_token.range;
3612
+ rbs_location_t *skip_loc = rbs_location_new(ALLOCATOR(), skip_range);
3613
+
3614
+ rbs_location_t *comment_loc = NULL;
3615
+ if (!parse_inline_comment(parser, &comment_loc)) {
3616
+ return false;
3617
+ }
3618
+
3619
+ rbs_range_t full_range = {
3620
+ .start = rbs_range.start,
3621
+ .end = parser->current_token.range.end
3622
+ };
3623
+
3624
+ rbs_location_t *full_loc = rbs_location_new(ALLOCATOR(), full_range);
3625
+ rbs_location_t *rbs_loc = rbs_location_new(ALLOCATOR(), rbs_range);
3626
+
3627
+ *annotation = (rbs_ast_ruby_annotations_t *) rbs_ast_ruby_annotations_skip_annotation_new(
3628
+ ALLOCATOR(),
3629
+ full_loc,
3630
+ rbs_loc,
3631
+ skip_loc,
3632
+ comment_loc
3633
+ );
3634
+ return true;
3635
+ }
3636
+ case kRETURN: {
3637
+ rbs_parser_advance(parser);
3638
+
3639
+ rbs_range_t return_range = parser->current_token.range;
3640
+ rbs_location_t *return_loc = rbs_location_new(ALLOCATOR(), return_range);
3641
+
3642
+ ADVANCE_ASSERT(parser, pCOLON);
3643
+
3644
+ rbs_range_t colon_range = parser->current_token.range;
3645
+ rbs_location_t *colon_loc = rbs_location_new(ALLOCATOR(), colon_range);
3646
+
3647
+ rbs_node_t *return_type = NULL;
3648
+ if (!rbs_parse_type(parser, &return_type)) {
3649
+ return false;
3650
+ }
3651
+
3652
+ rbs_location_t *comment_loc = NULL;
3653
+ if (!parse_inline_comment(parser, &comment_loc)) {
3654
+ return false;
3655
+ }
3656
+
3657
+ rbs_range_t full_range = {
3658
+ .start = rbs_range.start,
3659
+ .end = parser->current_token.range.end
3660
+ };
3661
+
3662
+ rbs_location_t *full_loc = rbs_location_new(ALLOCATOR(), full_range);
3663
+ rbs_location_t *rbs_loc = rbs_location_new(ALLOCATOR(), rbs_range);
3664
+
3665
+ *annotation = (rbs_ast_ruby_annotations_t *) rbs_ast_ruby_annotations_return_type_annotation_new(
3666
+ ALLOCATOR(),
3667
+ full_loc,
3668
+ rbs_loc,
3669
+ return_loc,
3670
+ colon_loc,
3671
+ return_type,
3672
+ comment_loc
3673
+ );
3674
+ return true;
3675
+ }
3676
+ default: {
3677
+ rbs_parser_set_error(parser, parser->next_token, true, "unexpected token for @rbs annotation");
3678
+ return false;
3679
+ }
3680
+ }
3681
+ }
3682
+ default: {
3683
+ rbs_parser_set_error(parser, parser->next_token, true, "unexpected token for inline leading annotation");
3684
+ return false;
3685
+ }
3686
+ }
3687
+ }
3688
+
3689
+ NODISCARD
3690
+ static bool parse_inline_trailing_annotation(rbs_parser_t *parser, rbs_ast_ruby_annotations_t **annotation) {
3691
+ rbs_range_t prefix_range = parser->next_token.range;
3692
+
3693
+ switch (parser->next_token.type) {
3694
+ case pCOLON: {
3695
+ rbs_parser_advance(parser);
3696
+
3697
+ rbs_node_t *type = NULL;
3698
+ if (!rbs_parse_type(parser, &type)) {
3699
+ return false;
3700
+ }
3701
+
3702
+ rbs_range_t full_range = {
3703
+ .start = prefix_range.start,
3704
+ .end = parser->current_token.range.end
3705
+ };
3706
+
3707
+ rbs_location_t *full_loc = rbs_location_new(ALLOCATOR(), full_range);
3708
+ rbs_location_t *prefix_loc = rbs_location_new(ALLOCATOR(), prefix_range);
3709
+
3710
+ *annotation = (rbs_ast_ruby_annotations_t *) rbs_ast_ruby_annotations_node_type_assertion_new(
3711
+ ALLOCATOR(),
3712
+ full_loc,
3713
+ prefix_loc,
3714
+ type
3715
+ );
3716
+ return true;
3717
+ }
3718
+ default: {
3719
+ rbs_parser_set_error(parser, parser->next_token, true, "unexpected token for inline trailing annotation");
3720
+ return false;
3721
+ }
3722
+ }
3723
+ }
3724
+
3725
+ bool rbs_parse_inline_leading_annotation(rbs_parser_t *parser, rbs_ast_ruby_annotations_t **annotation) {
3726
+ bool success = parse_inline_leading_annotation(parser, annotation);
3727
+
3728
+ ADVANCE_ASSERT(parser, pEOF);
3729
+
3730
+ return success;
3731
+ }
3732
+
3733
+ bool rbs_parse_inline_trailing_annotation(rbs_parser_t *parser, rbs_ast_ruby_annotations_t **annotation) {
3734
+ bool success = parse_inline_trailing_annotation(parser, annotation);
3735
+
3736
+ ADVANCE_ASSERT(parser, pEOF);
3737
+
3738
+ return success;
3739
+ }