rbs 4.0.0.dev.1 → 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.
- checksums.yaml +4 -4
- data/.clang-format +74 -0
- data/.clangd +2 -0
- data/.github/workflows/c-check.yml +51 -0
- data/.github/workflows/dependabot.yml +1 -1
- data/.github/workflows/ruby.yml +28 -17
- data/.github/workflows/typecheck.yml +0 -2
- data/.gitignore +4 -0
- data/.rubocop.yml +1 -1
- data/.vscode/extensions.json +5 -0
- data/.vscode/settings.json +19 -0
- data/README.md +37 -0
- data/Rakefile +90 -0
- data/config.yml +1 -1
- data/core/enumerable.rbs +9 -0
- data/core/io.rbs +4 -4
- data/core/thread.rbs +0 -7
- data/ext/rbs_extension/ast_translation.c +1010 -1074
- data/ext/rbs_extension/ast_translation.h +4 -0
- data/ext/rbs_extension/class_constants.c +78 -74
- data/ext/rbs_extension/class_constants.h +4 -0
- data/ext/rbs_extension/compat.h +10 -0
- data/ext/rbs_extension/extconf.rb +15 -1
- data/ext/rbs_extension/legacy_location.c +173 -172
- data/ext/rbs_extension/legacy_location.h +8 -3
- data/ext/rbs_extension/main.c +315 -273
- data/ext/rbs_extension/rbs_extension.h +3 -0
- data/ext/rbs_extension/rbs_string_bridging.h +4 -0
- data/include/rbs/ast.h +11 -12
- data/include/rbs/defines.h +11 -12
- data/include/rbs/lexer.h +108 -108
- data/include/rbs/location.h +14 -14
- data/include/rbs/parser.h +21 -19
- data/include/rbs/string.h +3 -3
- data/include/rbs/util/rbs_allocator.h +14 -14
- data/include/rbs/util/rbs_constant_pool.h +3 -3
- data/include/rbs/util/rbs_encoding.h +1 -1
- data/lib/rbs/environment.rb +4 -0
- data/lib/rbs/namespace.rb +0 -7
- data/lib/rbs/parser_aux.rb +5 -0
- data/lib/rbs/type_name.rb +0 -7
- data/lib/rbs/types.rb +3 -1
- data/lib/rbs/unit_test/convertibles.rb +1 -0
- data/lib/rbs/version.rb +1 -1
- data/sig/environment.rbs +3 -0
- data/sig/namespace.rbs +0 -5
- data/sig/parser.rbs +20 -0
- data/sig/typename.rbs +0 -5
- data/sig/types.rbs +4 -1
- data/src/ast.c +216 -214
- data/src/lexer.c +2923 -2675
- data/src/lexstate.c +157 -157
- data/src/location.c +40 -40
- data/src/parser.c +2591 -2586
- data/src/string.c +2 -2
- data/src/util/rbs_allocator.c +7 -9
- data/src/util/rbs_assert.c +9 -9
- data/src/util/rbs_constant_pool.c +5 -7
- data/src/util/rbs_encoding.c +20095 -4056
- data/src/util/rbs_unescape.c +33 -32
- data/stdlib/json/0/json.rbs +9 -43
- data/stdlib/ripper/0/ripper.rbs +3 -0
- data/stdlib/socket/0/addrinfo.rbs +2 -2
- metadata +8 -2
data/ext/rbs_extension/main.c
CHANGED
@@ -16,31 +16,31 @@
|
|
16
16
|
* ```
|
17
17
|
* */
|
18
18
|
static NORETURN(void) raise_error(rbs_error_t *error, VALUE buffer) {
|
19
|
-
|
19
|
+
rbs_assert(error != NULL, "raise_error() called with NULL error");
|
20
20
|
|
21
|
-
|
22
|
-
|
23
|
-
|
21
|
+
if (!error->syntax_error) {
|
22
|
+
rb_raise(rb_eRuntimeError, "Unexpected error");
|
23
|
+
}
|
24
24
|
|
25
|
-
|
26
|
-
|
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
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
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
|
-
|
37
|
+
rb_exc_raise(rb_error);
|
38
38
|
}
|
39
39
|
|
40
40
|
void raise_error_if_any(rbs_parser_t *parser, VALUE buffer) {
|
41
|
-
|
42
|
-
|
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
|
-
|
52
|
+
if (NIL_P(variables)) return; // Nothing to do.
|
53
53
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
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
|
-
|
59
|
+
rbs_parser_push_typevar_table(parser, true);
|
62
60
|
|
63
|
-
|
64
|
-
|
61
|
+
for (long i = 0; i < rb_array_len(variables); i++) {
|
62
|
+
VALUE symbol = rb_ary_entry(variables, i);
|
65
63
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
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
|
-
|
69
|
+
VALUE name_str = rb_sym2str(symbol);
|
74
70
|
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
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
|
-
|
82
|
-
|
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
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
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
|
-
|
96
|
-
|
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
|
-
|
101
|
-
|
96
|
+
struct parse_type_arg *arg = (struct parse_type_arg *) a;
|
97
|
+
rbs_parser_t *parser = arg->parser;
|
102
98
|
|
103
|
-
|
104
|
-
|
105
|
-
|
99
|
+
if (parser->next_token.type == pEOF) {
|
100
|
+
return Qnil;
|
101
|
+
}
|
106
102
|
|
107
|
-
|
108
|
-
|
103
|
+
rbs_node_t *type;
|
104
|
+
rbs_parse_type(parser, &type);
|
109
105
|
|
110
|
-
|
106
|
+
raise_error_if_any(parser, arg->buffer);
|
111
107
|
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
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
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
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
|
-
|
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
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
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
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
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
|
-
|
167
|
-
|
168
|
-
|
161
|
+
VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
|
162
|
+
StringValue(string);
|
163
|
+
rb_encoding *encoding = rb_enc_get(string);
|
169
164
|
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
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
|
-
|
174
|
+
VALUE result = rb_ensure(parse_type_try, (VALUE) &arg, ensure_free_parser, (VALUE) parser);
|
180
175
|
|
181
|
-
|
176
|
+
RB_GC_GUARD(string);
|
182
177
|
|
183
|
-
|
178
|
+
return result;
|
184
179
|
}
|
185
180
|
|
186
181
|
static VALUE parse_method_type_try(VALUE a) {
|
187
|
-
|
188
|
-
|
182
|
+
struct parse_type_arg *arg = (struct parse_type_arg *) a;
|
183
|
+
rbs_parser_t *parser = arg->parser;
|
189
184
|
|
190
|
-
|
191
|
-
|
192
|
-
|
185
|
+
if (parser->next_token.type == pEOF) {
|
186
|
+
return Qnil;
|
187
|
+
}
|
193
188
|
|
194
|
-
|
195
|
-
|
189
|
+
rbs_method_type_t *method_type = NULL;
|
190
|
+
rbs_parse_method_type(parser, &method_type);
|
196
191
|
|
197
|
-
|
192
|
+
raise_error_if_any(parser, arg->buffer);
|
198
193
|
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
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
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
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
|
-
|
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
|
-
|
218
|
-
|
219
|
-
|
212
|
+
VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
|
213
|
+
StringValue(string);
|
214
|
+
rb_encoding *encoding = rb_enc_get(string);
|
220
215
|
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
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
|
-
|
225
|
+
VALUE result = rb_ensure(parse_method_type_try, (VALUE) &arg, ensure_free_parser, (VALUE) parser);
|
231
226
|
|
232
|
-
|
227
|
+
RB_GC_GUARD(string);
|
233
228
|
|
234
|
-
|
229
|
+
return result;
|
235
230
|
}
|
236
231
|
|
237
232
|
static VALUE parse_signature_try(VALUE a) {
|
238
|
-
|
239
|
-
|
233
|
+
struct parse_type_arg *arg = (struct parse_type_arg *) a;
|
234
|
+
rbs_parser_t *parser = arg->parser;
|
240
235
|
|
241
|
-
|
242
|
-
|
236
|
+
rbs_signature_t *signature = NULL;
|
237
|
+
rbs_parse_signature(parser, &signature);
|
243
238
|
|
244
|
-
|
239
|
+
raise_error_if_any(parser, arg->buffer);
|
245
240
|
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
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
|
-
|
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
|
-
|
257
|
-
|
258
|
-
|
251
|
+
VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
|
252
|
+
StringValue(string);
|
253
|
+
rb_encoding *encoding = rb_enc_get(string);
|
259
254
|
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
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
|
-
|
263
|
+
VALUE result = rb_ensure(parse_signature_try, (VALUE) &arg, ensure_free_parser, (VALUE) parser);
|
269
264
|
|
270
|
-
|
265
|
+
RB_GC_GUARD(string);
|
271
266
|
|
272
|
-
|
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, ¶ms);
|
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
|
-
|
277
|
-
|
320
|
+
struct parse_type_arg *arg = (struct parse_type_arg *) a;
|
321
|
+
rbs_parser_t *parser = arg->parser;
|
278
322
|
|
279
|
-
|
280
|
-
|
323
|
+
rbs_ast_ruby_annotations_t *annotation = NULL;
|
324
|
+
bool success = rbs_parse_inline_leading_annotation(parser, &annotation);
|
281
325
|
|
282
|
-
|
326
|
+
raise_error_if_any(parser, arg->buffer);
|
283
327
|
|
284
|
-
|
285
|
-
|
286
|
-
|
328
|
+
if (!success || annotation == NULL) {
|
329
|
+
return Qnil;
|
330
|
+
}
|
287
331
|
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
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
|
-
|
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
|
-
|
299
|
-
|
300
|
-
|
342
|
+
VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
|
343
|
+
StringValue(string);
|
344
|
+
rb_encoding *encoding = rb_enc_get(string);
|
301
345
|
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
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
|
-
|
355
|
+
VALUE result = rb_ensure(parse_inline_leading_annotation_try, (VALUE) &arg, ensure_free_parser, (VALUE) parser);
|
312
356
|
|
313
|
-
|
357
|
+
RB_GC_GUARD(string);
|
314
358
|
|
315
|
-
|
359
|
+
return result;
|
316
360
|
}
|
317
361
|
|
318
362
|
static VALUE parse_inline_trailing_annotation_try(VALUE a) {
|
319
|
-
|
320
|
-
|
363
|
+
struct parse_type_arg *arg = (struct parse_type_arg *) a;
|
364
|
+
rbs_parser_t *parser = arg->parser;
|
321
365
|
|
322
|
-
|
323
|
-
|
366
|
+
rbs_ast_ruby_annotations_t *annotation = NULL;
|
367
|
+
bool success = rbs_parse_inline_trailing_annotation(parser, &annotation);
|
324
368
|
|
325
|
-
|
369
|
+
raise_error_if_any(parser, arg->buffer);
|
326
370
|
|
327
|
-
|
328
|
-
|
329
|
-
|
371
|
+
if (!success || annotation == NULL) {
|
372
|
+
return Qnil;
|
373
|
+
}
|
330
374
|
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
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
|
-
|
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
|
-
|
342
|
-
|
343
|
-
|
385
|
+
VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
|
386
|
+
StringValue(string);
|
387
|
+
rb_encoding *encoding = rb_enc_get(string);
|
344
388
|
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
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
|
-
|
398
|
+
VALUE result = rb_ensure(parse_inline_trailing_annotation_try, (VALUE) &arg, ensure_free_parser, (VALUE) parser);
|
355
399
|
|
356
|
-
|
400
|
+
RB_GC_GUARD(string);
|
357
401
|
|
358
|
-
|
402
|
+
return result;
|
359
403
|
}
|
360
404
|
|
361
405
|
static VALUE rbsparser_lex(VALUE self, VALUE buffer, VALUE end_pos) {
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
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
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
|
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
|
-
|
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
|
-
|
450
|
+
rb_ext_ractor_safe(true);
|
409
451
|
#endif
|
410
|
-
|
411
|
-
|
412
|
-
|
452
|
+
rbs__init_constants();
|
453
|
+
rbs__init_location();
|
454
|
+
rbs__init_parser();
|
413
455
|
|
414
|
-
|
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
|
-
|
424
|
-
|
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
|
-
|
468
|
+
ruby_vm_at_exit(Deinit_rbs_extension);
|
427
469
|
}
|