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