rbs 4.0.0.dev.4 → 4.1.0.pre.2
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 +1 -0
- data/.github/dependabot.yml +16 -14
- data/.github/workflows/bundle-update.yml +63 -0
- data/.github/workflows/c-check.yml +21 -11
- data/.github/workflows/comments.yml +5 -3
- data/.github/workflows/dependabot.yml +2 -2
- data/.github/workflows/jruby.yml +67 -0
- data/.github/workflows/milestone.yml +83 -0
- data/.github/workflows/ruby.yml +63 -24
- data/.github/workflows/rust.yml +184 -0
- data/.github/workflows/truffleruby.yml +54 -0
- data/.github/workflows/typecheck.yml +5 -2
- data/.github/workflows/wasm.yml +53 -0
- data/.github/workflows/windows.yml +8 -2
- data/.gitignore +11 -0
- data/.rubocop.yml +1 -1
- data/CHANGELOG.md +357 -0
- data/README.md +4 -4
- data/Rakefile +365 -33
- data/Steepfile +8 -0
- data/config.yml +450 -24
- data/core/array.rbs +443 -363
- data/core/basic_object.rbs +9 -8
- data/core/binding.rbs +0 -2
- data/core/builtin.rbs +9 -8
- data/core/class.rbs +11 -8
- data/core/comparable.rbs +55 -34
- data/core/complex.rbs +104 -78
- data/core/dir.rbs +61 -49
- data/core/encoding.rbs +12 -15
- data/core/enumerable.rbs +288 -196
- data/core/enumerator/arithmetic_sequence.rbs +70 -0
- data/core/enumerator/product.rbs +5 -5
- data/core/enumerator.rbs +91 -28
- data/core/errno.rbs +11 -2
- data/core/errors.rbs +58 -29
- data/core/exception.rbs +13 -13
- data/core/fiber.rbs +74 -54
- data/core/file.rbs +260 -1151
- data/core/file_constants.rbs +463 -0
- data/core/file_stat.rbs +534 -0
- data/core/file_test.rbs +3 -3
- data/core/float.rbs +257 -92
- data/core/gc.rbs +425 -281
- data/core/hash.rbs +1151 -829
- data/core/integer.rbs +156 -195
- data/core/io/buffer.rbs +53 -42
- data/core/io/wait.rbs +13 -35
- data/core/io.rbs +216 -150
- data/core/kernel.rbs +239 -163
- data/core/marshal.rbs +4 -4
- data/core/match_data.rbs +15 -13
- data/core/math.rbs +107 -66
- data/core/method.rbs +69 -33
- data/core/module.rbs +302 -150
- data/core/nil_class.rbs +7 -6
- data/core/numeric.rbs +77 -63
- data/core/object.rbs +9 -11
- data/core/object_space/weak_key_map.rbs +7 -7
- data/core/object_space.rbs +30 -23
- data/core/pathname.rbs +1322 -0
- data/core/proc.rbs +95 -58
- data/core/process.rbs +222 -202
- data/core/ractor.rbs +371 -515
- data/core/random.rbs +21 -3
- data/core/range.rbs +181 -79
- data/core/rational.rbs +60 -89
- data/core/rbs/ops.rbs +154 -0
- data/core/rbs/unnamed/argf.rbs +63 -56
- data/core/rbs/unnamed/env_class.rbs +19 -14
- data/core/rbs/unnamed/main_class.rbs +123 -0
- data/core/rbs/unnamed/random.rbs +11 -118
- data/core/regexp.rbs +258 -214
- data/core/ruby.rbs +53 -0
- data/core/ruby_vm.rbs +78 -34
- data/core/rubygems/config_file.rbs +5 -5
- data/core/rubygems/errors.rbs +4 -71
- data/core/rubygems/requirement.rbs +5 -5
- data/core/rubygems/rubygems.rbs +16 -82
- data/core/rubygems/version.rbs +2 -3
- data/core/set.rbs +493 -363
- data/core/signal.rbs +26 -16
- data/core/string.rbs +3234 -1285
- data/core/struct.rbs +43 -42
- data/core/symbol.rbs +41 -34
- data/core/thread.rbs +141 -73
- data/core/time.rbs +81 -50
- data/core/trace_point.rbs +41 -35
- data/core/true_class.rbs +2 -2
- data/core/unbound_method.rbs +24 -16
- data/core/warning.rbs +7 -7
- data/docs/CONTRIBUTING.md +2 -1
- data/docs/aliases.md +79 -0
- data/docs/collection.md +3 -3
- data/docs/config.md +171 -0
- data/docs/encoding.md +56 -0
- data/docs/gem.md +0 -1
- data/docs/inline.md +634 -0
- data/docs/rbs_by_example.md +20 -20
- data/docs/rust.md +96 -0
- data/docs/sigs.md +3 -3
- data/docs/syntax.md +48 -18
- data/docs/type_fingerprint.md +21 -0
- data/docs/wasm_serialization.md +80 -0
- data/exe/rbs +1 -1
- data/ext/rbs_extension/ast_translation.c +1441 -671
- data/ext/rbs_extension/ast_translation.h +7 -0
- data/ext/rbs_extension/class_constants.c +18 -2
- data/ext/rbs_extension/class_constants.h +9 -0
- data/ext/rbs_extension/extconf.rb +6 -1
- data/ext/rbs_extension/legacy_location.c +33 -56
- data/ext/rbs_extension/legacy_location.h +37 -0
- data/ext/rbs_extension/main.c +183 -39
- data/include/rbs/ast.h +597 -297
- data/include/rbs/defines.h +40 -0
- data/include/rbs/lexer.h +31 -11
- data/include/rbs/location.h +25 -44
- data/include/rbs/parser.h +6 -6
- data/include/rbs/serialize.h +39 -0
- data/include/rbs/string.h +0 -2
- data/include/rbs/util/rbs_allocator.h +34 -13
- data/include/rbs/util/rbs_assert.h +12 -1
- data/include/rbs/util/rbs_constant_pool.h +0 -3
- data/include/rbs/util/rbs_encoding.h +2 -0
- data/include/rbs/util/rbs_unescape.h +2 -1
- data/include/rbs.h +8 -0
- data/lib/rbs/annotate/rdoc_annotator.rb +27 -31
- data/lib/rbs/ast/annotation.rb +1 -1
- data/lib/rbs/ast/comment.rb +1 -1
- data/lib/rbs/ast/declarations.rb +10 -10
- data/lib/rbs/ast/members.rb +14 -14
- data/lib/rbs/ast/ruby/annotations.rb +335 -3
- data/lib/rbs/ast/ruby/comment_block.rb +30 -4
- data/lib/rbs/ast/ruby/declarations.rb +209 -4
- data/lib/rbs/ast/ruby/helpers/constant_helper.rb +4 -0
- data/lib/rbs/ast/ruby/helpers/location_helper.rb +1 -1
- data/lib/rbs/ast/ruby/members.rb +571 -22
- data/lib/rbs/ast/type_param.rb +24 -4
- data/lib/rbs/buffer.rb +66 -24
- data/lib/rbs/cli/diff.rb +16 -15
- data/lib/rbs/cli/validate.rb +38 -106
- data/lib/rbs/cli.rb +55 -24
- data/lib/rbs/collection/config/lockfile_generator.rb +28 -3
- data/lib/rbs/collection/sources/git.rb +7 -0
- data/lib/rbs/definition.rb +1 -1
- data/lib/rbs/definition_builder/ancestor_builder.rb +62 -9
- data/lib/rbs/definition_builder/method_builder.rb +32 -6
- data/lib/rbs/definition_builder.rb +147 -25
- data/lib/rbs/diff.rb +7 -1
- data/lib/rbs/environment.rb +235 -75
- data/lib/rbs/environment_loader.rb +0 -6
- data/lib/rbs/errors.rb +27 -18
- data/lib/rbs/inline_parser.rb +377 -15
- data/lib/rbs/location_aux.rb +1 -1
- data/lib/rbs/locator.rb +5 -1
- data/lib/rbs/method_type.rb +5 -3
- data/lib/rbs/namespace.rb +47 -11
- data/lib/rbs/parser_aux.rb +20 -7
- data/lib/rbs/prototype/helpers.rb +57 -0
- data/lib/rbs/prototype/rb.rb +3 -28
- data/lib/rbs/prototype/rbi.rb +3 -20
- data/lib/rbs/prototype/runtime.rb +10 -0
- data/lib/rbs/resolver/constant_resolver.rb +2 -2
- data/lib/rbs/resolver/type_name_resolver.rb +120 -44
- data/lib/rbs/rewriter.rb +70 -0
- data/lib/rbs/subtractor.rb +3 -1
- data/lib/rbs/test/type_check.rb +25 -3
- data/lib/rbs/type_name.rb +34 -14
- data/lib/rbs/types.rb +88 -78
- data/lib/rbs/unit_test/type_assertions.rb +44 -8
- data/lib/rbs/validator.rb +2 -2
- data/lib/rbs/version.rb +1 -1
- data/lib/rbs/wasm/deserializer.rb +213 -0
- data/lib/rbs/wasm/location.rb +61 -0
- data/lib/rbs/wasm/parser.rb +137 -0
- data/lib/rbs/wasm/runtime.rb +217 -0
- data/lib/rbs/wasm/serialization_schema.rb +110 -0
- data/lib/rbs.rb +13 -2
- data/lib/rdoc/discover.rb +1 -1
- data/lib/rdoc_plugin/parser.rb +1 -1
- data/rbs.gemspec +24 -6
- data/schema/typeParam.json +17 -1
- data/sig/annotate/rdoc_annotater.rbs +12 -9
- data/sig/ast/ruby/annotations.rbs +364 -4
- data/sig/ast/ruby/comment_block.rbs +8 -0
- data/sig/ast/ruby/declarations.rbs +102 -4
- data/sig/ast/ruby/members.rbs +128 -2
- data/sig/buffer.rbs +19 -1
- data/sig/cli/diff.rbs +5 -11
- data/sig/cli/validate.rbs +12 -8
- data/sig/cli.rbs +18 -18
- data/sig/collection/config/lockfile_generator.rbs +2 -0
- data/sig/definition.rbs +6 -1
- data/sig/definition_builder.rbs +2 -0
- data/sig/environment.rbs +70 -12
- data/sig/errors.rbs +13 -14
- data/sig/inline_parser.rbs +41 -2
- data/sig/locator.rbs +0 -2
- data/sig/manifest.yaml +0 -2
- data/sig/method_builder.rbs +3 -1
- data/sig/namespace.rbs +20 -0
- data/sig/parser.rbs +41 -13
- data/sig/prototype/helpers.rbs +2 -0
- data/sig/resolver/type_name_resolver.rbs +36 -10
- data/sig/rewriter.rbs +45 -0
- data/sig/source.rbs +3 -3
- data/sig/type_param.rbs +13 -8
- data/sig/typename.rbs +15 -0
- data/sig/types.rbs +6 -7
- data/sig/unit_test/spy.rbs +0 -8
- data/sig/unit_test/type_assertions.rbs +15 -0
- data/sig/wasm/deserializer.rbs +66 -0
- data/sig/wasm/serialization_schema.rbs +13 -0
- data/src/ast.c +443 -162
- data/src/lexer.c +1415 -1313
- data/src/lexer.re +4 -0
- data/src/lexstate.c +63 -37
- data/src/location.c +7 -47
- data/src/parser.c +1032 -521
- data/src/serialize.c +958 -0
- data/src/string.c +0 -48
- data/src/util/rbs_allocator.c +89 -74
- data/src/util/rbs_assert.c +1 -1
- data/src/util/rbs_buffer.c +2 -2
- data/src/util/rbs_constant_pool.c +10 -14
- data/src/util/rbs_encoding.c +4 -8
- data/src/util/rbs_unescape.c +56 -20
- data/stdlib/abbrev/0/array.rbs +1 -1
- data/stdlib/bigdecimal/0/big_decimal.rbs +116 -98
- data/stdlib/bigdecimal-math/0/big_math.rbs +169 -8
- data/stdlib/cgi/0/core.rbs +9 -393
- data/stdlib/cgi/0/manifest.yaml +1 -0
- data/stdlib/cgi-escape/0/escape.rbs +171 -0
- data/stdlib/coverage/0/coverage.rbs +7 -4
- data/stdlib/csv/0/csv.rbs +5 -5
- data/stdlib/date/0/date.rbs +92 -79
- data/stdlib/date/0/date_time.rbs +25 -24
- data/stdlib/delegate/0/delegator.rbs +10 -7
- data/stdlib/did_you_mean/0/did_you_mean.rbs +17 -16
- data/stdlib/digest/0/digest.rbs +111 -1
- data/stdlib/erb/0/erb.rbs +748 -347
- data/stdlib/etc/0/etc.rbs +73 -54
- data/stdlib/fileutils/0/fileutils.rbs +179 -160
- data/stdlib/forwardable/0/forwardable.rbs +13 -10
- data/stdlib/io-console/0/io-console.rbs +2 -2
- data/stdlib/json/0/json.rbs +223 -142
- data/stdlib/monitor/0/monitor.rbs +3 -3
- data/stdlib/net-http/0/net-http.rbs +162 -134
- data/stdlib/objspace/0/objspace.rbs +17 -34
- data/stdlib/open-uri/0/open-uri.rbs +48 -8
- data/stdlib/open3/0/open3.rbs +469 -10
- data/stdlib/openssl/0/openssl.rbs +482 -364
- data/stdlib/optparse/0/optparse.rbs +26 -17
- data/stdlib/pathname/0/pathname.rbs +11 -1381
- data/stdlib/pp/0/pp.rbs +9 -8
- data/stdlib/prettyprint/0/prettyprint.rbs +7 -7
- data/stdlib/pstore/0/pstore.rbs +35 -30
- data/stdlib/psych/0/psych.rbs +65 -12
- data/stdlib/psych/0/store.rbs +2 -4
- data/stdlib/pty/0/pty.rbs +9 -6
- data/stdlib/random-formatter/0/random-formatter.rbs +277 -0
- data/stdlib/rdoc/0/code_object.rbs +2 -1
- data/stdlib/rdoc/0/parser.rbs +1 -1
- data/stdlib/rdoc/0/rdoc.rbs +1 -1
- data/stdlib/rdoc/0/store.rbs +1 -1
- data/stdlib/resolv/0/resolv.rbs +26 -69
- data/stdlib/ripper/0/ripper.rbs +22 -19
- data/stdlib/securerandom/0/manifest.yaml +2 -0
- data/stdlib/securerandom/0/securerandom.rbs +7 -20
- data/stdlib/shellwords/0/shellwords.rbs +3 -3
- data/stdlib/singleton/0/singleton.rbs +3 -0
- data/stdlib/socket/0/addrinfo.rbs +7 -7
- data/stdlib/socket/0/basic_socket.rbs +3 -3
- data/stdlib/socket/0/ip_socket.rbs +10 -8
- data/stdlib/socket/0/socket.rbs +23 -10
- data/stdlib/socket/0/tcp_server.rbs +1 -1
- data/stdlib/socket/0/tcp_socket.rbs +11 -3
- data/stdlib/socket/0/udp_socket.rbs +1 -1
- data/stdlib/socket/0/unix_server.rbs +1 -1
- data/stdlib/stringio/0/stringio.rbs +1209 -95
- data/stdlib/strscan/0/string_scanner.rbs +101 -80
- data/stdlib/tempfile/0/tempfile.rbs +25 -21
- data/stdlib/time/0/time.rbs +8 -6
- data/stdlib/timeout/0/timeout.rbs +63 -7
- data/stdlib/tsort/0/cyclic.rbs +4 -1
- data/stdlib/tsort/0/interfaces.rbs +8 -8
- data/stdlib/tsort/0/tsort.rbs +16 -15
- data/stdlib/uri/0/common.rbs +42 -20
- data/stdlib/uri/0/file.rbs +3 -3
- data/stdlib/uri/0/generic.rbs +26 -18
- data/stdlib/uri/0/http.rbs +2 -2
- data/stdlib/uri/0/ldap.rbs +2 -2
- data/stdlib/uri/0/mailto.rbs +3 -3
- data/stdlib/uri/0/rfc2396_parser.rbs +12 -12
- data/stdlib/zlib/0/deflate.rbs +4 -3
- data/stdlib/zlib/0/gzip_reader.rbs +8 -8
- data/stdlib/zlib/0/gzip_writer.rbs +14 -12
- data/stdlib/zlib/0/inflate.rbs +1 -1
- data/stdlib/zlib/0/need_dict.rbs +1 -1
- data/stdlib/zlib/0/zstream.rbs +1 -0
- data/wasm/README.md +59 -0
- data/wasm/rbs_wasm.c +411 -0
- metadata +56 -8
- data/.vscode/extensions.json +0 -5
- data/.vscode/settings.json +0 -19
data/ext/rbs_extension/main.c
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
#include "rbs/util/rbs_assert.h"
|
|
3
3
|
#include "rbs/util/rbs_allocator.h"
|
|
4
4
|
#include "rbs/util/rbs_constant_pool.h"
|
|
5
|
+
#include "rbs/serialize.h"
|
|
5
6
|
#include "ast_translation.h"
|
|
6
7
|
#include "legacy_location.h"
|
|
7
8
|
#include "rbs_string_bridging.h"
|
|
@@ -16,7 +17,7 @@
|
|
|
16
17
|
* ```
|
|
17
18
|
* */
|
|
18
19
|
static NORETURN(void) raise_error(rbs_error_t *error, VALUE buffer) {
|
|
19
|
-
|
|
20
|
+
RBS_ASSERT(error != NULL, "raise_error() called with NULL error");
|
|
20
21
|
|
|
21
22
|
if (!error->syntax_error) {
|
|
22
23
|
rb_raise(rb_eRuntimeError, "Unexpected error");
|
|
@@ -67,10 +68,12 @@ static void declare_type_variables(rbs_parser_t *parser, VALUE variables, VALUE
|
|
|
67
68
|
}
|
|
68
69
|
|
|
69
70
|
VALUE name_str = rb_sym2str(symbol);
|
|
71
|
+
uint8_t *copied_name = (uint8_t *) malloc((size_t) RSTRING_LEN(name_str));
|
|
72
|
+
memcpy((void *) copied_name, RSTRING_PTR(name_str), RSTRING_LEN(name_str));
|
|
70
73
|
|
|
71
|
-
rbs_constant_id_t id =
|
|
74
|
+
rbs_constant_id_t id = rbs_constant_pool_insert_owned(
|
|
72
75
|
&parser->constant_pool,
|
|
73
|
-
|
|
76
|
+
copied_name,
|
|
74
77
|
RSTRING_LEN(name_str)
|
|
75
78
|
);
|
|
76
79
|
|
|
@@ -85,6 +88,23 @@ struct parse_type_arg {
|
|
|
85
88
|
rb_encoding *encoding;
|
|
86
89
|
rbs_parser_t *parser;
|
|
87
90
|
VALUE require_eof;
|
|
91
|
+
VALUE void_allowed;
|
|
92
|
+
VALUE self_allowed;
|
|
93
|
+
VALUE classish_allowed;
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
struct parse_method_type_arg {
|
|
97
|
+
VALUE buffer;
|
|
98
|
+
rb_encoding *encoding;
|
|
99
|
+
rbs_parser_t *parser;
|
|
100
|
+
VALUE require_eof;
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
struct parse_signature_arg {
|
|
104
|
+
VALUE buffer;
|
|
105
|
+
rb_encoding *encoding;
|
|
106
|
+
rbs_parser_t *parser;
|
|
107
|
+
VALUE require_eof;
|
|
88
108
|
};
|
|
89
109
|
|
|
90
110
|
static VALUE ensure_free_parser(VALUE parser) {
|
|
@@ -100,8 +120,12 @@ static VALUE parse_type_try(VALUE a) {
|
|
|
100
120
|
return Qnil;
|
|
101
121
|
}
|
|
102
122
|
|
|
123
|
+
bool void_allowed = RTEST(arg->void_allowed);
|
|
124
|
+
bool self_allowed = RTEST(arg->self_allowed);
|
|
125
|
+
bool classish_allowed = RTEST(arg->classish_allowed);
|
|
126
|
+
|
|
103
127
|
rbs_node_t *type;
|
|
104
|
-
rbs_parse_type(parser, &type);
|
|
128
|
+
rbs_parse_type(parser, &type, void_allowed, self_allowed, classish_allowed);
|
|
105
129
|
|
|
106
130
|
raise_error_if_any(parser, arg->buffer);
|
|
107
131
|
|
|
@@ -122,10 +146,17 @@ static VALUE parse_type_try(VALUE a) {
|
|
|
122
146
|
return rbs_struct_to_ruby_value(ctx, type);
|
|
123
147
|
}
|
|
124
148
|
|
|
125
|
-
static
|
|
149
|
+
static void validate_position_range(int start_pos, int end_pos) {
|
|
126
150
|
if (start_pos < 0 || end_pos < 0) {
|
|
127
151
|
rb_raise(rb_eArgError, "negative position range: %d...%d", start_pos, end_pos);
|
|
128
152
|
}
|
|
153
|
+
if (start_pos > end_pos) {
|
|
154
|
+
rb_raise(rb_eArgError, "invalid position range: %d...%d", start_pos, end_pos);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
static rbs_lexer_t *alloc_lexer_from_buffer(rbs_allocator_t *allocator, VALUE string, rb_encoding *encoding, int start_pos, int end_pos) {
|
|
159
|
+
validate_position_range(start_pos, end_pos);
|
|
129
160
|
|
|
130
161
|
const char *encoding_name = rb_enc_name(encoding);
|
|
131
162
|
|
|
@@ -139,9 +170,7 @@ static rbs_lexer_t *alloc_lexer_from_buffer(rbs_allocator_t *allocator, VALUE st
|
|
|
139
170
|
}
|
|
140
171
|
|
|
141
172
|
static rbs_parser_t *alloc_parser_from_buffer(VALUE buffer, int start_pos, int end_pos) {
|
|
142
|
-
|
|
143
|
-
rb_raise(rb_eArgError, "negative position range: %d...%d", start_pos, end_pos);
|
|
144
|
-
}
|
|
173
|
+
validate_position_range(start_pos, end_pos);
|
|
145
174
|
|
|
146
175
|
VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
|
|
147
176
|
StringValue(string);
|
|
@@ -157,7 +186,7 @@ static rbs_parser_t *alloc_parser_from_buffer(VALUE buffer, int start_pos, int e
|
|
|
157
186
|
);
|
|
158
187
|
}
|
|
159
188
|
|
|
160
|
-
static VALUE rbsparser_parse_type(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos, VALUE variables, VALUE require_eof) {
|
|
189
|
+
static VALUE rbsparser_parse_type(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos, VALUE variables, VALUE require_eof, VALUE void_allowed, VALUE self_allowed, VALUE classish_allowed) {
|
|
161
190
|
VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
|
|
162
191
|
StringValue(string);
|
|
163
192
|
rb_encoding *encoding = rb_enc_get(string);
|
|
@@ -168,7 +197,10 @@ static VALUE rbsparser_parse_type(VALUE self, VALUE buffer, VALUE start_pos, VAL
|
|
|
168
197
|
.buffer = buffer,
|
|
169
198
|
.encoding = encoding,
|
|
170
199
|
.parser = parser,
|
|
171
|
-
.require_eof = require_eof
|
|
200
|
+
.require_eof = require_eof,
|
|
201
|
+
.void_allowed = void_allowed,
|
|
202
|
+
.self_allowed = self_allowed,
|
|
203
|
+
.classish_allowed = classish_allowed
|
|
172
204
|
};
|
|
173
205
|
|
|
174
206
|
VALUE result = rb_ensure(parse_type_try, (VALUE) &arg, ensure_free_parser, (VALUE) parser);
|
|
@@ -179,7 +211,7 @@ static VALUE rbsparser_parse_type(VALUE self, VALUE buffer, VALUE start_pos, VAL
|
|
|
179
211
|
}
|
|
180
212
|
|
|
181
213
|
static VALUE parse_method_type_try(VALUE a) {
|
|
182
|
-
struct
|
|
214
|
+
struct parse_method_type_arg *arg = (struct parse_method_type_arg *) a;
|
|
183
215
|
rbs_parser_t *parser = arg->parser;
|
|
184
216
|
|
|
185
217
|
if (parser->next_token.type == pEOF) {
|
|
@@ -187,18 +219,10 @@ static VALUE parse_method_type_try(VALUE a) {
|
|
|
187
219
|
}
|
|
188
220
|
|
|
189
221
|
rbs_method_type_t *method_type = NULL;
|
|
190
|
-
rbs_parse_method_type(parser, &method_type);
|
|
222
|
+
rbs_parse_method_type(parser, &method_type, RB_TEST(arg->require_eof), true);
|
|
191
223
|
|
|
192
224
|
raise_error_if_any(parser, arg->buffer);
|
|
193
225
|
|
|
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
|
-
}
|
|
200
|
-
}
|
|
201
|
-
|
|
202
226
|
rbs_translation_context_t ctx = rbs_translation_context_create(
|
|
203
227
|
&parser->constant_pool,
|
|
204
228
|
arg->buffer,
|
|
@@ -215,7 +239,7 @@ static VALUE rbsparser_parse_method_type(VALUE self, VALUE buffer, VALUE start_p
|
|
|
215
239
|
|
|
216
240
|
rbs_parser_t *parser = alloc_parser_from_buffer(buffer, FIX2INT(start_pos), FIX2INT(end_pos));
|
|
217
241
|
declare_type_variables(parser, variables, buffer);
|
|
218
|
-
struct
|
|
242
|
+
struct parse_method_type_arg arg = {
|
|
219
243
|
.buffer = buffer,
|
|
220
244
|
.encoding = encoding,
|
|
221
245
|
.parser = parser,
|
|
@@ -230,7 +254,7 @@ static VALUE rbsparser_parse_method_type(VALUE self, VALUE buffer, VALUE start_p
|
|
|
230
254
|
}
|
|
231
255
|
|
|
232
256
|
static VALUE parse_signature_try(VALUE a) {
|
|
233
|
-
struct
|
|
257
|
+
struct parse_signature_arg *arg = (struct parse_signature_arg *) a;
|
|
234
258
|
rbs_parser_t *parser = arg->parser;
|
|
235
259
|
|
|
236
260
|
rbs_signature_t *signature = NULL;
|
|
@@ -253,7 +277,7 @@ static VALUE rbsparser_parse_signature(VALUE self, VALUE buffer, VALUE start_pos
|
|
|
253
277
|
rb_encoding *encoding = rb_enc_get(string);
|
|
254
278
|
|
|
255
279
|
rbs_parser_t *parser = alloc_parser_from_buffer(buffer, FIX2INT(start_pos), FIX2INT(end_pos));
|
|
256
|
-
struct
|
|
280
|
+
struct parse_signature_arg arg = {
|
|
257
281
|
.buffer = buffer,
|
|
258
282
|
.encoding = encoding,
|
|
259
283
|
.parser = parser,
|
|
@@ -267,6 +291,132 @@ static VALUE rbsparser_parse_signature(VALUE self, VALUE buffer, VALUE start_pos
|
|
|
267
291
|
return result;
|
|
268
292
|
}
|
|
269
293
|
|
|
294
|
+
// Serialize a parsed node into a binary Ruby string using the same encoder the
|
|
295
|
+
// WebAssembly build uses. These `_*_to_bytes` entry points exist so the
|
|
296
|
+
// round-trip (parse -> serialize -> deserialize) can be exercised on CRuby,
|
|
297
|
+
// where it can be compared against the direct C -> Ruby translation.
|
|
298
|
+
static VALUE serialized_node_to_string(rbs_parser_t *parser, rbs_node_t *node) {
|
|
299
|
+
rbs_string_t bytes = rbs_serialize_node(parser->allocator, &parser->constant_pool, node);
|
|
300
|
+
return rb_str_new(bytes.start, (long) rbs_string_len(bytes));
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
static VALUE parse_type_to_bytes_try(VALUE a) {
|
|
304
|
+
struct parse_type_arg *arg = (struct parse_type_arg *) a;
|
|
305
|
+
rbs_parser_t *parser = arg->parser;
|
|
306
|
+
|
|
307
|
+
if (parser->next_token.type == pEOF) {
|
|
308
|
+
return Qnil;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
rbs_node_t *type;
|
|
312
|
+
rbs_parse_type(parser, &type, RTEST(arg->void_allowed), RTEST(arg->self_allowed), RTEST(arg->classish_allowed));
|
|
313
|
+
|
|
314
|
+
raise_error_if_any(parser, arg->buffer);
|
|
315
|
+
|
|
316
|
+
if (RB_TEST(arg->require_eof)) {
|
|
317
|
+
rbs_parser_advance(parser);
|
|
318
|
+
if (parser->current_token.type != pEOF) {
|
|
319
|
+
rbs_parser_set_error(parser, parser->current_token, true, "expected a token `%s`", rbs_token_type_str(pEOF));
|
|
320
|
+
raise_error(parser->error, arg->buffer);
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
return serialized_node_to_string(parser, type);
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
static VALUE rbsparser_parse_type_to_bytes(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos, VALUE variables, VALUE require_eof, VALUE void_allowed, VALUE self_allowed, VALUE classish_allowed) {
|
|
328
|
+
VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
|
|
329
|
+
StringValue(string);
|
|
330
|
+
rb_encoding *encoding = rb_enc_get(string);
|
|
331
|
+
|
|
332
|
+
rbs_parser_t *parser = alloc_parser_from_buffer(buffer, FIX2INT(start_pos), FIX2INT(end_pos));
|
|
333
|
+
declare_type_variables(parser, variables, buffer);
|
|
334
|
+
struct parse_type_arg arg = {
|
|
335
|
+
.buffer = buffer,
|
|
336
|
+
.encoding = encoding,
|
|
337
|
+
.parser = parser,
|
|
338
|
+
.require_eof = require_eof,
|
|
339
|
+
.void_allowed = void_allowed,
|
|
340
|
+
.self_allowed = self_allowed,
|
|
341
|
+
.classish_allowed = classish_allowed
|
|
342
|
+
};
|
|
343
|
+
|
|
344
|
+
VALUE result = rb_ensure(parse_type_to_bytes_try, (VALUE) &arg, ensure_free_parser, (VALUE) parser);
|
|
345
|
+
|
|
346
|
+
RB_GC_GUARD(string);
|
|
347
|
+
|
|
348
|
+
return result;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
static VALUE parse_method_type_to_bytes_try(VALUE a) {
|
|
352
|
+
struct parse_method_type_arg *arg = (struct parse_method_type_arg *) a;
|
|
353
|
+
rbs_parser_t *parser = arg->parser;
|
|
354
|
+
|
|
355
|
+
if (parser->next_token.type == pEOF) {
|
|
356
|
+
return Qnil;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
rbs_method_type_t *method_type = NULL;
|
|
360
|
+
rbs_parse_method_type(parser, &method_type, RB_TEST(arg->require_eof), true);
|
|
361
|
+
|
|
362
|
+
raise_error_if_any(parser, arg->buffer);
|
|
363
|
+
|
|
364
|
+
return serialized_node_to_string(parser, (rbs_node_t *) method_type);
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
static VALUE rbsparser_parse_method_type_to_bytes(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos, VALUE variables, VALUE require_eof) {
|
|
368
|
+
VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
|
|
369
|
+
StringValue(string);
|
|
370
|
+
rb_encoding *encoding = rb_enc_get(string);
|
|
371
|
+
|
|
372
|
+
rbs_parser_t *parser = alloc_parser_from_buffer(buffer, FIX2INT(start_pos), FIX2INT(end_pos));
|
|
373
|
+
declare_type_variables(parser, variables, buffer);
|
|
374
|
+
struct parse_method_type_arg arg = {
|
|
375
|
+
.buffer = buffer,
|
|
376
|
+
.encoding = encoding,
|
|
377
|
+
.parser = parser,
|
|
378
|
+
.require_eof = require_eof
|
|
379
|
+
};
|
|
380
|
+
|
|
381
|
+
VALUE result = rb_ensure(parse_method_type_to_bytes_try, (VALUE) &arg, ensure_free_parser, (VALUE) parser);
|
|
382
|
+
|
|
383
|
+
RB_GC_GUARD(string);
|
|
384
|
+
|
|
385
|
+
return result;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
static VALUE parse_signature_to_bytes_try(VALUE a) {
|
|
389
|
+
struct parse_signature_arg *arg = (struct parse_signature_arg *) a;
|
|
390
|
+
rbs_parser_t *parser = arg->parser;
|
|
391
|
+
|
|
392
|
+
rbs_signature_t *signature = NULL;
|
|
393
|
+
rbs_parse_signature(parser, &signature);
|
|
394
|
+
|
|
395
|
+
raise_error_if_any(parser, arg->buffer);
|
|
396
|
+
|
|
397
|
+
return serialized_node_to_string(parser, (rbs_node_t *) signature);
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
static VALUE rbsparser_parse_signature_to_bytes(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos) {
|
|
401
|
+
VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
|
|
402
|
+
StringValue(string);
|
|
403
|
+
rb_encoding *encoding = rb_enc_get(string);
|
|
404
|
+
|
|
405
|
+
rbs_parser_t *parser = alloc_parser_from_buffer(buffer, FIX2INT(start_pos), FIX2INT(end_pos));
|
|
406
|
+
struct parse_signature_arg arg = {
|
|
407
|
+
.buffer = buffer,
|
|
408
|
+
.encoding = encoding,
|
|
409
|
+
.parser = parser,
|
|
410
|
+
.require_eof = false
|
|
411
|
+
};
|
|
412
|
+
|
|
413
|
+
VALUE result = rb_ensure(parse_signature_to_bytes_try, (VALUE) &arg, ensure_free_parser, (VALUE) parser);
|
|
414
|
+
|
|
415
|
+
RB_GC_GUARD(string);
|
|
416
|
+
|
|
417
|
+
return result;
|
|
418
|
+
}
|
|
419
|
+
|
|
270
420
|
struct parse_type_params_arg {
|
|
271
421
|
VALUE buffer;
|
|
272
422
|
rb_encoding *encoding;
|
|
@@ -429,12 +579,19 @@ static VALUE rbsparser_lex(VALUE self, VALUE buffer, VALUE end_pos) {
|
|
|
429
579
|
void rbs__init_parser(void) {
|
|
430
580
|
RBS_Parser = rb_define_class_under(RBS, "Parser", rb_cObject);
|
|
431
581
|
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
582
|
|
|
435
|
-
|
|
583
|
+
EMPTY_ARRAY = rb_obj_freeze(rb_ary_new());
|
|
584
|
+
rb_gc_register_mark_object(EMPTY_ARRAY);
|
|
585
|
+
|
|
586
|
+
EMPTY_HASH = rb_obj_freeze(rb_hash_new());
|
|
587
|
+
rb_gc_register_mark_object(EMPTY_HASH);
|
|
588
|
+
|
|
589
|
+
rb_define_singleton_method(RBS_Parser, "_parse_type", rbsparser_parse_type, 8);
|
|
436
590
|
rb_define_singleton_method(RBS_Parser, "_parse_method_type", rbsparser_parse_method_type, 5);
|
|
437
591
|
rb_define_singleton_method(RBS_Parser, "_parse_signature", rbsparser_parse_signature, 3);
|
|
592
|
+
rb_define_singleton_method(RBS_Parser, "_parse_type_to_bytes", rbsparser_parse_type_to_bytes, 8);
|
|
593
|
+
rb_define_singleton_method(RBS_Parser, "_parse_method_type_to_bytes", rbsparser_parse_method_type_to_bytes, 5);
|
|
594
|
+
rb_define_singleton_method(RBS_Parser, "_parse_signature_to_bytes", rbsparser_parse_signature_to_bytes, 3);
|
|
438
595
|
rb_define_singleton_method(RBS_Parser, "_parse_type_params", rbsparser_parse_type_params, 4);
|
|
439
596
|
rb_define_singleton_method(RBS_Parser, "_parse_inline_leading_annotation", rbsparser_parse_inline_leading_annotation, 4);
|
|
440
597
|
rb_define_singleton_method(RBS_Parser, "_parse_inline_trailing_annotation", rbsparser_parse_inline_trailing_annotation, 4);
|
|
@@ -442,7 +599,6 @@ void rbs__init_parser(void) {
|
|
|
442
599
|
}
|
|
443
600
|
|
|
444
601
|
static void Deinit_rbs_extension(ruby_vm_t *_) {
|
|
445
|
-
rbs_constant_pool_free(RBS_GLOBAL_CONSTANT_POOL);
|
|
446
602
|
}
|
|
447
603
|
|
|
448
604
|
void Init_rbs_extension(void) {
|
|
@@ -453,17 +609,5 @@ void Init_rbs_extension(void) {
|
|
|
453
609
|
rbs__init_location();
|
|
454
610
|
rbs__init_parser();
|
|
455
611
|
|
|
456
|
-
/* Calculated based on the number of unique strings used with the `INTERN` macro in `parser.c`.
|
|
457
|
-
*
|
|
458
|
-
* ```bash
|
|
459
|
-
* grep -o 'INTERN("\([^"]*\)")' ext/rbs_extension/parser.c \
|
|
460
|
-
* | sed 's/INTERN("\(.*\)")/\1/' \
|
|
461
|
-
* | sort -u \
|
|
462
|
-
* | wc -l
|
|
463
|
-
* ```
|
|
464
|
-
*/
|
|
465
|
-
const size_t num_uniquely_interned_strings = 26;
|
|
466
|
-
rbs_constant_pool_init(RBS_GLOBAL_CONSTANT_POOL, num_uniquely_interned_strings);
|
|
467
|
-
|
|
468
612
|
ruby_vm_at_exit(Deinit_rbs_extension);
|
|
469
613
|
}
|