rbs 4.0.0.dev.2 → 4.0.0.dev.3

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 (60) hide show
  1. checksums.yaml +4 -4
  2. data/.clang-format +74 -0
  3. data/.clangd +2 -0
  4. data/.github/workflows/c-check.yml +51 -0
  5. data/.github/workflows/dependabot.yml +1 -1
  6. data/.github/workflows/ruby.yml +0 -17
  7. data/.github/workflows/typecheck.yml +0 -2
  8. data/.gitignore +4 -0
  9. data/.rubocop.yml +1 -1
  10. data/.vscode/extensions.json +5 -0
  11. data/.vscode/settings.json +19 -0
  12. data/README.md +37 -0
  13. data/Rakefile +82 -0
  14. data/config.yml +1 -1
  15. data/core/enumerable.rbs +9 -0
  16. data/core/io.rbs +4 -4
  17. data/core/thread.rbs +0 -7
  18. data/ext/rbs_extension/ast_translation.c +1008 -1074
  19. data/ext/rbs_extension/class_constants.c +78 -74
  20. data/ext/rbs_extension/compat.h +3 -3
  21. data/ext/rbs_extension/extconf.rb +11 -1
  22. data/ext/rbs_extension/legacy_location.c +173 -172
  23. data/ext/rbs_extension/legacy_location.h +3 -3
  24. data/ext/rbs_extension/main.c +315 -273
  25. data/include/rbs/ast.h +11 -12
  26. data/include/rbs/defines.h +11 -12
  27. data/include/rbs/lexer.h +105 -105
  28. data/include/rbs/location.h +14 -14
  29. data/include/rbs/parser.h +21 -19
  30. data/include/rbs/string.h +3 -3
  31. data/include/rbs/util/rbs_allocator.h +14 -14
  32. data/include/rbs/util/rbs_constant_pool.h +3 -3
  33. data/include/rbs/util/rbs_encoding.h +1 -1
  34. data/lib/rbs/environment.rb +4 -0
  35. data/lib/rbs/namespace.rb +0 -7
  36. data/lib/rbs/parser_aux.rb +5 -0
  37. data/lib/rbs/type_name.rb +0 -7
  38. data/lib/rbs/types.rb +3 -1
  39. data/lib/rbs/unit_test/convertibles.rb +1 -0
  40. data/lib/rbs/version.rb +1 -1
  41. data/sig/environment.rbs +3 -0
  42. data/sig/namespace.rbs +0 -5
  43. data/sig/parser.rbs +20 -0
  44. data/sig/typename.rbs +0 -5
  45. data/sig/types.rbs +4 -1
  46. data/src/ast.c +216 -214
  47. data/src/lexer.c +2923 -2675
  48. data/src/lexstate.c +155 -155
  49. data/src/location.c +40 -40
  50. data/src/parser.c +2591 -2586
  51. data/src/string.c +2 -2
  52. data/src/util/rbs_allocator.c +7 -9
  53. data/src/util/rbs_assert.c +9 -9
  54. data/src/util/rbs_constant_pool.c +5 -7
  55. data/src/util/rbs_encoding.c +20095 -4056
  56. data/src/util/rbs_unescape.c +33 -32
  57. data/stdlib/json/0/json.rbs +9 -43
  58. data/stdlib/ripper/0/ripper.rbs +3 -0
  59. data/stdlib/socket/0/addrinfo.rbs +2 -2
  60. metadata +7 -2
@@ -16,31 +16,31 @@
16
16
  * ```
17
17
  * */
18
18
  static NORETURN(void) raise_error(rbs_error_t *error, VALUE buffer) {
19
- rbs_assert(error != NULL, "raise_error() called with NULL error");
19
+ rbs_assert(error != NULL, "raise_error() called with NULL error");
20
20
 
21
- if (!error->syntax_error) {
22
- rb_raise(rb_eRuntimeError, "Unexpected error");
23
- }
21
+ if (!error->syntax_error) {
22
+ rb_raise(rb_eRuntimeError, "Unexpected error");
23
+ }
24
24
 
25
- VALUE location = rbs_new_location(buffer, error->token.range);
26
- VALUE type = rb_str_new_cstr(rbs_token_type_str(error->token.type));
25
+ VALUE location = rbs_new_location(buffer, error->token.range);
26
+ VALUE type = rb_str_new_cstr(rbs_token_type_str(error->token.type));
27
27
 
28
- VALUE rb_error = rb_funcall(
29
- RBS_ParsingError,
30
- rb_intern("new"),
31
- 3,
32
- location,
33
- rb_str_new_cstr(error->message),
34
- type
35
- );
28
+ VALUE rb_error = rb_funcall(
29
+ RBS_ParsingError,
30
+ rb_intern("new"),
31
+ 3,
32
+ location,
33
+ rb_str_new_cstr(error->message),
34
+ type
35
+ );
36
36
 
37
- rb_exc_raise(rb_error);
37
+ rb_exc_raise(rb_error);
38
38
  }
39
39
 
40
40
  void raise_error_if_any(rbs_parser_t *parser, VALUE buffer) {
41
- if (parser->error != NULL) {
42
- raise_error(parser->error, buffer);
43
- }
41
+ if (parser->error != NULL) {
42
+ raise_error(parser->error, buffer);
43
+ }
44
44
  }
45
45
 
46
46
  /**
@@ -49,369 +49,411 @@ void raise_error_if_any(rbs_parser_t *parser, VALUE buffer) {
49
49
  * @param variables A Ruby Array of Symbols, or nil.
50
50
  */
51
51
  static void declare_type_variables(rbs_parser_t *parser, VALUE variables, VALUE buffer) {
52
- if (NIL_P(variables)) return; // Nothing to do.
52
+ if (NIL_P(variables)) return; // Nothing to do.
53
53
 
54
- if (!RB_TYPE_P(variables, T_ARRAY)) {
55
- rbs_parser_free(parser);
56
- rb_raise(rb_eTypeError,
57
- "wrong argument type %"PRIsVALUE" (must be an Array of Symbols or nil)",
58
- rb_obj_class(variables));
59
- }
54
+ if (!RB_TYPE_P(variables, T_ARRAY)) {
55
+ rbs_parser_free(parser);
56
+ rb_raise(rb_eTypeError, "wrong argument type %" PRIsVALUE " (must be an Array of Symbols or nil)", rb_obj_class(variables));
57
+ }
60
58
 
61
- rbs_parser_push_typevar_table(parser, true);
59
+ rbs_parser_push_typevar_table(parser, true);
62
60
 
63
- for (long i = 0; i < rb_array_len(variables); i++) {
64
- VALUE symbol = rb_ary_entry(variables, i);
61
+ for (long i = 0; i < rb_array_len(variables); i++) {
62
+ VALUE symbol = rb_ary_entry(variables, i);
65
63
 
66
- if (!RB_TYPE_P(symbol, T_SYMBOL)) {
67
- rbs_parser_free(parser);
68
- rb_raise(rb_eTypeError,
69
- "Type variables Array contains invalid value %"PRIsVALUE" of type %"PRIsVALUE" (must be an Array of Symbols or nil)",
70
- rb_inspect(symbol), rb_obj_class(symbol));
71
- }
64
+ if (!RB_TYPE_P(symbol, T_SYMBOL)) {
65
+ rbs_parser_free(parser);
66
+ rb_raise(rb_eTypeError, "Type variables Array contains invalid value %" PRIsVALUE " of type %" PRIsVALUE " (must be an Array of Symbols or nil)", rb_inspect(symbol), rb_obj_class(symbol));
67
+ }
72
68
 
73
- VALUE name_str = rb_sym2str(symbol);
69
+ VALUE name_str = rb_sym2str(symbol);
74
70
 
75
- rbs_constant_id_t id = rbs_constant_pool_insert_shared(
76
- &parser->constant_pool,
77
- (const uint8_t *) RSTRING_PTR(name_str),
78
- RSTRING_LEN(name_str)
79
- );
71
+ rbs_constant_id_t id = rbs_constant_pool_insert_shared(
72
+ &parser->constant_pool,
73
+ (const uint8_t *) RSTRING_PTR(name_str),
74
+ RSTRING_LEN(name_str)
75
+ );
80
76
 
81
- if (!rbs_parser_insert_typevar(parser, id)) {
82
- raise_error(parser->error, buffer);
77
+ if (!rbs_parser_insert_typevar(parser, id)) {
78
+ raise_error(parser->error, buffer);
79
+ }
83
80
  }
84
- }
85
81
  }
86
82
 
87
83
  struct parse_type_arg {
88
- VALUE buffer;
89
- rb_encoding *encoding;
90
- rbs_parser_t *parser;
91
- VALUE require_eof;
84
+ VALUE buffer;
85
+ rb_encoding *encoding;
86
+ rbs_parser_t *parser;
87
+ VALUE require_eof;
92
88
  };
93
89
 
94
90
  static VALUE ensure_free_parser(VALUE parser) {
95
- rbs_parser_free((rbs_parser_t *)parser);
96
- return Qnil;
91
+ rbs_parser_free((rbs_parser_t *) parser);
92
+ return Qnil;
97
93
  }
98
94
 
99
95
  static VALUE parse_type_try(VALUE a) {
100
- struct parse_type_arg *arg = (struct parse_type_arg *)a;
101
- rbs_parser_t *parser = arg->parser;
96
+ struct parse_type_arg *arg = (struct parse_type_arg *) a;
97
+ rbs_parser_t *parser = arg->parser;
102
98
 
103
- if (parser->next_token.type == pEOF) {
104
- return Qnil;
105
- }
99
+ if (parser->next_token.type == pEOF) {
100
+ return Qnil;
101
+ }
106
102
 
107
- rbs_node_t *type;
108
- rbs_parse_type(parser, &type);
103
+ rbs_node_t *type;
104
+ rbs_parse_type(parser, &type);
109
105
 
110
- raise_error_if_any(parser, arg->buffer);
106
+ raise_error_if_any(parser, arg->buffer);
111
107
 
112
- if (RB_TEST(arg->require_eof)) {
113
- rbs_parser_advance(parser);
114
- if (parser->current_token.type != pEOF) {
115
- rbs_parser_set_error(parser, parser->current_token, true, "expected a token `%s`", rbs_token_type_str(pEOF));
116
- raise_error(parser->error, arg->buffer);
108
+ if (RB_TEST(arg->require_eof)) {
109
+ rbs_parser_advance(parser);
110
+ if (parser->current_token.type != pEOF) {
111
+ rbs_parser_set_error(parser, parser->current_token, true, "expected a token `%s`", rbs_token_type_str(pEOF));
112
+ raise_error(parser->error, arg->buffer);
113
+ }
117
114
  }
118
- }
119
115
 
120
- rbs_translation_context_t ctx = rbs_translation_context_create(
121
- &parser->constant_pool,
122
- arg->buffer,
123
- arg->encoding
124
- );
116
+ rbs_translation_context_t ctx = rbs_translation_context_create(
117
+ &parser->constant_pool,
118
+ arg->buffer,
119
+ arg->encoding
120
+ );
125
121
 
126
- return rbs_struct_to_ruby_value(ctx, type);
122
+ return rbs_struct_to_ruby_value(ctx, type);
127
123
  }
128
124
 
129
125
  static rbs_lexer_t *alloc_lexer_from_buffer(rbs_allocator_t *allocator, VALUE string, rb_encoding *encoding, int start_pos, int end_pos) {
130
- if (start_pos < 0 || end_pos < 0) {
131
- rb_raise(rb_eArgError, "negative position range: %d...%d", start_pos, end_pos);
132
- }
133
-
134
- const char *encoding_name = rb_enc_name(encoding);
135
-
136
- return rbs_lexer_new(
137
- allocator,
138
- rbs_string_from_ruby_string(string),
139
- rbs_encoding_find((const uint8_t *) encoding_name, (const uint8_t *) (encoding_name + strlen(encoding_name))),
140
- start_pos,
141
- end_pos
142
- );
126
+ if (start_pos < 0 || end_pos < 0) {
127
+ rb_raise(rb_eArgError, "negative position range: %d...%d", start_pos, end_pos);
128
+ }
129
+
130
+ const char *encoding_name = rb_enc_name(encoding);
131
+
132
+ return rbs_lexer_new(
133
+ allocator,
134
+ rbs_string_from_ruby_string(string),
135
+ rbs_encoding_find((const uint8_t *) encoding_name, (const uint8_t *) (encoding_name + strlen(encoding_name))),
136
+ start_pos,
137
+ end_pos
138
+ );
143
139
  }
144
140
 
145
141
  static rbs_parser_t *alloc_parser_from_buffer(VALUE buffer, int start_pos, int end_pos) {
146
- if (start_pos < 0 || end_pos < 0) {
147
- rb_raise(rb_eArgError, "negative position range: %d...%d", start_pos, end_pos);
148
- }
149
-
150
- VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
151
- StringValue(string);
152
-
153
- rb_encoding *encoding = rb_enc_get(string);
154
- const char *encoding_name = rb_enc_name(encoding);
155
-
156
- return rbs_parser_new(
157
- rbs_string_from_ruby_string(string),
158
- rbs_encoding_find((const uint8_t *) encoding_name,
159
- (const uint8_t *) (encoding_name + strlen(encoding_name))),
160
- start_pos,
161
- end_pos
162
- );
142
+ if (start_pos < 0 || end_pos < 0) {
143
+ rb_raise(rb_eArgError, "negative position range: %d...%d", start_pos, end_pos);
144
+ }
145
+
146
+ VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
147
+ StringValue(string);
148
+
149
+ rb_encoding *encoding = rb_enc_get(string);
150
+ const char *encoding_name = rb_enc_name(encoding);
151
+
152
+ return rbs_parser_new(
153
+ rbs_string_from_ruby_string(string),
154
+ rbs_encoding_find((const uint8_t *) encoding_name, (const uint8_t *) (encoding_name + strlen(encoding_name))),
155
+ start_pos,
156
+ end_pos
157
+ );
163
158
  }
164
159
 
165
160
  static VALUE rbsparser_parse_type(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos, VALUE variables, VALUE require_eof) {
166
- VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
167
- StringValue(string);
168
- rb_encoding *encoding = rb_enc_get(string);
161
+ VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
162
+ StringValue(string);
163
+ rb_encoding *encoding = rb_enc_get(string);
169
164
 
170
- rbs_parser_t *parser = alloc_parser_from_buffer(buffer, FIX2INT(start_pos), FIX2INT(end_pos));
171
- declare_type_variables(parser, variables, buffer);
172
- struct parse_type_arg arg = {
173
- .buffer = buffer,
174
- .encoding = encoding,
175
- .parser = parser,
176
- .require_eof = require_eof
177
- };
165
+ rbs_parser_t *parser = alloc_parser_from_buffer(buffer, FIX2INT(start_pos), FIX2INT(end_pos));
166
+ declare_type_variables(parser, variables, buffer);
167
+ struct parse_type_arg arg = {
168
+ .buffer = buffer,
169
+ .encoding = encoding,
170
+ .parser = parser,
171
+ .require_eof = require_eof
172
+ };
178
173
 
179
- VALUE result = rb_ensure(parse_type_try, (VALUE)&arg, ensure_free_parser, (VALUE)parser);
174
+ VALUE result = rb_ensure(parse_type_try, (VALUE) &arg, ensure_free_parser, (VALUE) parser);
180
175
 
181
- RB_GC_GUARD(string);
176
+ RB_GC_GUARD(string);
182
177
 
183
- return result;
178
+ return result;
184
179
  }
185
180
 
186
181
  static VALUE parse_method_type_try(VALUE a) {
187
- struct parse_type_arg *arg = (struct parse_type_arg *)a;
188
- rbs_parser_t *parser = arg->parser;
182
+ struct parse_type_arg *arg = (struct parse_type_arg *) a;
183
+ rbs_parser_t *parser = arg->parser;
189
184
 
190
- if (parser->next_token.type == pEOF) {
191
- return Qnil;
192
- }
185
+ if (parser->next_token.type == pEOF) {
186
+ return Qnil;
187
+ }
193
188
 
194
- rbs_method_type_t *method_type = NULL;
195
- rbs_parse_method_type(parser, &method_type);
189
+ rbs_method_type_t *method_type = NULL;
190
+ rbs_parse_method_type(parser, &method_type);
196
191
 
197
- raise_error_if_any(parser, arg->buffer);
192
+ raise_error_if_any(parser, arg->buffer);
198
193
 
199
- if (RB_TEST(arg->require_eof)) {
200
- rbs_parser_advance(parser);
201
- if (parser->current_token.type != pEOF) {
202
- rbs_parser_set_error(parser, parser->current_token, true, "expected a token `%s`", rbs_token_type_str(pEOF));
203
- raise_error(parser->error, arg->buffer);
194
+ if (RB_TEST(arg->require_eof)) {
195
+ rbs_parser_advance(parser);
196
+ if (parser->current_token.type != pEOF) {
197
+ rbs_parser_set_error(parser, parser->current_token, true, "expected a token `%s`", rbs_token_type_str(pEOF));
198
+ raise_error(parser->error, arg->buffer);
199
+ }
204
200
  }
205
- }
206
201
 
207
- rbs_translation_context_t ctx = rbs_translation_context_create(
208
- &parser->constant_pool,
209
- arg->buffer,
210
- arg->encoding
211
- );
202
+ rbs_translation_context_t ctx = rbs_translation_context_create(
203
+ &parser->constant_pool,
204
+ arg->buffer,
205
+ arg->encoding
206
+ );
212
207
 
213
- return rbs_struct_to_ruby_value(ctx, (rbs_node_t *) method_type);
208
+ return rbs_struct_to_ruby_value(ctx, (rbs_node_t *) method_type);
214
209
  }
215
210
 
216
211
  static VALUE rbsparser_parse_method_type(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos, VALUE variables, VALUE require_eof) {
217
- VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
218
- StringValue(string);
219
- rb_encoding *encoding = rb_enc_get(string);
212
+ VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
213
+ StringValue(string);
214
+ rb_encoding *encoding = rb_enc_get(string);
220
215
 
221
- rbs_parser_t *parser = alloc_parser_from_buffer(buffer, FIX2INT(start_pos), FIX2INT(end_pos));
222
- declare_type_variables(parser, variables, buffer);
223
- struct parse_type_arg arg = {
224
- .buffer = buffer,
225
- .encoding = encoding,
226
- .parser = parser,
227
- .require_eof = require_eof
228
- };
216
+ rbs_parser_t *parser = alloc_parser_from_buffer(buffer, FIX2INT(start_pos), FIX2INT(end_pos));
217
+ declare_type_variables(parser, variables, buffer);
218
+ struct parse_type_arg arg = {
219
+ .buffer = buffer,
220
+ .encoding = encoding,
221
+ .parser = parser,
222
+ .require_eof = require_eof
223
+ };
229
224
 
230
- VALUE result = rb_ensure(parse_method_type_try, (VALUE)&arg, ensure_free_parser, (VALUE)parser);
225
+ VALUE result = rb_ensure(parse_method_type_try, (VALUE) &arg, ensure_free_parser, (VALUE) parser);
231
226
 
232
- RB_GC_GUARD(string);
227
+ RB_GC_GUARD(string);
233
228
 
234
- return result;
229
+ return result;
235
230
  }
236
231
 
237
232
  static VALUE parse_signature_try(VALUE a) {
238
- struct parse_type_arg *arg = (struct parse_type_arg *)a;
239
- rbs_parser_t *parser = arg->parser;
233
+ struct parse_type_arg *arg = (struct parse_type_arg *) a;
234
+ rbs_parser_t *parser = arg->parser;
240
235
 
241
- rbs_signature_t *signature = NULL;
242
- rbs_parse_signature(parser, &signature);
236
+ rbs_signature_t *signature = NULL;
237
+ rbs_parse_signature(parser, &signature);
243
238
 
244
- raise_error_if_any(parser, arg->buffer);
239
+ raise_error_if_any(parser, arg->buffer);
245
240
 
246
- rbs_translation_context_t ctx = rbs_translation_context_create(
247
- &parser->constant_pool,
248
- arg->buffer,
249
- arg->encoding
250
- );
241
+ rbs_translation_context_t ctx = rbs_translation_context_create(
242
+ &parser->constant_pool,
243
+ arg->buffer,
244
+ arg->encoding
245
+ );
251
246
 
252
- return rbs_struct_to_ruby_value(ctx, (rbs_node_t *) signature);
247
+ return rbs_struct_to_ruby_value(ctx, (rbs_node_t *) signature);
253
248
  }
254
249
 
255
250
  static VALUE rbsparser_parse_signature(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos) {
256
- VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
257
- StringValue(string);
258
- rb_encoding *encoding = rb_enc_get(string);
251
+ VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
252
+ StringValue(string);
253
+ rb_encoding *encoding = rb_enc_get(string);
259
254
 
260
- rbs_parser_t *parser = alloc_parser_from_buffer(buffer, FIX2INT(start_pos), FIX2INT(end_pos));
261
- struct parse_type_arg arg = {
262
- .buffer = buffer,
263
- .encoding = encoding,
264
- .parser = parser,
265
- .require_eof = false
266
- };
255
+ rbs_parser_t *parser = alloc_parser_from_buffer(buffer, FIX2INT(start_pos), FIX2INT(end_pos));
256
+ struct parse_type_arg arg = {
257
+ .buffer = buffer,
258
+ .encoding = encoding,
259
+ .parser = parser,
260
+ .require_eof = false
261
+ };
267
262
 
268
- VALUE result = rb_ensure(parse_signature_try, (VALUE)&arg, ensure_free_parser, (VALUE)parser);
263
+ VALUE result = rb_ensure(parse_signature_try, (VALUE) &arg, ensure_free_parser, (VALUE) parser);
269
264
 
270
- RB_GC_GUARD(string);
265
+ RB_GC_GUARD(string);
271
266
 
272
- return result;
267
+ return result;
268
+ }
269
+
270
+ struct parse_type_params_arg {
271
+ VALUE buffer;
272
+ rb_encoding *encoding;
273
+ rbs_parser_t *parser;
274
+ VALUE module_type_params;
275
+ };
276
+
277
+ static VALUE parse_type_params_try(VALUE a) {
278
+ struct parse_type_params_arg *arg = (struct parse_type_params_arg *) a;
279
+ rbs_parser_t *parser = arg->parser;
280
+
281
+ if (parser->next_token.type == pEOF) {
282
+ return Qnil;
283
+ }
284
+
285
+ rbs_node_list_t *params = NULL;
286
+ rbs_parse_type_params(parser, arg->module_type_params, &params);
287
+
288
+ raise_error_if_any(parser, arg->buffer);
289
+
290
+ rbs_translation_context_t ctx = rbs_translation_context_create(
291
+ &parser->constant_pool,
292
+ arg->buffer,
293
+ arg->encoding
294
+ );
295
+
296
+ return rbs_node_list_to_ruby_array(ctx, params);
297
+ }
298
+
299
+ static VALUE rbsparser_parse_type_params(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos, VALUE module_type_params) {
300
+ VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
301
+ StringValue(string);
302
+ rb_encoding *encoding = rb_enc_get(string);
303
+
304
+ rbs_parser_t *parser = alloc_parser_from_buffer(buffer, FIX2INT(start_pos), FIX2INT(end_pos));
305
+ struct parse_type_params_arg arg = {
306
+ .buffer = buffer,
307
+ .encoding = encoding,
308
+ .parser = parser,
309
+ .module_type_params = module_type_params
310
+ };
311
+
312
+ VALUE result = rb_ensure(parse_type_params_try, (VALUE) &arg, ensure_free_parser, (VALUE) parser);
313
+
314
+ RB_GC_GUARD(string);
315
+
316
+ return result;
273
317
  }
274
318
 
275
319
  static VALUE parse_inline_leading_annotation_try(VALUE a) {
276
- struct parse_type_arg *arg = (struct parse_type_arg *) a;
277
- rbs_parser_t *parser = arg->parser;
320
+ struct parse_type_arg *arg = (struct parse_type_arg *) a;
321
+ rbs_parser_t *parser = arg->parser;
278
322
 
279
- rbs_ast_ruby_annotations_t *annotation = NULL;
280
- bool success = rbs_parse_inline_leading_annotation(parser, &annotation);
323
+ rbs_ast_ruby_annotations_t *annotation = NULL;
324
+ bool success = rbs_parse_inline_leading_annotation(parser, &annotation);
281
325
 
282
- raise_error_if_any(parser, arg->buffer);
326
+ raise_error_if_any(parser, arg->buffer);
283
327
 
284
- if (!success || annotation == NULL) {
285
- return Qnil;
286
- }
328
+ if (!success || annotation == NULL) {
329
+ return Qnil;
330
+ }
287
331
 
288
- rbs_translation_context_t ctx = rbs_translation_context_create(
289
- &parser->constant_pool,
290
- arg->buffer,
291
- arg->encoding
292
- );
332
+ rbs_translation_context_t ctx = rbs_translation_context_create(
333
+ &parser->constant_pool,
334
+ arg->buffer,
335
+ arg->encoding
336
+ );
293
337
 
294
- return rbs_struct_to_ruby_value(ctx, (rbs_node_t *) annotation);
338
+ return rbs_struct_to_ruby_value(ctx, (rbs_node_t *) annotation);
295
339
  }
296
340
 
297
341
  static VALUE rbsparser_parse_inline_leading_annotation(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos, VALUE variables) {
298
- VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
299
- StringValue(string);
300
- rb_encoding *encoding = rb_enc_get(string);
342
+ VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
343
+ StringValue(string);
344
+ rb_encoding *encoding = rb_enc_get(string);
301
345
 
302
- rbs_parser_t *parser = alloc_parser_from_buffer(buffer, FIX2INT(start_pos), FIX2INT(end_pos));
303
- declare_type_variables(parser, variables, buffer);
304
- struct parse_type_arg arg = {
305
- .buffer = buffer,
306
- .encoding = encoding,
307
- .parser = parser,
308
- .require_eof = Qfalse
309
- };
346
+ rbs_parser_t *parser = alloc_parser_from_buffer(buffer, FIX2INT(start_pos), FIX2INT(end_pos));
347
+ declare_type_variables(parser, variables, buffer);
348
+ struct parse_type_arg arg = {
349
+ .buffer = buffer,
350
+ .encoding = encoding,
351
+ .parser = parser,
352
+ .require_eof = Qfalse
353
+ };
310
354
 
311
- VALUE result = rb_ensure(parse_inline_leading_annotation_try, (VALUE)&arg, ensure_free_parser, (VALUE)parser);
355
+ VALUE result = rb_ensure(parse_inline_leading_annotation_try, (VALUE) &arg, ensure_free_parser, (VALUE) parser);
312
356
 
313
- RB_GC_GUARD(string);
357
+ RB_GC_GUARD(string);
314
358
 
315
- return result;
359
+ return result;
316
360
  }
317
361
 
318
362
  static VALUE parse_inline_trailing_annotation_try(VALUE a) {
319
- struct parse_type_arg *arg = (struct parse_type_arg *) a;
320
- rbs_parser_t *parser = arg->parser;
363
+ struct parse_type_arg *arg = (struct parse_type_arg *) a;
364
+ rbs_parser_t *parser = arg->parser;
321
365
 
322
- rbs_ast_ruby_annotations_t *annotation = NULL;
323
- bool success = rbs_parse_inline_trailing_annotation(parser, &annotation);
366
+ rbs_ast_ruby_annotations_t *annotation = NULL;
367
+ bool success = rbs_parse_inline_trailing_annotation(parser, &annotation);
324
368
 
325
- raise_error_if_any(parser, arg->buffer);
369
+ raise_error_if_any(parser, arg->buffer);
326
370
 
327
- if (!success || annotation == NULL) {
328
- return Qnil;
329
- }
371
+ if (!success || annotation == NULL) {
372
+ return Qnil;
373
+ }
330
374
 
331
- rbs_translation_context_t ctx = rbs_translation_context_create(
332
- &parser->constant_pool,
333
- arg->buffer,
334
- arg->encoding
335
- );
375
+ rbs_translation_context_t ctx = rbs_translation_context_create(
376
+ &parser->constant_pool,
377
+ arg->buffer,
378
+ arg->encoding
379
+ );
336
380
 
337
- return rbs_struct_to_ruby_value(ctx, (rbs_node_t *) annotation);
381
+ return rbs_struct_to_ruby_value(ctx, (rbs_node_t *) annotation);
338
382
  }
339
383
 
340
384
  static VALUE rbsparser_parse_inline_trailing_annotation(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos, VALUE variables) {
341
- VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
342
- StringValue(string);
343
- rb_encoding *encoding = rb_enc_get(string);
385
+ VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
386
+ StringValue(string);
387
+ rb_encoding *encoding = rb_enc_get(string);
344
388
 
345
- rbs_parser_t *parser = alloc_parser_from_buffer(buffer, FIX2INT(start_pos), FIX2INT(end_pos));
346
- declare_type_variables(parser, variables, buffer);
347
- struct parse_type_arg arg = {
348
- .buffer = buffer,
349
- .encoding = encoding,
350
- .parser = parser,
351
- .require_eof = Qfalse
352
- };
389
+ rbs_parser_t *parser = alloc_parser_from_buffer(buffer, FIX2INT(start_pos), FIX2INT(end_pos));
390
+ declare_type_variables(parser, variables, buffer);
391
+ struct parse_type_arg arg = {
392
+ .buffer = buffer,
393
+ .encoding = encoding,
394
+ .parser = parser,
395
+ .require_eof = Qfalse
396
+ };
353
397
 
354
- VALUE result = rb_ensure(parse_inline_trailing_annotation_try, (VALUE) &arg, ensure_free_parser, (VALUE) parser);
398
+ VALUE result = rb_ensure(parse_inline_trailing_annotation_try, (VALUE) &arg, ensure_free_parser, (VALUE) parser);
355
399
 
356
- RB_GC_GUARD(string);
400
+ RB_GC_GUARD(string);
357
401
 
358
- return result;
402
+ return result;
359
403
  }
360
404
 
361
405
  static VALUE rbsparser_lex(VALUE self, VALUE buffer, VALUE end_pos) {
362
- VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
363
- StringValue(string);
364
- rb_encoding *encoding = rb_enc_get(string);
365
-
366
- rbs_allocator_t *allocator = rbs_allocator_init();
367
- rbs_lexer_t *lexer = alloc_lexer_from_buffer(allocator, string, encoding, 0, FIX2INT(end_pos));
368
-
369
- VALUE results = rb_ary_new();
370
- rbs_token_t token = NullToken;
371
- while (token.type != pEOF) {
372
- token = rbs_lexer_next_token(lexer);
373
- VALUE type = ID2SYM(rb_intern(rbs_token_type_str(token.type)));
374
- VALUE location = rbs_new_location(buffer, token.range);
375
- VALUE pair = rb_ary_new3(2, type, location);
376
- rb_ary_push(results, pair);
377
- }
378
-
379
- rbs_allocator_free(allocator);
380
- RB_GC_GUARD(string);
381
-
382
- return results;
406
+ VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
407
+ StringValue(string);
408
+ rb_encoding *encoding = rb_enc_get(string);
409
+
410
+ rbs_allocator_t *allocator = rbs_allocator_init();
411
+ rbs_lexer_t *lexer = alloc_lexer_from_buffer(allocator, string, encoding, 0, FIX2INT(end_pos));
412
+
413
+ VALUE results = rb_ary_new();
414
+ rbs_token_t token = NullToken;
415
+ while (token.type != pEOF) {
416
+ token = rbs_lexer_next_token(lexer);
417
+ VALUE type = ID2SYM(rb_intern(rbs_token_type_str(token.type)));
418
+ VALUE location = rbs_new_location(buffer, token.range);
419
+ VALUE pair = rb_ary_new3(2, type, location);
420
+ rb_ary_push(results, pair);
421
+ }
422
+
423
+ rbs_allocator_free(allocator);
424
+ RB_GC_GUARD(string);
425
+
426
+ return results;
383
427
  }
384
428
 
385
429
  void rbs__init_parser(void) {
386
- RBS_Parser = rb_define_class_under(RBS, "Parser", rb_cObject);
387
- rb_gc_register_mark_object(RBS_Parser);
388
- VALUE empty_array = rb_obj_freeze(rb_ary_new());
389
- rb_gc_register_mark_object(empty_array);
390
-
391
- rb_define_singleton_method(RBS_Parser, "_parse_type", rbsparser_parse_type, 5);
392
- rb_define_singleton_method(RBS_Parser, "_parse_method_type", rbsparser_parse_method_type, 5);
393
- rb_define_singleton_method(RBS_Parser, "_parse_signature", rbsparser_parse_signature, 3);
394
- rb_define_singleton_method(RBS_Parser, "_parse_inline_leading_annotation", rbsparser_parse_inline_leading_annotation, 4);
395
- rb_define_singleton_method(RBS_Parser, "_parse_inline_trailing_annotation", rbsparser_parse_inline_trailing_annotation, 4);
396
- rb_define_singleton_method(RBS_Parser, "_lex", rbsparser_lex, 2);
430
+ RBS_Parser = rb_define_class_under(RBS, "Parser", rb_cObject);
431
+ rb_gc_register_mark_object(RBS_Parser);
432
+ VALUE empty_array = rb_obj_freeze(rb_ary_new());
433
+ rb_gc_register_mark_object(empty_array);
434
+
435
+ rb_define_singleton_method(RBS_Parser, "_parse_type", rbsparser_parse_type, 5);
436
+ rb_define_singleton_method(RBS_Parser, "_parse_method_type", rbsparser_parse_method_type, 5);
437
+ rb_define_singleton_method(RBS_Parser, "_parse_signature", rbsparser_parse_signature, 3);
438
+ rb_define_singleton_method(RBS_Parser, "_parse_type_params", rbsparser_parse_type_params, 4);
439
+ rb_define_singleton_method(RBS_Parser, "_parse_inline_leading_annotation", rbsparser_parse_inline_leading_annotation, 4);
440
+ rb_define_singleton_method(RBS_Parser, "_parse_inline_trailing_annotation", rbsparser_parse_inline_trailing_annotation, 4);
441
+ rb_define_singleton_method(RBS_Parser, "_lex", rbsparser_lex, 2);
397
442
  }
398
443
 
399
- static
400
- void Deinit_rbs_extension(ruby_vm_t *_) {
401
- rbs_constant_pool_free(RBS_GLOBAL_CONSTANT_POOL);
444
+ static void Deinit_rbs_extension(ruby_vm_t *_) {
445
+ rbs_constant_pool_free(RBS_GLOBAL_CONSTANT_POOL);
402
446
  }
403
447
 
404
- void
405
- Init_rbs_extension(void)
406
- {
448
+ void Init_rbs_extension(void) {
407
449
  #ifdef HAVE_RB_EXT_RACTOR_SAFE
408
- rb_ext_ractor_safe(true);
450
+ rb_ext_ractor_safe(true);
409
451
  #endif
410
- rbs__init_constants();
411
- rbs__init_location();
412
- rbs__init_parser();
452
+ rbs__init_constants();
453
+ rbs__init_location();
454
+ rbs__init_parser();
413
455
 
414
- /* Calculated based on the number of unique strings used with the `INTERN` macro in `parser.c`.
456
+ /* Calculated based on the number of unique strings used with the `INTERN` macro in `parser.c`.
415
457
  *
416
458
  * ```bash
417
459
  * grep -o 'INTERN("\([^"]*\)")' ext/rbs_extension/parser.c \
@@ -420,8 +462,8 @@ Init_rbs_extension(void)
420
462
  * | wc -l
421
463
  * ```
422
464
  */
423
- const size_t num_uniquely_interned_strings = 26;
424
- rbs_constant_pool_init(RBS_GLOBAL_CONSTANT_POOL, num_uniquely_interned_strings);
465
+ const size_t num_uniquely_interned_strings = 26;
466
+ rbs_constant_pool_init(RBS_GLOBAL_CONSTANT_POOL, num_uniquely_interned_strings);
425
467
 
426
- ruby_vm_at_exit(Deinit_rbs_extension);
468
+ ruby_vm_at_exit(Deinit_rbs_extension);
427
469
  }