rbs 3.9.2 → 4.0.0.dev.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/workflows/ruby.yml +1 -1
- data/.github/workflows/windows.yml +1 -1
- data/CHANGELOG.md +0 -13
- data/Rakefile +28 -21
- data/Steepfile +1 -0
- data/config.yml +232 -62
- data/ext/rbs_extension/ast_translation.c +1149 -0
- data/ext/rbs_extension/ast_translation.h +30 -0
- data/{src/constants.c → ext/rbs_extension/class_constants.c} +15 -1
- data/{include/rbs/constants.h → ext/rbs_extension/class_constants.h} +10 -1
- data/ext/rbs_extension/extconf.rb +3 -1
- data/ext/rbs_extension/{location.c → legacy_location.c} +25 -34
- data/ext/rbs_extension/legacy_location.h +40 -0
- data/ext/rbs_extension/main.c +402 -8
- data/ext/rbs_extension/rbs_extension.h +3 -21
- data/ext/rbs_extension/rbs_string_bridging.c +9 -0
- data/ext/rbs_extension/rbs_string_bridging.h +20 -0
- data/include/rbs/ast.h +748 -0
- data/include/rbs/defines.h +60 -0
- data/{ext/rbs_extension → include/rbs}/lexer.h +40 -32
- data/include/rbs/location.h +59 -0
- data/include/rbs/parser.h +151 -0
- data/include/rbs/string.h +49 -0
- data/include/rbs/util/rbs_allocator.h +38 -0
- data/include/rbs/util/rbs_assert.h +9 -0
- data/include/rbs/util/rbs_buffer.h +83 -0
- data/include/rbs/util/rbs_constant_pool.h +3 -64
- data/include/rbs/util/rbs_encoding.h +280 -0
- data/include/rbs/util/rbs_unescape.h +23 -0
- data/include/rbs.h +1 -2
- data/lib/rbs/annotate/formatter.rb +3 -13
- data/lib/rbs/annotate/rdoc_annotator.rb +3 -1
- data/lib/rbs/annotate/rdoc_source.rb +1 -1
- data/lib/rbs/ast/ruby/annotations.rb +119 -0
- data/lib/rbs/ast/ruby/comment_block.rb +221 -0
- data/lib/rbs/ast/ruby/declarations.rb +86 -0
- data/lib/rbs/ast/ruby/helpers/constant_helper.rb +24 -0
- data/lib/rbs/ast/ruby/helpers/location_helper.rb +15 -0
- data/lib/rbs/ast/ruby/members.rb +213 -0
- data/lib/rbs/buffer.rb +104 -24
- data/lib/rbs/cli/validate.rb +39 -34
- data/lib/rbs/cli.rb +4 -5
- data/lib/rbs/definition.rb +6 -1
- data/lib/rbs/definition_builder/ancestor_builder.rb +63 -60
- data/lib/rbs/definition_builder/method_builder.rb +45 -30
- data/lib/rbs/definition_builder.rb +44 -9
- data/lib/rbs/environment/class_entry.rb +69 -0
- data/lib/rbs/environment/module_entry.rb +66 -0
- data/lib/rbs/environment.rb +185 -154
- data/lib/rbs/environment_loader.rb +2 -2
- data/lib/rbs/errors.rb +4 -3
- data/lib/rbs/inline_parser/comment_association.rb +117 -0
- data/lib/rbs/inline_parser.rb +206 -0
- data/lib/rbs/location_aux.rb +35 -3
- data/lib/rbs/parser_aux.rb +11 -1
- data/lib/rbs/prototype/runtime.rb +2 -2
- data/lib/rbs/source.rb +99 -0
- data/lib/rbs/subtractor.rb +4 -3
- data/lib/rbs/version.rb +1 -1
- data/lib/rbs.rb +12 -0
- data/lib/rdoc/discover.rb +1 -1
- data/lib/rdoc_plugin/parser.rb +2 -2
- data/rbs.gemspec +1 -0
- data/sig/ancestor_builder.rbs +1 -1
- data/sig/annotate/formatter.rbs +2 -2
- data/sig/annotate/rdoc_annotater.rbs +1 -1
- data/sig/ast/ruby/annotations.rbs +110 -0
- data/sig/ast/ruby/comment_block.rbs +119 -0
- data/sig/ast/ruby/declarations.rbs +60 -0
- data/sig/ast/ruby/helpers/constant_helper.rbs +11 -0
- data/sig/ast/ruby/helpers/location_helper.rbs +15 -0
- data/sig/ast/ruby/members.rbs +72 -0
- data/sig/buffer.rbs +63 -5
- data/sig/definition.rbs +1 -0
- data/sig/definition_builder.rbs +1 -1
- data/sig/environment/class_entry.rbs +50 -0
- data/sig/environment/module_entry.rbs +50 -0
- data/sig/environment.rbs +22 -76
- data/sig/errors.rbs +13 -6
- data/sig/inline_parser/comment_association.rbs +71 -0
- data/sig/inline_parser.rbs +87 -0
- data/sig/location.rbs +32 -7
- data/sig/method_builder.rbs +7 -4
- data/sig/parser.rbs +16 -0
- data/sig/source.rbs +48 -0
- data/src/ast.c +1345 -0
- data/src/lexer.c +2867 -0
- data/src/lexer.re +151 -0
- data/{ext/rbs_extension → src}/lexstate.c +58 -42
- data/src/location.c +71 -0
- data/src/parser.c +3739 -0
- data/src/string.c +89 -0
- data/src/util/rbs_allocator.c +149 -0
- data/src/util/rbs_assert.c +19 -0
- data/src/util/rbs_buffer.c +54 -0
- data/src/util/rbs_constant_pool.c +13 -81
- data/src/util/rbs_encoding.c +5273 -0
- data/src/util/rbs_unescape.c +130 -0
- data/stdlib/rdoc/0/code_object.rbs +2 -2
- data/stdlib/rdoc/0/comment.rbs +2 -0
- data/stdlib/rdoc/0/options.rbs +76 -0
- data/stdlib/rdoc/0/rdoc.rbs +6 -4
- data/stdlib/rdoc/0/store.rbs +1 -1
- metadata +70 -17
- data/ext/rbs_extension/lexer.c +0 -2728
- data/ext/rbs_extension/lexer.re +0 -147
- data/ext/rbs_extension/location.h +0 -85
- data/ext/rbs_extension/parser.c +0 -2982
- data/ext/rbs_extension/parser.h +0 -18
- data/ext/rbs_extension/parserstate.c +0 -411
- data/ext/rbs_extension/parserstate.h +0 -163
- data/ext/rbs_extension/unescape.c +0 -32
- data/include/rbs/ruby_objs.h +0 -72
- data/src/ruby_objs.c +0 -799
data/ext/rbs_extension/main.c
CHANGED
@@ -1,8 +1,401 @@
|
|
1
1
|
#include "rbs_extension.h"
|
2
|
+
#include "rbs/util/rbs_assert.h"
|
3
|
+
#include "rbs/util/rbs_allocator.h"
|
2
4
|
#include "rbs/util/rbs_constant_pool.h"
|
5
|
+
#include "ast_translation.h"
|
6
|
+
#include "legacy_location.h"
|
7
|
+
#include "rbs_string_bridging.h"
|
3
8
|
|
4
9
|
#include "ruby/vm.h"
|
5
10
|
|
11
|
+
/**
|
12
|
+
* Raises `RBS::ParsingError` or `RuntimeError` on `tok` with message constructed with given `fmt`.
|
13
|
+
*
|
14
|
+
* ```
|
15
|
+
* foo.rbs:11:21...11:25: Syntax error: {message}, token=`{tok source}` ({tok type})
|
16
|
+
* ```
|
17
|
+
* */
|
18
|
+
static NORETURN(void) raise_error(rbs_error_t *error, VALUE buffer) {
|
19
|
+
rbs_assert(error != NULL, "raise_error() called with NULL error");
|
20
|
+
|
21
|
+
if (!error->syntax_error) {
|
22
|
+
rb_raise(rb_eRuntimeError, "Unexpected error");
|
23
|
+
}
|
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));
|
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
|
+
);
|
36
|
+
|
37
|
+
rb_exc_raise(rb_error);
|
38
|
+
}
|
39
|
+
|
40
|
+
void raise_error_if_any(rbs_parser_t *parser, VALUE buffer) {
|
41
|
+
if (parser->error != NULL) {
|
42
|
+
raise_error(parser->error, buffer);
|
43
|
+
}
|
44
|
+
}
|
45
|
+
|
46
|
+
/**
|
47
|
+
* Inserts the given array of type variables names into the parser's type variable table.
|
48
|
+
* @param parser
|
49
|
+
* @param variables A Ruby Array of Symbols, or nil.
|
50
|
+
*/
|
51
|
+
static void declare_type_variables(rbs_parser_t *parser, VALUE variables, VALUE buffer) {
|
52
|
+
if (NIL_P(variables)) return; // Nothing to do.
|
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
|
+
}
|
60
|
+
|
61
|
+
rbs_parser_push_typevar_table(parser, true);
|
62
|
+
|
63
|
+
for (long i = 0; i < rb_array_len(variables); i++) {
|
64
|
+
VALUE symbol = rb_ary_entry(variables, i);
|
65
|
+
|
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
|
+
}
|
72
|
+
|
73
|
+
VALUE name_str = rb_sym2str(symbol);
|
74
|
+
|
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
|
+
);
|
80
|
+
|
81
|
+
if (!rbs_parser_insert_typevar(parser, id)) {
|
82
|
+
raise_error(parser->error, buffer);
|
83
|
+
}
|
84
|
+
}
|
85
|
+
}
|
86
|
+
|
87
|
+
struct parse_type_arg {
|
88
|
+
VALUE buffer;
|
89
|
+
rb_encoding *encoding;
|
90
|
+
rbs_parser_t *parser;
|
91
|
+
VALUE require_eof;
|
92
|
+
};
|
93
|
+
|
94
|
+
static VALUE ensure_free_parser(VALUE parser) {
|
95
|
+
rbs_parser_free((rbs_parser_t *)parser);
|
96
|
+
return Qnil;
|
97
|
+
}
|
98
|
+
|
99
|
+
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;
|
102
|
+
|
103
|
+
if (parser->next_token.type == pEOF) {
|
104
|
+
return Qnil;
|
105
|
+
}
|
106
|
+
|
107
|
+
rbs_node_t *type;
|
108
|
+
rbs_parse_type(parser, &type);
|
109
|
+
|
110
|
+
raise_error_if_any(parser, arg->buffer);
|
111
|
+
|
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);
|
117
|
+
}
|
118
|
+
}
|
119
|
+
|
120
|
+
rbs_translation_context_t ctx = rbs_translation_context_create(
|
121
|
+
&parser->constant_pool,
|
122
|
+
arg->buffer,
|
123
|
+
arg->encoding
|
124
|
+
);
|
125
|
+
|
126
|
+
return rbs_struct_to_ruby_value(ctx, type);
|
127
|
+
}
|
128
|
+
|
129
|
+
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
|
+
);
|
143
|
+
}
|
144
|
+
|
145
|
+
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
|
+
);
|
163
|
+
}
|
164
|
+
|
165
|
+
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);
|
169
|
+
|
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
|
+
};
|
178
|
+
|
179
|
+
VALUE result = rb_ensure(parse_type_try, (VALUE)&arg, ensure_free_parser, (VALUE)parser);
|
180
|
+
|
181
|
+
RB_GC_GUARD(string);
|
182
|
+
|
183
|
+
return result;
|
184
|
+
}
|
185
|
+
|
186
|
+
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;
|
189
|
+
|
190
|
+
if (parser->next_token.type == pEOF) {
|
191
|
+
return Qnil;
|
192
|
+
}
|
193
|
+
|
194
|
+
rbs_method_type_t *method_type = NULL;
|
195
|
+
rbs_parse_method_type(parser, &method_type);
|
196
|
+
|
197
|
+
raise_error_if_any(parser, arg->buffer);
|
198
|
+
|
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);
|
204
|
+
}
|
205
|
+
}
|
206
|
+
|
207
|
+
rbs_translation_context_t ctx = rbs_translation_context_create(
|
208
|
+
&parser->constant_pool,
|
209
|
+
arg->buffer,
|
210
|
+
arg->encoding
|
211
|
+
);
|
212
|
+
|
213
|
+
return rbs_struct_to_ruby_value(ctx, (rbs_node_t *) method_type);
|
214
|
+
}
|
215
|
+
|
216
|
+
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);
|
220
|
+
|
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
|
+
};
|
229
|
+
|
230
|
+
VALUE result = rb_ensure(parse_method_type_try, (VALUE)&arg, ensure_free_parser, (VALUE)parser);
|
231
|
+
|
232
|
+
RB_GC_GUARD(string);
|
233
|
+
|
234
|
+
return result;
|
235
|
+
}
|
236
|
+
|
237
|
+
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;
|
240
|
+
|
241
|
+
rbs_signature_t *signature = NULL;
|
242
|
+
rbs_parse_signature(parser, &signature);
|
243
|
+
|
244
|
+
raise_error_if_any(parser, arg->buffer);
|
245
|
+
|
246
|
+
rbs_translation_context_t ctx = rbs_translation_context_create(
|
247
|
+
&parser->constant_pool,
|
248
|
+
arg->buffer,
|
249
|
+
arg->encoding
|
250
|
+
);
|
251
|
+
|
252
|
+
return rbs_struct_to_ruby_value(ctx, (rbs_node_t *) signature);
|
253
|
+
}
|
254
|
+
|
255
|
+
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);
|
259
|
+
|
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
|
+
};
|
267
|
+
|
268
|
+
VALUE result = rb_ensure(parse_signature_try, (VALUE)&arg, ensure_free_parser, (VALUE)parser);
|
269
|
+
|
270
|
+
RB_GC_GUARD(string);
|
271
|
+
|
272
|
+
return result;
|
273
|
+
}
|
274
|
+
|
275
|
+
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;
|
278
|
+
|
279
|
+
rbs_ast_ruby_annotations_t *annotation = NULL;
|
280
|
+
bool success = rbs_parse_inline_leading_annotation(parser, &annotation);
|
281
|
+
|
282
|
+
raise_error_if_any(parser, arg->buffer);
|
283
|
+
|
284
|
+
if (!success || annotation == NULL) {
|
285
|
+
return Qnil;
|
286
|
+
}
|
287
|
+
|
288
|
+
rbs_translation_context_t ctx = rbs_translation_context_create(
|
289
|
+
&parser->constant_pool,
|
290
|
+
arg->buffer,
|
291
|
+
arg->encoding
|
292
|
+
);
|
293
|
+
|
294
|
+
return rbs_struct_to_ruby_value(ctx, (rbs_node_t *) annotation);
|
295
|
+
}
|
296
|
+
|
297
|
+
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);
|
301
|
+
|
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
|
+
};
|
310
|
+
|
311
|
+
VALUE result = rb_ensure(parse_inline_leading_annotation_try, (VALUE)&arg, ensure_free_parser, (VALUE)parser);
|
312
|
+
|
313
|
+
RB_GC_GUARD(string);
|
314
|
+
|
315
|
+
return result;
|
316
|
+
}
|
317
|
+
|
318
|
+
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;
|
321
|
+
|
322
|
+
rbs_ast_ruby_annotations_t *annotation = NULL;
|
323
|
+
bool success = rbs_parse_inline_trailing_annotation(parser, &annotation);
|
324
|
+
|
325
|
+
raise_error_if_any(parser, arg->buffer);
|
326
|
+
|
327
|
+
if (!success || annotation == NULL) {
|
328
|
+
return Qnil;
|
329
|
+
}
|
330
|
+
|
331
|
+
rbs_translation_context_t ctx = rbs_translation_context_create(
|
332
|
+
&parser->constant_pool,
|
333
|
+
arg->buffer,
|
334
|
+
arg->encoding
|
335
|
+
);
|
336
|
+
|
337
|
+
return rbs_struct_to_ruby_value(ctx, (rbs_node_t *) annotation);
|
338
|
+
}
|
339
|
+
|
340
|
+
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);
|
344
|
+
|
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
|
+
};
|
353
|
+
|
354
|
+
VALUE result = rb_ensure(parse_inline_trailing_annotation_try, (VALUE) &arg, ensure_free_parser, (VALUE) parser);
|
355
|
+
|
356
|
+
RB_GC_GUARD(string);
|
357
|
+
|
358
|
+
return result;
|
359
|
+
}
|
360
|
+
|
361
|
+
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;
|
383
|
+
}
|
384
|
+
|
385
|
+
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);
|
397
|
+
}
|
398
|
+
|
6
399
|
static
|
7
400
|
void Deinit_rbs_extension(ruby_vm_t *_) {
|
8
401
|
rbs_constant_pool_free(RBS_GLOBAL_CONSTANT_POOL);
|
@@ -18,14 +411,15 @@ Init_rbs_extension(void)
|
|
18
411
|
rbs__init_location();
|
19
412
|
rbs__init_parser();
|
20
413
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
414
|
+
/* Calculated based on the number of unique strings used with the `INTERN` macro in `parser.c`.
|
415
|
+
*
|
416
|
+
* ```bash
|
417
|
+
* grep -o 'INTERN("\([^"]*\)")' ext/rbs_extension/parser.c \
|
418
|
+
* | sed 's/INTERN("\(.*\)")/\1/' \
|
419
|
+
* | sort -u \
|
420
|
+
* | wc -l
|
421
|
+
* ```
|
422
|
+
*/
|
29
423
|
const size_t num_uniquely_interned_strings = 26;
|
30
424
|
rbs_constant_pool_init(RBS_GLOBAL_CONSTANT_POOL, num_uniquely_interned_strings);
|
31
425
|
|
@@ -4,28 +4,10 @@
|
|
4
4
|
#include "ruby/re.h"
|
5
5
|
#include "ruby/encoding.h"
|
6
6
|
|
7
|
+
#include "class_constants.h"
|
7
8
|
#include "rbs.h"
|
8
|
-
#include "lexer.h"
|
9
|
-
#include "parser.h"
|
10
9
|
|
11
10
|
/**
|
12
|
-
*
|
13
|
-
*
|
14
|
-
* Input token | Output string
|
15
|
-
* ------------+-------------
|
16
|
-
* "foo\\n" | foo\n
|
17
|
-
* 'foo' | foo
|
18
|
-
* `bar` | bar
|
19
|
-
* :"baz\\t" | baz\t
|
20
|
-
* :'baz' | baz
|
11
|
+
* RBS::Parser class
|
21
12
|
* */
|
22
|
-
VALUE
|
23
|
-
|
24
|
-
/**
|
25
|
-
* Raises RBS::ParsingError on `tok` with message constructed with given `fmt`.
|
26
|
-
*
|
27
|
-
* ```
|
28
|
-
* foo.rbs:11:21...11:25: Syntax error: {message}, token=`{tok source}` ({tok type})
|
29
|
-
* ```
|
30
|
-
* */
|
31
|
-
PRINTF_ARGS(NORETURN(void) raise_syntax_error(parserstate *state, token tok, const char *fmt, ...), 3, 4);
|
13
|
+
extern VALUE RBS_Parser;
|
@@ -0,0 +1,9 @@
|
|
1
|
+
#include "rbs_string_bridging.h"
|
2
|
+
|
3
|
+
rbs_string_t rbs_string_from_ruby_string(VALUE ruby_string) {
|
4
|
+
return rbs_string_new(StringValueCStr(ruby_string), RSTRING_END(ruby_string));
|
5
|
+
}
|
6
|
+
|
7
|
+
VALUE rbs_string_to_ruby_string(rbs_string_t *self, rb_encoding *encoding) {
|
8
|
+
return rb_enc_str_new(self->start, rbs_string_len(*self), encoding);
|
9
|
+
}
|
@@ -0,0 +1,20 @@
|
|
1
|
+
#ifndef RBS__RBS_STRING_BRIDGING_H
|
2
|
+
#define RBS__RBS_STRING_BRIDGING_H
|
3
|
+
|
4
|
+
#include "ruby.h"
|
5
|
+
#include "ruby/encoding.h"
|
6
|
+
|
7
|
+
#include "rbs/string.h"
|
8
|
+
|
9
|
+
/**
|
10
|
+
* @returns A new shared rbs_string_t from the given Ruby string, which points into the given Ruby String's memory,
|
11
|
+
* and does not need to be `free()`ed. However, the Ruby String needs to be kept alive for the duration of the rbs_string_t.
|
12
|
+
*/
|
13
|
+
rbs_string_t rbs_string_from_ruby_string(VALUE ruby_string);
|
14
|
+
|
15
|
+
/**
|
16
|
+
* Returns a new Ruby string from the given rbs_string_t.
|
17
|
+
*/
|
18
|
+
VALUE rbs_string_to_ruby_string(rbs_string_t *self, rb_encoding *encoding);
|
19
|
+
|
20
|
+
#endif
|