rbs 3.10.0 → 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/comments.yml +3 -3
- data/.github/workflows/ruby.yml +24 -35
- data/.github/workflows/typecheck.yml +3 -1
- data/.github/workflows/windows.yml +2 -2
- data/.gitignore +0 -4
- data/CHANGELOG.md +0 -88
- data/README.md +1 -38
- data/Rakefile +20 -142
- data/Steepfile +1 -0
- data/config.yml +43 -1
- data/core/array.rbs +46 -100
- data/core/complex.rbs +21 -32
- data/core/dir.rbs +2 -2
- data/core/encoding.rbs +9 -6
- data/core/enumerable.rbs +3 -90
- data/core/enumerator.rbs +1 -18
- data/core/errno.rbs +0 -8
- data/core/errors.rbs +1 -28
- data/core/exception.rbs +2 -2
- data/core/fiber.rbs +4 -5
- data/core/file.rbs +12 -27
- data/core/file_test.rbs +1 -1
- data/core/float.rbs +22 -209
- data/core/gc.rbs +281 -417
- data/core/hash.rbs +727 -1024
- data/core/integer.rbs +38 -78
- data/core/io/buffer.rbs +7 -18
- data/core/io/wait.rbs +33 -11
- data/core/io.rbs +12 -14
- data/core/kernel.rbs +51 -57
- data/core/marshal.rbs +1 -1
- data/core/match_data.rbs +1 -1
- data/core/math.rbs +3 -42
- data/core/method.rbs +6 -14
- data/core/module.rbs +17 -88
- data/core/nil_class.rbs +3 -3
- data/core/numeric.rbs +16 -16
- data/core/object.rbs +3 -3
- data/core/object_space.rbs +15 -21
- data/core/proc.rbs +8 -15
- data/core/process.rbs +2 -2
- data/core/ractor.rbs +437 -278
- data/core/range.rbs +8 -7
- data/core/rational.rbs +24 -37
- data/core/rbs/unnamed/argf.rbs +2 -2
- data/core/rbs/unnamed/env_class.rbs +1 -1
- data/core/rbs/unnamed/random.rbs +2 -4
- data/core/regexp.rbs +20 -25
- data/core/ruby_vm.rbs +4 -6
- data/core/rubygems/errors.rbs +70 -3
- data/core/rubygems/rubygems.rbs +79 -11
- data/core/rubygems/version.rbs +3 -2
- data/core/set.rbs +359 -488
- data/core/string.rbs +1228 -3153
- data/core/struct.rbs +1 -1
- data/core/symbol.rbs +4 -4
- data/core/thread.rbs +29 -92
- data/core/time.rbs +9 -35
- data/core/trace_point.rbs +4 -7
- data/core/unbound_method.rbs +6 -14
- data/docs/collection.md +2 -2
- data/docs/gem.md +1 -0
- data/docs/sigs.md +3 -3
- data/ext/rbs_extension/ast_translation.c +1077 -944
- data/ext/rbs_extension/ast_translation.h +0 -7
- data/ext/rbs_extension/class_constants.c +83 -71
- data/ext/rbs_extension/class_constants.h +7 -4
- data/ext/rbs_extension/extconf.rb +2 -24
- data/ext/rbs_extension/legacy_location.c +172 -173
- data/ext/rbs_extension/legacy_location.h +3 -8
- data/ext/rbs_extension/main.c +289 -239
- data/ext/rbs_extension/rbs_extension.h +0 -3
- data/ext/rbs_extension/rbs_string_bridging.h +0 -4
- data/include/rbs/ast.h +98 -37
- data/include/rbs/defines.h +12 -38
- data/include/rbs/lexer.h +114 -126
- data/include/rbs/location.h +14 -14
- data/include/rbs/parser.h +37 -21
- data/include/rbs/string.h +5 -3
- data/include/rbs/util/rbs_allocator.h +19 -40
- data/include/rbs/util/rbs_assert.h +1 -12
- data/include/rbs/util/rbs_constant_pool.h +3 -3
- data/include/rbs/util/rbs_encoding.h +1 -3
- data/include/rbs/util/rbs_unescape.h +1 -2
- 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 +40 -35
- data/lib/rbs/cli.rb +5 -6
- data/lib/rbs/collection/config/lockfile_generator.rb +0 -1
- data/lib/rbs/collection.rb +0 -1
- data/lib/rbs/definition.rb +6 -1
- data/lib/rbs/definition_builder/ancestor_builder.rb +65 -62
- 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 +244 -218
- data/lib/rbs/environment_loader.rb +3 -3
- data/lib/rbs/errors.rb +5 -4
- 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 -6
- data/lib/rbs/prototype/runtime.rb +2 -2
- data/lib/rbs/resolver/constant_resolver.rb +2 -2
- data/lib/rbs/resolver/type_name_resolver.rb +38 -124
- data/lib/rbs/source.rb +99 -0
- data/lib/rbs/subtractor.rb +4 -3
- data/lib/rbs/test/type_check.rb +0 -14
- data/lib/rbs/types.rb +1 -3
- data/lib/rbs/version.rb +1 -1
- data/lib/rbs.rb +13 -1
- data/lib/rdoc/discover.rb +1 -1
- data/lib/rdoc_plugin/parser.rb +1 -1
- data/rbs.gemspec +1 -0
- data/sig/ancestor_builder.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 +28 -133
- 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/manifest.yaml +1 -0
- data/sig/method_builder.rbs +7 -4
- data/sig/parser.rbs +16 -20
- data/sig/resolver/type_name_resolver.rbs +7 -38
- data/sig/source.rbs +48 -0
- data/sig/types.rbs +1 -4
- data/src/ast.c +290 -201
- data/src/lexer.c +2813 -2902
- data/src/lexer.re +4 -0
- data/src/lexstate.c +155 -169
- data/src/location.c +40 -40
- data/src/parser.c +2665 -2433
- data/src/string.c +48 -0
- data/src/util/rbs_allocator.c +77 -80
- data/src/util/rbs_assert.c +10 -10
- data/src/util/rbs_buffer.c +2 -2
- data/src/util/rbs_constant_pool.c +15 -13
- data/src/util/rbs_encoding.c +4062 -20097
- data/src/util/rbs_unescape.c +48 -85
- data/stdlib/bigdecimal/0/big_decimal.rbs +82 -100
- data/stdlib/bigdecimal-math/0/big_math.rbs +8 -169
- data/stdlib/cgi/0/core.rbs +396 -2
- data/stdlib/cgi/0/manifest.yaml +0 -1
- data/stdlib/coverage/0/coverage.rbs +1 -3
- data/stdlib/date/0/date.rbs +59 -67
- data/stdlib/date/0/date_time.rbs +1 -1
- data/stdlib/delegate/0/delegator.rbs +7 -10
- data/stdlib/erb/0/erb.rbs +347 -737
- data/stdlib/fileutils/0/fileutils.rbs +13 -18
- data/stdlib/forwardable/0/forwardable.rbs +0 -3
- data/stdlib/json/0/json.rbs +48 -68
- data/stdlib/net-http/0/net-http.rbs +0 -3
- data/stdlib/objspace/0/objspace.rbs +4 -9
- data/stdlib/open-uri/0/open-uri.rbs +0 -40
- data/stdlib/openssl/0/openssl.rbs +228 -331
- data/stdlib/optparse/0/optparse.rbs +3 -3
- data/{core → stdlib/pathname/0}/pathname.rbs +355 -255
- data/stdlib/psych/0/psych.rbs +3 -3
- data/stdlib/rdoc/0/rdoc.rbs +1 -1
- data/stdlib/resolv/0/resolv.rbs +68 -25
- data/stdlib/ripper/0/ripper.rbs +2 -5
- data/stdlib/singleton/0/singleton.rbs +0 -3
- data/stdlib/socket/0/socket.rbs +1 -13
- data/stdlib/socket/0/tcp_socket.rbs +2 -10
- data/stdlib/stringio/0/stringio.rbs +85 -1176
- data/stdlib/strscan/0/string_scanner.rbs +31 -31
- data/stdlib/tempfile/0/tempfile.rbs +3 -3
- data/stdlib/time/0/time.rbs +1 -1
- data/stdlib/timeout/0/timeout.rbs +7 -63
- data/stdlib/tsort/0/cyclic.rbs +0 -3
- data/stdlib/uri/0/common.rbs +2 -11
- data/stdlib/uri/0/file.rbs +1 -1
- data/stdlib/uri/0/generic.rbs +16 -17
- data/stdlib/uri/0/rfc2396_parser.rbs +7 -6
- data/stdlib/zlib/0/zstream.rbs +0 -1
- metadata +40 -12
- data/.clang-format +0 -74
- data/.clangd +0 -2
- data/.github/workflows/c-check.yml +0 -54
- data/core/ruby.rbs +0 -53
- data/docs/aliases.md +0 -79
- data/docs/encoding.md +0 -56
- data/ext/rbs_extension/compat.h +0 -10
- data/stdlib/cgi-escape/0/escape.rbs +0 -153
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,319 +49,369 @@ 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
|
-
|
|
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
|
+
}
|
|
58
60
|
|
|
59
|
-
|
|
61
|
+
rbs_parser_push_typevar_table(parser, true);
|
|
60
62
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
+
for (long i = 0; i < rb_array_len(variables); i++) {
|
|
64
|
+
VALUE symbol = rb_ary_entry(variables, i);
|
|
63
65
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
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
|
+
}
|
|
68
72
|
|
|
69
|
-
|
|
73
|
+
VALUE name_str = rb_sym2str(symbol);
|
|
70
74
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
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
|
+
);
|
|
76
80
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
}
|
|
81
|
+
if (!rbs_parser_insert_typevar(parser, id)) {
|
|
82
|
+
raise_error(parser->error, buffer);
|
|
80
83
|
}
|
|
84
|
+
}
|
|
81
85
|
}
|
|
82
86
|
|
|
83
87
|
struct parse_type_arg {
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
+
VALUE buffer;
|
|
89
|
+
rb_encoding *encoding;
|
|
90
|
+
rbs_parser_t *parser;
|
|
91
|
+
VALUE require_eof;
|
|
88
92
|
};
|
|
89
93
|
|
|
90
94
|
static VALUE ensure_free_parser(VALUE parser) {
|
|
91
|
-
|
|
92
|
-
|
|
95
|
+
rbs_parser_free((rbs_parser_t *)parser);
|
|
96
|
+
return Qnil;
|
|
93
97
|
}
|
|
94
98
|
|
|
95
99
|
static VALUE parse_type_try(VALUE a) {
|
|
96
|
-
|
|
97
|
-
|
|
100
|
+
struct parse_type_arg *arg = (struct parse_type_arg *)a;
|
|
101
|
+
rbs_parser_t *parser = arg->parser;
|
|
98
102
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
103
|
+
if (parser->next_token.type == pEOF) {
|
|
104
|
+
return Qnil;
|
|
105
|
+
}
|
|
102
106
|
|
|
103
|
-
|
|
104
|
-
|
|
107
|
+
rbs_node_t *type;
|
|
108
|
+
rbs_parse_type(parser, &type);
|
|
105
109
|
|
|
106
|
-
|
|
110
|
+
raise_error_if_any(parser, arg->buffer);
|
|
107
111
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
}
|
|
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);
|
|
114
117
|
}
|
|
118
|
+
}
|
|
115
119
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
120
|
+
rbs_translation_context_t ctx = rbs_translation_context_create(
|
|
121
|
+
&parser->constant_pool,
|
|
122
|
+
arg->buffer,
|
|
123
|
+
arg->encoding
|
|
124
|
+
);
|
|
121
125
|
|
|
122
|
-
|
|
126
|
+
return rbs_struct_to_ruby_value(ctx, type);
|
|
123
127
|
}
|
|
124
128
|
|
|
125
129
|
static rbs_lexer_t *alloc_lexer_from_buffer(rbs_allocator_t *allocator, VALUE string, rb_encoding *encoding, int start_pos, int end_pos) {
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
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
|
+
);
|
|
139
143
|
}
|
|
140
144
|
|
|
141
145
|
static rbs_parser_t *alloc_parser_from_buffer(VALUE buffer, int start_pos, int end_pos) {
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
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
|
+
);
|
|
158
163
|
}
|
|
159
164
|
|
|
160
165
|
static VALUE rbsparser_parse_type(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos, VALUE variables, VALUE require_eof) {
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
166
|
+
VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
|
|
167
|
+
StringValue(string);
|
|
168
|
+
rb_encoding *encoding = rb_enc_get(string);
|
|
164
169
|
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
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
|
+
};
|
|
173
178
|
|
|
174
|
-
|
|
179
|
+
VALUE result = rb_ensure(parse_type_try, (VALUE)&arg, ensure_free_parser, (VALUE)parser);
|
|
175
180
|
|
|
176
|
-
|
|
181
|
+
RB_GC_GUARD(string);
|
|
177
182
|
|
|
178
|
-
|
|
183
|
+
return result;
|
|
179
184
|
}
|
|
180
185
|
|
|
181
186
|
static VALUE parse_method_type_try(VALUE a) {
|
|
182
|
-
|
|
183
|
-
|
|
187
|
+
struct parse_type_arg *arg = (struct parse_type_arg *)a;
|
|
188
|
+
rbs_parser_t *parser = arg->parser;
|
|
184
189
|
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
190
|
+
if (parser->next_token.type == pEOF) {
|
|
191
|
+
return Qnil;
|
|
192
|
+
}
|
|
188
193
|
|
|
189
|
-
|
|
190
|
-
|
|
194
|
+
rbs_method_type_t *method_type = NULL;
|
|
195
|
+
rbs_parse_method_type(parser, &method_type);
|
|
191
196
|
|
|
192
|
-
|
|
197
|
+
raise_error_if_any(parser, arg->buffer);
|
|
193
198
|
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
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
|
+
);
|
|
199
212
|
|
|
200
|
-
|
|
213
|
+
return rbs_struct_to_ruby_value(ctx, (rbs_node_t *) method_type);
|
|
201
214
|
}
|
|
202
215
|
|
|
203
216
|
static VALUE rbsparser_parse_method_type(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos, VALUE variables, VALUE require_eof) {
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
217
|
+
VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
|
|
218
|
+
StringValue(string);
|
|
219
|
+
rb_encoding *encoding = rb_enc_get(string);
|
|
207
220
|
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
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
229
|
|
|
217
|
-
|
|
230
|
+
VALUE result = rb_ensure(parse_method_type_try, (VALUE)&arg, ensure_free_parser, (VALUE)parser);
|
|
218
231
|
|
|
219
|
-
|
|
232
|
+
RB_GC_GUARD(string);
|
|
220
233
|
|
|
221
|
-
|
|
234
|
+
return result;
|
|
222
235
|
}
|
|
223
236
|
|
|
224
237
|
static VALUE parse_signature_try(VALUE a) {
|
|
225
|
-
|
|
226
|
-
|
|
238
|
+
struct parse_type_arg *arg = (struct parse_type_arg *)a;
|
|
239
|
+
rbs_parser_t *parser = arg->parser;
|
|
227
240
|
|
|
228
|
-
|
|
229
|
-
|
|
241
|
+
rbs_signature_t *signature = NULL;
|
|
242
|
+
rbs_parse_signature(parser, &signature);
|
|
230
243
|
|
|
231
|
-
|
|
244
|
+
raise_error_if_any(parser, arg->buffer);
|
|
232
245
|
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
246
|
+
rbs_translation_context_t ctx = rbs_translation_context_create(
|
|
247
|
+
&parser->constant_pool,
|
|
248
|
+
arg->buffer,
|
|
249
|
+
arg->encoding
|
|
250
|
+
);
|
|
238
251
|
|
|
239
|
-
|
|
252
|
+
return rbs_struct_to_ruby_value(ctx, (rbs_node_t *) signature);
|
|
240
253
|
}
|
|
241
254
|
|
|
242
255
|
static VALUE rbsparser_parse_signature(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos) {
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
256
|
+
VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
|
|
257
|
+
StringValue(string);
|
|
258
|
+
rb_encoding *encoding = rb_enc_get(string);
|
|
246
259
|
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
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
|
+
};
|
|
254
267
|
|
|
255
|
-
|
|
268
|
+
VALUE result = rb_ensure(parse_signature_try, (VALUE)&arg, ensure_free_parser, (VALUE)parser);
|
|
256
269
|
|
|
257
|
-
|
|
270
|
+
RB_GC_GUARD(string);
|
|
258
271
|
|
|
259
|
-
|
|
272
|
+
return result;
|
|
260
273
|
}
|
|
261
274
|
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
rbs_parser_t *parser;
|
|
266
|
-
VALUE module_type_params;
|
|
267
|
-
};
|
|
268
|
-
|
|
269
|
-
static VALUE parse_type_params_try(VALUE a) {
|
|
270
|
-
struct parse_type_params_arg *arg = (struct parse_type_params_arg *) a;
|
|
271
|
-
rbs_parser_t *parser = arg->parser;
|
|
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;
|
|
272
278
|
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
}
|
|
279
|
+
rbs_ast_ruby_annotations_t *annotation = NULL;
|
|
280
|
+
bool success = rbs_parse_inline_leading_annotation(parser, &annotation);
|
|
276
281
|
|
|
277
|
-
|
|
278
|
-
rbs_parse_type_params(parser, arg->module_type_params, ¶ms);
|
|
282
|
+
raise_error_if_any(parser, arg->buffer);
|
|
279
283
|
|
|
280
|
-
|
|
284
|
+
if (!success || annotation == NULL) {
|
|
285
|
+
return Qnil;
|
|
286
|
+
}
|
|
281
287
|
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
288
|
+
rbs_translation_context_t ctx = rbs_translation_context_create(
|
|
289
|
+
&parser->constant_pool,
|
|
290
|
+
arg->buffer,
|
|
291
|
+
arg->encoding
|
|
292
|
+
);
|
|
287
293
|
|
|
288
|
-
|
|
294
|
+
return rbs_struct_to_ruby_value(ctx, (rbs_node_t *) annotation);
|
|
289
295
|
}
|
|
290
296
|
|
|
291
|
-
static VALUE
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
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);
|
|
295
301
|
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
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
|
+
};
|
|
303
310
|
|
|
304
|
-
|
|
311
|
+
VALUE result = rb_ensure(parse_inline_leading_annotation_try, (VALUE)&arg, ensure_free_parser, (VALUE)parser);
|
|
305
312
|
|
|
306
|
-
|
|
313
|
+
RB_GC_GUARD(string);
|
|
307
314
|
|
|
308
|
-
|
|
315
|
+
return result;
|
|
309
316
|
}
|
|
310
317
|
|
|
311
|
-
static VALUE
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
rbs_lexer_t *lexer = alloc_lexer_from_buffer(allocator, string, encoding, 0, FIX2INT(end_pos));
|
|
318
|
-
|
|
319
|
-
VALUE results = rb_ary_new();
|
|
320
|
-
rbs_token_t token = NullToken;
|
|
321
|
-
while (token.type != pEOF) {
|
|
322
|
-
token = rbs_lexer_next_token(lexer);
|
|
323
|
-
VALUE type = ID2SYM(rb_intern(rbs_token_type_str(token.type)));
|
|
324
|
-
VALUE location = rbs_new_location(buffer, token.range);
|
|
325
|
-
VALUE pair = rb_ary_new3(2, type, location);
|
|
326
|
-
rb_ary_push(results, pair);
|
|
327
|
-
}
|
|
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);
|
|
328
324
|
|
|
329
|
-
|
|
330
|
-
RB_GC_GUARD(string);
|
|
325
|
+
raise_error_if_any(parser, arg->buffer);
|
|
331
326
|
|
|
332
|
-
|
|
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);
|
|
333
338
|
}
|
|
334
339
|
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
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
|
+
};
|
|
338
353
|
|
|
339
|
-
|
|
340
|
-
rb_gc_register_mark_object(EMPTY_ARRAY);
|
|
354
|
+
VALUE result = rb_ensure(parse_inline_trailing_annotation_try, (VALUE) &arg, ensure_free_parser, (VALUE) parser);
|
|
341
355
|
|
|
342
|
-
|
|
343
|
-
rb_gc_register_mark_object(EMPTY_HASH);
|
|
356
|
+
RB_GC_GUARD(string);
|
|
344
357
|
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
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);
|
|
350
397
|
}
|
|
351
398
|
|
|
352
|
-
static
|
|
353
|
-
|
|
399
|
+
static
|
|
400
|
+
void Deinit_rbs_extension(ruby_vm_t *_) {
|
|
401
|
+
rbs_constant_pool_free(RBS_GLOBAL_CONSTANT_POOL);
|
|
354
402
|
}
|
|
355
403
|
|
|
356
|
-
void
|
|
404
|
+
void
|
|
405
|
+
Init_rbs_extension(void)
|
|
406
|
+
{
|
|
357
407
|
#ifdef HAVE_RB_EXT_RACTOR_SAFE
|
|
358
|
-
|
|
408
|
+
rb_ext_ractor_safe(true);
|
|
359
409
|
#endif
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
410
|
+
rbs__init_constants();
|
|
411
|
+
rbs__init_location();
|
|
412
|
+
rbs__init_parser();
|
|
363
413
|
|
|
364
|
-
|
|
414
|
+
/* Calculated based on the number of unique strings used with the `INTERN` macro in `parser.c`.
|
|
365
415
|
*
|
|
366
416
|
* ```bash
|
|
367
417
|
* grep -o 'INTERN("\([^"]*\)")' ext/rbs_extension/parser.c \
|
|
@@ -370,8 +420,8 @@ void Init_rbs_extension(void) {
|
|
|
370
420
|
* | wc -l
|
|
371
421
|
* ```
|
|
372
422
|
*/
|
|
373
|
-
|
|
374
|
-
|
|
423
|
+
const size_t num_uniquely_interned_strings = 26;
|
|
424
|
+
rbs_constant_pool_init(RBS_GLOBAL_CONSTANT_POOL, num_uniquely_interned_strings);
|
|
375
425
|
|
|
376
|
-
|
|
426
|
+
ruby_vm_at_exit(Deinit_rbs_extension);
|
|
377
427
|
}
|