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.
Files changed (202) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/comments.yml +3 -3
  3. data/.github/workflows/ruby.yml +24 -35
  4. data/.github/workflows/typecheck.yml +3 -1
  5. data/.github/workflows/windows.yml +2 -2
  6. data/.gitignore +0 -4
  7. data/CHANGELOG.md +0 -88
  8. data/README.md +1 -38
  9. data/Rakefile +20 -142
  10. data/Steepfile +1 -0
  11. data/config.yml +43 -1
  12. data/core/array.rbs +46 -100
  13. data/core/complex.rbs +21 -32
  14. data/core/dir.rbs +2 -2
  15. data/core/encoding.rbs +9 -6
  16. data/core/enumerable.rbs +3 -90
  17. data/core/enumerator.rbs +1 -18
  18. data/core/errno.rbs +0 -8
  19. data/core/errors.rbs +1 -28
  20. data/core/exception.rbs +2 -2
  21. data/core/fiber.rbs +4 -5
  22. data/core/file.rbs +12 -27
  23. data/core/file_test.rbs +1 -1
  24. data/core/float.rbs +22 -209
  25. data/core/gc.rbs +281 -417
  26. data/core/hash.rbs +727 -1024
  27. data/core/integer.rbs +38 -78
  28. data/core/io/buffer.rbs +7 -18
  29. data/core/io/wait.rbs +33 -11
  30. data/core/io.rbs +12 -14
  31. data/core/kernel.rbs +51 -57
  32. data/core/marshal.rbs +1 -1
  33. data/core/match_data.rbs +1 -1
  34. data/core/math.rbs +3 -42
  35. data/core/method.rbs +6 -14
  36. data/core/module.rbs +17 -88
  37. data/core/nil_class.rbs +3 -3
  38. data/core/numeric.rbs +16 -16
  39. data/core/object.rbs +3 -3
  40. data/core/object_space.rbs +15 -21
  41. data/core/proc.rbs +8 -15
  42. data/core/process.rbs +2 -2
  43. data/core/ractor.rbs +437 -278
  44. data/core/range.rbs +8 -7
  45. data/core/rational.rbs +24 -37
  46. data/core/rbs/unnamed/argf.rbs +2 -2
  47. data/core/rbs/unnamed/env_class.rbs +1 -1
  48. data/core/rbs/unnamed/random.rbs +2 -4
  49. data/core/regexp.rbs +20 -25
  50. data/core/ruby_vm.rbs +4 -6
  51. data/core/rubygems/errors.rbs +70 -3
  52. data/core/rubygems/rubygems.rbs +79 -11
  53. data/core/rubygems/version.rbs +3 -2
  54. data/core/set.rbs +359 -488
  55. data/core/string.rbs +1228 -3153
  56. data/core/struct.rbs +1 -1
  57. data/core/symbol.rbs +4 -4
  58. data/core/thread.rbs +29 -92
  59. data/core/time.rbs +9 -35
  60. data/core/trace_point.rbs +4 -7
  61. data/core/unbound_method.rbs +6 -14
  62. data/docs/collection.md +2 -2
  63. data/docs/gem.md +1 -0
  64. data/docs/sigs.md +3 -3
  65. data/ext/rbs_extension/ast_translation.c +1077 -944
  66. data/ext/rbs_extension/ast_translation.h +0 -7
  67. data/ext/rbs_extension/class_constants.c +83 -71
  68. data/ext/rbs_extension/class_constants.h +7 -4
  69. data/ext/rbs_extension/extconf.rb +2 -24
  70. data/ext/rbs_extension/legacy_location.c +172 -173
  71. data/ext/rbs_extension/legacy_location.h +3 -8
  72. data/ext/rbs_extension/main.c +289 -239
  73. data/ext/rbs_extension/rbs_extension.h +0 -3
  74. data/ext/rbs_extension/rbs_string_bridging.h +0 -4
  75. data/include/rbs/ast.h +98 -37
  76. data/include/rbs/defines.h +12 -38
  77. data/include/rbs/lexer.h +114 -126
  78. data/include/rbs/location.h +14 -14
  79. data/include/rbs/parser.h +37 -21
  80. data/include/rbs/string.h +5 -3
  81. data/include/rbs/util/rbs_allocator.h +19 -40
  82. data/include/rbs/util/rbs_assert.h +1 -12
  83. data/include/rbs/util/rbs_constant_pool.h +3 -3
  84. data/include/rbs/util/rbs_encoding.h +1 -3
  85. data/include/rbs/util/rbs_unescape.h +1 -2
  86. data/lib/rbs/ast/ruby/annotations.rb +119 -0
  87. data/lib/rbs/ast/ruby/comment_block.rb +221 -0
  88. data/lib/rbs/ast/ruby/declarations.rb +86 -0
  89. data/lib/rbs/ast/ruby/helpers/constant_helper.rb +24 -0
  90. data/lib/rbs/ast/ruby/helpers/location_helper.rb +15 -0
  91. data/lib/rbs/ast/ruby/members.rb +213 -0
  92. data/lib/rbs/buffer.rb +104 -24
  93. data/lib/rbs/cli/validate.rb +40 -35
  94. data/lib/rbs/cli.rb +5 -6
  95. data/lib/rbs/collection/config/lockfile_generator.rb +0 -1
  96. data/lib/rbs/collection.rb +0 -1
  97. data/lib/rbs/definition.rb +6 -1
  98. data/lib/rbs/definition_builder/ancestor_builder.rb +65 -62
  99. data/lib/rbs/definition_builder/method_builder.rb +45 -30
  100. data/lib/rbs/definition_builder.rb +44 -9
  101. data/lib/rbs/environment/class_entry.rb +69 -0
  102. data/lib/rbs/environment/module_entry.rb +66 -0
  103. data/lib/rbs/environment.rb +244 -218
  104. data/lib/rbs/environment_loader.rb +3 -3
  105. data/lib/rbs/errors.rb +5 -4
  106. data/lib/rbs/inline_parser/comment_association.rb +117 -0
  107. data/lib/rbs/inline_parser.rb +206 -0
  108. data/lib/rbs/location_aux.rb +35 -3
  109. data/lib/rbs/parser_aux.rb +11 -6
  110. data/lib/rbs/prototype/runtime.rb +2 -2
  111. data/lib/rbs/resolver/constant_resolver.rb +2 -2
  112. data/lib/rbs/resolver/type_name_resolver.rb +38 -124
  113. data/lib/rbs/source.rb +99 -0
  114. data/lib/rbs/subtractor.rb +4 -3
  115. data/lib/rbs/test/type_check.rb +0 -14
  116. data/lib/rbs/types.rb +1 -3
  117. data/lib/rbs/version.rb +1 -1
  118. data/lib/rbs.rb +13 -1
  119. data/lib/rdoc/discover.rb +1 -1
  120. data/lib/rdoc_plugin/parser.rb +1 -1
  121. data/rbs.gemspec +1 -0
  122. data/sig/ancestor_builder.rbs +1 -1
  123. data/sig/ast/ruby/annotations.rbs +110 -0
  124. data/sig/ast/ruby/comment_block.rbs +119 -0
  125. data/sig/ast/ruby/declarations.rbs +60 -0
  126. data/sig/ast/ruby/helpers/constant_helper.rbs +11 -0
  127. data/sig/ast/ruby/helpers/location_helper.rbs +15 -0
  128. data/sig/ast/ruby/members.rbs +72 -0
  129. data/sig/buffer.rbs +63 -5
  130. data/sig/definition.rbs +1 -0
  131. data/sig/definition_builder.rbs +1 -1
  132. data/sig/environment/class_entry.rbs +50 -0
  133. data/sig/environment/module_entry.rbs +50 -0
  134. data/sig/environment.rbs +28 -133
  135. data/sig/errors.rbs +13 -6
  136. data/sig/inline_parser/comment_association.rbs +71 -0
  137. data/sig/inline_parser.rbs +87 -0
  138. data/sig/location.rbs +32 -7
  139. data/sig/manifest.yaml +1 -0
  140. data/sig/method_builder.rbs +7 -4
  141. data/sig/parser.rbs +16 -20
  142. data/sig/resolver/type_name_resolver.rbs +7 -38
  143. data/sig/source.rbs +48 -0
  144. data/sig/types.rbs +1 -4
  145. data/src/ast.c +290 -201
  146. data/src/lexer.c +2813 -2902
  147. data/src/lexer.re +4 -0
  148. data/src/lexstate.c +155 -169
  149. data/src/location.c +40 -40
  150. data/src/parser.c +2665 -2433
  151. data/src/string.c +48 -0
  152. data/src/util/rbs_allocator.c +77 -80
  153. data/src/util/rbs_assert.c +10 -10
  154. data/src/util/rbs_buffer.c +2 -2
  155. data/src/util/rbs_constant_pool.c +15 -13
  156. data/src/util/rbs_encoding.c +4062 -20097
  157. data/src/util/rbs_unescape.c +48 -85
  158. data/stdlib/bigdecimal/0/big_decimal.rbs +82 -100
  159. data/stdlib/bigdecimal-math/0/big_math.rbs +8 -169
  160. data/stdlib/cgi/0/core.rbs +396 -2
  161. data/stdlib/cgi/0/manifest.yaml +0 -1
  162. data/stdlib/coverage/0/coverage.rbs +1 -3
  163. data/stdlib/date/0/date.rbs +59 -67
  164. data/stdlib/date/0/date_time.rbs +1 -1
  165. data/stdlib/delegate/0/delegator.rbs +7 -10
  166. data/stdlib/erb/0/erb.rbs +347 -737
  167. data/stdlib/fileutils/0/fileutils.rbs +13 -18
  168. data/stdlib/forwardable/0/forwardable.rbs +0 -3
  169. data/stdlib/json/0/json.rbs +48 -68
  170. data/stdlib/net-http/0/net-http.rbs +0 -3
  171. data/stdlib/objspace/0/objspace.rbs +4 -9
  172. data/stdlib/open-uri/0/open-uri.rbs +0 -40
  173. data/stdlib/openssl/0/openssl.rbs +228 -331
  174. data/stdlib/optparse/0/optparse.rbs +3 -3
  175. data/{core → stdlib/pathname/0}/pathname.rbs +355 -255
  176. data/stdlib/psych/0/psych.rbs +3 -3
  177. data/stdlib/rdoc/0/rdoc.rbs +1 -1
  178. data/stdlib/resolv/0/resolv.rbs +68 -25
  179. data/stdlib/ripper/0/ripper.rbs +2 -5
  180. data/stdlib/singleton/0/singleton.rbs +0 -3
  181. data/stdlib/socket/0/socket.rbs +1 -13
  182. data/stdlib/socket/0/tcp_socket.rbs +2 -10
  183. data/stdlib/stringio/0/stringio.rbs +85 -1176
  184. data/stdlib/strscan/0/string_scanner.rbs +31 -31
  185. data/stdlib/tempfile/0/tempfile.rbs +3 -3
  186. data/stdlib/time/0/time.rbs +1 -1
  187. data/stdlib/timeout/0/timeout.rbs +7 -63
  188. data/stdlib/tsort/0/cyclic.rbs +0 -3
  189. data/stdlib/uri/0/common.rbs +2 -11
  190. data/stdlib/uri/0/file.rbs +1 -1
  191. data/stdlib/uri/0/generic.rbs +16 -17
  192. data/stdlib/uri/0/rfc2396_parser.rbs +7 -6
  193. data/stdlib/zlib/0/zstream.rbs +0 -1
  194. metadata +40 -12
  195. data/.clang-format +0 -74
  196. data/.clangd +0 -2
  197. data/.github/workflows/c-check.yml +0 -54
  198. data/core/ruby.rbs +0 -53
  199. data/docs/aliases.md +0 -79
  200. data/docs/encoding.md +0 -56
  201. data/ext/rbs_extension/compat.h +0 -10
  202. data/stdlib/cgi-escape/0/escape.rbs +0 -153
@@ -16,31 +16,31 @@
16
16
  * ```
17
17
  * */
18
18
  static NORETURN(void) raise_error(rbs_error_t *error, VALUE buffer) {
19
- RBS_ASSERT(error != NULL, "raise_error() called with NULL error");
19
+ rbs_assert(error != NULL, "raise_error() called with NULL error");
20
20
 
21
- if (!error->syntax_error) {
22
- rb_raise(rb_eRuntimeError, "Unexpected error");
23
- }
21
+ if (!error->syntax_error) {
22
+ rb_raise(rb_eRuntimeError, "Unexpected error");
23
+ }
24
24
 
25
- VALUE location = rbs_new_location(buffer, error->token.range);
26
- VALUE type = rb_str_new_cstr(rbs_token_type_str(error->token.type));
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
- 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
- );
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
- rb_exc_raise(rb_error);
37
+ rb_exc_raise(rb_error);
38
38
  }
39
39
 
40
40
  void raise_error_if_any(rbs_parser_t *parser, VALUE buffer) {
41
- if (parser->error != NULL) {
42
- raise_error(parser->error, buffer);
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
- if (NIL_P(variables)) return; // Nothing to do.
52
+ if (NIL_P(variables)) return; // Nothing to do.
53
53
 
54
- if (!RB_TYPE_P(variables, T_ARRAY)) {
55
- rbs_parser_free(parser);
56
- rb_raise(rb_eTypeError, "wrong argument type %" PRIsVALUE " (must be an Array of Symbols or nil)", rb_obj_class(variables));
57
- }
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
- rbs_parser_push_typevar_table(parser, true);
61
+ rbs_parser_push_typevar_table(parser, true);
60
62
 
61
- for (long i = 0; i < rb_array_len(variables); i++) {
62
- VALUE symbol = rb_ary_entry(variables, i);
63
+ for (long i = 0; i < rb_array_len(variables); i++) {
64
+ VALUE symbol = rb_ary_entry(variables, i);
63
65
 
64
- if (!RB_TYPE_P(symbol, T_SYMBOL)) {
65
- rbs_parser_free(parser);
66
- rb_raise(rb_eTypeError, "Type variables Array contains invalid value %" PRIsVALUE " of type %" PRIsVALUE " (must be an Array of Symbols or nil)", rb_inspect(symbol), rb_obj_class(symbol));
67
- }
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
- VALUE name_str = rb_sym2str(symbol);
73
+ VALUE name_str = rb_sym2str(symbol);
70
74
 
71
- rbs_constant_id_t id = rbs_constant_pool_insert_shared(
72
- &parser->constant_pool,
73
- (const uint8_t *) RSTRING_PTR(name_str),
74
- RSTRING_LEN(name_str)
75
- );
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
- if (!rbs_parser_insert_typevar(parser, id)) {
78
- raise_error(parser->error, buffer);
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
- VALUE buffer;
85
- rb_encoding *encoding;
86
- rbs_parser_t *parser;
87
- VALUE require_eof;
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
- rbs_parser_free((rbs_parser_t *) parser);
92
- return Qnil;
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
- struct parse_type_arg *arg = (struct parse_type_arg *) a;
97
- rbs_parser_t *parser = arg->parser;
100
+ struct parse_type_arg *arg = (struct parse_type_arg *)a;
101
+ rbs_parser_t *parser = arg->parser;
98
102
 
99
- if (parser->next_token.type == pEOF) {
100
- return Qnil;
101
- }
103
+ if (parser->next_token.type == pEOF) {
104
+ return Qnil;
105
+ }
102
106
 
103
- rbs_node_t *type;
104
- rbs_parse_type(parser, &type);
107
+ rbs_node_t *type;
108
+ rbs_parse_type(parser, &type);
105
109
 
106
- raise_error_if_any(parser, arg->buffer);
110
+ raise_error_if_any(parser, arg->buffer);
107
111
 
108
- if (RB_TEST(arg->require_eof)) {
109
- rbs_parser_advance(parser);
110
- if (parser->current_token.type != pEOF) {
111
- rbs_parser_set_error(parser, parser->current_token, true, "expected a token `%s`", rbs_token_type_str(pEOF));
112
- raise_error(parser->error, arg->buffer);
113
- }
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
- rbs_translation_context_t ctx = rbs_translation_context_create(
117
- &parser->constant_pool,
118
- arg->buffer,
119
- arg->encoding
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
- return rbs_struct_to_ruby_value(ctx, type);
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
- if (start_pos < 0 || end_pos < 0) {
127
- rb_raise(rb_eArgError, "negative position range: %d...%d", start_pos, end_pos);
128
- }
129
-
130
- const char *encoding_name = rb_enc_name(encoding);
131
-
132
- return rbs_lexer_new(
133
- allocator,
134
- rbs_string_from_ruby_string(string),
135
- rbs_encoding_find((const uint8_t *) encoding_name, (const uint8_t *) (encoding_name + strlen(encoding_name))),
136
- start_pos,
137
- end_pos
138
- );
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
- if (start_pos < 0 || end_pos < 0) {
143
- rb_raise(rb_eArgError, "negative position range: %d...%d", start_pos, end_pos);
144
- }
145
-
146
- VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
147
- StringValue(string);
148
-
149
- rb_encoding *encoding = rb_enc_get(string);
150
- const char *encoding_name = rb_enc_name(encoding);
151
-
152
- return rbs_parser_new(
153
- rbs_string_from_ruby_string(string),
154
- rbs_encoding_find((const uint8_t *) encoding_name, (const uint8_t *) (encoding_name + strlen(encoding_name))),
155
- start_pos,
156
- end_pos
157
- );
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
- VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
162
- StringValue(string);
163
- rb_encoding *encoding = rb_enc_get(string);
166
+ VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
167
+ StringValue(string);
168
+ rb_encoding *encoding = rb_enc_get(string);
164
169
 
165
- rbs_parser_t *parser = alloc_parser_from_buffer(buffer, FIX2INT(start_pos), FIX2INT(end_pos));
166
- declare_type_variables(parser, variables, buffer);
167
- struct parse_type_arg arg = {
168
- .buffer = buffer,
169
- .encoding = encoding,
170
- .parser = parser,
171
- .require_eof = require_eof
172
- };
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
- VALUE result = rb_ensure(parse_type_try, (VALUE) &arg, ensure_free_parser, (VALUE) parser);
179
+ VALUE result = rb_ensure(parse_type_try, (VALUE)&arg, ensure_free_parser, (VALUE)parser);
175
180
 
176
- RB_GC_GUARD(string);
181
+ RB_GC_GUARD(string);
177
182
 
178
- return result;
183
+ return result;
179
184
  }
180
185
 
181
186
  static VALUE parse_method_type_try(VALUE a) {
182
- struct parse_type_arg *arg = (struct parse_type_arg *) a;
183
- rbs_parser_t *parser = arg->parser;
187
+ struct parse_type_arg *arg = (struct parse_type_arg *)a;
188
+ rbs_parser_t *parser = arg->parser;
184
189
 
185
- if (parser->next_token.type == pEOF) {
186
- return Qnil;
187
- }
190
+ if (parser->next_token.type == pEOF) {
191
+ return Qnil;
192
+ }
188
193
 
189
- rbs_method_type_t *method_type = NULL;
190
- rbs_parse_method_type(parser, &method_type, RB_TEST(arg->require_eof));
194
+ rbs_method_type_t *method_type = NULL;
195
+ rbs_parse_method_type(parser, &method_type);
191
196
 
192
- raise_error_if_any(parser, arg->buffer);
197
+ raise_error_if_any(parser, arg->buffer);
193
198
 
194
- rbs_translation_context_t ctx = rbs_translation_context_create(
195
- &parser->constant_pool,
196
- arg->buffer,
197
- arg->encoding
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
- return rbs_struct_to_ruby_value(ctx, (rbs_node_t *) method_type);
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
- VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
205
- StringValue(string);
206
- rb_encoding *encoding = rb_enc_get(string);
217
+ VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
218
+ StringValue(string);
219
+ rb_encoding *encoding = rb_enc_get(string);
207
220
 
208
- rbs_parser_t *parser = alloc_parser_from_buffer(buffer, FIX2INT(start_pos), FIX2INT(end_pos));
209
- declare_type_variables(parser, variables, buffer);
210
- struct parse_type_arg arg = {
211
- .buffer = buffer,
212
- .encoding = encoding,
213
- .parser = parser,
214
- .require_eof = require_eof
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
- VALUE result = rb_ensure(parse_method_type_try, (VALUE) &arg, ensure_free_parser, (VALUE) parser);
230
+ VALUE result = rb_ensure(parse_method_type_try, (VALUE)&arg, ensure_free_parser, (VALUE)parser);
218
231
 
219
- RB_GC_GUARD(string);
232
+ RB_GC_GUARD(string);
220
233
 
221
- return result;
234
+ return result;
222
235
  }
223
236
 
224
237
  static VALUE parse_signature_try(VALUE a) {
225
- struct parse_type_arg *arg = (struct parse_type_arg *) a;
226
- rbs_parser_t *parser = arg->parser;
238
+ struct parse_type_arg *arg = (struct parse_type_arg *)a;
239
+ rbs_parser_t *parser = arg->parser;
227
240
 
228
- rbs_signature_t *signature = NULL;
229
- rbs_parse_signature(parser, &signature);
241
+ rbs_signature_t *signature = NULL;
242
+ rbs_parse_signature(parser, &signature);
230
243
 
231
- raise_error_if_any(parser, arg->buffer);
244
+ raise_error_if_any(parser, arg->buffer);
232
245
 
233
- rbs_translation_context_t ctx = rbs_translation_context_create(
234
- &parser->constant_pool,
235
- arg->buffer,
236
- arg->encoding
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
- return rbs_struct_to_ruby_value(ctx, (rbs_node_t *) signature);
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
- VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
244
- StringValue(string);
245
- rb_encoding *encoding = rb_enc_get(string);
256
+ VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
257
+ StringValue(string);
258
+ rb_encoding *encoding = rb_enc_get(string);
246
259
 
247
- rbs_parser_t *parser = alloc_parser_from_buffer(buffer, FIX2INT(start_pos), FIX2INT(end_pos));
248
- struct parse_type_arg arg = {
249
- .buffer = buffer,
250
- .encoding = encoding,
251
- .parser = parser,
252
- .require_eof = false
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
- VALUE result = rb_ensure(parse_signature_try, (VALUE) &arg, ensure_free_parser, (VALUE) parser);
268
+ VALUE result = rb_ensure(parse_signature_try, (VALUE)&arg, ensure_free_parser, (VALUE)parser);
256
269
 
257
- RB_GC_GUARD(string);
270
+ RB_GC_GUARD(string);
258
271
 
259
- return result;
272
+ return result;
260
273
  }
261
274
 
262
- struct parse_type_params_arg {
263
- VALUE buffer;
264
- rb_encoding *encoding;
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
- if (parser->next_token.type == pEOF) {
274
- return Qnil;
275
- }
279
+ rbs_ast_ruby_annotations_t *annotation = NULL;
280
+ bool success = rbs_parse_inline_leading_annotation(parser, &annotation);
276
281
 
277
- rbs_node_list_t *params = NULL;
278
- rbs_parse_type_params(parser, arg->module_type_params, &params);
282
+ raise_error_if_any(parser, arg->buffer);
279
283
 
280
- raise_error_if_any(parser, arg->buffer);
284
+ if (!success || annotation == NULL) {
285
+ return Qnil;
286
+ }
281
287
 
282
- rbs_translation_context_t ctx = rbs_translation_context_create(
283
- &parser->constant_pool,
284
- arg->buffer,
285
- arg->encoding
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
- return rbs_node_list_to_ruby_array(ctx, params);
294
+ return rbs_struct_to_ruby_value(ctx, (rbs_node_t *) annotation);
289
295
  }
290
296
 
291
- static VALUE rbsparser_parse_type_params(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos, VALUE module_type_params) {
292
- VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
293
- StringValue(string);
294
- rb_encoding *encoding = rb_enc_get(string);
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
- rbs_parser_t *parser = alloc_parser_from_buffer(buffer, FIX2INT(start_pos), FIX2INT(end_pos));
297
- struct parse_type_params_arg arg = {
298
- .buffer = buffer,
299
- .encoding = encoding,
300
- .parser = parser,
301
- .module_type_params = module_type_params
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
- VALUE result = rb_ensure(parse_type_params_try, (VALUE) &arg, ensure_free_parser, (VALUE) parser);
311
+ VALUE result = rb_ensure(parse_inline_leading_annotation_try, (VALUE)&arg, ensure_free_parser, (VALUE)parser);
305
312
 
306
- RB_GC_GUARD(string);
313
+ RB_GC_GUARD(string);
307
314
 
308
- return result;
315
+ return result;
309
316
  }
310
317
 
311
- static VALUE rbsparser_lex(VALUE self, VALUE buffer, VALUE end_pos) {
312
- VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
313
- StringValue(string);
314
- rb_encoding *encoding = rb_enc_get(string);
315
-
316
- rbs_allocator_t *allocator = rbs_allocator_init();
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
- rbs_allocator_free(allocator);
330
- RB_GC_GUARD(string);
325
+ raise_error_if_any(parser, arg->buffer);
331
326
 
332
- return results;
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
- void rbs__init_parser(void) {
336
- RBS_Parser = rb_define_class_under(RBS, "Parser", rb_cObject);
337
- rb_gc_register_mark_object(RBS_Parser);
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
- EMPTY_ARRAY = rb_obj_freeze(rb_ary_new());
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
- EMPTY_HASH = rb_obj_freeze(rb_hash_new());
343
- rb_gc_register_mark_object(EMPTY_HASH);
356
+ RB_GC_GUARD(string);
344
357
 
345
- rb_define_singleton_method(RBS_Parser, "_parse_type", rbsparser_parse_type, 5);
346
- rb_define_singleton_method(RBS_Parser, "_parse_method_type", rbsparser_parse_method_type, 5);
347
- rb_define_singleton_method(RBS_Parser, "_parse_signature", rbsparser_parse_signature, 3);
348
- rb_define_singleton_method(RBS_Parser, "_parse_type_params", rbsparser_parse_type_params, 4);
349
- rb_define_singleton_method(RBS_Parser, "_lex", rbsparser_lex, 2);
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 void Deinit_rbs_extension(ruby_vm_t *_) {
353
- rbs_constant_pool_free(RBS_GLOBAL_CONSTANT_POOL);
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 Init_rbs_extension(void) {
404
+ void
405
+ Init_rbs_extension(void)
406
+ {
357
407
  #ifdef HAVE_RB_EXT_RACTOR_SAFE
358
- rb_ext_ractor_safe(true);
408
+ rb_ext_ractor_safe(true);
359
409
  #endif
360
- rbs__init_constants();
361
- rbs__init_location();
362
- rbs__init_parser();
410
+ rbs__init_constants();
411
+ rbs__init_location();
412
+ rbs__init_parser();
363
413
 
364
- /* Calculated based on the number of unique strings used with the `INTERN` macro in `parser.c`.
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
- const size_t num_uniquely_interned_strings = 26;
374
- rbs_constant_pool_init(RBS_GLOBAL_CONSTANT_POOL, num_uniquely_interned_strings);
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
- ruby_vm_at_exit(Deinit_rbs_extension);
426
+ ruby_vm_at_exit(Deinit_rbs_extension);
377
427
  }
@@ -1,11 +1,8 @@
1
1
  #include <stdbool.h>
2
- #include "compat.h"
3
2
 
4
- SUPPRESS_RUBY_HEADER_DIAGNOSTICS_BEGIN
5
3
  #include "ruby.h"
6
4
  #include "ruby/re.h"
7
5
  #include "ruby/encoding.h"
8
- SUPPRESS_RUBY_HEADER_DIAGNOSTICS_END
9
6
 
10
7
  #include "class_constants.h"
11
8
  #include "rbs.h"