rbs 3.9.1 → 4.0.0.dev.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (115) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby.yml +1 -1
  3. data/.github/workflows/windows.yml +1 -1
  4. data/CHANGELOG.md +0 -7
  5. data/Rakefile +28 -21
  6. data/Steepfile +1 -0
  7. data/config.yml +232 -62
  8. data/ext/rbs_extension/ast_translation.c +1149 -0
  9. data/ext/rbs_extension/ast_translation.h +30 -0
  10. data/{src/constants.c → ext/rbs_extension/class_constants.c} +15 -1
  11. data/{include/rbs/constants.h → ext/rbs_extension/class_constants.h} +10 -1
  12. data/ext/rbs_extension/extconf.rb +3 -1
  13. data/ext/rbs_extension/{location.c → legacy_location.c} +25 -34
  14. data/ext/rbs_extension/legacy_location.h +40 -0
  15. data/ext/rbs_extension/main.c +402 -8
  16. data/ext/rbs_extension/rbs_extension.h +3 -21
  17. data/ext/rbs_extension/rbs_string_bridging.c +9 -0
  18. data/ext/rbs_extension/rbs_string_bridging.h +20 -0
  19. data/include/rbs/ast.h +748 -0
  20. data/include/rbs/defines.h +60 -0
  21. data/{ext/rbs_extension → include/rbs}/lexer.h +40 -32
  22. data/include/rbs/location.h +59 -0
  23. data/include/rbs/parser.h +151 -0
  24. data/include/rbs/string.h +49 -0
  25. data/include/rbs/util/rbs_allocator.h +38 -0
  26. data/include/rbs/util/rbs_assert.h +9 -0
  27. data/include/rbs/util/rbs_buffer.h +83 -0
  28. data/include/rbs/util/rbs_constant_pool.h +3 -64
  29. data/include/rbs/util/rbs_encoding.h +280 -0
  30. data/include/rbs/util/rbs_unescape.h +23 -0
  31. data/include/rbs.h +1 -2
  32. data/lib/rbs/annotate/formatter.rb +3 -13
  33. data/lib/rbs/annotate/rdoc_annotator.rb +3 -1
  34. data/lib/rbs/annotate/rdoc_source.rb +1 -1
  35. data/lib/rbs/ast/ruby/annotations.rb +119 -0
  36. data/lib/rbs/ast/ruby/comment_block.rb +221 -0
  37. data/lib/rbs/ast/ruby/declarations.rb +86 -0
  38. data/lib/rbs/ast/ruby/helpers/constant_helper.rb +24 -0
  39. data/lib/rbs/ast/ruby/helpers/location_helper.rb +15 -0
  40. data/lib/rbs/ast/ruby/members.rb +213 -0
  41. data/lib/rbs/buffer.rb +104 -24
  42. data/lib/rbs/cli/validate.rb +39 -34
  43. data/lib/rbs/cli.rb +4 -5
  44. data/lib/rbs/definition.rb +6 -1
  45. data/lib/rbs/definition_builder/ancestor_builder.rb +63 -60
  46. data/lib/rbs/definition_builder/method_builder.rb +45 -30
  47. data/lib/rbs/definition_builder.rb +44 -9
  48. data/lib/rbs/environment/class_entry.rb +69 -0
  49. data/lib/rbs/environment/module_entry.rb +66 -0
  50. data/lib/rbs/environment.rb +185 -154
  51. data/lib/rbs/environment_loader.rb +2 -2
  52. data/lib/rbs/errors.rb +4 -3
  53. data/lib/rbs/inline_parser/comment_association.rb +117 -0
  54. data/lib/rbs/inline_parser.rb +206 -0
  55. data/lib/rbs/location_aux.rb +35 -3
  56. data/lib/rbs/parser_aux.rb +11 -1
  57. data/lib/rbs/prototype/runtime.rb +2 -2
  58. data/lib/rbs/source.rb +99 -0
  59. data/lib/rbs/subtractor.rb +4 -3
  60. data/lib/rbs/version.rb +1 -1
  61. data/lib/rbs.rb +12 -0
  62. data/lib/rdoc/discover.rb +1 -1
  63. data/lib/rdoc_plugin/parser.rb +2 -2
  64. data/rbs.gemspec +1 -0
  65. data/sig/ancestor_builder.rbs +1 -1
  66. data/sig/annotate/formatter.rbs +2 -2
  67. data/sig/annotate/rdoc_annotater.rbs +1 -1
  68. data/sig/ast/ruby/annotations.rbs +110 -0
  69. data/sig/ast/ruby/comment_block.rbs +119 -0
  70. data/sig/ast/ruby/declarations.rbs +60 -0
  71. data/sig/ast/ruby/helpers/constant_helper.rbs +11 -0
  72. data/sig/ast/ruby/helpers/location_helper.rbs +15 -0
  73. data/sig/ast/ruby/members.rbs +72 -0
  74. data/sig/buffer.rbs +63 -5
  75. data/sig/definition.rbs +1 -0
  76. data/sig/definition_builder.rbs +1 -1
  77. data/sig/environment/class_entry.rbs +50 -0
  78. data/sig/environment/module_entry.rbs +50 -0
  79. data/sig/environment.rbs +22 -76
  80. data/sig/errors.rbs +13 -6
  81. data/sig/inline_parser/comment_association.rbs +71 -0
  82. data/sig/inline_parser.rbs +87 -0
  83. data/sig/location.rbs +32 -7
  84. data/sig/method_builder.rbs +7 -4
  85. data/sig/parser.rbs +16 -0
  86. data/sig/source.rbs +48 -0
  87. data/src/ast.c +1345 -0
  88. data/src/lexer.c +2867 -0
  89. data/src/lexer.re +151 -0
  90. data/{ext/rbs_extension → src}/lexstate.c +58 -42
  91. data/src/location.c +71 -0
  92. data/src/parser.c +3739 -0
  93. data/src/string.c +89 -0
  94. data/src/util/rbs_allocator.c +149 -0
  95. data/src/util/rbs_assert.c +19 -0
  96. data/src/util/rbs_buffer.c +54 -0
  97. data/src/util/rbs_constant_pool.c +12 -80
  98. data/src/util/rbs_encoding.c +5273 -0
  99. data/src/util/rbs_unescape.c +130 -0
  100. data/stdlib/rdoc/0/code_object.rbs +2 -2
  101. data/stdlib/rdoc/0/comment.rbs +2 -0
  102. data/stdlib/rdoc/0/options.rbs +76 -0
  103. data/stdlib/rdoc/0/rdoc.rbs +6 -4
  104. data/stdlib/rdoc/0/store.rbs +1 -1
  105. metadata +70 -17
  106. data/ext/rbs_extension/lexer.c +0 -2728
  107. data/ext/rbs_extension/lexer.re +0 -147
  108. data/ext/rbs_extension/location.h +0 -85
  109. data/ext/rbs_extension/parser.c +0 -2982
  110. data/ext/rbs_extension/parser.h +0 -18
  111. data/ext/rbs_extension/parserstate.c +0 -411
  112. data/ext/rbs_extension/parserstate.h +0 -163
  113. data/ext/rbs_extension/unescape.c +0 -32
  114. data/include/rbs/ruby_objs.h +0 -72
  115. data/src/ruby_objs.c +0 -799
@@ -1,2982 +0,0 @@
1
- #include "rbs_extension.h"
2
- #include "rbs/util/rbs_constant_pool.h"
3
-
4
- #define INTERN(str) \
5
- rbs_constant_pool_insert_constant( \
6
- RBS_GLOBAL_CONSTANT_POOL, \
7
- (const uint8_t *) str, \
8
- strlen(str) \
9
- )
10
-
11
- #define INTERN_TOKEN(parserstate, tok) \
12
- rb_intern3(\
13
- peek_token(parserstate->lexstate, tok),\
14
- token_bytes(tok),\
15
- rb_enc_get(parserstate->lexstate->string)\
16
- )
17
-
18
- #define KEYWORD_CASES \
19
- case kBOOL:\
20
- case kBOT: \
21
- case kCLASS: \
22
- case kFALSE: \
23
- case kINSTANCE: \
24
- case kINTERFACE: \
25
- case kNIL: \
26
- case kSELF: \
27
- case kSINGLETON: \
28
- case kTOP: \
29
- case kTRUE: \
30
- case kVOID: \
31
- case kTYPE: \
32
- case kUNCHECKED: \
33
- case kIN: \
34
- case kOUT: \
35
- case kEND: \
36
- case kDEF: \
37
- case kINCLUDE: \
38
- case kEXTEND: \
39
- case kPREPEND: \
40
- case kALIAS: \
41
- case kMODULE: \
42
- case kATTRREADER: \
43
- case kATTRWRITER: \
44
- case kATTRACCESSOR: \
45
- case kPUBLIC: \
46
- case kPRIVATE: \
47
- case kUNTYPED: \
48
- case kUSE: \
49
- case kAS: \
50
- case k__TODO__: \
51
- /* nop */
52
-
53
- typedef struct {
54
- VALUE required_positionals;
55
- VALUE optional_positionals;
56
- VALUE rest_positionals;
57
- VALUE trailing_positionals;
58
- VALUE required_keywords;
59
- VALUE optional_keywords;
60
- VALUE rest_keywords;
61
- } method_params;
62
-
63
- static VALUE EMPTY_ARRAY;
64
- static VALUE EMPTY_HASH;
65
-
66
- static inline void melt_array(VALUE *array) {
67
- if (*array == EMPTY_ARRAY) {
68
- *array = rb_ary_new();
69
- }
70
- }
71
-
72
- static inline void melt_hash(VALUE *hash) {
73
- if (*hash == EMPTY_HASH) {
74
- *hash = rb_hash_new();
75
- }
76
- }
77
-
78
- static bool rbs_is_untyped_params(method_params *params) {
79
- return NIL_P(params->required_positionals);
80
- }
81
-
82
- // /**
83
- // * Returns RBS::Location object of `current_token` of a parser state.
84
- // *
85
- // * @param state
86
- // * @return New RBS::Location object.
87
- // * */
88
- static VALUE rbs_location_current_token(parserstate *state) {
89
- return rbs_location_pp(
90
- state->buffer,
91
- &state->current_token.range.start,
92
- &state->current_token.range.end
93
- );
94
- }
95
-
96
- static VALUE parse_optional(parserstate *state);
97
- static VALUE parse_simple(parserstate *state);
98
-
99
- static VALUE string_of_loc(parserstate *state, position start, position end) {
100
- return rb_enc_str_new(
101
- RSTRING_PTR(state->lexstate->string) + start.byte_pos,
102
- end.byte_pos - start.byte_pos,
103
- rb_enc_get(state->lexstate->string)
104
- );
105
- }
106
-
107
- /**
108
- * Raises RuntimeError with "Unexpected error " message.
109
- * */
110
- static NORETURN(void) rbs_abort(void) {
111
- rb_raise(
112
- rb_eRuntimeError,
113
- "Unexpected error"
114
- );
115
- }
116
-
117
- NORETURN(void) raise_syntax_error(parserstate *state, token tok, const char *fmt, ...) {
118
- va_list args;
119
- va_start(args, fmt);
120
- VALUE message = rb_vsprintf(fmt, args);
121
- va_end(args);
122
-
123
- VALUE location = rbs_new_location(state->buffer, tok.range);
124
- VALUE type = rb_str_new_cstr(token_type_str(tok.type));
125
-
126
- VALUE error = rb_funcall(
127
- RBS_ParsingError,
128
- rb_intern("new"),
129
- 3,
130
- location,
131
- message,
132
- type
133
- );
134
-
135
- rb_exc_raise(error);
136
- }
137
-
138
- typedef enum {
139
- CLASS_NAME = 1,
140
- INTERFACE_NAME = 2,
141
- ALIAS_NAME = 4
142
- } TypeNameKind;
143
-
144
- void parser_advance_no_gap(parserstate *state) {
145
- if (state->current_token.range.end.byte_pos == state->next_token.range.start.byte_pos) {
146
- parser_advance(state);
147
- } else {
148
- raise_syntax_error(
149
- state,
150
- state->next_token,
151
- "unexpected token"
152
- );
153
- }
154
- }
155
-
156
- /*
157
- type_name ::= {`::`} (tUIDENT `::`)* <tXIDENT>
158
- | {(tUIDENT `::`)*} <tXIDENT>
159
- | {<tXIDENT>}
160
- */
161
- static VALUE parse_type_name(parserstate *state, TypeNameKind kind, range *rg) {
162
- VALUE absolute = Qfalse;
163
- VALUE path = EMPTY_ARRAY;
164
-
165
- if (rg) {
166
- rg->start = state->current_token.range.start;
167
- }
168
-
169
- if (state->current_token.type == pCOLON2) {
170
- absolute = Qtrue;
171
- parser_advance_no_gap(state);
172
- }
173
-
174
- while (
175
- state->current_token.type == tUIDENT
176
- && state->next_token.type == pCOLON2
177
- && state->current_token.range.end.byte_pos == state->next_token.range.start.byte_pos
178
- && state->next_token.range.end.byte_pos == state->next_token2.range.start.byte_pos
179
- ) {
180
- melt_array(&path);
181
- rb_ary_push(path, ID2SYM(INTERN_TOKEN(state, state->current_token)));
182
-
183
- parser_advance(state);
184
- parser_advance(state);
185
- }
186
-
187
- VALUE namespace = rbs_namespace(path, absolute);
188
-
189
- switch (state->current_token.type) {
190
- case tLIDENT:
191
- if (kind & ALIAS_NAME) goto success;
192
- goto error;
193
- case tULIDENT:
194
- if (kind & INTERFACE_NAME) goto success;
195
- goto error;
196
- case tUIDENT:
197
- if (kind & CLASS_NAME) goto success;
198
- goto error;
199
- default:
200
- goto error;
201
- }
202
-
203
- success: {
204
- if (rg) {
205
- rg->end = state->current_token.range.end;
206
- }
207
-
208
- return rbs_type_name(namespace, ID2SYM(INTERN_TOKEN(state, state->current_token)));
209
- }
210
-
211
- error: {
212
- VALUE ids = rb_ary_new();
213
- if (kind & ALIAS_NAME) {
214
- rb_ary_push(ids, rb_str_new_literal("alias name"));
215
- }
216
- if (kind & INTERFACE_NAME) {
217
- rb_ary_push(ids, rb_str_new_literal("interface name"));
218
- }
219
- if (kind & CLASS_NAME) {
220
- rb_ary_push(ids, rb_str_new_literal("class/module/constant name"));
221
- }
222
-
223
- VALUE string = rb_funcall(ids, rb_intern("join"), 1, rb_str_new_cstr(", "));
224
-
225
- raise_syntax_error(
226
- state,
227
- state->current_token,
228
- "expected one of %"PRIsVALUE,
229
- string
230
- );
231
- }
232
- }
233
-
234
- /*
235
- type_list ::= {} type `,` ... <`,`> eol
236
- | {} type `,` ... `,` <type> eol
237
- */
238
- static void parse_type_list(parserstate *state, enum TokenType eol, VALUE *types) {
239
- while (true) {
240
- melt_array(types);
241
- rb_ary_push(*types, parse_type(state));
242
-
243
- if (state->next_token.type == pCOMMA) {
244
- parser_advance(state);
245
-
246
- if (state->next_token.type == eol) {
247
- break;
248
- }
249
- } else {
250
- if (state->next_token.type == eol) {
251
- break;
252
- } else {
253
- raise_syntax_error(
254
- state,
255
- state->next_token,
256
- "comma delimited type list is expected"
257
- );
258
- }
259
- }
260
- }
261
- }
262
-
263
- static bool is_keyword_token(enum TokenType type) {
264
- switch (type)
265
- {
266
- case tLIDENT:
267
- case tUIDENT:
268
- case tULIDENT:
269
- case tULLIDENT:
270
- case tQIDENT:
271
- case tBANGIDENT:
272
- KEYWORD_CASES
273
- return true;
274
- default:
275
- return false;
276
- }
277
- }
278
-
279
- /*
280
- function_param ::= {} <type>
281
- | {} type <param>
282
- */
283
- static VALUE parse_function_param(parserstate *state) {
284
- range type_range;
285
- type_range.start = state->next_token.range.start;
286
- VALUE type = parse_type(state);
287
- type_range.end = state->current_token.range.end;
288
-
289
- if (state->next_token.type == pCOMMA || state->next_token.type == pRPAREN) {
290
- range param_range = type_range;
291
-
292
- VALUE location = rbs_new_location(state->buffer, param_range);
293
- rbs_loc *loc = rbs_check_location(location);
294
- rbs_loc_alloc_children(loc, 1);
295
- rbs_loc_add_optional_child(loc, INTERN("name"), NULL_RANGE);
296
-
297
- return rbs_function_param(type, Qnil, location);
298
- } else {
299
- range name_range = state->next_token.range;
300
-
301
- parser_advance(state);
302
-
303
- range param_range = {
304
- .start = type_range.start,
305
- .end = name_range.end,
306
- };
307
-
308
- if (!is_keyword_token(state->current_token.type)) {
309
- raise_syntax_error(
310
- state,
311
- state->current_token,
312
- "unexpected token for function parameter name"
313
- );
314
- }
315
-
316
- VALUE name = rb_to_symbol(rbs_unquote_string(state, state->current_token.range, 0));
317
- VALUE location = rbs_new_location(state->buffer, param_range);
318
- rbs_loc *loc = rbs_check_location(location);
319
- rbs_loc_alloc_children(loc, 1);
320
- rbs_loc_add_optional_child(loc, INTERN("name"), name_range);
321
-
322
- return rbs_function_param(type, name, location);
323
- }
324
- }
325
-
326
- static ID intern_token_start_end(parserstate *state, token start_token, token end_token) {
327
- return rb_intern3(
328
- peek_token(state->lexstate, start_token),
329
- end_token.range.end.byte_pos - start_token.range.start.byte_pos,
330
- rb_enc_get(state->lexstate->string)
331
- );
332
- }
333
-
334
- /*
335
- keyword_key ::= {} <keyword> `:`
336
- | {} keyword <`?`> `:`
337
- */
338
- static VALUE parse_keyword_key(parserstate *state) {
339
- parser_advance(state);
340
-
341
- if (state->next_token.type == pQUESTION) {
342
- VALUE key = ID2SYM(intern_token_start_end(state, state->current_token, state->next_token));
343
- parser_advance(state);
344
- return key;
345
- } else {
346
- return ID2SYM(INTERN_TOKEN(state, state->current_token));
347
- }
348
- }
349
-
350
- /*
351
- keyword ::= {} keyword `:` <function_param>
352
- */
353
- static void parse_keyword(parserstate *state, VALUE *keywords, VALUE memo) {
354
- VALUE key = parse_keyword_key(state);
355
-
356
- if (!NIL_P(rb_hash_aref(memo, key))) {
357
- raise_syntax_error(
358
- state,
359
- state->current_token,
360
- "duplicated keyword argument"
361
- );
362
- } else {
363
- rb_hash_aset(memo, key, Qtrue);
364
- }
365
-
366
- parser_advance_assert(state, pCOLON);
367
- VALUE param = parse_function_param(state);
368
-
369
- melt_hash(keywords);
370
- rb_hash_aset(*keywords, key, param);
371
-
372
- return;
373
- }
374
-
375
- /*
376
- Returns true if keyword is given.
377
-
378
- is_keyword === {} KEYWORD `:`
379
- */
380
- static bool is_keyword(parserstate *state) {
381
- if (is_keyword_token(state->next_token.type)) {
382
- if (state->next_token2.type == pCOLON && state->next_token.range.end.byte_pos == state->next_token2.range.start.byte_pos) {
383
- return true;
384
- }
385
-
386
- if (state->next_token2.type == pQUESTION
387
- && state->next_token3.type == pCOLON
388
- && state->next_token.range.end.byte_pos == state->next_token2.range.start.byte_pos
389
- && state->next_token2.range.end.byte_pos == state->next_token3.range.start.byte_pos) {
390
- return true;
391
- }
392
- }
393
-
394
- return false;
395
- }
396
-
397
- /*
398
- params ::= {} `)`
399
- | {} `?` `)` -- Untyped function params (assign params.required = nil)
400
- | <required_params> `)`
401
- | <required_params> `,` `)`
402
-
403
- required_params ::= {} function_param `,` <required_params>
404
- | {} <function_param>
405
- | {} <optional_params>
406
-
407
- optional_params ::= {} `?` function_param `,` <optional_params>
408
- | {} `?` <function_param>
409
- | {} <rest_params>
410
-
411
- rest_params ::= {} `*` function_param `,` <trailing_params>
412
- | {} `*` <function_param>
413
- | {} <trailing_params>
414
-
415
- trailing_params ::= {} function_param `,` <trailing_params>
416
- | {} <function_param>
417
- | {} <keywords>
418
-
419
- keywords ::= {} required_keyword `,` <keywords>
420
- | {} `?` optional_keyword `,` <keywords>
421
- | {} `**` function_param `,` <keywords>
422
- | {} <required_keyword>
423
- | {} `?` <optional_keyword>
424
- | {} `**` <function_param>
425
- */
426
- static void parse_params(parserstate *state, method_params *params) {
427
- if (state->next_token.type == pQUESTION && state->next_token2.type == pRPAREN) {
428
- params->required_positionals = Qnil;
429
- parser_advance(state);
430
- return;
431
- }
432
- if (state->next_token.type == pRPAREN) {
433
- return;
434
- }
435
-
436
- VALUE memo = rb_hash_new();
437
-
438
- while (true) {
439
- VALUE param;
440
-
441
- switch (state->next_token.type) {
442
- case pQUESTION:
443
- goto PARSE_OPTIONAL_PARAMS;
444
- case pSTAR:
445
- goto PARSE_REST_PARAM;
446
- case pSTAR2:
447
- goto PARSE_KEYWORDS;
448
- case pRPAREN:
449
- goto EOP;
450
-
451
- default:
452
- if (is_keyword(state)) {
453
- goto PARSE_KEYWORDS;
454
- }
455
-
456
- param = parse_function_param(state);
457
- melt_array(&params->required_positionals);
458
- rb_ary_push(params->required_positionals, param);
459
-
460
- break;
461
- }
462
-
463
- if (!parser_advance_if(state, pCOMMA)) {
464
- goto EOP;
465
- }
466
- }
467
-
468
- PARSE_OPTIONAL_PARAMS:
469
- while (true) {
470
- VALUE param;
471
-
472
- switch (state->next_token.type) {
473
- case pQUESTION:
474
- parser_advance(state);
475
-
476
- if (is_keyword(state)) {
477
- parse_keyword(state, &params->optional_keywords, memo);
478
- parser_advance_if(state, pCOMMA);
479
- goto PARSE_KEYWORDS;
480
- }
481
-
482
- param = parse_function_param(state);
483
- melt_array(&params->optional_positionals);
484
- rb_ary_push(params->optional_positionals, param);
485
-
486
- break;
487
- default:
488
- goto PARSE_REST_PARAM;
489
- }
490
-
491
- if (!parser_advance_if(state, pCOMMA)) {
492
- goto EOP;
493
- }
494
- }
495
-
496
- PARSE_REST_PARAM:
497
- if (state->next_token.type == pSTAR) {
498
- parser_advance(state);
499
- params->rest_positionals = parse_function_param(state);
500
-
501
- if (!parser_advance_if(state, pCOMMA)) {
502
- goto EOP;
503
- }
504
- }
505
- goto PARSE_TRAILING_PARAMS;
506
-
507
- PARSE_TRAILING_PARAMS:
508
- while (true) {
509
- VALUE param;
510
-
511
- switch (state->next_token.type) {
512
- case pQUESTION:
513
- goto PARSE_KEYWORDS;
514
- case pSTAR:
515
- goto EOP;
516
- case pSTAR2:
517
- goto PARSE_KEYWORDS;
518
- case pRPAREN:
519
- goto EOP;
520
-
521
- default:
522
- if (is_keyword(state)) {
523
- goto PARSE_KEYWORDS;
524
- }
525
-
526
- param = parse_function_param(state);
527
- melt_array(&params->trailing_positionals);
528
- rb_ary_push(params->trailing_positionals, param);
529
-
530
- break;
531
- }
532
-
533
- if (!parser_advance_if(state, pCOMMA)) {
534
- goto EOP;
535
- }
536
- }
537
-
538
- PARSE_KEYWORDS:
539
- while (true) {
540
- switch (state->next_token.type) {
541
- case pQUESTION:
542
- parser_advance(state);
543
- if (is_keyword(state)) {
544
- parse_keyword(state, &params->optional_keywords, memo);
545
- } else {
546
- raise_syntax_error(
547
- state,
548
- state->next_token,
549
- "optional keyword argument type is expected"
550
- );
551
- }
552
- break;
553
-
554
- case pSTAR2:
555
- parser_advance(state);
556
- params->rest_keywords = parse_function_param(state);
557
- break;
558
-
559
- case tUIDENT:
560
- case tLIDENT:
561
- case tQIDENT:
562
- case tULIDENT:
563
- case tULLIDENT:
564
- case tBANGIDENT:
565
- KEYWORD_CASES
566
- if (is_keyword(state)) {
567
- parse_keyword(state, &params->required_keywords, memo);
568
- } else {
569
- raise_syntax_error(
570
- state,
571
- state->next_token,
572
- "required keyword argument type is expected"
573
- );
574
- }
575
- break;
576
-
577
- default:
578
- goto EOP;
579
- }
580
-
581
- if (!parser_advance_if(state, pCOMMA)) {
582
- goto EOP;
583
- }
584
- }
585
-
586
- EOP:
587
- if (state->next_token.type != pRPAREN) {
588
- raise_syntax_error(
589
- state,
590
- state->next_token,
591
- "unexpected token for method type parameters"
592
- );
593
- }
594
-
595
- return;
596
- }
597
-
598
- /*
599
- optional ::= {} <simple_type>
600
- | {} simple_type <`?`>
601
- */
602
- static VALUE parse_optional(parserstate *state) {
603
- range rg;
604
- rg.start = state->next_token.range.start;
605
-
606
- VALUE type = parse_simple(state);
607
-
608
- if (state->next_token.type == pQUESTION) {
609
- parser_advance(state);
610
- rg.end = state->current_token.range.end;
611
- VALUE location = rbs_new_location(state->buffer, rg);
612
- return rbs_optional(type, location);
613
- } else {
614
- return type;
615
- }
616
- }
617
-
618
- static void initialize_method_params(method_params *params){
619
- *params = (method_params) {
620
- .required_positionals = EMPTY_ARRAY,
621
- .optional_positionals = EMPTY_ARRAY,
622
- .rest_positionals = Qnil,
623
- .trailing_positionals = EMPTY_ARRAY,
624
- .required_keywords = EMPTY_HASH,
625
- .optional_keywords = EMPTY_HASH,
626
- .rest_keywords = Qnil,
627
- };
628
- }
629
-
630
- /*
631
- self_type_binding ::= {} <>
632
- | {} `[` `self` `:` type <`]`>
633
- */
634
- static VALUE parse_self_type_binding(parserstate *state) {
635
- if (state->next_token.type == pLBRACKET) {
636
- parser_advance(state);
637
- parser_advance_assert(state, kSELF);
638
- parser_advance_assert(state, pCOLON);
639
- VALUE type = parse_type(state);
640
- parser_advance_assert(state, pRBRACKET);
641
- return type;
642
- } else {
643
- return Qnil;
644
- }
645
- }
646
-
647
- /*
648
- function ::= {} `(` params `)` self_type_binding? `{` `(` params `)` self_type_binding? `->` optional `}` `->` <optional>
649
- | {} `(` params `)` self_type_binding? `->` <optional>
650
- | {} self_type_binding? `{` `(` params `)` self_type_binding? `->` optional `}` `->` <optional>
651
- | {} self_type_binding? `{` self_type_binding `->` optional `}` `->` <optional>
652
- | {} self_type_binding? `->` <optional>
653
- */
654
- static void parse_function(parserstate *state, VALUE *function, VALUE *block, VALUE *function_self_type) {
655
- method_params params;
656
- initialize_method_params(&params);
657
-
658
- if (state->next_token.type == pLPAREN) {
659
- parser_advance(state);
660
- parse_params(state, &params);
661
- parser_advance_assert(state, pRPAREN);
662
- }
663
-
664
- // Passing NULL to function_self_type means the function itself doesn't accept self type binding. (== method type)
665
- if (function_self_type) {
666
- *function_self_type = parse_self_type_binding(state);
667
- } else {
668
- // Parsing method type. untyped_params means it cannot have a block
669
- if (rbs_is_untyped_params(&params)) {
670
- if (state->next_token.type != pARROW) {
671
- raise_syntax_error(state, state->next_token2, "A method type with untyped method parameter cannot have block");
672
- }
673
- }
674
- }
675
-
676
- VALUE required = Qtrue;
677
- if (state->next_token.type == pQUESTION && state->next_token2.type == pLBRACE) {
678
- // Optional block
679
- required = Qfalse;
680
- parser_advance(state);
681
- }
682
- if (state->next_token.type == pLBRACE) {
683
- parser_advance(state);
684
-
685
- method_params block_params;
686
- initialize_method_params(&block_params);
687
-
688
- if (state->next_token.type == pLPAREN) {
689
- parser_advance(state);
690
- parse_params(state, &block_params);
691
- parser_advance_assert(state, pRPAREN);
692
- }
693
-
694
- VALUE block_self_type = parse_self_type_binding(state);
695
-
696
- parser_advance_assert(state, pARROW);
697
- VALUE block_return_type = parse_optional(state);
698
-
699
- VALUE block_function = Qnil;
700
- if (rbs_is_untyped_params(&block_params)) {
701
- block_function = rbs_untyped_function(block_return_type);
702
- } else {
703
- block_function = rbs_function(
704
- block_params.required_positionals,
705
- block_params.optional_positionals,
706
- block_params.rest_positionals,
707
- block_params.trailing_positionals,
708
- block_params.required_keywords,
709
- block_params.optional_keywords,
710
- block_params.rest_keywords,
711
- block_return_type
712
- );
713
- }
714
-
715
- *block = rbs_block(block_function, required, block_self_type);
716
-
717
- parser_advance_assert(state, pRBRACE);
718
- }
719
-
720
- parser_advance_assert(state, pARROW);
721
- VALUE type = parse_optional(state);
722
-
723
- if (rbs_is_untyped_params(&params)) {
724
- *function = rbs_untyped_function(type);
725
- } else {
726
- *function = rbs_function(
727
- params.required_positionals,
728
- params.optional_positionals,
729
- params.rest_positionals,
730
- params.trailing_positionals,
731
- params.required_keywords,
732
- params.optional_keywords,
733
- params.rest_keywords,
734
- type
735
- );
736
- }
737
- }
738
-
739
- /*
740
- proc_type ::= {`^`} <function>
741
- */
742
- static VALUE parse_proc_type(parserstate *state) {
743
- position start = state->current_token.range.start;
744
- VALUE function = Qnil;
745
- VALUE block = Qnil;
746
- VALUE proc_self = Qnil;
747
- parse_function(state, &function, &block, &proc_self);
748
- position end = state->current_token.range.end;
749
- VALUE loc = rbs_location_pp(state->buffer, &start, &end);
750
-
751
- return rbs_proc(function, block, loc, proc_self);
752
- }
753
-
754
- static void check_key_duplication(parserstate *state, VALUE fields, VALUE key) {
755
- if (!NIL_P(rb_hash_aref(fields, key))) {
756
- raise_syntax_error(
757
- state,
758
- state->current_token,
759
- "duplicated record key"
760
- );
761
- }
762
- }
763
-
764
- /**
765
- * ... `{` ... `}` ...
766
- * > >
767
- * */
768
- /*
769
- record_attributes ::= {`{`} record_attribute... <record_attribute> `}`
770
-
771
- record_attribute ::= {} keyword_token `:` <type>
772
- | {} literal_type `=>` <type>
773
- */
774
- static VALUE parse_record_attributes(parserstate *state) {
775
- VALUE fields = rb_hash_new();
776
-
777
- if (state->next_token.type == pRBRACE) {
778
- return fields;
779
- }
780
-
781
- while (true) {
782
- VALUE key, type,
783
- value = rb_ary_new(),
784
- required = Qtrue;
785
-
786
- if (state->next_token.type == pQUESTION) {
787
- // { ?foo: type } syntax
788
- required = Qfalse;
789
- parser_advance(state);
790
- }
791
-
792
- if (is_keyword(state)) {
793
- // { foo: type } syntax
794
- key = parse_keyword_key(state);
795
- check_key_duplication(state, fields, key);
796
- parser_advance_assert(state, pCOLON);
797
- } else {
798
- // { key => type } syntax
799
- switch (state->next_token.type) {
800
- case tSYMBOL:
801
- case tSQSYMBOL:
802
- case tDQSYMBOL:
803
- case tSQSTRING:
804
- case tDQSTRING:
805
- case tINTEGER:
806
- case kTRUE:
807
- case kFALSE: {
808
- key = rb_funcall(parse_simple(state), rb_intern("literal"), 0);
809
- break;
810
- }
811
- default:
812
- raise_syntax_error(
813
- state,
814
- state->next_token,
815
- "unexpected record key token"
816
- );
817
- }
818
- check_key_duplication(state, fields, key);
819
- parser_advance_assert(state, pFATARROW);
820
- }
821
- type = parse_type(state);
822
- rb_ary_push(value, type);
823
- rb_ary_push(value, required);
824
- rb_hash_aset(fields, key, value);
825
-
826
- if (parser_advance_if(state, pCOMMA)) {
827
- if (state->next_token.type == pRBRACE) {
828
- break;
829
- }
830
- } else {
831
- break;
832
- }
833
- }
834
- return fields;
835
- }
836
-
837
- /*
838
- symbol ::= {<tSYMBOL>}
839
- */
840
- static VALUE parse_symbol(parserstate *state) {
841
- VALUE string = state->lexstate->string;
842
- rb_encoding *enc = rb_enc_get(string);
843
-
844
- int offset_bytes = rb_enc_codelen(':', enc);
845
- int bytes = token_bytes(state->current_token) - offset_bytes;
846
-
847
- VALUE literal;
848
-
849
- switch (state->current_token.type)
850
- {
851
- case tSYMBOL: {
852
- char *buffer = peek_token(state->lexstate, state->current_token);
853
- literal = ID2SYM(rb_intern3(buffer+offset_bytes, bytes, enc));
854
- break;
855
- }
856
- case tDQSYMBOL:
857
- case tSQSYMBOL: {
858
- literal = rb_funcall(
859
- rbs_unquote_string(state, state->current_token.range, offset_bytes),
860
- rb_intern("to_sym"),
861
- 0
862
- );
863
- break;
864
- }
865
- default:
866
- rbs_abort();
867
- }
868
-
869
- return rbs_literal(
870
- literal,
871
- rbs_location_current_token(state)
872
- );
873
- }
874
-
875
- /*
876
- instance_type ::= {type_name} <type_args>
877
-
878
- type_args ::= {} <> /empty/
879
- | {} `[` type_list <`]`>
880
- */
881
- static VALUE parse_instance_type(parserstate *state, bool parse_alias) {
882
- TypeNameKind expected_kind = INTERFACE_NAME | CLASS_NAME;
883
- if (parse_alias) {
884
- expected_kind |= ALIAS_NAME;
885
- }
886
-
887
- range name_range;
888
- VALUE typename = parse_type_name(state, expected_kind, &name_range);
889
- VALUE types = EMPTY_ARRAY;
890
-
891
- TypeNameKind kind;
892
- if (state->current_token.type == tUIDENT) {
893
- kind = CLASS_NAME;
894
- } else if (state->current_token.type == tULIDENT) {
895
- kind = INTERFACE_NAME;
896
- } else if (state->current_token.type == tLIDENT) {
897
- kind = ALIAS_NAME;
898
- } else {
899
- rbs_abort();
900
- }
901
-
902
- range args_range;
903
- if (state->next_token.type == pLBRACKET) {
904
- parser_advance(state);
905
- args_range.start = state->current_token.range.start;
906
- parse_type_list(state, pRBRACKET, &types);
907
- parser_advance_assert(state, pRBRACKET);
908
- args_range.end = state->current_token.range.end;
909
- } else {
910
- args_range = NULL_RANGE;
911
- }
912
-
913
- range type_range = {
914
- .start = name_range.start,
915
- .end = nonnull_pos_or(args_range.end, name_range.end),
916
- };
917
-
918
- VALUE location = rbs_new_location(state->buffer, type_range);
919
- rbs_loc *loc = rbs_check_location(location);
920
- rbs_loc_alloc_children(loc, 2);
921
- rbs_loc_add_required_child(loc, INTERN("name"), name_range);
922
- rbs_loc_add_optional_child(loc, INTERN("args"), args_range);
923
-
924
- if (kind == CLASS_NAME) {
925
- return rbs_class_instance(typename, types, location);
926
- } else if (kind == INTERFACE_NAME) {
927
- return rbs_interface(typename, types, location);
928
- } else if (kind == ALIAS_NAME) {
929
- return rbs_alias(typename, types, location);
930
- } else {
931
- return Qnil;
932
- }
933
- }
934
-
935
- /*
936
- singleton_type ::= {`singleton`} `(` type_name <`)`>
937
- */
938
- static VALUE parse_singleton_type(parserstate *state) {
939
- parser_assert(state, kSINGLETON);
940
-
941
- range type_range;
942
- type_range.start = state->current_token.range.start;
943
- parser_advance_assert(state, pLPAREN);
944
- parser_advance(state);
945
-
946
- range name_range;
947
- VALUE typename = parse_type_name(state, CLASS_NAME, &name_range);
948
-
949
- parser_advance_assert(state, pRPAREN);
950
- type_range.end = state->current_token.range.end;
951
-
952
- VALUE location = rbs_new_location(state->buffer, type_range);
953
- rbs_loc *loc = rbs_check_location(location);
954
- rbs_loc_alloc_children(loc, 1);
955
- rbs_loc_add_required_child(loc, INTERN("name"), name_range);
956
-
957
- return rbs_class_singleton(typename, location);
958
- }
959
-
960
- /*
961
- simple ::= {} `(` type <`)`>
962
- | {} <base type>
963
- | {} <type_name>
964
- | {} class_instance `[` type_list <`]`>
965
- | {} `singleton` `(` type_name <`)`>
966
- | {} `[` type_list <`]`>
967
- | {} `{` record_attributes <`}`>
968
- | {} `^` <function>
969
- */
970
- static VALUE parse_simple(parserstate *state) {
971
- parser_advance(state);
972
-
973
- switch (state->current_token.type) {
974
- case pLPAREN: {
975
- VALUE type = parse_type(state);
976
- parser_advance_assert(state, pRPAREN);
977
- return type;
978
- }
979
- case kBOOL: {
980
- return rbs_bases_bool(rbs_location_current_token(state));
981
- }
982
- case kBOT: {
983
- return rbs_bases_bottom(rbs_location_current_token(state));
984
- }
985
- case kCLASS: {
986
- return rbs_bases_class(rbs_location_current_token(state));
987
- }
988
- case kINSTANCE: {
989
- return rbs_bases_instance(rbs_location_current_token(state));
990
- }
991
- case kNIL: {
992
- return rbs_bases_nil(rbs_location_current_token(state));
993
- }
994
- case kSELF: {
995
- return rbs_bases_self(rbs_location_current_token(state));
996
- }
997
- case kTOP: {
998
- return rbs_bases_top(rbs_location_current_token(state));
999
- }
1000
- case kVOID: {
1001
- return rbs_bases_void(rbs_location_current_token(state));
1002
- }
1003
- case kUNTYPED: {
1004
- return rbs_bases_any(false, rbs_location_current_token(state));
1005
- }
1006
- case k__TODO__: {
1007
- return rbs_bases_any(true, rbs_location_current_token(state));
1008
- }
1009
- case tINTEGER: {
1010
- VALUE literal = rb_funcall(
1011
- string_of_loc(state, state->current_token.range.start, state->current_token.range.end),
1012
- rb_intern("to_i"),
1013
- 0
1014
- );
1015
- return rbs_literal(
1016
- literal,
1017
- rbs_location_current_token(state)
1018
- );
1019
- }
1020
- case kTRUE: {
1021
- return rbs_literal(Qtrue, rbs_location_current_token(state));
1022
- }
1023
- case kFALSE: {
1024
- return rbs_literal(Qfalse, rbs_location_current_token(state));
1025
- }
1026
- case tSQSTRING:
1027
- case tDQSTRING: {
1028
- VALUE literal = rbs_unquote_string(state, state->current_token.range, 0);
1029
- return rbs_literal(
1030
- literal,
1031
- rbs_location_current_token(state)
1032
- );
1033
- }
1034
- case tSYMBOL:
1035
- case tSQSYMBOL:
1036
- case tDQSYMBOL: {
1037
- return parse_symbol(state);
1038
- }
1039
- case tUIDENT: {
1040
- const char *name_str = peek_token(state->lexstate, state->current_token);
1041
- size_t name_len = token_bytes(state->current_token);
1042
-
1043
- rbs_constant_id_t name = rbs_constant_pool_find(&state->constant_pool, (const uint8_t *) name_str, name_len);
1044
-
1045
- if (parser_typevar_member(state, name)) {
1046
- ID name = rb_intern3(name_str, name_len, rb_enc_get(state->lexstate->string));
1047
- return rbs_variable(ID2SYM(name), rbs_location_current_token(state));
1048
- }
1049
- // fallthrough for type name
1050
- }
1051
- case tULIDENT: // fallthrough
1052
- case tLIDENT: // fallthrough
1053
- case pCOLON2: {
1054
- return parse_instance_type(state, true);
1055
- }
1056
- case kSINGLETON: {
1057
- return parse_singleton_type(state);
1058
- }
1059
- case pLBRACKET: {
1060
- range rg;
1061
- rg.start = state->current_token.range.start;
1062
- VALUE types = EMPTY_ARRAY;
1063
- if (state->next_token.type != pRBRACKET) {
1064
- parse_type_list(state, pRBRACKET, &types);
1065
- }
1066
- parser_advance_assert(state, pRBRACKET);
1067
- rg.end = state->current_token.range.end;
1068
-
1069
- return rbs_tuple(types, rbs_new_location(state->buffer, rg));
1070
- }
1071
- case pAREF_OPR: {
1072
- return rbs_tuple(EMPTY_ARRAY, rbs_new_location(state->buffer, state->current_token.range));
1073
- }
1074
- case pLBRACE: {
1075
- position start = state->current_token.range.start;
1076
- VALUE fields = parse_record_attributes(state);
1077
- parser_advance_assert(state, pRBRACE);
1078
- position end = state->current_token.range.end;
1079
- VALUE location = rbs_location_pp(state->buffer, &start, &end);
1080
- return rbs_record(fields, location);
1081
- }
1082
- case pHAT: {
1083
- return parse_proc_type(state);
1084
- }
1085
- default:
1086
- raise_syntax_error(
1087
- state,
1088
- state->current_token,
1089
- "unexpected token for simple type"
1090
- );
1091
- }
1092
- }
1093
-
1094
- /*
1095
- intersection ::= {} optional `&` ... '&' <optional>
1096
- | {} <optional>
1097
- */
1098
- static VALUE parse_intersection(parserstate *state) {
1099
- position start = state->next_token.range.start;
1100
- VALUE type = parse_optional(state);
1101
- if (state->next_token.type != pAMP) {
1102
- return type;
1103
- }
1104
-
1105
- VALUE intersection_types = rb_ary_new();
1106
- rb_ary_push(intersection_types, type);
1107
- while (state->next_token.type == pAMP) {
1108
- parser_advance(state);
1109
- rb_ary_push(intersection_types, parse_optional(state));
1110
- }
1111
- range rg = (range) {
1112
- .start = start,
1113
- .end = state->current_token.range.end,
1114
- };
1115
- VALUE location = rbs_new_location(state->buffer, rg);
1116
- return rbs_intersection(intersection_types, location);
1117
- }
1118
-
1119
- /*
1120
- union ::= {} intersection '|' ... '|' <intersection>
1121
- | {} <intersection>
1122
- */
1123
- VALUE parse_type(parserstate *state) {
1124
- position start = state->next_token.range.start;
1125
- VALUE type = parse_intersection(state);
1126
- if (state->next_token.type != pBAR) {
1127
- return type;
1128
- }
1129
-
1130
- VALUE union_types = rb_ary_new();
1131
- rb_ary_push(union_types, type);
1132
- while (state->next_token.type == pBAR) {
1133
- parser_advance(state);
1134
- rb_ary_push(union_types, parse_intersection(state));
1135
- }
1136
- range rg = (range) {
1137
- .start = start,
1138
- .end = state->current_token.range.end,
1139
- };
1140
- VALUE location = rbs_new_location(state->buffer, rg);
1141
- return rbs_union(union_types, location);
1142
- }
1143
-
1144
- /*
1145
- type_params ::= {} `[` type_param `,` ... <`]`>
1146
- | {<>}
1147
-
1148
- type_param ::= kUNCHECKED? (kIN|kOUT|) tUIDENT upper_bound? default_type? (module_type_params == true)
1149
-
1150
- type_param ::= tUIDENT upper_bound? default_type? (module_type_params == false)
1151
- */
1152
- static VALUE parse_type_params(parserstate *state, range *rg, bool module_type_params) {
1153
- VALUE params = EMPTY_ARRAY;
1154
-
1155
- bool required_param_allowed = true;
1156
-
1157
- if (state->next_token.type == pLBRACKET) {
1158
- parser_advance(state);
1159
-
1160
- rg->start = state->current_token.range.start;
1161
-
1162
- while (true) {
1163
- VALUE unchecked = Qfalse;
1164
- VALUE variance = ID2SYM(rb_intern("invariant"));
1165
- VALUE upper_bound = Qnil;
1166
- VALUE default_type = Qnil;
1167
-
1168
- range param_range;
1169
- param_range.start = state->next_token.range.start;
1170
-
1171
- range variance_range = NULL_RANGE;
1172
- range unchecked_range = NULL_RANGE;
1173
- if (module_type_params) {
1174
- if (state->next_token.type == kUNCHECKED) {
1175
- unchecked = Qtrue;
1176
- parser_advance(state);
1177
- unchecked_range = state->current_token.range;
1178
- }
1179
-
1180
- if (state->next_token.type == kIN || state->next_token.type == kOUT) {
1181
- switch (state->next_token.type) {
1182
- case kIN:
1183
- variance = ID2SYM(rb_intern("contravariant"));
1184
- break;
1185
- case kOUT:
1186
- variance = ID2SYM(rb_intern("covariant"));
1187
- break;
1188
- default:
1189
- rbs_abort();
1190
- }
1191
-
1192
- parser_advance(state);
1193
- variance_range = state->current_token.range;
1194
- }
1195
- }
1196
-
1197
- parser_advance_assert(state, tUIDENT);
1198
- range name_range = state->current_token.range;
1199
-
1200
- rbs_constant_id_t id = rbs_constant_pool_insert_shared(
1201
- &state->constant_pool,
1202
- (const uint8_t *) peek_token(state->lexstate, state->current_token),
1203
- token_bytes(state->current_token)
1204
- );
1205
-
1206
- VALUE name = ID2SYM(INTERN_TOKEN(state, state->current_token));
1207
-
1208
- parser_insert_typevar(state, id);
1209
-
1210
- range upper_bound_range = NULL_RANGE;
1211
- if (state->next_token.type == pLT) {
1212
- parser_advance(state);
1213
- upper_bound_range.start = state->current_token.range.start;
1214
- upper_bound = parse_type(state);
1215
- upper_bound_range.end = state->current_token.range.end;
1216
- }
1217
-
1218
- range default_type_range = NULL_RANGE;
1219
- if (module_type_params) {
1220
- if (state->next_token.type == pEQ) {
1221
- parser_advance(state);
1222
-
1223
- default_type_range.start = state->current_token.range.start;
1224
- default_type = parse_type(state);
1225
- default_type_range.end = state->current_token.range.end;
1226
-
1227
- required_param_allowed = false;
1228
- } else {
1229
- if (!required_param_allowed) {
1230
- raise_syntax_error(
1231
- state,
1232
- state->current_token,
1233
- "required type parameter is not allowed after optional type parameter"
1234
- );
1235
- }
1236
- }
1237
- }
1238
-
1239
- param_range.end = state->current_token.range.end;
1240
-
1241
- VALUE location = rbs_new_location(state->buffer, param_range);
1242
- rbs_loc *loc = rbs_check_location(location);
1243
- rbs_loc_alloc_children(loc, 5);
1244
- rbs_loc_add_required_child(loc, INTERN("name"), name_range);
1245
- rbs_loc_add_optional_child(loc, INTERN("variance"), variance_range);
1246
- rbs_loc_add_optional_child(loc, INTERN("unchecked"), unchecked_range);
1247
- rbs_loc_add_optional_child(loc, INTERN("upper_bound"), upper_bound_range);
1248
- rbs_loc_add_optional_child(loc, INTERN("default"), default_type_range);
1249
-
1250
- VALUE param = rbs_ast_type_param(name, variance, upper_bound, default_type, unchecked, location);
1251
-
1252
- melt_array(&params);
1253
- rb_ary_push(params, param);
1254
-
1255
- if (state->next_token.type == pCOMMA) {
1256
- parser_advance(state);
1257
- }
1258
-
1259
- if (state->next_token.type == pRBRACKET) {
1260
- break;
1261
- }
1262
- }
1263
-
1264
- parser_advance_assert(state, pRBRACKET);
1265
- rg->end = state->current_token.range.end;
1266
- } else {
1267
- *rg = NULL_RANGE;
1268
- }
1269
-
1270
- rb_funcall(
1271
- RBS_AST_TypeParam,
1272
- rb_intern("resolve_variables"),
1273
- 1,
1274
- params
1275
- );
1276
-
1277
- return params;
1278
- }
1279
-
1280
- /*
1281
- method_type ::= {} type_params <function>
1282
- */
1283
- VALUE parse_method_type(parserstate *state) {
1284
- parser_push_typevar_table(state, false);
1285
-
1286
- range rg;
1287
- rg.start = state->next_token.range.start;
1288
-
1289
- range params_range = NULL_RANGE;
1290
- VALUE type_params = parse_type_params(state, &params_range, false);
1291
-
1292
- range type_range;
1293
- type_range.start = state->next_token.range.start;
1294
-
1295
- VALUE function = Qnil;
1296
- VALUE block = Qnil;
1297
- parse_function(state, &function, &block, NULL);
1298
-
1299
- rg.end = state->current_token.range.end;
1300
- type_range.end = rg.end;
1301
-
1302
- parser_pop_typevar_table(state);
1303
-
1304
- VALUE location = rbs_new_location(state->buffer, rg);
1305
- rbs_loc *loc = rbs_check_location(location);
1306
- rbs_loc_alloc_children(loc, 2);
1307
- rbs_loc_add_required_child(loc, INTERN("type"), type_range);
1308
- rbs_loc_add_optional_child(loc, INTERN("type_params"), params_range);
1309
-
1310
- return rbs_method_type(
1311
- type_params,
1312
- function,
1313
- block,
1314
- location
1315
- );
1316
- }
1317
-
1318
- /*
1319
- global_decl ::= {tGIDENT} `:` <type>
1320
- */
1321
- static VALUE parse_global_decl(parserstate *state, VALUE annotations) {
1322
- range decl_range;
1323
- decl_range.start = state->current_token.range.start;
1324
-
1325
- VALUE comment = get_comment(state, decl_range.start.line);
1326
- range name_range = state->current_token.range;
1327
- VALUE typename = ID2SYM(INTERN_TOKEN(state, state->current_token));
1328
-
1329
- parser_advance_assert(state, pCOLON);
1330
- range colon_range = state->current_token.range;
1331
-
1332
- VALUE type = parse_type(state);
1333
- decl_range.end = state->current_token.range.end;
1334
-
1335
- VALUE location = rbs_new_location(state->buffer, decl_range);
1336
- rbs_loc *loc = rbs_check_location(location);
1337
- rbs_loc_alloc_children(loc, 2);
1338
- rbs_loc_add_required_child(loc, INTERN("name"), name_range);
1339
- rbs_loc_add_required_child(loc, INTERN("colon"), colon_range);
1340
-
1341
- return rbs_ast_decl_global(typename, type, location, comment, annotations);
1342
- }
1343
-
1344
- /*
1345
- const_decl ::= {const_name} `:` <type>
1346
- */
1347
- static VALUE parse_const_decl(parserstate *state, VALUE annotations) {
1348
- range decl_range;
1349
-
1350
- decl_range.start = state->current_token.range.start;
1351
- VALUE comment = get_comment(state, decl_range.start.line);
1352
-
1353
- range name_range;
1354
- VALUE typename = parse_type_name(state, CLASS_NAME, &name_range);
1355
-
1356
- parser_advance_assert(state, pCOLON);
1357
- range colon_range = state->current_token.range;
1358
-
1359
- VALUE type = parse_type(state);
1360
- decl_range.end = state->current_token.range.end;
1361
-
1362
- VALUE location = rbs_new_location(state->buffer, decl_range);
1363
- rbs_loc *loc = rbs_check_location(location);
1364
- rbs_loc_alloc_children(loc, 2);
1365
- rbs_loc_add_required_child(loc, INTERN("name"), name_range);
1366
- rbs_loc_add_required_child(loc, INTERN("colon"), colon_range);
1367
-
1368
- return rbs_ast_decl_constant(typename, type, location, comment, annotations);
1369
- }
1370
-
1371
- /*
1372
- type_decl ::= {kTYPE} alias_name `=` <type>
1373
- */
1374
- static VALUE parse_type_decl(parserstate *state, position comment_pos, VALUE annotations) {
1375
- parser_push_typevar_table(state, true);
1376
-
1377
- range decl_range;
1378
- decl_range.start = state->current_token.range.start;
1379
- comment_pos = nonnull_pos_or(comment_pos, decl_range.start);
1380
-
1381
- range keyword_range = state->current_token.range;
1382
-
1383
- parser_advance(state);
1384
-
1385
- range name_range;
1386
- VALUE typename = parse_type_name(state, ALIAS_NAME, &name_range);
1387
-
1388
- range params_range;
1389
- VALUE type_params = parse_type_params(state, &params_range, true);
1390
-
1391
- parser_advance_assert(state, pEQ);
1392
- range eq_range = state->current_token.range;
1393
-
1394
- VALUE type = parse_type(state);
1395
- decl_range.end = state->current_token.range.end;
1396
-
1397
- VALUE location = rbs_new_location(state->buffer, decl_range);
1398
- rbs_loc *loc = rbs_check_location(location);
1399
- rbs_loc_alloc_children(loc, 4);
1400
- rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range);
1401
- rbs_loc_add_required_child(loc, INTERN("name"), name_range);
1402
- rbs_loc_add_optional_child(loc, INTERN("type_params"), params_range);
1403
- rbs_loc_add_required_child(loc, INTERN("eq"), eq_range);
1404
-
1405
- parser_pop_typevar_table(state);
1406
-
1407
- return rbs_ast_decl_type_alias(
1408
- typename,
1409
- type_params,
1410
- type,
1411
- annotations,
1412
- location,
1413
- get_comment(state, comment_pos.line)
1414
- );
1415
- }
1416
-
1417
- /*
1418
- annotation ::= {<tANNOTATION>}
1419
- */
1420
- static VALUE parse_annotation(parserstate *state) {
1421
- VALUE content = rb_funcall(state->buffer, rb_intern("content"), 0);
1422
- rb_encoding *enc = rb_enc_get(content);
1423
-
1424
- range rg = state->current_token.range;
1425
-
1426
- int offset_bytes = rb_enc_codelen('%', enc) + rb_enc_codelen('a', enc);
1427
-
1428
- unsigned int open_char = rb_enc_mbc_to_codepoint(
1429
- RSTRING_PTR(state->lexstate->string) + rg.start.byte_pos + offset_bytes,
1430
- RSTRING_END(state->lexstate->string),
1431
- enc
1432
- );
1433
-
1434
- unsigned int close_char;
1435
-
1436
- switch (open_char) {
1437
- case '{':
1438
- close_char = '}';
1439
- break;
1440
- case '(':
1441
- close_char = ')';
1442
- break;
1443
- case '[':
1444
- close_char = ']';
1445
- break;
1446
- case '<':
1447
- close_char = '>';
1448
- break;
1449
- case '|':
1450
- close_char = '|';
1451
- break;
1452
- default:
1453
- rbs_abort();
1454
- }
1455
-
1456
- int open_bytes = rb_enc_codelen(open_char, enc);
1457
- int close_bytes = rb_enc_codelen(close_char, enc);
1458
-
1459
- char *buffer = RSTRING_PTR(state->lexstate->string) + rg.start.byte_pos + offset_bytes + open_bytes;
1460
- VALUE string = rb_enc_str_new(
1461
- buffer,
1462
- rg.end.byte_pos - rg.start.byte_pos - offset_bytes - open_bytes - close_bytes,
1463
- enc
1464
- );
1465
- rb_funcall(string, rb_intern("strip!"), 0);
1466
-
1467
- VALUE location = rbs_location_current_token(state);
1468
-
1469
- return rbs_ast_annotation(string, location);
1470
- }
1471
-
1472
- /*
1473
- annotations ::= {} annotation ... <annotation>
1474
- | {<>}
1475
- */
1476
- static void parse_annotations(parserstate *state, VALUE *annotations, position *annot_pos) {
1477
- *annot_pos = NullPosition;
1478
-
1479
- while (true) {
1480
- if (state->next_token.type == tANNOTATION) {
1481
- parser_advance(state);
1482
-
1483
- if (null_position_p((*annot_pos))) {
1484
- *annot_pos = state->current_token.range.start;
1485
- }
1486
-
1487
- melt_array(annotations);
1488
- rb_ary_push(*annotations, parse_annotation(state));
1489
- } else {
1490
- break;
1491
- }
1492
- }
1493
- }
1494
-
1495
- /*
1496
- method_name ::= {} <IDENT | keyword>
1497
- | {} (IDENT | keyword)~<`?`>
1498
- */
1499
- static VALUE parse_method_name(parserstate *state, range *range) {
1500
- parser_advance(state);
1501
-
1502
- switch (state->current_token.type)
1503
- {
1504
- case tUIDENT:
1505
- case tLIDENT:
1506
- case tULIDENT:
1507
- case tULLIDENT:
1508
- KEYWORD_CASES
1509
- if (state->next_token.type == pQUESTION && state->current_token.range.end.byte_pos == state->next_token.range.start.byte_pos) {
1510
- range->start = state->current_token.range.start;
1511
- range->end = state->next_token.range.end;
1512
- parser_advance(state);
1513
-
1514
- ID id = rb_intern3(
1515
- RSTRING_PTR(state->lexstate->string) + range->start.byte_pos,
1516
- range->end.byte_pos - range->start.byte_pos,
1517
- rb_enc_get(state->lexstate->string)
1518
- );
1519
-
1520
- return ID2SYM(id);
1521
- } else {
1522
- *range = state->current_token.range;
1523
- return ID2SYM(INTERN_TOKEN(state, state->current_token));
1524
- }
1525
-
1526
- case tBANGIDENT:
1527
- case tEQIDENT:
1528
- *range = state->current_token.range;
1529
- return ID2SYM(INTERN_TOKEN(state, state->current_token));
1530
-
1531
- case tQIDENT: {
1532
- return rb_to_symbol(rbs_unquote_string(state, state->current_token.range, 0));
1533
- }
1534
-
1535
- case pBAR:
1536
- case pHAT:
1537
- case pAMP:
1538
- case pSTAR:
1539
- case pSTAR2:
1540
- case pLT:
1541
- case pAREF_OPR:
1542
- case tOPERATOR:
1543
- *range = state->current_token.range;
1544
- return ID2SYM(INTERN_TOKEN(state, state->current_token));
1545
-
1546
- default:
1547
- raise_syntax_error(
1548
- state,
1549
- state->current_token,
1550
- "unexpected token for method name"
1551
- );
1552
- }
1553
- }
1554
-
1555
- typedef enum {
1556
- INSTANCE_KIND,
1557
- SINGLETON_KIND,
1558
- INSTANCE_SINGLETON_KIND
1559
- } InstanceSingletonKind;
1560
-
1561
- /*
1562
- instance_singleton_kind ::= {<>}
1563
- | {} kSELF <`.`>
1564
- | {} kSELF~`?` <`.`>
1565
-
1566
- @param allow_selfq `true` to accept `self?` kind.
1567
- */
1568
- static InstanceSingletonKind parse_instance_singleton_kind(parserstate *state, bool allow_selfq, range *rg) {
1569
- InstanceSingletonKind kind = INSTANCE_KIND;
1570
-
1571
- if (state->next_token.type == kSELF) {
1572
- range self_range = state->next_token.range;
1573
-
1574
- if (state->next_token2.type == pDOT) {
1575
- parser_advance(state);
1576
- parser_advance(state);
1577
- kind = SINGLETON_KIND;
1578
- } else if (
1579
- state->next_token2.type == pQUESTION
1580
- && state->next_token.range.end.char_pos == state->next_token2.range.start.char_pos
1581
- && state->next_token3.type == pDOT
1582
- && allow_selfq) {
1583
- parser_advance(state);
1584
- parser_advance(state);
1585
- parser_advance(state);
1586
- kind = INSTANCE_SINGLETON_KIND;
1587
- }
1588
-
1589
- *rg = (range) {
1590
- .start = self_range.start,
1591
- .end = state->current_token.range.end,
1592
- };
1593
- } else {
1594
- *rg = NULL_RANGE;
1595
- }
1596
-
1597
- return kind;
1598
- }
1599
-
1600
- /**
1601
- * def_member ::= {kDEF} method_name `:` <method_types>
1602
- * | {kPRIVATE} kDEF method_name `:` <method_types>
1603
- * | {kPUBLIC} kDEF method_name `:` <method_types>
1604
- *
1605
- * method_types ::= {} <method_type>
1606
- * | {} <`...`>
1607
- * | {} method_type `|` <method_types>
1608
- *
1609
- * @param instance_only `true` to reject singleton method definition.
1610
- * @param accept_overload `true` to accept overloading (...) definition.
1611
- * */
1612
- static VALUE parse_member_def(parserstate *state, bool instance_only, bool accept_overload, position comment_pos, VALUE annotations) {
1613
- range member_range;
1614
- member_range.start = state->current_token.range.start;
1615
- comment_pos = nonnull_pos_or(comment_pos, member_range.start);
1616
-
1617
- VALUE comment = get_comment(state, comment_pos.line);
1618
-
1619
- range visibility_range;
1620
- VALUE visibility;
1621
- switch (state->current_token.type)
1622
- {
1623
- case kPRIVATE: {
1624
- visibility_range = state->current_token.range;
1625
- visibility = ID2SYM(rb_intern("private"));
1626
- member_range.start = visibility_range.start;
1627
- parser_advance(state);
1628
- break;
1629
- }
1630
- case kPUBLIC: {
1631
- visibility_range = state->current_token.range;
1632
- visibility = ID2SYM(rb_intern("public"));
1633
- member_range.start = visibility_range.start;
1634
- parser_advance(state);
1635
- break;
1636
- }
1637
- default:
1638
- visibility_range = NULL_RANGE;
1639
- visibility = Qnil;
1640
- break;
1641
- }
1642
-
1643
- range keyword_range = state->current_token.range;
1644
-
1645
- range kind_range;
1646
- InstanceSingletonKind kind;
1647
- if (instance_only) {
1648
- kind_range = NULL_RANGE;
1649
- kind = INSTANCE_KIND;
1650
- } else {
1651
- kind = parse_instance_singleton_kind(state, NIL_P(visibility), &kind_range);
1652
- }
1653
-
1654
- range name_range;
1655
- VALUE name = parse_method_name(state, &name_range);
1656
- VALUE overloads = rb_ary_new();
1657
- VALUE overloading = Qfalse;
1658
-
1659
- if (state->next_token.type == pDOT && RB_SYM2ID(name) == rb_intern("self?")) {
1660
- raise_syntax_error(
1661
- state,
1662
- state->next_token,
1663
- "`self?` method cannot have visibility"
1664
- );
1665
- } else {
1666
- parser_advance_assert(state, pCOLON);
1667
- }
1668
-
1669
- parser_push_typevar_table(state, kind != INSTANCE_KIND);
1670
-
1671
- range overloading_range = NULL_RANGE;
1672
- bool loop = true;
1673
- while (loop) {
1674
- VALUE annotations = EMPTY_ARRAY;
1675
- position overload_annot_pos = NullPosition;
1676
-
1677
- if (state->next_token.type == tANNOTATION) {
1678
- parse_annotations(state, &annotations, &overload_annot_pos);
1679
- }
1680
-
1681
- switch (state->next_token.type) {
1682
- case pLPAREN:
1683
- case pARROW:
1684
- case pLBRACE:
1685
- case pLBRACKET:
1686
- case pQUESTION:
1687
- {
1688
- VALUE method_type = parse_method_type(state);
1689
- rb_ary_push(overloads, rbs_ast_members_method_definition_overload(annotations, method_type));
1690
- member_range.end = state->current_token.range.end;
1691
- break;
1692
- }
1693
-
1694
- case pDOT3:
1695
- if (accept_overload) {
1696
- overloading = Qtrue;
1697
- parser_advance(state);
1698
- loop = false;
1699
- overloading_range = state->current_token.range;
1700
- member_range.end = overloading_range.end;
1701
- break;
1702
- } else {
1703
- raise_syntax_error(
1704
- state,
1705
- state->next_token,
1706
- "unexpected overloading method definition"
1707
- );
1708
- }
1709
-
1710
- default:
1711
- raise_syntax_error(
1712
- state,
1713
- state->next_token,
1714
- "unexpected token for method type"
1715
- );
1716
- }
1717
-
1718
- if (state->next_token.type == pBAR) {
1719
- parser_advance(state);
1720
- } else {
1721
- loop = false;
1722
- }
1723
- }
1724
-
1725
- parser_pop_typevar_table(state);
1726
-
1727
- VALUE k;
1728
- switch (kind) {
1729
- case INSTANCE_KIND:
1730
- k = ID2SYM(rb_intern("instance"));
1731
- break;
1732
- case SINGLETON_KIND:
1733
- k = ID2SYM(rb_intern("singleton"));
1734
- break;
1735
- case INSTANCE_SINGLETON_KIND:
1736
- k = ID2SYM(rb_intern("singleton_instance"));
1737
- break;
1738
- default:
1739
- rbs_abort();
1740
- }
1741
-
1742
- VALUE location = rbs_new_location(state->buffer, member_range);
1743
- rbs_loc *loc = rbs_check_location(location);
1744
- rbs_loc_alloc_children(loc, 5);
1745
- rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range);
1746
- rbs_loc_add_required_child(loc, INTERN("name"), name_range);
1747
- rbs_loc_add_optional_child(loc, INTERN("kind"), kind_range);
1748
- rbs_loc_add_optional_child(loc, INTERN("overloading"), overloading_range);
1749
- rbs_loc_add_optional_child(loc, INTERN("visibility"), visibility_range);
1750
-
1751
- return rbs_ast_members_method_definition(
1752
- name,
1753
- k,
1754
- overloads,
1755
- annotations,
1756
- location,
1757
- comment,
1758
- overloading,
1759
- visibility
1760
- );
1761
- }
1762
-
1763
- /**
1764
- * class_instance_name ::= {} <class_name>
1765
- * | {} class_name `[` type args <`]`>
1766
- *
1767
- * @param kind
1768
- * */
1769
- void class_instance_name(parserstate *state, TypeNameKind kind, VALUE *name, VALUE *args, range *name_range, range *args_range) {
1770
- parser_advance(state);
1771
-
1772
- *name = parse_type_name(state, kind, name_range);
1773
-
1774
- if (state->next_token.type == pLBRACKET) {
1775
- parser_advance(state);
1776
- args_range->start = state->current_token.range.start;
1777
- parse_type_list(state, pRBRACKET, args);
1778
- parser_advance_assert(state, pRBRACKET);
1779
- args_range->end = state->current_token.range.end;
1780
- } else {
1781
- *args_range = NULL_RANGE;
1782
- }
1783
- }
1784
-
1785
- /**
1786
- * mixin_member ::= {kINCLUDE} <class_instance_name>
1787
- * | {kPREPEND} <class_instance_name>
1788
- * | {kEXTEND} <class_instance_name>
1789
- *
1790
- * @param from_interface `true` when the member is in an interface.
1791
- * */
1792
- static VALUE parse_mixin_member(parserstate *state, bool from_interface, position comment_pos, VALUE annotations) {
1793
- range member_range;
1794
- member_range.start = state->current_token.range.start;
1795
- comment_pos = nonnull_pos_or(comment_pos, member_range.start);
1796
-
1797
- enum TokenType type = state->current_token.type;
1798
- range keyword_range = state->current_token.range;
1799
-
1800
- bool reset_typevar_scope;
1801
- switch (type)
1802
- {
1803
- case kINCLUDE:
1804
- reset_typevar_scope = false;
1805
- break;
1806
- case kEXTEND:
1807
- reset_typevar_scope = true;
1808
- break;
1809
- case kPREPEND:
1810
- reset_typevar_scope = false;
1811
- break;
1812
- default:
1813
- rbs_abort();
1814
- }
1815
-
1816
- if (from_interface) {
1817
- if (state->current_token.type != kINCLUDE) {
1818
- raise_syntax_error(
1819
- state,
1820
- state->current_token,
1821
- "unexpected mixin in interface declaration"
1822
- );
1823
- }
1824
- }
1825
-
1826
- parser_push_typevar_table(state, reset_typevar_scope);
1827
-
1828
- VALUE name;
1829
- VALUE args = EMPTY_ARRAY;
1830
- range name_range;
1831
- range args_range = NULL_RANGE;
1832
- class_instance_name(
1833
- state,
1834
- from_interface ? INTERFACE_NAME : (INTERFACE_NAME | CLASS_NAME),
1835
- &name, &args, &name_range, &args_range
1836
- );
1837
-
1838
- parser_pop_typevar_table(state);
1839
-
1840
- member_range.end = state->current_token.range.end;
1841
-
1842
- VALUE location = rbs_new_location(state->buffer, member_range);
1843
- rbs_loc *loc = rbs_check_location(location);
1844
- rbs_loc_alloc_children(loc, 3);
1845
- rbs_loc_add_required_child(loc, INTERN("name"), name_range);
1846
- rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range);
1847
- rbs_loc_add_optional_child(loc, INTERN("args"), args_range);
1848
-
1849
- VALUE comment = get_comment(state, comment_pos.line);
1850
- switch (type)
1851
- {
1852
- case kINCLUDE:
1853
- return rbs_ast_members_include(name, args, annotations, location, comment);
1854
- case kEXTEND:
1855
- return rbs_ast_members_extend(name, args, annotations, location, comment);
1856
- case kPREPEND:
1857
- return rbs_ast_members_prepend(name, args, annotations, location, comment);
1858
- default:
1859
- rbs_abort();
1860
- }
1861
- }
1862
-
1863
- /**
1864
- * @code
1865
- * alias_member ::= {kALIAS} method_name <method_name>
1866
- * | {kALIAS} kSELF `.` method_name kSELF `.` <method_name>
1867
- * @endcode
1868
- *
1869
- * @param[in] instance_only `true` to reject `self.` alias.
1870
- * */
1871
- static VALUE parse_alias_member(parserstate *state, bool instance_only, position comment_pos, VALUE annotations) {
1872
- range member_range;
1873
- member_range.start = state->current_token.range.start;
1874
- range keyword_range = state->current_token.range;
1875
-
1876
- comment_pos = nonnull_pos_or(comment_pos, member_range.start);
1877
- VALUE comment = get_comment(state, comment_pos.line);
1878
-
1879
- VALUE kind, new_name, old_name;
1880
- range new_kind_range, old_kind_range, new_name_range, old_name_range;
1881
- if (!instance_only && state->next_token.type == kSELF) {
1882
- kind = ID2SYM(rb_intern("singleton"));
1883
-
1884
- new_kind_range.start = state->next_token.range.start;
1885
- new_kind_range.end = state->next_token2.range.end;
1886
- parser_advance_assert(state, kSELF);
1887
- parser_advance_assert(state, pDOT);
1888
- new_name = parse_method_name(state, &new_name_range);
1889
-
1890
- old_kind_range.start = state->next_token.range.start;
1891
- old_kind_range.end = state->next_token2.range.end;
1892
- parser_advance_assert(state, kSELF);
1893
- parser_advance_assert(state, pDOT);
1894
- old_name = parse_method_name(state, &old_name_range);
1895
- } else {
1896
- kind = ID2SYM(rb_intern("instance"));
1897
- new_name = parse_method_name(state, &new_name_range);
1898
- old_name = parse_method_name(state, &old_name_range);
1899
-
1900
- new_kind_range = NULL_RANGE;
1901
- old_kind_range = NULL_RANGE;
1902
- }
1903
-
1904
- member_range.end = state->current_token.range.end;
1905
- VALUE location = rbs_new_location(state->buffer, member_range);
1906
- rbs_loc *loc = rbs_check_location(location);
1907
- rbs_loc_alloc_children(loc, 5);
1908
- rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range);
1909
- rbs_loc_add_required_child(loc, INTERN("new_name"), new_name_range);
1910
- rbs_loc_add_required_child(loc, INTERN("old_name"), old_name_range);
1911
- rbs_loc_add_optional_child(loc, INTERN("new_kind"), new_kind_range);
1912
- rbs_loc_add_optional_child(loc, INTERN("old_kind"), old_kind_range);
1913
-
1914
- return rbs_ast_members_alias(
1915
- new_name,
1916
- old_name,
1917
- kind,
1918
- annotations,
1919
- location,
1920
- comment
1921
- );
1922
- }
1923
-
1924
- /*
1925
- variable_member ::= {tAIDENT} `:` <type>
1926
- | {kSELF} `.` tAIDENT `:` <type>
1927
- | {tA2IDENT} `:` <type>
1928
- */
1929
- static VALUE parse_variable_member(parserstate *state, position comment_pos, VALUE annotations) {
1930
- if (rb_array_len(annotations) > 0) {
1931
- raise_syntax_error(
1932
- state,
1933
- state->current_token,
1934
- "annotation cannot be given to variable members"
1935
- );
1936
- }
1937
-
1938
- range member_range;
1939
- member_range.start = state->current_token.range.start;
1940
- comment_pos = nonnull_pos_or(comment_pos, member_range.start);
1941
- VALUE comment = get_comment(state, comment_pos.line);
1942
-
1943
- switch (state->current_token.type)
1944
- {
1945
- case tAIDENT: {
1946
- range name_range = state->current_token.range;
1947
- VALUE name = ID2SYM(INTERN_TOKEN(state, state->current_token));
1948
-
1949
- parser_advance_assert(state, pCOLON);
1950
- range colon_range = state->current_token.range;
1951
-
1952
- VALUE type = parse_type(state);
1953
- member_range.end = state->current_token.range.end;
1954
-
1955
- VALUE location = rbs_new_location(state->buffer, member_range);
1956
- rbs_loc *loc = rbs_check_location(location);
1957
- rbs_loc_alloc_children(loc, 3);
1958
- rbs_loc_add_required_child(loc, INTERN("name"), name_range);
1959
- rbs_loc_add_required_child(loc, INTERN("colon"), colon_range);
1960
- rbs_loc_add_optional_child(loc, INTERN("kind"), NULL_RANGE);
1961
-
1962
- return rbs_ast_members_instance_variable(name, type, location, comment);
1963
- }
1964
- case tA2IDENT: {
1965
- range name_range = state->current_token.range;
1966
- VALUE name = ID2SYM(INTERN_TOKEN(state, state->current_token));
1967
-
1968
- parser_advance_assert(state, pCOLON);
1969
- range colon_range = state->current_token.range;
1970
-
1971
- parser_push_typevar_table(state, true);
1972
- VALUE type = parse_type(state);
1973
- parser_pop_typevar_table(state);
1974
- member_range.end = state->current_token.range.end;
1975
-
1976
- VALUE location = rbs_new_location(state->buffer, member_range);
1977
- rbs_loc *loc = rbs_check_location(location);
1978
- rbs_loc_alloc_children(loc, 3);
1979
- rbs_loc_add_required_child(loc, INTERN("name"), name_range);
1980
- rbs_loc_add_required_child(loc, INTERN("colon"), colon_range);
1981
- rbs_loc_add_optional_child(loc, INTERN("kind"), NULL_RANGE);
1982
-
1983
- return rbs_ast_members_class_variable(name, type, location, comment);
1984
- }
1985
- case kSELF: {
1986
- range kind_range = {
1987
- .start = state->current_token.range.start,
1988
- .end = state->next_token.range.end
1989
- };
1990
-
1991
- parser_advance_assert(state, pDOT);
1992
- parser_advance_assert(state, tAIDENT);
1993
-
1994
- range name_range = state->current_token.range;
1995
- VALUE name = ID2SYM(INTERN_TOKEN(state, state->current_token));
1996
-
1997
- parser_advance_assert(state, pCOLON);
1998
- range colon_range = state->current_token.range;
1999
-
2000
- parser_push_typevar_table(state, true);
2001
- VALUE type = parse_type(state);
2002
- parser_pop_typevar_table(state);
2003
- member_range.end = state->current_token.range.end;
2004
-
2005
- VALUE location = rbs_new_location(state->buffer, member_range);
2006
- rbs_loc *loc = rbs_check_location(location);
2007
- rbs_loc_alloc_children(loc, 3);
2008
- rbs_loc_add_required_child(loc, INTERN("name"), name_range);
2009
- rbs_loc_add_required_child(loc, INTERN("colon"), colon_range);
2010
- rbs_loc_add_optional_child(loc, INTERN("kind"), kind_range);
2011
-
2012
- return rbs_ast_members_class_instance_variable(name, type, location, comment);
2013
- }
2014
- default:
2015
- rbs_abort();
2016
- }
2017
- }
2018
-
2019
- /*
2020
- visibility_member ::= {<`public`>}
2021
- | {<`private`>}
2022
- */
2023
- static VALUE parse_visibility_member(parserstate *state, VALUE annotations) {
2024
- if (rb_array_len(annotations) > 0) {
2025
- raise_syntax_error(
2026
- state,
2027
- state->current_token,
2028
- "annotation cannot be given to visibility members"
2029
- );
2030
- }
2031
-
2032
- VALUE location = rbs_new_location(state->buffer, state->current_token.range);
2033
-
2034
- switch (state->current_token.type)
2035
- {
2036
- case kPUBLIC:
2037
- return rbs_ast_members_public(location);
2038
- case kPRIVATE:
2039
- return rbs_ast_members_private(location);
2040
- default:
2041
- rbs_abort();
2042
- }
2043
- }
2044
-
2045
- /*
2046
- attribute_member ::= {attr_keyword} attr_name attr_var `:` <type>
2047
- | {visibility} attr_keyword attr_name attr_var `:` <type>
2048
- | {attr_keyword} `self` `.` attr_name attr_var `:` <type>
2049
- | {visibility} attr_keyword `self` `.` attr_name attr_var `:` <type>
2050
-
2051
- attr_keyword ::= `attr_reader` | `attr_writer` | `attr_accessor`
2052
-
2053
- visibility ::= `public` | `private`
2054
-
2055
- attr_var ::= # empty
2056
- | `(` tAIDENT `)` # Ivar name
2057
- | `(` `)` # No variable
2058
- */
2059
- static VALUE parse_attribute_member(parserstate *state, position comment_pos, VALUE annotations) {
2060
- range member_range;
2061
- member_range.start = state->current_token.range.start;
2062
- comment_pos = nonnull_pos_or(comment_pos, member_range.start);
2063
- VALUE comment = get_comment(state, comment_pos.line);
2064
-
2065
- VALUE visibility;
2066
- range visibility_range;
2067
- switch (state->current_token.type)
2068
- {
2069
- case kPRIVATE:
2070
- visibility = ID2SYM(rb_intern("private"));
2071
- visibility_range = state->current_token.range;
2072
- parser_advance(state);
2073
- break;
2074
- case kPUBLIC:
2075
- visibility = ID2SYM(rb_intern("public"));
2076
- visibility_range = state->current_token.range;
2077
- parser_advance(state);
2078
- break;
2079
- default:
2080
- visibility = Qnil;
2081
- visibility_range = NULL_RANGE;
2082
- break;
2083
- }
2084
-
2085
- enum TokenType attr_type = state->current_token.type;
2086
- range keyword_range = state->current_token.range;
2087
-
2088
- range kind_range;
2089
- InstanceSingletonKind is_kind = parse_instance_singleton_kind(state, false, &kind_range);
2090
- VALUE kind = ID2SYM(rb_intern((is_kind == INSTANCE_KIND) ? "instance" : "singleton"));
2091
-
2092
- range name_range;
2093
- VALUE attr_name = parse_method_name(state, &name_range);
2094
-
2095
- VALUE ivar_name;
2096
- range ivar_range, ivar_name_range;
2097
- if (state->next_token.type == pLPAREN) {
2098
- parser_advance_assert(state, pLPAREN);
2099
- ivar_range.start = state->current_token.range.start;
2100
-
2101
- if (parser_advance_if(state, tAIDENT)) {
2102
- ivar_name = ID2SYM(INTERN_TOKEN(state, state->current_token));
2103
- ivar_name_range = state->current_token.range;
2104
- } else {
2105
- ivar_name = Qfalse;
2106
- ivar_name_range = NULL_RANGE;
2107
- }
2108
-
2109
- parser_advance_assert(state, pRPAREN);
2110
- ivar_range.end = state->current_token.range.end;
2111
- } else {
2112
- ivar_range = NULL_RANGE;
2113
- ivar_name = Qnil;
2114
- ivar_name_range = NULL_RANGE;
2115
- }
2116
-
2117
- parser_advance_assert(state, pCOLON);
2118
- range colon_range = state->current_token.range;
2119
-
2120
- parser_push_typevar_table(state, is_kind == SINGLETON_KIND);
2121
- VALUE type = parse_type(state);
2122
- parser_pop_typevar_table(state);
2123
- member_range.end = state->current_token.range.end;
2124
-
2125
- VALUE location = rbs_new_location(state->buffer, member_range);
2126
- rbs_loc *loc = rbs_check_location(location);
2127
- rbs_loc_alloc_children(loc, 7);
2128
- rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range);
2129
- rbs_loc_add_required_child(loc, INTERN("name"), name_range);
2130
- rbs_loc_add_required_child(loc, INTERN("colon"), colon_range);
2131
- rbs_loc_add_optional_child(loc, INTERN("kind"), kind_range);
2132
- rbs_loc_add_optional_child(loc, INTERN("ivar"), ivar_range);
2133
- rbs_loc_add_optional_child(loc, INTERN("ivar_name"), ivar_name_range);
2134
- rbs_loc_add_optional_child(loc, INTERN("visibility"), visibility_range);
2135
-
2136
- switch (attr_type)
2137
- {
2138
- case kATTRREADER:
2139
- return rbs_ast_members_attr_reader(attr_name, type, ivar_name, kind, annotations, location, comment, visibility);
2140
- case kATTRWRITER:
2141
- return rbs_ast_members_attr_writer(attr_name, type, ivar_name, kind, annotations, location, comment, visibility);
2142
- case kATTRACCESSOR:
2143
- return rbs_ast_members_attr_accessor(attr_name, type, ivar_name, kind, annotations, location, comment, visibility);
2144
- default:
2145
- rbs_abort();
2146
- }
2147
- }
2148
-
2149
- /*
2150
- interface_members ::= {} ...<interface_member> kEND
2151
-
2152
- interface_member ::= def_member (instance method only && no overloading)
2153
- | mixin_member (interface only)
2154
- | alias_member (instance only)
2155
- */
2156
- static VALUE parse_interface_members(parserstate *state) {
2157
- VALUE members = EMPTY_ARRAY;
2158
-
2159
- while (state->next_token.type != kEND) {
2160
- VALUE annotations = EMPTY_ARRAY;
2161
- position annot_pos = NullPosition;
2162
-
2163
- parse_annotations(state, &annotations, &annot_pos);
2164
-
2165
- parser_advance(state);
2166
-
2167
- VALUE member;
2168
- switch (state->current_token.type) {
2169
- case kDEF: {
2170
- member = parse_member_def(state, true, true, annot_pos, annotations);
2171
- break;
2172
- }
2173
-
2174
- case kINCLUDE:
2175
- case kEXTEND:
2176
- case kPREPEND: {
2177
- member = parse_mixin_member(state, true, annot_pos, annotations);
2178
- break;
2179
- }
2180
-
2181
- case kALIAS: {
2182
- member = parse_alias_member(state, true, annot_pos, annotations);
2183
- break;
2184
- }
2185
-
2186
- default:
2187
- raise_syntax_error(
2188
- state,
2189
- state->current_token,
2190
- "unexpected token for interface declaration member"
2191
- );
2192
- }
2193
-
2194
- melt_array(&members);
2195
- rb_ary_push(members, member);
2196
- }
2197
-
2198
- return members;
2199
- }
2200
-
2201
- /*
2202
- interface_decl ::= {`interface`} interface_name module_type_params interface_members <kEND>
2203
- */
2204
- static VALUE parse_interface_decl(parserstate *state, position comment_pos, VALUE annotations) {
2205
- parser_push_typevar_table(state, true);
2206
-
2207
- range member_range;
2208
- member_range.start = state->current_token.range.start;
2209
- comment_pos = nonnull_pos_or(comment_pos, member_range.start);
2210
-
2211
- range keyword_range = state->current_token.range;
2212
-
2213
- parser_advance(state);
2214
-
2215
- range name_range;
2216
- VALUE name = parse_type_name(state, INTERFACE_NAME, &name_range);
2217
- range type_params_range;
2218
- VALUE params = parse_type_params(state, &type_params_range, true);
2219
- VALUE members = parse_interface_members(state);
2220
-
2221
- parser_advance_assert(state, kEND);
2222
- range end_range = state->current_token.range;
2223
- member_range.end = end_range.end;
2224
-
2225
- parser_pop_typevar_table(state);
2226
-
2227
- VALUE location = rbs_new_location(state->buffer, member_range);
2228
- rbs_loc *loc = rbs_check_location(location);
2229
- rbs_loc_alloc_children(loc, 4);
2230
- rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range);
2231
- rbs_loc_add_required_child(loc, INTERN("name"), name_range);
2232
- rbs_loc_add_required_child(loc, INTERN("end"), end_range);
2233
- rbs_loc_add_optional_child(loc, INTERN("type_params"), type_params_range);
2234
-
2235
- return rbs_ast_decl_interface(
2236
- name,
2237
- params,
2238
- members,
2239
- annotations,
2240
- location,
2241
- get_comment(state, comment_pos.line)
2242
- );
2243
- }
2244
-
2245
- /*
2246
- module_self_types ::= {`:`} module_self_type `,` ... `,` <module_self_type>
2247
-
2248
- module_self_type ::= <module_name>
2249
- | module_name `[` type_list <`]`>
2250
- */
2251
- static void parse_module_self_types(parserstate *state, VALUE *array) {
2252
- while (true) {
2253
- parser_advance(state);
2254
-
2255
- range self_range;
2256
- self_range.start = state->current_token.range.start;
2257
- range name_range;
2258
- VALUE module_name = parse_type_name(state, CLASS_NAME | INTERFACE_NAME, &name_range);
2259
- self_range.end = name_range.end;
2260
-
2261
- VALUE args = EMPTY_ARRAY;
2262
- range args_range = NULL_RANGE;
2263
- if (state->next_token.type == pLBRACKET) {
2264
- parser_advance(state);
2265
- args_range.start = state->current_token.range.start;
2266
- parse_type_list(state, pRBRACKET, &args);
2267
- parser_advance(state);
2268
- self_range.end = args_range.end = state->current_token.range.end;
2269
- }
2270
-
2271
- VALUE location = rbs_new_location(state->buffer, self_range);
2272
- rbs_loc *loc = rbs_check_location(location);
2273
- rbs_loc_alloc_children(loc, 2);
2274
- rbs_loc_add_required_child(loc, INTERN("name"), name_range);
2275
- rbs_loc_add_optional_child(loc, INTERN("args"), args_range);
2276
-
2277
- VALUE self_type = rbs_ast_decl_module_self(module_name, args, location);
2278
- melt_array(array);
2279
- rb_ary_push(*array, self_type);
2280
-
2281
- if (state->next_token.type == pCOMMA) {
2282
- parser_advance(state);
2283
- } else {
2284
- break;
2285
- }
2286
- }
2287
- }
2288
-
2289
- static VALUE parse_nested_decl(parserstate *state, const char *nested_in, position annot_pos, VALUE annotations);
2290
-
2291
- /*
2292
- module_members ::= {} ...<module_member> kEND
2293
-
2294
- module_member ::= def_member
2295
- | variable_member
2296
- | mixin_member
2297
- | alias_member
2298
- | attribute_member
2299
- | `public`
2300
- | `private`
2301
- */
2302
- static VALUE parse_module_members(parserstate *state) {
2303
- VALUE members = EMPTY_ARRAY;
2304
-
2305
- while (state->next_token.type != kEND) {
2306
- VALUE annotations = EMPTY_ARRAY;
2307
- position annot_pos = NullPosition;
2308
- parse_annotations(state, &annotations, &annot_pos);
2309
-
2310
- parser_advance(state);
2311
-
2312
- VALUE member;
2313
- switch (state->current_token.type)
2314
- {
2315
- case kDEF: {
2316
- member = parse_member_def(state, false, true, annot_pos, annotations);
2317
- break;
2318
- }
2319
-
2320
- case kINCLUDE:
2321
- case kEXTEND:
2322
- case kPREPEND: {
2323
- member = parse_mixin_member(state, false, annot_pos, annotations);
2324
- break;
2325
- }
2326
-
2327
- case kALIAS: {
2328
- member = parse_alias_member(state, false, annot_pos, annotations);
2329
- break;
2330
- }
2331
-
2332
- case tAIDENT:
2333
- case tA2IDENT:
2334
- case kSELF: {
2335
- member = parse_variable_member(state, annot_pos, annotations);
2336
- break;
2337
- }
2338
-
2339
- case kATTRREADER:
2340
- case kATTRWRITER:
2341
- case kATTRACCESSOR: {
2342
- member = parse_attribute_member(state, annot_pos, annotations);
2343
- break;
2344
- }
2345
-
2346
- case kPUBLIC:
2347
- case kPRIVATE:
2348
- if (state->next_token.range.start.line == state->current_token.range.start.line) {
2349
- switch (state->next_token.type)
2350
- {
2351
- case kDEF: {
2352
- member = parse_member_def(state, false, true, annot_pos, annotations);
2353
- break;
2354
- }
2355
- case kATTRREADER:
2356
- case kATTRWRITER:
2357
- case kATTRACCESSOR: {
2358
- member = parse_attribute_member(state, annot_pos, annotations);
2359
- break;
2360
- }
2361
- default:
2362
- raise_syntax_error(state, state->next_token, "method or attribute definition is expected after visibility modifier");
2363
- }
2364
- } else {
2365
- member = parse_visibility_member(state, annotations);
2366
- }
2367
- break;
2368
-
2369
- default:
2370
- member = parse_nested_decl(state, "module", annot_pos, annotations);
2371
- break;
2372
- }
2373
-
2374
- melt_array(&members);
2375
- rb_ary_push(members, member);
2376
- }
2377
-
2378
- return members;
2379
- }
2380
-
2381
- /*
2382
- module_decl ::= {module_name} module_type_params module_members <kEND>
2383
- | {module_name} module_name module_type_params `:` module_self_types module_members <kEND>
2384
- */
2385
- static VALUE parse_module_decl0(parserstate *state, range keyword_range, VALUE module_name, range name_range, VALUE comment, VALUE annotations) {
2386
- parser_push_typevar_table(state, true);
2387
-
2388
- range decl_range;
2389
- decl_range.start = keyword_range.start;
2390
- range type_params_range;
2391
- VALUE type_params = parse_type_params(state, &type_params_range, true);
2392
-
2393
- VALUE self_types = EMPTY_ARRAY;
2394
- range colon_range;
2395
- range self_types_range;
2396
- if (state->next_token.type == pCOLON) {
2397
- parser_advance(state);
2398
- colon_range = state->current_token.range;
2399
- self_types_range.start = state->next_token.range.start;
2400
- parse_module_self_types(state, &self_types);
2401
- self_types_range.end = state->current_token.range.end;
2402
- } else {
2403
- colon_range = NULL_RANGE;
2404
- self_types_range = NULL_RANGE;
2405
- }
2406
-
2407
- VALUE members = parse_module_members(state);
2408
-
2409
- parser_advance_assert(state, kEND);
2410
- range end_range = state->current_token.range;
2411
- decl_range.end = state->current_token.range.end;
2412
-
2413
- VALUE location = rbs_new_location(state->buffer, decl_range);
2414
- rbs_loc *loc = rbs_check_location(location);
2415
- rbs_loc_alloc_children(loc, 6);
2416
- rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range);
2417
- rbs_loc_add_required_child(loc, INTERN("name"), name_range);
2418
- rbs_loc_add_required_child(loc, INTERN("end"), end_range);
2419
- rbs_loc_add_optional_child(loc, INTERN("type_params"), type_params_range);
2420
- rbs_loc_add_optional_child(loc, INTERN("colon"), colon_range);
2421
- rbs_loc_add_optional_child(loc, INTERN("self_types"), self_types_range);
2422
-
2423
- parser_pop_typevar_table(state);
2424
-
2425
- return rbs_ast_decl_module(
2426
- module_name,
2427
- type_params,
2428
- self_types,
2429
- members,
2430
- annotations,
2431
- location,
2432
- comment
2433
- );
2434
- }
2435
-
2436
- /*
2437
- module_decl ::= {`module`} module_name `=` old_module_name <kEND>
2438
- | {`module`} module_name module_decl0 <kEND>
2439
-
2440
- */
2441
- static VALUE parse_module_decl(parserstate *state, position comment_pos, VALUE annotations) {
2442
- range keyword_range = state->current_token.range;
2443
-
2444
- comment_pos = nonnull_pos_or(comment_pos, state->current_token.range.start);
2445
- VALUE comment = get_comment(state, comment_pos.line);
2446
-
2447
- parser_advance(state);
2448
- range module_name_range;
2449
- VALUE module_name = parse_type_name(state, CLASS_NAME, &module_name_range);
2450
-
2451
- if (state->next_token.type == pEQ) {
2452
- range eq_range = state->next_token.range;
2453
- parser_advance(state);
2454
- parser_advance(state);
2455
-
2456
- range old_name_range;
2457
- VALUE old_name = parse_type_name(state, CLASS_NAME, &old_name_range);
2458
-
2459
- range decl_range = {
2460
- .start = keyword_range.start,
2461
- .end = old_name_range.end
2462
- };
2463
-
2464
- VALUE location = rbs_new_location(state->buffer, decl_range);
2465
- rbs_loc *loc = rbs_check_location(location);
2466
- rbs_loc_alloc_children(loc, 4);
2467
- rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range);
2468
- rbs_loc_add_required_child(loc, INTERN("new_name"), module_name_range);
2469
- rbs_loc_add_required_child(loc, INTERN("eq"), eq_range);
2470
- rbs_loc_add_optional_child(loc, INTERN("old_name"), old_name_range);
2471
-
2472
- return rbs_ast_decl_module_alias(module_name, old_name, location, comment, annotations);
2473
- } else {
2474
- return parse_module_decl0(state, keyword_range, module_name, module_name_range, comment, annotations);
2475
- }
2476
- }
2477
-
2478
- /*
2479
- class_decl_super ::= {} `<` <class_instance_name>
2480
- | {<>}
2481
- */
2482
- static VALUE parse_class_decl_super(parserstate *state, range *lt_range) {
2483
- if (parser_advance_if(state, pLT)) {
2484
- *lt_range = state->current_token.range;
2485
-
2486
- range super_range;
2487
- super_range.start = state->next_token.range.start;
2488
-
2489
- VALUE name;
2490
- VALUE args = EMPTY_ARRAY;
2491
- range name_range, args_range;
2492
- class_instance_name(state, CLASS_NAME, &name, &args, &name_range, &args_range);
2493
-
2494
- super_range.end = state->current_token.range.end;
2495
-
2496
- VALUE location = rbs_new_location(state->buffer, super_range);
2497
- rbs_loc *loc = rbs_check_location(location);
2498
- rbs_loc_alloc_children(loc, 2);
2499
- rbs_loc_add_required_child(loc, INTERN("name"), name_range);
2500
- rbs_loc_add_optional_child(loc, INTERN("args"), args_range);
2501
-
2502
- return rbs_ast_decl_class_super(name, args, location);
2503
- } else {
2504
- *lt_range = NULL_RANGE;
2505
- return Qnil;
2506
- }
2507
- }
2508
-
2509
- /*
2510
- class_decl ::= {class_name} type_params class_decl_super class_members <`end`>
2511
- */
2512
- static VALUE parse_class_decl0(parserstate *state, range keyword_range, VALUE name, range name_range, VALUE comment, VALUE annotations) {
2513
- parser_push_typevar_table(state, true);
2514
-
2515
- range decl_range;
2516
- decl_range.start = keyword_range.start;
2517
-
2518
- range type_params_range;
2519
- VALUE type_params = parse_type_params(state, &type_params_range, true);
2520
-
2521
- range lt_range;
2522
- VALUE super = parse_class_decl_super(state, &lt_range);
2523
-
2524
- VALUE members = parse_module_members(state);
2525
-
2526
- parser_advance_assert(state, kEND);
2527
-
2528
- range end_range = state->current_token.range;
2529
-
2530
- decl_range.end = end_range.end;
2531
-
2532
- parser_pop_typevar_table(state);
2533
-
2534
- VALUE location = rbs_new_location(state->buffer, decl_range);
2535
- rbs_loc *loc = rbs_check_location(location);
2536
- rbs_loc_alloc_children(loc, 5);
2537
- rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range);
2538
- rbs_loc_add_required_child(loc, INTERN("name"), name_range);
2539
- rbs_loc_add_required_child(loc, INTERN("end"), end_range);
2540
- rbs_loc_add_optional_child(loc, INTERN("type_params"), type_params_range);
2541
- rbs_loc_add_optional_child(loc, INTERN("lt"), lt_range);
2542
-
2543
- return rbs_ast_decl_class(
2544
- name,
2545
- type_params,
2546
- super,
2547
- members,
2548
- annotations,
2549
- location,
2550
- comment
2551
- );
2552
- }
2553
-
2554
- /*
2555
- class_decl ::= {`class`} class_name `=` <class_name>
2556
- | {`class`} class_name <class_decl0>
2557
- */
2558
- static VALUE parse_class_decl(parserstate *state, position comment_pos, VALUE annotations) {
2559
- range keyword_range = state->current_token.range;
2560
-
2561
- comment_pos = nonnull_pos_or(comment_pos, state->current_token.range.start);
2562
- VALUE comment = get_comment(state, comment_pos.line);
2563
-
2564
- parser_advance(state);
2565
- range class_name_range;
2566
- VALUE class_name = parse_type_name(state, CLASS_NAME, &class_name_range);
2567
-
2568
- if (state->next_token.type == pEQ) {
2569
- range eq_range = state->next_token.range;
2570
- parser_advance(state);
2571
- parser_advance(state);
2572
-
2573
- range old_name_range;
2574
- VALUE old_name = parse_type_name(state, CLASS_NAME, &old_name_range);
2575
-
2576
- range decl_range = {
2577
- .start = keyword_range.start,
2578
- .end = old_name_range.end,
2579
- };
2580
-
2581
- VALUE location = rbs_new_location(state->buffer, decl_range);
2582
- rbs_loc *loc = rbs_check_location(location);
2583
- rbs_loc_alloc_children(loc, 4);
2584
- rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range);
2585
- rbs_loc_add_required_child(loc, INTERN("new_name"), class_name_range);
2586
- rbs_loc_add_required_child(loc, INTERN("eq"), eq_range);
2587
- rbs_loc_add_optional_child(loc, INTERN("old_name"), old_name_range);
2588
-
2589
- return rbs_ast_decl_class_alias(class_name, old_name, location, comment, annotations);
2590
- } else {
2591
- return parse_class_decl0(state, keyword_range, class_name, class_name_range, comment, annotations);
2592
- }
2593
- }
2594
-
2595
- /*
2596
- nested_decl ::= {<const_decl>}
2597
- | {<class_decl>}
2598
- | {<interface_decl>}
2599
- | {<module_decl>}
2600
- | {<class_decl>}
2601
- */
2602
- static VALUE parse_nested_decl(parserstate *state, const char *nested_in, position annot_pos, VALUE annotations) {
2603
- parser_push_typevar_table(state, true);
2604
-
2605
- VALUE decl;
2606
- switch (state->current_token.type) {
2607
- case tUIDENT:
2608
- case pCOLON2: {
2609
- decl = parse_const_decl(state, annotations);
2610
- break;
2611
- }
2612
- case tGIDENT: {
2613
- decl = parse_global_decl(state, annotations);
2614
- break;
2615
- }
2616
- case kTYPE: {
2617
- decl = parse_type_decl(state, annot_pos, annotations);
2618
- break;
2619
- }
2620
- case kINTERFACE: {
2621
- decl = parse_interface_decl(state, annot_pos, annotations);
2622
- break;
2623
- }
2624
- case kMODULE: {
2625
- decl = parse_module_decl(state, annot_pos, annotations);
2626
- break;
2627
- }
2628
- case kCLASS: {
2629
- decl = parse_class_decl(state, annot_pos, annotations);
2630
- break;
2631
- }
2632
- default:
2633
- raise_syntax_error(
2634
- state,
2635
- state->current_token,
2636
- "unexpected token for class/module declaration member"
2637
- );
2638
- }
2639
-
2640
- parser_pop_typevar_table(state);
2641
-
2642
- return decl;
2643
- }
2644
-
2645
- static VALUE parse_decl(parserstate *state) {
2646
- VALUE annotations = EMPTY_ARRAY;
2647
- position annot_pos = NullPosition;
2648
-
2649
- parse_annotations(state, &annotations, &annot_pos);
2650
-
2651
- parser_advance(state);
2652
- switch (state->current_token.type) {
2653
- case tUIDENT:
2654
- case pCOLON2: {
2655
- return parse_const_decl(state, annotations);
2656
- }
2657
- case tGIDENT: {
2658
- return parse_global_decl(state, annotations);
2659
- }
2660
- case kTYPE: {
2661
- return parse_type_decl(state, annot_pos, annotations);
2662
- }
2663
- case kINTERFACE: {
2664
- return parse_interface_decl(state, annot_pos, annotations);
2665
- }
2666
- case kMODULE: {
2667
- return parse_module_decl(state, annot_pos, annotations);
2668
- }
2669
- case kCLASS: {
2670
- return parse_class_decl(state, annot_pos, annotations);
2671
- }
2672
- default:
2673
- raise_syntax_error(
2674
- state,
2675
- state->current_token,
2676
- "cannot start a declaration"
2677
- );
2678
- }
2679
- }
2680
-
2681
- /*
2682
- namespace ::= {} (`::`)? (`tUIDENT` `::`)* `tUIDENT` <`::`>
2683
- | {} <> (empty -- returns empty namespace)
2684
- */
2685
- static VALUE parse_namespace(parserstate *state, range *rg) {
2686
- bool is_absolute = false;
2687
-
2688
- if (state->next_token.type == pCOLON2) {
2689
- *rg = (range) {
2690
- .start = state->next_token.range.start,
2691
- .end = state->next_token.range.end,
2692
- };
2693
- is_absolute = true;
2694
-
2695
- parser_advance(state);
2696
- }
2697
-
2698
- VALUE path = EMPTY_ARRAY;
2699
-
2700
- while (true) {
2701
- if (state->next_token.type == tUIDENT && state->next_token2.type == pCOLON2) {
2702
- melt_array(&path);
2703
- rb_ary_push(path, ID2SYM(INTERN_TOKEN(state, state->next_token)));
2704
- if (null_position_p(rg->start)) {
2705
- rg->start = state->next_token.range.start;
2706
- }
2707
- rg->end = state->next_token2.range.end;
2708
- parser_advance(state);
2709
- parser_advance(state);
2710
- } else {
2711
- break;
2712
- }
2713
- }
2714
-
2715
- return rbs_namespace(path, is_absolute ? Qtrue : Qfalse);
2716
- }
2717
-
2718
- /*
2719
- use_clauses ::= {} use_clause `,` ... `,` <use_clause>
2720
-
2721
- use_clause ::= {} namespace <tUIDENT>
2722
- | {} namespace tUIDENT `as` <tUIDENT>
2723
- | {} namespace <tSTAR>
2724
- */
2725
- static void parse_use_clauses(parserstate *state, VALUE clauses) {
2726
- while (true) {
2727
- range namespace_range = NULL_RANGE;
2728
- VALUE namespace = parse_namespace(state, &namespace_range);
2729
-
2730
- switch (state->next_token.type)
2731
- {
2732
- case tLIDENT:
2733
- case tULIDENT:
2734
- case tUIDENT: {
2735
- parser_advance(state);
2736
-
2737
- enum TokenType ident_type = state->current_token.type;
2738
-
2739
- range type_name_range = null_range_p(namespace_range)
2740
- ? state->current_token.range
2741
- : (range) { .start = namespace_range.start, .end = state->current_token.range.end };
2742
-
2743
- VALUE type_name = rbs_type_name(namespace, ID2SYM(INTERN_TOKEN(state, state->current_token)));
2744
-
2745
- range keyword_range = NULL_RANGE;
2746
- range new_name_range = NULL_RANGE;
2747
-
2748
- VALUE new_name = Qnil;
2749
- range clause_range = type_name_range;
2750
- if (state->next_token.type == kAS) {
2751
- parser_advance(state);
2752
- keyword_range = state->current_token.range;
2753
-
2754
- if (ident_type == tUIDENT) parser_advance_assert(state, tUIDENT);
2755
- if (ident_type == tLIDENT) parser_advance_assert(state, tLIDENT);
2756
- if (ident_type == tULIDENT) parser_advance_assert(state, tULIDENT);
2757
-
2758
- new_name = ID2SYM(INTERN_TOKEN(state, state->current_token));
2759
- new_name_range = state->current_token.range;
2760
- clause_range.end = new_name_range.end;
2761
- }
2762
-
2763
- VALUE location = rbs_new_location(state->buffer, clause_range);
2764
- rbs_loc *loc = rbs_check_location(location);
2765
- rbs_loc_alloc_children(loc, 3);
2766
- rbs_loc_add_required_child(loc, INTERN("type_name"), type_name_range);
2767
- rbs_loc_add_optional_child(loc, INTERN("keyword"), keyword_range);
2768
- rbs_loc_add_optional_child(loc, INTERN("new_name"), new_name_range);
2769
-
2770
- rb_ary_push(clauses, rbs_ast_directives_use_single_clause(type_name, new_name, location));
2771
-
2772
- break;
2773
- }
2774
- case pSTAR:
2775
- {
2776
- range clause_range = namespace_range;
2777
- parser_advance(state);
2778
-
2779
- range star_range = state->current_token.range;
2780
- clause_range.end = star_range.end;
2781
-
2782
- VALUE location = rbs_new_location(state->buffer, clause_range);
2783
- rbs_loc *loc = rbs_check_location(location);
2784
- rbs_loc_alloc_children(loc, 2);
2785
- rbs_loc_add_required_child(loc, INTERN("namespace"), namespace_range);
2786
- rbs_loc_add_required_child(loc, INTERN("star"), star_range);
2787
-
2788
- rb_ary_push(clauses, rbs_ast_directives_use_wildcard_clause(namespace, location));
2789
-
2790
- break;
2791
- }
2792
- default:
2793
- raise_syntax_error(
2794
- state,
2795
- state->next_token,
2796
- "use clause is expected"
2797
- );
2798
- }
2799
-
2800
- if (state->next_token.type == pCOMMA) {
2801
- parser_advance(state);
2802
- } else {
2803
- break;
2804
- }
2805
- }
2806
-
2807
- return;
2808
- }
2809
-
2810
- /*
2811
- use_directive ::= {} `use` <clauses>
2812
- */
2813
- static VALUE parse_use_directive(parserstate *state) {
2814
- if (state->next_token.type == kUSE) {
2815
- parser_advance(state);
2816
-
2817
- range keyword_range = state->current_token.range;
2818
-
2819
- VALUE clauses = rb_ary_new();
2820
- parse_use_clauses(state, clauses);
2821
-
2822
- range directive_range = keyword_range;
2823
- directive_range.end = state->current_token.range.end;
2824
-
2825
- VALUE location = rbs_new_location(state->buffer, directive_range);
2826
- rbs_loc *loc = rbs_check_location(location);
2827
- rbs_loc_alloc_children(loc, 1);
2828
- rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range);
2829
-
2830
- return rbs_ast_directives_use(clauses, location);
2831
- } else {
2832
- return Qnil;
2833
- }
2834
- }
2835
-
2836
- VALUE parse_signature(parserstate *state) {
2837
- VALUE dirs = EMPTY_ARRAY;
2838
- VALUE decls = EMPTY_ARRAY;
2839
-
2840
- while (state->next_token.type == kUSE) {
2841
- melt_array(&dirs);
2842
- rb_ary_push(dirs, parse_use_directive(state));
2843
- }
2844
-
2845
- while (state->next_token.type != pEOF) {
2846
- melt_array(&decls);
2847
- rb_ary_push(decls, parse_decl(state));
2848
- }
2849
-
2850
- VALUE ret = rb_ary_new();
2851
- rb_ary_push(ret, dirs);
2852
- rb_ary_push(ret, decls);
2853
- return ret;
2854
- }
2855
-
2856
- struct parse_type_arg {
2857
- parserstate *parser;
2858
- VALUE require_eof;
2859
- };
2860
-
2861
- static VALUE
2862
- ensure_free_parser(VALUE parser) {
2863
- free_parser((parserstate *)parser);
2864
- return Qnil;
2865
- }
2866
-
2867
- static VALUE
2868
- parse_type_try(VALUE a) {
2869
- struct parse_type_arg *arg = (struct parse_type_arg *)a;
2870
-
2871
- if (arg->parser->next_token.type == pEOF) {
2872
- return Qnil;
2873
- }
2874
-
2875
- VALUE type = parse_type(arg->parser);
2876
-
2877
- if (RB_TEST(arg->require_eof)) {
2878
- parser_advance_assert(arg->parser, pEOF);
2879
- }
2880
-
2881
- return type;
2882
- }
2883
-
2884
- static VALUE
2885
- rbsparser_parse_type(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos, VALUE variables, VALUE require_eof)
2886
- {
2887
- VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
2888
- StringValue(string);
2889
- lexstate *lexer = alloc_lexer(string, FIX2INT(start_pos), FIX2INT(end_pos));
2890
- parserstate *parser = alloc_parser(buffer, lexer, FIX2INT(start_pos), FIX2INT(end_pos), variables);
2891
- struct parse_type_arg arg = {
2892
- parser,
2893
- require_eof
2894
- };
2895
- return rb_ensure(parse_type_try, (VALUE)&arg, ensure_free_parser, (VALUE)parser);
2896
- }
2897
-
2898
- static VALUE
2899
- parse_method_type_try(VALUE a) {
2900
- struct parse_type_arg *arg = (struct parse_type_arg *)a;
2901
-
2902
- if (arg->parser->next_token.type == pEOF) {
2903
- return Qnil;
2904
- }
2905
-
2906
- VALUE method_type = parse_method_type(arg->parser);
2907
-
2908
- if (RB_TEST(arg->require_eof)) {
2909
- parser_advance_assert(arg->parser, pEOF);
2910
- }
2911
-
2912
- return method_type;
2913
- }
2914
-
2915
- static VALUE
2916
- rbsparser_parse_method_type(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos, VALUE variables, VALUE require_eof)
2917
- {
2918
- VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
2919
- StringValue(string);
2920
- lexstate *lexer = alloc_lexer(string, FIX2INT(start_pos), FIX2INT(end_pos));
2921
- parserstate *parser = alloc_parser(buffer, lexer, FIX2INT(start_pos), FIX2INT(end_pos), variables);
2922
- struct parse_type_arg arg = {
2923
- parser,
2924
- require_eof
2925
- };
2926
- return rb_ensure(parse_method_type_try, (VALUE)&arg, ensure_free_parser, (VALUE)parser);
2927
- }
2928
-
2929
- static VALUE
2930
- parse_signature_try(VALUE a) {
2931
- parserstate *parser = (parserstate *)a;
2932
- return parse_signature(parser);
2933
- }
2934
-
2935
- static VALUE
2936
- rbsparser_parse_signature(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos)
2937
- {
2938
- VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
2939
- StringValue(string);
2940
- lexstate *lexer = alloc_lexer(string, FIX2INT(start_pos), FIX2INT(end_pos));
2941
- parserstate *parser = alloc_parser(buffer, lexer, FIX2INT(start_pos), FIX2INT(end_pos), Qnil);
2942
- return rb_ensure(parse_signature_try, (VALUE)parser, ensure_free_parser, (VALUE)parser);
2943
- }
2944
-
2945
- static VALUE
2946
- rbsparser_lex(VALUE self, VALUE buffer, VALUE end_pos) {
2947
- VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
2948
- StringValue(string);
2949
- lexstate *lexer = alloc_lexer(string, 0, FIX2INT(end_pos));
2950
- VALUE results = rb_ary_new();
2951
-
2952
- token token = NullToken;
2953
- while (token.type != pEOF) {
2954
- token = rbsparser_next_token(lexer);
2955
- VALUE type = ID2SYM(rb_intern(token_type_str(token.type)));
2956
- VALUE location = rbs_new_location(buffer, token.range);
2957
- VALUE pair = rb_ary_new3(2, type, location);
2958
- rb_ary_push(results, pair);
2959
- }
2960
-
2961
- free(lexer);
2962
-
2963
- return results;
2964
- }
2965
-
2966
- void rbs__init_parser(void) {
2967
- RBS_Parser = rb_define_class_under(RBS, "Parser", rb_cObject);
2968
- rb_gc_register_mark_object(RBS_Parser);
2969
-
2970
- VALUE empty_array = rb_obj_freeze(rb_ary_new());
2971
- rb_gc_register_mark_object(empty_array);
2972
- EMPTY_ARRAY = empty_array;
2973
-
2974
- VALUE empty_hash = rb_obj_freeze(rb_hash_new());
2975
- rb_gc_register_mark_object(empty_hash);
2976
- EMPTY_HASH = empty_hash;
2977
-
2978
- rb_define_singleton_method(RBS_Parser, "_parse_type", rbsparser_parse_type, 5);
2979
- rb_define_singleton_method(RBS_Parser, "_parse_method_type", rbsparser_parse_method_type, 5);
2980
- rb_define_singleton_method(RBS_Parser, "_parse_signature", rbsparser_parse_signature, 3);
2981
- rb_define_singleton_method(RBS_Parser, "_lex", rbsparser_lex, 2);
2982
- }