rbs 3.9.4 → 4.2.0
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 +75 -0
- data/.clangd +2 -0
- data/.dockerignore +37 -0
- data/.gitattributes +1 -0
- data/.github/dependabot.yml +16 -14
- data/.github/workflows/bundle-update.yml +63 -0
- data/.github/workflows/c-check.yml +66 -0
- data/.github/workflows/changelog.yml +121 -0
- data/.github/workflows/comments.yml +6 -4
- data/.github/workflows/dependabot.yml +2 -2
- data/.github/workflows/jruby.yml +74 -0
- data/.github/workflows/release-gems.yml +234 -0
- data/.github/workflows/ruby.yml +86 -32
- data/.github/workflows/rust.yml +186 -0
- data/.github/workflows/truffleruby.yml +54 -0
- data/.github/workflows/typecheck.yml +6 -3
- data/.github/workflows/wasm.yml +55 -0
- data/.github/workflows/windows.yml +10 -4
- data/.gitignore +16 -0
- data/.rubocop.yml +1 -2
- data/CHANGELOG.md +502 -0
- data/Dockerfile.jruby +53 -0
- data/README.md +42 -5
- data/Rakefile +892 -111
- data/Steepfile +11 -0
- data/config.yml +665 -62
- data/core/array.rbs +541 -398
- 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 +297 -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 -116
- 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 +222 -155
- data/core/kernel.rbs +239 -163
- data/core/marshal.rbs +4 -4
- data/core/match_data.rbs +16 -14
- 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 +1312 -0
- data/core/proc.rbs +95 -58
- data/core/process.rbs +222 -202
- data/core/ractor.rbs +364 -518
- 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 +6 -70
- data/core/rubygems/requirement.rbs +5 -15
- data/core/rubygems/rubygems.rbs +18 -81
- data/core/rubygems/specification.rbs +8 -0
- data/core/rubygems/version.rbs +2 -163
- 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 +139 -83
- 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 +3 -2
- 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/release.md +303 -0
- data/docs/rust.md +96 -0
- data/docs/sigs.md +3 -3
- data/docs/stdlib.md +8 -0
- data/docs/syntax.md +60 -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 +1870 -0
- data/ext/rbs_extension/ast_translation.h +41 -0
- data/ext/rbs_extension/class_constants.c +189 -0
- data/{include/rbs/constants.h → ext/rbs_extension/class_constants.h} +24 -1
- data/ext/rbs_extension/compat.h +10 -0
- data/ext/rbs_extension/extconf.rb +26 -1
- data/ext/rbs_extension/legacy_location.c +299 -0
- data/ext/rbs_extension/legacy_location.h +82 -0
- data/ext/rbs_extension/main.c +639 -23
- data/ext/rbs_extension/rbs_extension.h +6 -21
- data/ext/rbs_extension/rbs_string_bridging.c +9 -0
- data/ext/rbs_extension/rbs_string_bridging.h +24 -0
- data/include/rbs/ast.h +1055 -0
- data/include/rbs/defines.h +104 -0
- data/include/rbs/lexer.h +208 -0
- data/include/rbs/location.h +40 -0
- data/include/rbs/parser.h +187 -0
- data/include/rbs/serialize.h +39 -0
- data/include/rbs/string.h +47 -0
- data/include/rbs/util/rbs_allocator.h +59 -0
- data/include/rbs/util/rbs_assert.h +20 -0
- data/include/rbs/util/rbs_buffer.h +83 -0
- data/include/rbs/util/rbs_constant_pool.h +6 -70
- data/include/rbs/util/rbs_encoding.h +282 -0
- data/include/rbs/util/rbs_unescape.h +24 -0
- data/include/rbs.h +9 -2
- data/lib/rbs/annotate/formatter.rb +3 -13
- data/lib/rbs/annotate/rdoc_annotator.rb +30 -32
- data/lib/rbs/annotate/rdoc_source.rb +1 -1
- data/lib/rbs/ast/annotation.rb +1 -1
- data/lib/rbs/ast/comment.rb +1 -1
- data/lib/rbs/ast/declarations.rb +11 -11
- data/lib/rbs/ast/members.rb +14 -14
- data/lib/rbs/ast/ruby/annotations.rb +451 -0
- data/lib/rbs/ast/ruby/comment_block.rb +247 -0
- data/lib/rbs/ast/ruby/declarations.rb +291 -0
- data/lib/rbs/ast/ruby/helpers/constant_helper.rb +28 -0
- data/lib/rbs/ast/ruby/helpers/location_helper.rb +15 -0
- data/lib/rbs/ast/ruby/members.rb +762 -0
- data/lib/rbs/ast/type_param.rb +25 -5
- data/lib/rbs/buffer.rb +142 -20
- data/lib/rbs/cli/diff.rb +16 -15
- data/lib/rbs/cli/validate.rb +63 -126
- data/lib/rbs/cli.rb +59 -29
- data/lib/rbs/collection/config/lockfile_generator.rb +28 -3
- data/lib/rbs/collection/sources/git.rb +7 -0
- data/lib/rbs/definition.rb +6 -1
- data/lib/rbs/definition_builder/ancestor_builder.rb +129 -70
- data/lib/rbs/definition_builder/method_builder.rb +74 -33
- data/lib/rbs/definition_builder.rb +181 -21
- data/lib/rbs/diff.rb +7 -1
- data/lib/rbs/environment/class_entry.rb +81 -0
- data/lib/rbs/environment/module_entry.rb +91 -0
- data/lib/rbs/environment.rb +410 -215
- data/lib/rbs/environment_loader.rb +2 -8
- data/lib/rbs/errors.rb +31 -21
- data/lib/rbs/inline_parser/comment_association.rb +117 -0
- data/lib/rbs/inline_parser.rb +568 -0
- data/lib/rbs/location_aux.rb +36 -4
- data/lib/rbs/locator.rb +5 -1
- data/lib/rbs/method_type.rb +5 -3
- data/lib/rbs/namespace.rb +47 -18
- data/lib/rbs/parser_aux.rb +37 -7
- data/lib/rbs/prototype/helpers.rb +24 -0
- data/lib/rbs/prototype/rb.rb +3 -28
- data/lib/rbs/prototype/rbi.rb +196 -45
- data/lib/rbs/prototype/runtime/value_object_generator.rb +0 -1
- data/lib/rbs/prototype/runtime.rb +13 -3
- 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/source.rb +99 -0
- data/lib/rbs/subtractor.rb +7 -4
- data/lib/rbs/test/type_check.rb +25 -3
- data/lib/rbs/type_name.rb +34 -21
- data/lib/rbs/types.rb +135 -81
- data/lib/rbs/unit_test/convertibles.rb +1 -0
- data/lib/rbs/unit_test/type_assertions.rb +47 -11
- 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 +174 -0
- data/lib/rbs/wasm/runtime.rb +211 -0
- data/lib/rbs/wasm/serialization_schema.rb +111 -0
- data/lib/rbs.rb +25 -2
- data/lib/rbs_jars.rb +39 -0
- data/lib/rdoc/discover.rb +1 -1
- data/lib/rdoc_plugin/parser.rb +8 -3
- data/rbs.gemspec +34 -6
- data/schema/function.json +12 -1
- data/schema/typeParam.json +17 -1
- data/sig/ancestor_builder.rbs +1 -1
- data/sig/annotate/formatter.rbs +2 -2
- data/sig/annotate/rdoc_annotater.rbs +13 -10
- data/sig/ast/ruby/annotations.rbs +470 -0
- data/sig/ast/ruby/comment_block.rbs +127 -0
- data/sig/ast/ruby/declarations.rbs +158 -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 +198 -0
- data/sig/buffer.rbs +81 -5
- 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 -0
- data/sig/definition_builder.rbs +3 -1
- data/sig/environment/class_entry.rbs +56 -0
- data/sig/environment/module_entry.rbs +65 -0
- data/sig/environment.rbs +94 -87
- data/sig/errors.rbs +26 -20
- data/sig/inline_parser/comment_association.rbs +71 -0
- data/sig/inline_parser.rbs +126 -0
- data/sig/location.rbs +32 -7
- data/sig/locator.rbs +0 -2
- data/sig/manifest.yaml +0 -2
- data/sig/method_builder.rbs +9 -4
- data/sig/namespace.rbs +20 -5
- data/sig/parser.rbs +79 -15
- data/sig/prototype/helpers.rbs +2 -0
- data/sig/prototype/rbi.rbs +33 -4
- data/sig/resolver/type_name_resolver.rbs +36 -10
- data/sig/rewriter.rbs +45 -0
- data/sig/source.rbs +48 -0
- data/sig/type_param.rbs +13 -8
- data/sig/typename.rbs +15 -5
- data/sig/types.rbs +21 -9
- data/sig/unit_test/spy.rbs +0 -8
- data/sig/unit_test/type_assertions.rbs +17 -2
- data/sig/wasm/deserializer.rbs +66 -0
- data/sig/wasm/serialization_schema.rbs +13 -0
- data/src/ast.c +1644 -0
- data/src/lexer.c +3223 -0
- data/src/lexer.re +187 -0
- data/src/lexstate.c +222 -0
- data/src/location.c +31 -0
- data/src/parser.c +4318 -0
- data/src/serialize.c +965 -0
- data/src/string.c +41 -0
- data/src/util/rbs_allocator.c +171 -0
- data/src/util/rbs_assert.c +19 -0
- data/src/util/rbs_buffer.c +54 -0
- data/src/util/rbs_constant_pool.c +18 -92
- data/src/util/rbs_encoding.c +21454 -0
- data/src/util/rbs_unescape.c +167 -0
- 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 +11 -7
- data/stdlib/did_you_mean/0/did_you_mean.rbs +17 -16
- data/stdlib/digest/0/digest.rbs +117 -1
- data/stdlib/erb/0/erb.rbs +754 -353
- 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/ipaddr/0/ipaddr.rbs +0 -5
- data/stdlib/json/0/json.rbs +232 -185
- data/stdlib/monitor/0/monitor.rbs +9 -9
- 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 +521 -397
- 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 +4 -3
- data/stdlib/rdoc/0/comment.rbs +2 -0
- data/stdlib/rdoc/0/options.rbs +76 -0
- data/stdlib/rdoc/0/parser.rbs +1 -1
- data/stdlib/rdoc/0/rdoc.rbs +7 -5
- data/stdlib/rdoc/0/store.rbs +2 -2
- data/stdlib/resolv/0/resolv.rbs +26 -69
- data/stdlib/ripper/0/ripper.rbs +25 -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 +6 -0
- data/stdlib/socket/0/addrinfo.rbs +9 -9
- 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 +1211 -96
- data/stdlib/strscan/0/string_scanner.rbs +101 -80
- data/stdlib/tempfile/0/manifest.yaml +3 -0
- data/stdlib/tempfile/0/tempfile.rbs +25 -21
- data/stdlib/time/0/time.rbs +8 -6
- data/stdlib/timeout/0/timeout.rbs +58 -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 +21 -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_file.rbs +1 -1
- data/stdlib/zlib/0/gzip_reader.rbs +8 -8
- data/stdlib/zlib/0/gzip_writer.rbs +16 -13
- data/stdlib/zlib/0/inflate.rbs +1 -1
- data/stdlib/zlib/0/need_dict.rbs +1 -1
- data/wasm/README.md +109 -0
- data/wasm/rbs_wasm.c +479 -0
- metadata +133 -19
- data/ext/rbs_extension/lexer.c +0 -2728
- data/ext/rbs_extension/lexer.h +0 -179
- data/ext/rbs_extension/lexer.re +0 -147
- data/ext/rbs_extension/lexstate.c +0 -175
- data/ext/rbs_extension/location.c +0 -325
- 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/constants.c +0 -153
- data/src/ruby_objs.c +0 -799
data/ext/rbs_extension/main.c
CHANGED
|
@@ -1,33 +1,649 @@
|
|
|
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 "rbs/serialize.h"
|
|
6
|
+
#include "ast_translation.h"
|
|
7
|
+
#include "legacy_location.h"
|
|
8
|
+
#include "rbs_string_bridging.h"
|
|
3
9
|
|
|
4
10
|
#include "ruby/vm.h"
|
|
5
11
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
12
|
+
/**
|
|
13
|
+
* Raises `RBS::ParsingError` or `RuntimeError` on `tok` with message constructed with given `fmt`.
|
|
14
|
+
*
|
|
15
|
+
* ```
|
|
16
|
+
* foo.rbs:11:21...11:25: Syntax error: {message}, token=`{tok source}` ({tok type})
|
|
17
|
+
* ```
|
|
18
|
+
* */
|
|
19
|
+
static NORETURN(void) raise_error(rbs_error_t *error, VALUE buffer) {
|
|
20
|
+
RBS_ASSERT(error != NULL, "raise_error() called with NULL error");
|
|
21
|
+
|
|
22
|
+
if (!error->syntax_error) {
|
|
23
|
+
rb_raise(rb_eRuntimeError, "Unexpected error");
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
VALUE location = rbs_new_location(buffer, error->token.range);
|
|
27
|
+
VALUE type = rb_str_new_cstr(rbs_token_type_str(error->token.type));
|
|
28
|
+
|
|
29
|
+
VALUE rb_error = rb_funcall(
|
|
30
|
+
RBS_ParsingError,
|
|
31
|
+
rb_intern("new"),
|
|
32
|
+
3,
|
|
33
|
+
location,
|
|
34
|
+
rb_str_new_cstr(error->message),
|
|
35
|
+
type
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
rb_exc_raise(rb_error);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
void raise_error_if_any(rbs_parser_t *parser, VALUE buffer) {
|
|
42
|
+
if (parser->error != NULL) {
|
|
43
|
+
raise_error(parser->error, buffer);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Inserts the given array of type variables names into the parser's type variable table.
|
|
49
|
+
* @param parser
|
|
50
|
+
* @param variables A Ruby Array of Symbols, or nil.
|
|
51
|
+
*/
|
|
52
|
+
static void declare_type_variables(rbs_parser_t *parser, VALUE variables, VALUE buffer) {
|
|
53
|
+
if (NIL_P(variables)) return; // Nothing to do.
|
|
54
|
+
|
|
55
|
+
if (!RB_TYPE_P(variables, T_ARRAY)) {
|
|
56
|
+
rbs_parser_free(parser);
|
|
57
|
+
rb_raise(rb_eTypeError, "wrong argument type %" PRIsVALUE " (must be an Array of Symbols or nil)", rb_obj_class(variables));
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
rbs_parser_push_typevar_table(parser, true);
|
|
61
|
+
|
|
62
|
+
for (long i = 0; i < rb_array_len(variables); i++) {
|
|
63
|
+
VALUE symbol = rb_ary_entry(variables, i);
|
|
64
|
+
|
|
65
|
+
if (!RB_TYPE_P(symbol, T_SYMBOL)) {
|
|
66
|
+
rbs_parser_free(parser);
|
|
67
|
+
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));
|
|
68
|
+
}
|
|
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));
|
|
73
|
+
|
|
74
|
+
rbs_constant_id_t id = rbs_constant_pool_insert_owned(
|
|
75
|
+
&parser->constant_pool,
|
|
76
|
+
copied_name,
|
|
77
|
+
RSTRING_LEN(name_str)
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
if (!rbs_parser_insert_typevar(parser, id)) {
|
|
81
|
+
raise_error(parser->error, buffer);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
struct parse_type_arg {
|
|
87
|
+
VALUE buffer;
|
|
88
|
+
rb_encoding *encoding;
|
|
89
|
+
rbs_parser_t *parser;
|
|
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;
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
static VALUE ensure_free_parser(VALUE parser) {
|
|
111
|
+
rbs_parser_free((rbs_parser_t *) parser);
|
|
112
|
+
return Qnil;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
static VALUE parse_type_try(VALUE a) {
|
|
116
|
+
struct parse_type_arg *arg = (struct parse_type_arg *) a;
|
|
117
|
+
rbs_parser_t *parser = arg->parser;
|
|
118
|
+
|
|
119
|
+
if (parser->next_token.type == pEOF) {
|
|
120
|
+
return Qnil;
|
|
121
|
+
}
|
|
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
|
+
|
|
127
|
+
rbs_node_t *type;
|
|
128
|
+
rbs_parse_type(parser, &type, void_allowed, self_allowed, classish_allowed);
|
|
129
|
+
|
|
130
|
+
raise_error_if_any(parser, arg->buffer);
|
|
131
|
+
|
|
132
|
+
if (RB_TEST(arg->require_eof)) {
|
|
133
|
+
rbs_parser_advance(parser);
|
|
134
|
+
if (parser->current_token.type != pEOF) {
|
|
135
|
+
rbs_parser_set_error(parser, parser->current_token, true, "expected a token `%s`", rbs_token_type_str(pEOF));
|
|
136
|
+
raise_error(parser->error, arg->buffer);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
rbs_translation_context_t ctx = rbs_translation_context_create(
|
|
141
|
+
&parser->constant_pool,
|
|
142
|
+
arg->buffer,
|
|
143
|
+
arg->encoding
|
|
144
|
+
);
|
|
145
|
+
|
|
146
|
+
return rbs_struct_to_ruby_value(ctx, type);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* `end_pos` may point past the end of the buffer: clamping with a large
|
|
151
|
+
* number instead of measuring the buffer is ordinary, and the lexer stops at
|
|
152
|
+
* the end on its own.
|
|
153
|
+
* */
|
|
154
|
+
static void validate_position_range(VALUE string, int start_pos, int end_pos) {
|
|
155
|
+
if (start_pos < 0 || end_pos < 0) {
|
|
156
|
+
rb_raise(rb_eArgError, "negative position range: %d...%d", start_pos, end_pos);
|
|
157
|
+
}
|
|
158
|
+
if (start_pos > end_pos) {
|
|
159
|
+
rb_raise(rb_eArgError, "invalid position range: %d...%d", start_pos, end_pos);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
long size = RSTRING_LEN(string);
|
|
163
|
+
if ((long) start_pos > size) {
|
|
164
|
+
rb_raise(rb_eArgError, "position range starts past the end of the buffer: %d...%d, buffer is %ld bytes", start_pos, end_pos, size);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
static rbs_lexer_t *alloc_lexer_from_buffer(rbs_allocator_t *allocator, VALUE string, rb_encoding *encoding, int start_pos, int end_pos) {
|
|
169
|
+
validate_position_range(string, start_pos, end_pos);
|
|
170
|
+
|
|
171
|
+
const char *encoding_name = rb_enc_name(encoding);
|
|
172
|
+
|
|
173
|
+
rbs_lexer_t *lexer = rbs_lexer_new(
|
|
174
|
+
allocator,
|
|
175
|
+
rbs_string_from_ruby_string(string),
|
|
176
|
+
rbs_encoding_find((const uint8_t *) encoding_name, (const uint8_t *) (encoding_name + strlen(encoding_name))),
|
|
177
|
+
start_pos,
|
|
178
|
+
end_pos
|
|
179
|
+
);
|
|
180
|
+
|
|
181
|
+
if (lexer == NULL) {
|
|
182
|
+
rb_raise(rb_eArgError, "position range starts inside a character: %d...%d", start_pos, end_pos);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
return lexer;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// Build the parser options from the arguments the `_parse_*` entry points
|
|
189
|
+
// receive. The optional syntax these enable is not part of the public
|
|
190
|
+
// `RBS::Parser` API, so only the private entry points pass them through.
|
|
191
|
+
static rbs_parser_options_t parser_options(VALUE enable_forwarding_params) {
|
|
192
|
+
return (rbs_parser_options_t) {
|
|
193
|
+
.enable_forwarding_params = RB_TEST(enable_forwarding_params),
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
static rbs_parser_t *alloc_parser_from_buffer_with_options(VALUE buffer, int start_pos, int end_pos, rbs_parser_options_t options) {
|
|
198
|
+
VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
|
|
199
|
+
StringValue(string);
|
|
200
|
+
|
|
201
|
+
validate_position_range(string, start_pos, end_pos);
|
|
202
|
+
|
|
203
|
+
rb_encoding *encoding = rb_enc_get(string);
|
|
204
|
+
const char *encoding_name = rb_enc_name(encoding);
|
|
205
|
+
|
|
206
|
+
rbs_parser_t *parser = rbs_parser_new_with_options(
|
|
207
|
+
rbs_string_from_ruby_string(string),
|
|
208
|
+
rbs_encoding_find((const uint8_t *) encoding_name, (const uint8_t *) (encoding_name + strlen(encoding_name))),
|
|
209
|
+
start_pos,
|
|
210
|
+
end_pos,
|
|
211
|
+
options
|
|
212
|
+
);
|
|
213
|
+
|
|
214
|
+
if (parser == NULL) {
|
|
215
|
+
rb_raise(rb_eArgError, "position range starts inside a character: %d...%d", start_pos, end_pos);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
return parser;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
static rbs_parser_t *alloc_parser_from_buffer(VALUE buffer, int start_pos, int end_pos) {
|
|
222
|
+
return alloc_parser_from_buffer_with_options(buffer, start_pos, end_pos, (rbs_parser_options_t) { 0 });
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
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) {
|
|
226
|
+
VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
|
|
227
|
+
StringValue(string);
|
|
228
|
+
rb_encoding *encoding = rb_enc_get(string);
|
|
229
|
+
|
|
230
|
+
rbs_parser_t *parser = alloc_parser_from_buffer(buffer, FIX2INT(start_pos), FIX2INT(end_pos));
|
|
231
|
+
declare_type_variables(parser, variables, buffer);
|
|
232
|
+
struct parse_type_arg arg = {
|
|
233
|
+
.buffer = buffer,
|
|
234
|
+
.encoding = encoding,
|
|
235
|
+
.parser = parser,
|
|
236
|
+
.require_eof = require_eof,
|
|
237
|
+
.void_allowed = void_allowed,
|
|
238
|
+
.self_allowed = self_allowed,
|
|
239
|
+
.classish_allowed = classish_allowed
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
VALUE result = rb_ensure(parse_type_try, (VALUE) &arg, ensure_free_parser, (VALUE) parser);
|
|
243
|
+
|
|
244
|
+
RB_GC_GUARD(string);
|
|
245
|
+
|
|
246
|
+
return result;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
static VALUE parse_method_type_try(VALUE a) {
|
|
250
|
+
struct parse_method_type_arg *arg = (struct parse_method_type_arg *) a;
|
|
251
|
+
rbs_parser_t *parser = arg->parser;
|
|
252
|
+
|
|
253
|
+
if (parser->next_token.type == pEOF) {
|
|
254
|
+
return Qnil;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
rbs_method_type_t *method_type = NULL;
|
|
258
|
+
rbs_parse_method_type(parser, &method_type, RB_TEST(arg->require_eof), true);
|
|
259
|
+
|
|
260
|
+
raise_error_if_any(parser, arg->buffer);
|
|
261
|
+
|
|
262
|
+
rbs_translation_context_t ctx = rbs_translation_context_create(
|
|
263
|
+
&parser->constant_pool,
|
|
264
|
+
arg->buffer,
|
|
265
|
+
arg->encoding
|
|
266
|
+
);
|
|
267
|
+
|
|
268
|
+
return rbs_struct_to_ruby_value(ctx, (rbs_node_t *) method_type);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
static VALUE rbsparser_parse_method_type(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos, VALUE variables, VALUE require_eof, VALUE enable_forwarding_params) {
|
|
272
|
+
VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
|
|
273
|
+
StringValue(string);
|
|
274
|
+
rb_encoding *encoding = rb_enc_get(string);
|
|
275
|
+
|
|
276
|
+
rbs_parser_t *parser = alloc_parser_from_buffer_with_options(buffer, FIX2INT(start_pos), FIX2INT(end_pos), parser_options(enable_forwarding_params));
|
|
277
|
+
declare_type_variables(parser, variables, buffer);
|
|
278
|
+
struct parse_method_type_arg arg = {
|
|
279
|
+
.buffer = buffer,
|
|
280
|
+
.encoding = encoding,
|
|
281
|
+
.parser = parser,
|
|
282
|
+
.require_eof = require_eof
|
|
283
|
+
};
|
|
284
|
+
|
|
285
|
+
VALUE result = rb_ensure(parse_method_type_try, (VALUE) &arg, ensure_free_parser, (VALUE) parser);
|
|
286
|
+
|
|
287
|
+
RB_GC_GUARD(string);
|
|
288
|
+
|
|
289
|
+
return result;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
static VALUE parse_signature_try(VALUE a) {
|
|
293
|
+
struct parse_signature_arg *arg = (struct parse_signature_arg *) a;
|
|
294
|
+
rbs_parser_t *parser = arg->parser;
|
|
295
|
+
|
|
296
|
+
rbs_signature_t *signature = NULL;
|
|
297
|
+
rbs_parse_signature(parser, &signature);
|
|
298
|
+
|
|
299
|
+
raise_error_if_any(parser, arg->buffer);
|
|
300
|
+
|
|
301
|
+
rbs_translation_context_t ctx = rbs_translation_context_create(
|
|
302
|
+
&parser->constant_pool,
|
|
303
|
+
arg->buffer,
|
|
304
|
+
arg->encoding
|
|
305
|
+
);
|
|
306
|
+
|
|
307
|
+
return rbs_struct_to_ruby_value(ctx, (rbs_node_t *) signature);
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
static VALUE rbsparser_parse_signature(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos, VALUE enable_forwarding_params) {
|
|
311
|
+
VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
|
|
312
|
+
StringValue(string);
|
|
313
|
+
rb_encoding *encoding = rb_enc_get(string);
|
|
314
|
+
|
|
315
|
+
rbs_parser_t *parser = alloc_parser_from_buffer_with_options(buffer, FIX2INT(start_pos), FIX2INT(end_pos), parser_options(enable_forwarding_params));
|
|
316
|
+
struct parse_signature_arg arg = {
|
|
317
|
+
.buffer = buffer,
|
|
318
|
+
.encoding = encoding,
|
|
319
|
+
.parser = parser,
|
|
320
|
+
.require_eof = false
|
|
321
|
+
};
|
|
322
|
+
|
|
323
|
+
VALUE result = rb_ensure(parse_signature_try, (VALUE) &arg, ensure_free_parser, (VALUE) parser);
|
|
324
|
+
|
|
325
|
+
RB_GC_GUARD(string);
|
|
326
|
+
|
|
327
|
+
return result;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
// Serialize a parsed node into a binary Ruby string using the same encoder the
|
|
331
|
+
// WebAssembly build uses. These `_*_to_bytes` entry points exist so the
|
|
332
|
+
// round-trip (parse -> serialize -> deserialize) can be exercised on CRuby,
|
|
333
|
+
// where it can be compared against the direct C -> Ruby translation.
|
|
334
|
+
static VALUE serialized_node_to_string(rbs_parser_t *parser, rbs_node_t *node) {
|
|
335
|
+
rbs_string_t bytes = rbs_serialize_node(parser->allocator, &parser->constant_pool, node);
|
|
336
|
+
return rb_str_new(bytes.start, (long) rbs_string_len(bytes));
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
static VALUE parse_type_to_bytes_try(VALUE a) {
|
|
340
|
+
struct parse_type_arg *arg = (struct parse_type_arg *) a;
|
|
341
|
+
rbs_parser_t *parser = arg->parser;
|
|
342
|
+
|
|
343
|
+
if (parser->next_token.type == pEOF) {
|
|
344
|
+
return Qnil;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
rbs_node_t *type;
|
|
348
|
+
rbs_parse_type(parser, &type, RTEST(arg->void_allowed), RTEST(arg->self_allowed), RTEST(arg->classish_allowed));
|
|
349
|
+
|
|
350
|
+
raise_error_if_any(parser, arg->buffer);
|
|
351
|
+
|
|
352
|
+
if (RB_TEST(arg->require_eof)) {
|
|
353
|
+
rbs_parser_advance(parser);
|
|
354
|
+
if (parser->current_token.type != pEOF) {
|
|
355
|
+
rbs_parser_set_error(parser, parser->current_token, true, "expected a token `%s`", rbs_token_type_str(pEOF));
|
|
356
|
+
raise_error(parser->error, arg->buffer);
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
return serialized_node_to_string(parser, type);
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
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) {
|
|
364
|
+
VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
|
|
365
|
+
StringValue(string);
|
|
366
|
+
rb_encoding *encoding = rb_enc_get(string);
|
|
367
|
+
|
|
368
|
+
rbs_parser_t *parser = alloc_parser_from_buffer(buffer, FIX2INT(start_pos), FIX2INT(end_pos));
|
|
369
|
+
declare_type_variables(parser, variables, buffer);
|
|
370
|
+
struct parse_type_arg arg = {
|
|
371
|
+
.buffer = buffer,
|
|
372
|
+
.encoding = encoding,
|
|
373
|
+
.parser = parser,
|
|
374
|
+
.require_eof = require_eof,
|
|
375
|
+
.void_allowed = void_allowed,
|
|
376
|
+
.self_allowed = self_allowed,
|
|
377
|
+
.classish_allowed = classish_allowed
|
|
378
|
+
};
|
|
379
|
+
|
|
380
|
+
VALUE result = rb_ensure(parse_type_to_bytes_try, (VALUE) &arg, ensure_free_parser, (VALUE) parser);
|
|
381
|
+
|
|
382
|
+
RB_GC_GUARD(string);
|
|
383
|
+
|
|
384
|
+
return result;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
static VALUE parse_method_type_to_bytes_try(VALUE a) {
|
|
388
|
+
struct parse_method_type_arg *arg = (struct parse_method_type_arg *) a;
|
|
389
|
+
rbs_parser_t *parser = arg->parser;
|
|
390
|
+
|
|
391
|
+
if (parser->next_token.type == pEOF) {
|
|
392
|
+
return Qnil;
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
rbs_method_type_t *method_type = NULL;
|
|
396
|
+
rbs_parse_method_type(parser, &method_type, RB_TEST(arg->require_eof), true);
|
|
397
|
+
|
|
398
|
+
raise_error_if_any(parser, arg->buffer);
|
|
399
|
+
|
|
400
|
+
return serialized_node_to_string(parser, (rbs_node_t *) method_type);
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
static VALUE rbsparser_parse_method_type_to_bytes(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos, VALUE variables, VALUE require_eof, VALUE enable_forwarding_params) {
|
|
404
|
+
VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
|
|
405
|
+
StringValue(string);
|
|
406
|
+
rb_encoding *encoding = rb_enc_get(string);
|
|
407
|
+
|
|
408
|
+
rbs_parser_t *parser = alloc_parser_from_buffer_with_options(buffer, FIX2INT(start_pos), FIX2INT(end_pos), parser_options(enable_forwarding_params));
|
|
409
|
+
declare_type_variables(parser, variables, buffer);
|
|
410
|
+
struct parse_method_type_arg arg = {
|
|
411
|
+
.buffer = buffer,
|
|
412
|
+
.encoding = encoding,
|
|
413
|
+
.parser = parser,
|
|
414
|
+
.require_eof = require_eof
|
|
415
|
+
};
|
|
416
|
+
|
|
417
|
+
VALUE result = rb_ensure(parse_method_type_to_bytes_try, (VALUE) &arg, ensure_free_parser, (VALUE) parser);
|
|
418
|
+
|
|
419
|
+
RB_GC_GUARD(string);
|
|
420
|
+
|
|
421
|
+
return result;
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
static VALUE parse_signature_to_bytes_try(VALUE a) {
|
|
425
|
+
struct parse_signature_arg *arg = (struct parse_signature_arg *) a;
|
|
426
|
+
rbs_parser_t *parser = arg->parser;
|
|
427
|
+
|
|
428
|
+
rbs_signature_t *signature = NULL;
|
|
429
|
+
rbs_parse_signature(parser, &signature);
|
|
430
|
+
|
|
431
|
+
raise_error_if_any(parser, arg->buffer);
|
|
432
|
+
|
|
433
|
+
return serialized_node_to_string(parser, (rbs_node_t *) signature);
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
static VALUE rbsparser_parse_signature_to_bytes(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos, VALUE enable_forwarding_params) {
|
|
437
|
+
VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
|
|
438
|
+
StringValue(string);
|
|
439
|
+
rb_encoding *encoding = rb_enc_get(string);
|
|
440
|
+
|
|
441
|
+
rbs_parser_t *parser = alloc_parser_from_buffer_with_options(buffer, FIX2INT(start_pos), FIX2INT(end_pos), parser_options(enable_forwarding_params));
|
|
442
|
+
struct parse_signature_arg arg = {
|
|
443
|
+
.buffer = buffer,
|
|
444
|
+
.encoding = encoding,
|
|
445
|
+
.parser = parser,
|
|
446
|
+
.require_eof = false
|
|
447
|
+
};
|
|
448
|
+
|
|
449
|
+
VALUE result = rb_ensure(parse_signature_to_bytes_try, (VALUE) &arg, ensure_free_parser, (VALUE) parser);
|
|
450
|
+
|
|
451
|
+
RB_GC_GUARD(string);
|
|
452
|
+
|
|
453
|
+
return result;
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
struct parse_type_params_arg {
|
|
457
|
+
VALUE buffer;
|
|
458
|
+
rb_encoding *encoding;
|
|
459
|
+
rbs_parser_t *parser;
|
|
460
|
+
VALUE module_type_params;
|
|
461
|
+
};
|
|
462
|
+
|
|
463
|
+
static VALUE parse_type_params_try(VALUE a) {
|
|
464
|
+
struct parse_type_params_arg *arg = (struct parse_type_params_arg *) a;
|
|
465
|
+
rbs_parser_t *parser = arg->parser;
|
|
466
|
+
|
|
467
|
+
if (parser->next_token.type == pEOF) {
|
|
468
|
+
return Qnil;
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
rbs_node_list_t *params = NULL;
|
|
472
|
+
rbs_parse_type_params(parser, arg->module_type_params, ¶ms);
|
|
473
|
+
|
|
474
|
+
raise_error_if_any(parser, arg->buffer);
|
|
475
|
+
|
|
476
|
+
rbs_translation_context_t ctx = rbs_translation_context_create(
|
|
477
|
+
&parser->constant_pool,
|
|
478
|
+
arg->buffer,
|
|
479
|
+
arg->encoding
|
|
480
|
+
);
|
|
481
|
+
|
|
482
|
+
return rbs_node_list_to_ruby_array(ctx, params);
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
static VALUE rbsparser_parse_type_params(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos, VALUE module_type_params) {
|
|
486
|
+
VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
|
|
487
|
+
StringValue(string);
|
|
488
|
+
rb_encoding *encoding = rb_enc_get(string);
|
|
489
|
+
|
|
490
|
+
rbs_parser_t *parser = alloc_parser_from_buffer(buffer, FIX2INT(start_pos), FIX2INT(end_pos));
|
|
491
|
+
struct parse_type_params_arg arg = {
|
|
492
|
+
.buffer = buffer,
|
|
493
|
+
.encoding = encoding,
|
|
494
|
+
.parser = parser,
|
|
495
|
+
.module_type_params = module_type_params
|
|
496
|
+
};
|
|
497
|
+
|
|
498
|
+
VALUE result = rb_ensure(parse_type_params_try, (VALUE) &arg, ensure_free_parser, (VALUE) parser);
|
|
499
|
+
|
|
500
|
+
RB_GC_GUARD(string);
|
|
501
|
+
|
|
502
|
+
return result;
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
static VALUE parse_inline_leading_annotation_try(VALUE a) {
|
|
506
|
+
struct parse_type_arg *arg = (struct parse_type_arg *) a;
|
|
507
|
+
rbs_parser_t *parser = arg->parser;
|
|
508
|
+
|
|
509
|
+
rbs_ast_ruby_annotations_t *annotation = NULL;
|
|
510
|
+
bool success = rbs_parse_inline_leading_annotation(parser, &annotation);
|
|
511
|
+
|
|
512
|
+
raise_error_if_any(parser, arg->buffer);
|
|
513
|
+
|
|
514
|
+
if (!success || annotation == NULL) {
|
|
515
|
+
return Qnil;
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
rbs_translation_context_t ctx = rbs_translation_context_create(
|
|
519
|
+
&parser->constant_pool,
|
|
520
|
+
arg->buffer,
|
|
521
|
+
arg->encoding
|
|
522
|
+
);
|
|
523
|
+
|
|
524
|
+
return rbs_struct_to_ruby_value(ctx, (rbs_node_t *) annotation);
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
static VALUE rbsparser_parse_inline_leading_annotation(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos, VALUE variables) {
|
|
528
|
+
VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
|
|
529
|
+
StringValue(string);
|
|
530
|
+
rb_encoding *encoding = rb_enc_get(string);
|
|
531
|
+
|
|
532
|
+
rbs_parser_t *parser = alloc_parser_from_buffer(buffer, FIX2INT(start_pos), FIX2INT(end_pos));
|
|
533
|
+
declare_type_variables(parser, variables, buffer);
|
|
534
|
+
struct parse_type_arg arg = {
|
|
535
|
+
.buffer = buffer,
|
|
536
|
+
.encoding = encoding,
|
|
537
|
+
.parser = parser,
|
|
538
|
+
.require_eof = Qfalse
|
|
539
|
+
};
|
|
540
|
+
|
|
541
|
+
VALUE result = rb_ensure(parse_inline_leading_annotation_try, (VALUE) &arg, ensure_free_parser, (VALUE) parser);
|
|
542
|
+
|
|
543
|
+
RB_GC_GUARD(string);
|
|
544
|
+
|
|
545
|
+
return result;
|
|
9
546
|
}
|
|
10
547
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
548
|
+
static VALUE parse_inline_trailing_annotation_try(VALUE a) {
|
|
549
|
+
struct parse_type_arg *arg = (struct parse_type_arg *) a;
|
|
550
|
+
rbs_parser_t *parser = arg->parser;
|
|
551
|
+
|
|
552
|
+
rbs_ast_ruby_annotations_t *annotation = NULL;
|
|
553
|
+
bool success = rbs_parse_inline_trailing_annotation(parser, &annotation);
|
|
554
|
+
|
|
555
|
+
raise_error_if_any(parser, arg->buffer);
|
|
556
|
+
|
|
557
|
+
if (!success || annotation == NULL) {
|
|
558
|
+
return Qnil;
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
rbs_translation_context_t ctx = rbs_translation_context_create(
|
|
562
|
+
&parser->constant_pool,
|
|
563
|
+
arg->buffer,
|
|
564
|
+
arg->encoding
|
|
565
|
+
);
|
|
566
|
+
|
|
567
|
+
return rbs_struct_to_ruby_value(ctx, (rbs_node_t *) annotation);
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
static VALUE rbsparser_parse_inline_trailing_annotation(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos, VALUE variables) {
|
|
571
|
+
VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
|
|
572
|
+
StringValue(string);
|
|
573
|
+
rb_encoding *encoding = rb_enc_get(string);
|
|
574
|
+
|
|
575
|
+
rbs_parser_t *parser = alloc_parser_from_buffer(buffer, FIX2INT(start_pos), FIX2INT(end_pos));
|
|
576
|
+
declare_type_variables(parser, variables, buffer);
|
|
577
|
+
struct parse_type_arg arg = {
|
|
578
|
+
.buffer = buffer,
|
|
579
|
+
.encoding = encoding,
|
|
580
|
+
.parser = parser,
|
|
581
|
+
.require_eof = Qfalse
|
|
582
|
+
};
|
|
583
|
+
|
|
584
|
+
VALUE result = rb_ensure(parse_inline_trailing_annotation_try, (VALUE) &arg, ensure_free_parser, (VALUE) parser);
|
|
585
|
+
|
|
586
|
+
RB_GC_GUARD(string);
|
|
587
|
+
|
|
588
|
+
return result;
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
static VALUE rbsparser_lex(VALUE self, VALUE buffer, VALUE end_pos) {
|
|
592
|
+
VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
|
|
593
|
+
StringValue(string);
|
|
594
|
+
rb_encoding *encoding = rb_enc_get(string);
|
|
595
|
+
|
|
596
|
+
rbs_allocator_t *allocator = rbs_allocator_init();
|
|
597
|
+
rbs_lexer_t *lexer = alloc_lexer_from_buffer(allocator, string, encoding, 0, FIX2INT(end_pos));
|
|
598
|
+
|
|
599
|
+
VALUE results = rb_ary_new();
|
|
600
|
+
rbs_token_t token = NullToken;
|
|
601
|
+
while (token.type != pEOF) {
|
|
602
|
+
token = rbs_lexer_next_token(lexer);
|
|
603
|
+
VALUE type = ID2SYM(rb_intern(rbs_token_type_str(token.type)));
|
|
604
|
+
VALUE location = rbs_new_location(buffer, token.range);
|
|
605
|
+
VALUE pair = rb_ary_new3(2, type, location);
|
|
606
|
+
rb_ary_push(results, pair);
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
rbs_allocator_free(allocator);
|
|
610
|
+
RB_GC_GUARD(string);
|
|
611
|
+
|
|
612
|
+
return results;
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
void rbs__init_parser(void) {
|
|
616
|
+
RBS_Parser = rb_define_class_under(RBS, "Parser", rb_cObject);
|
|
617
|
+
rb_gc_register_mark_object(RBS_Parser);
|
|
618
|
+
|
|
619
|
+
EMPTY_ARRAY = rb_obj_freeze(rb_ary_new());
|
|
620
|
+
rb_gc_register_mark_object(EMPTY_ARRAY);
|
|
621
|
+
|
|
622
|
+
EMPTY_HASH = rb_obj_freeze(rb_hash_new());
|
|
623
|
+
rb_gc_register_mark_object(EMPTY_HASH);
|
|
624
|
+
|
|
625
|
+
rb_define_singleton_method(RBS_Parser, "_parse_type", rbsparser_parse_type, 8);
|
|
626
|
+
rb_define_singleton_method(RBS_Parser, "_parse_method_type", rbsparser_parse_method_type, 6);
|
|
627
|
+
rb_define_singleton_method(RBS_Parser, "_parse_signature", rbsparser_parse_signature, 4);
|
|
628
|
+
rb_define_singleton_method(RBS_Parser, "_parse_type_to_bytes", rbsparser_parse_type_to_bytes, 8);
|
|
629
|
+
rb_define_singleton_method(RBS_Parser, "_parse_method_type_to_bytes", rbsparser_parse_method_type_to_bytes, 6);
|
|
630
|
+
rb_define_singleton_method(RBS_Parser, "_parse_signature_to_bytes", rbsparser_parse_signature_to_bytes, 4);
|
|
631
|
+
rb_define_singleton_method(RBS_Parser, "_parse_type_params", rbsparser_parse_type_params, 4);
|
|
632
|
+
rb_define_singleton_method(RBS_Parser, "_parse_inline_leading_annotation", rbsparser_parse_inline_leading_annotation, 4);
|
|
633
|
+
rb_define_singleton_method(RBS_Parser, "_parse_inline_trailing_annotation", rbsparser_parse_inline_trailing_annotation, 4);
|
|
634
|
+
rb_define_singleton_method(RBS_Parser, "_lex", rbsparser_lex, 2);
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
static void Deinit_rbs_extension(ruby_vm_t *_) {
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
void Init_rbs_extension(void) {
|
|
14
641
|
#ifdef HAVE_RB_EXT_RACTOR_SAFE
|
|
15
|
-
|
|
642
|
+
rb_ext_ractor_safe(true);
|
|
16
643
|
#endif
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
//
|
|
23
|
-
// ```bash
|
|
24
|
-
// grep -o 'INTERN("\([^"]*\)")' ext/rbs_extension/parser.c \
|
|
25
|
-
// | sed 's/INTERN("\(.*\)")/\1/' \
|
|
26
|
-
// | sort -u \
|
|
27
|
-
// | wc -l
|
|
28
|
-
// ```
|
|
29
|
-
const size_t num_uniquely_interned_strings = 26;
|
|
30
|
-
rbs_constant_pool_init(RBS_GLOBAL_CONSTANT_POOL, num_uniquely_interned_strings);
|
|
31
|
-
|
|
32
|
-
ruby_vm_at_exit(Deinit_rbs_extension);
|
|
644
|
+
rbs__init_constants();
|
|
645
|
+
rbs__init_location();
|
|
646
|
+
rbs__init_parser();
|
|
647
|
+
|
|
648
|
+
ruby_vm_at_exit(Deinit_rbs_extension);
|
|
33
649
|
}
|
|
@@ -1,31 +1,16 @@
|
|
|
1
1
|
#include <stdbool.h>
|
|
2
|
+
#include "compat.h"
|
|
2
3
|
|
|
4
|
+
SUPPRESS_RUBY_HEADER_DIAGNOSTICS_BEGIN
|
|
3
5
|
#include "ruby.h"
|
|
4
6
|
#include "ruby/re.h"
|
|
5
7
|
#include "ruby/encoding.h"
|
|
8
|
+
SUPPRESS_RUBY_HEADER_DIAGNOSTICS_END
|
|
6
9
|
|
|
10
|
+
#include "class_constants.h"
|
|
7
11
|
#include "rbs.h"
|
|
8
|
-
#include "lexer.h"
|
|
9
|
-
#include "parser.h"
|
|
10
12
|
|
|
11
13
|
/**
|
|
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
|
|
14
|
+
* RBS::Parser class
|
|
21
15
|
* */
|
|
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);
|
|
16
|
+
extern VALUE RBS_Parser;
|