rbs 3.9.5 → 3.10.0.pre.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 (171) hide show
  1. checksums.yaml +4 -4
  2. data/.clang-format +74 -0
  3. data/.clangd +2 -0
  4. data/.github/workflows/c-check.yml +54 -0
  5. data/.github/workflows/comments.yml +3 -3
  6. data/.github/workflows/ruby.yml +34 -19
  7. data/.github/workflows/typecheck.yml +1 -1
  8. data/.github/workflows/windows.yml +1 -1
  9. data/.gitignore +4 -0
  10. data/README.md +38 -1
  11. data/Rakefile +152 -23
  12. data/config.yml +190 -62
  13. data/core/array.rbs +44 -43
  14. data/core/dir.rbs +2 -2
  15. data/core/encoding.rbs +3 -2
  16. data/core/enumerable.rbs +89 -2
  17. data/core/errno.rbs +8 -0
  18. data/core/errors.rbs +28 -1
  19. data/core/exception.rbs +2 -2
  20. data/core/fiber.rbs +3 -3
  21. data/core/file.rbs +26 -11
  22. data/core/float.rbs +1 -1
  23. data/core/gc.rbs +422 -281
  24. data/core/hash.rbs +1024 -727
  25. data/core/io/wait.rbs +11 -33
  26. data/core/io.rbs +6 -4
  27. data/core/kernel.rbs +49 -43
  28. data/core/marshal.rbs +1 -1
  29. data/core/match_data.rbs +1 -1
  30. data/core/math.rbs +42 -3
  31. data/core/method.rbs +14 -6
  32. data/core/module.rbs +71 -11
  33. data/core/nil_class.rbs +3 -3
  34. data/core/numeric.rbs +8 -8
  35. data/core/object.rbs +3 -3
  36. data/core/object_space.rbs +13 -0
  37. data/{stdlib/pathname/0 → core}/pathname.rbs +253 -352
  38. data/core/proc.rbs +15 -8
  39. data/core/process.rbs +2 -2
  40. data/core/ractor.rbs +278 -437
  41. data/core/range.rbs +6 -7
  42. data/core/rbs/unnamed/argf.rbs +1 -1
  43. data/core/rbs/unnamed/env_class.rbs +1 -1
  44. data/core/rbs/unnamed/random.rbs +4 -2
  45. data/core/regexp.rbs +22 -17
  46. data/core/ruby_vm.rbs +6 -4
  47. data/core/rubygems/errors.rbs +3 -70
  48. data/core/rubygems/rubygems.rbs +11 -79
  49. data/core/set.rbs +439 -332
  50. data/core/string.rbs +2897 -1117
  51. data/core/struct.rbs +1 -1
  52. data/core/symbol.rbs +4 -4
  53. data/core/thread.rbs +83 -20
  54. data/core/time.rbs +35 -9
  55. data/core/unbound_method.rbs +14 -6
  56. data/docs/aliases.md +79 -0
  57. data/docs/collection.md +2 -2
  58. data/docs/gem.md +0 -1
  59. data/docs/sigs.md +3 -3
  60. data/ext/rbs_extension/ast_translation.c +1016 -0
  61. data/ext/rbs_extension/ast_translation.h +37 -0
  62. data/ext/rbs_extension/class_constants.c +157 -0
  63. data/{include/rbs/constants.h → ext/rbs_extension/class_constants.h} +7 -1
  64. data/ext/rbs_extension/compat.h +10 -0
  65. data/ext/rbs_extension/extconf.rb +25 -1
  66. data/ext/rbs_extension/legacy_location.c +317 -0
  67. data/ext/rbs_extension/legacy_location.h +45 -0
  68. data/ext/rbs_extension/main.c +365 -14
  69. data/ext/rbs_extension/rbs_extension.h +6 -21
  70. data/ext/rbs_extension/rbs_string_bridging.c +9 -0
  71. data/ext/rbs_extension/rbs_string_bridging.h +24 -0
  72. data/include/rbs/ast.h +687 -0
  73. data/include/rbs/defines.h +86 -0
  74. data/include/rbs/lexer.h +199 -0
  75. data/include/rbs/location.h +59 -0
  76. data/include/rbs/parser.h +135 -0
  77. data/include/rbs/string.h +49 -0
  78. data/include/rbs/util/rbs_allocator.h +59 -0
  79. data/include/rbs/util/rbs_assert.h +20 -0
  80. data/include/rbs/util/rbs_buffer.h +83 -0
  81. data/include/rbs/util/rbs_constant_pool.h +6 -67
  82. data/include/rbs/util/rbs_encoding.h +282 -0
  83. data/include/rbs/util/rbs_unescape.h +23 -0
  84. data/include/rbs.h +1 -2
  85. data/lib/rbs/annotate/formatter.rb +3 -13
  86. data/lib/rbs/annotate/rdoc_annotator.rb +3 -1
  87. data/lib/rbs/annotate/rdoc_source.rb +1 -1
  88. data/lib/rbs/cli/validate.rb +2 -2
  89. data/lib/rbs/cli.rb +1 -1
  90. data/lib/rbs/collection/config/lockfile_generator.rb +1 -0
  91. data/lib/rbs/definition_builder/ancestor_builder.rb +5 -5
  92. data/lib/rbs/environment.rb +64 -59
  93. data/lib/rbs/environment_loader.rb +1 -1
  94. data/lib/rbs/errors.rb +1 -1
  95. data/lib/rbs/parser_aux.rb +5 -0
  96. data/lib/rbs/resolver/constant_resolver.rb +2 -2
  97. data/lib/rbs/resolver/type_name_resolver.rb +124 -38
  98. data/lib/rbs/test/type_check.rb +13 -0
  99. data/lib/rbs/types.rb +3 -1
  100. data/lib/rbs/version.rb +1 -1
  101. data/lib/rbs.rb +1 -1
  102. data/lib/rdoc/discover.rb +1 -1
  103. data/lib/rdoc_plugin/parser.rb +3 -3
  104. data/sig/annotate/formatter.rbs +2 -2
  105. data/sig/annotate/rdoc_annotater.rbs +1 -1
  106. data/sig/environment.rbs +57 -6
  107. data/sig/manifest.yaml +0 -1
  108. data/sig/parser.rbs +20 -0
  109. data/sig/resolver/type_name_resolver.rbs +38 -7
  110. data/sig/types.rbs +4 -1
  111. data/src/ast.c +1256 -0
  112. data/src/lexer.c +2956 -0
  113. data/src/lexer.re +147 -0
  114. data/src/lexstate.c +205 -0
  115. data/src/location.c +71 -0
  116. data/src/parser.c +3495 -0
  117. data/src/string.c +90 -0
  118. data/src/util/rbs_allocator.c +152 -0
  119. data/src/util/rbs_assert.c +21 -0
  120. data/src/util/rbs_buffer.c +54 -0
  121. data/src/util/rbs_constant_pool.c +16 -86
  122. data/src/util/rbs_encoding.c +21308 -0
  123. data/src/util/rbs_unescape.c +131 -0
  124. data/stdlib/cgi/0/core.rbs +2 -396
  125. data/stdlib/cgi/0/manifest.yaml +1 -0
  126. data/stdlib/cgi-escape/0/escape.rbs +153 -0
  127. data/stdlib/coverage/0/coverage.rbs +3 -1
  128. data/stdlib/delegate/0/delegator.rbs +10 -7
  129. data/stdlib/erb/0/erb.rbs +737 -347
  130. data/stdlib/fileutils/0/fileutils.rbs +18 -13
  131. data/stdlib/forwardable/0/forwardable.rbs +3 -0
  132. data/stdlib/json/0/json.rbs +67 -48
  133. data/stdlib/net-http/0/net-http.rbs +3 -0
  134. data/stdlib/objspace/0/objspace.rbs +8 -3
  135. data/stdlib/open-uri/0/open-uri.rbs +40 -0
  136. data/stdlib/openssl/0/openssl.rbs +182 -149
  137. data/stdlib/optparse/0/optparse.rbs +3 -3
  138. data/stdlib/rdoc/0/code_object.rbs +2 -2
  139. data/stdlib/rdoc/0/comment.rbs +2 -0
  140. data/stdlib/rdoc/0/options.rbs +76 -0
  141. data/stdlib/rdoc/0/rdoc.rbs +7 -5
  142. data/stdlib/rdoc/0/store.rbs +1 -1
  143. data/stdlib/resolv/0/resolv.rbs +25 -68
  144. data/stdlib/ripper/0/ripper.rbs +5 -2
  145. data/stdlib/singleton/0/singleton.rbs +3 -0
  146. data/stdlib/socket/0/socket.rbs +13 -1
  147. data/stdlib/socket/0/tcp_socket.rbs +10 -2
  148. data/stdlib/stringio/0/stringio.rbs +412 -80
  149. data/stdlib/strscan/0/string_scanner.rbs +31 -31
  150. data/stdlib/tempfile/0/tempfile.rbs +1 -1
  151. data/stdlib/tsort/0/cyclic.rbs +3 -0
  152. data/stdlib/uri/0/common.rbs +11 -2
  153. data/stdlib/uri/0/file.rbs +1 -1
  154. data/stdlib/uri/0/generic.rbs +16 -15
  155. data/stdlib/uri/0/rfc2396_parser.rbs +6 -7
  156. data/stdlib/zlib/0/zstream.rbs +1 -0
  157. metadata +41 -18
  158. data/ext/rbs_extension/lexer.c +0 -2728
  159. data/ext/rbs_extension/lexer.h +0 -179
  160. data/ext/rbs_extension/lexer.re +0 -147
  161. data/ext/rbs_extension/lexstate.c +0 -175
  162. data/ext/rbs_extension/location.c +0 -325
  163. data/ext/rbs_extension/location.h +0 -85
  164. data/ext/rbs_extension/parser.c +0 -2982
  165. data/ext/rbs_extension/parser.h +0 -18
  166. data/ext/rbs_extension/parserstate.c +0 -411
  167. data/ext/rbs_extension/parserstate.h +0 -163
  168. data/ext/rbs_extension/unescape.c +0 -32
  169. data/include/rbs/ruby_objs.h +0 -72
  170. data/src/constants.c +0 -153
  171. data/src/ruby_objs.c +0 -799
@@ -1,24 +1,375 @@
1
1
  #include "rbs_extension.h"
2
+ #include "rbs/util/rbs_assert.h"
3
+ #include "rbs/util/rbs_allocator.h"
2
4
  #include "rbs/util/rbs_constant_pool.h"
5
+ #include "ast_translation.h"
6
+ #include "legacy_location.h"
7
+ #include "rbs_string_bridging.h"
3
8
 
4
9
  #include "ruby/vm.h"
5
10
 
6
- static
7
- void Deinit_rbs_extension(ruby_vm_t *_) {
8
- rbs_constant_pool_free(RBS_GLOBAL_CONSTANT_POOL);
11
+ /**
12
+ * Raises `RBS::ParsingError` or `RuntimeError` on `tok` with message constructed with given `fmt`.
13
+ *
14
+ * ```
15
+ * foo.rbs:11:21...11:25: Syntax error: {message}, token=`{tok source}` ({tok type})
16
+ * ```
17
+ * */
18
+ static NORETURN(void) raise_error(rbs_error_t *error, VALUE buffer) {
19
+ RBS_ASSERT(error != NULL, "raise_error() called with NULL error");
20
+
21
+ if (!error->syntax_error) {
22
+ rb_raise(rb_eRuntimeError, "Unexpected error");
23
+ }
24
+
25
+ VALUE location = rbs_new_location(buffer, error->token.range);
26
+ VALUE type = rb_str_new_cstr(rbs_token_type_str(error->token.type));
27
+
28
+ VALUE rb_error = rb_funcall(
29
+ RBS_ParsingError,
30
+ rb_intern("new"),
31
+ 3,
32
+ location,
33
+ rb_str_new_cstr(error->message),
34
+ type
35
+ );
36
+
37
+ rb_exc_raise(rb_error);
38
+ }
39
+
40
+ void raise_error_if_any(rbs_parser_t *parser, VALUE buffer) {
41
+ if (parser->error != NULL) {
42
+ raise_error(parser->error, buffer);
43
+ }
44
+ }
45
+
46
+ /**
47
+ * Inserts the given array of type variables names into the parser's type variable table.
48
+ * @param parser
49
+ * @param variables A Ruby Array of Symbols, or nil.
50
+ */
51
+ static void declare_type_variables(rbs_parser_t *parser, VALUE variables, VALUE buffer) {
52
+ if (NIL_P(variables)) return; // Nothing to do.
53
+
54
+ if (!RB_TYPE_P(variables, T_ARRAY)) {
55
+ rbs_parser_free(parser);
56
+ rb_raise(rb_eTypeError, "wrong argument type %" PRIsVALUE " (must be an Array of Symbols or nil)", rb_obj_class(variables));
57
+ }
58
+
59
+ rbs_parser_push_typevar_table(parser, true);
60
+
61
+ for (long i = 0; i < rb_array_len(variables); i++) {
62
+ VALUE symbol = rb_ary_entry(variables, i);
63
+
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
+ }
68
+
69
+ VALUE name_str = rb_sym2str(symbol);
70
+
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
+ );
76
+
77
+ if (!rbs_parser_insert_typevar(parser, id)) {
78
+ raise_error(parser->error, buffer);
79
+ }
80
+ }
81
+ }
82
+
83
+ struct parse_type_arg {
84
+ VALUE buffer;
85
+ rb_encoding *encoding;
86
+ rbs_parser_t *parser;
87
+ VALUE require_eof;
88
+ };
89
+
90
+ static VALUE ensure_free_parser(VALUE parser) {
91
+ rbs_parser_free((rbs_parser_t *) parser);
92
+ return Qnil;
93
+ }
94
+
95
+ 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;
98
+
99
+ if (parser->next_token.type == pEOF) {
100
+ return Qnil;
101
+ }
102
+
103
+ rbs_node_t *type;
104
+ rbs_parse_type(parser, &type);
105
+
106
+ raise_error_if_any(parser, arg->buffer);
107
+
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
+ }
114
+ }
115
+
116
+ rbs_translation_context_t ctx = rbs_translation_context_create(
117
+ &parser->constant_pool,
118
+ arg->buffer,
119
+ arg->encoding
120
+ );
121
+
122
+ return rbs_struct_to_ruby_value(ctx, type);
123
+ }
124
+
125
+ 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
+ );
139
+ }
140
+
141
+ 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
+ );
158
+ }
159
+
160
+ 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);
164
+
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
+ };
173
+
174
+ VALUE result = rb_ensure(parse_type_try, (VALUE) &arg, ensure_free_parser, (VALUE) parser);
175
+
176
+ RB_GC_GUARD(string);
177
+
178
+ return result;
179
+ }
180
+
181
+ 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;
184
+
185
+ if (parser->next_token.type == pEOF) {
186
+ return Qnil;
187
+ }
188
+
189
+ rbs_method_type_t *method_type = NULL;
190
+ rbs_parse_method_type(parser, &method_type);
191
+
192
+ raise_error_if_any(parser, arg->buffer);
193
+
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
+ rbs_translation_context_t ctx = rbs_translation_context_create(
203
+ &parser->constant_pool,
204
+ arg->buffer,
205
+ arg->encoding
206
+ );
207
+
208
+ return rbs_struct_to_ruby_value(ctx, (rbs_node_t *) method_type);
209
+ }
210
+
211
+ static VALUE rbsparser_parse_method_type(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos, VALUE variables, VALUE require_eof) {
212
+ VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
213
+ StringValue(string);
214
+ rb_encoding *encoding = rb_enc_get(string);
215
+
216
+ rbs_parser_t *parser = alloc_parser_from_buffer(buffer, FIX2INT(start_pos), FIX2INT(end_pos));
217
+ declare_type_variables(parser, variables, buffer);
218
+ struct parse_type_arg arg = {
219
+ .buffer = buffer,
220
+ .encoding = encoding,
221
+ .parser = parser,
222
+ .require_eof = require_eof
223
+ };
224
+
225
+ VALUE result = rb_ensure(parse_method_type_try, (VALUE) &arg, ensure_free_parser, (VALUE) parser);
226
+
227
+ RB_GC_GUARD(string);
228
+
229
+ return result;
230
+ }
231
+
232
+ static VALUE parse_signature_try(VALUE a) {
233
+ struct parse_type_arg *arg = (struct parse_type_arg *) a;
234
+ rbs_parser_t *parser = arg->parser;
235
+
236
+ rbs_signature_t *signature = NULL;
237
+ rbs_parse_signature(parser, &signature);
238
+
239
+ raise_error_if_any(parser, arg->buffer);
240
+
241
+ rbs_translation_context_t ctx = rbs_translation_context_create(
242
+ &parser->constant_pool,
243
+ arg->buffer,
244
+ arg->encoding
245
+ );
246
+
247
+ return rbs_struct_to_ruby_value(ctx, (rbs_node_t *) signature);
248
+ }
249
+
250
+ static VALUE rbsparser_parse_signature(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos) {
251
+ VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
252
+ StringValue(string);
253
+ rb_encoding *encoding = rb_enc_get(string);
254
+
255
+ rbs_parser_t *parser = alloc_parser_from_buffer(buffer, FIX2INT(start_pos), FIX2INT(end_pos));
256
+ struct parse_type_arg arg = {
257
+ .buffer = buffer,
258
+ .encoding = encoding,
259
+ .parser = parser,
260
+ .require_eof = false
261
+ };
262
+
263
+ VALUE result = rb_ensure(parse_signature_try, (VALUE) &arg, ensure_free_parser, (VALUE) parser);
264
+
265
+ RB_GC_GUARD(string);
266
+
267
+ return result;
268
+ }
269
+
270
+ struct parse_type_params_arg {
271
+ VALUE buffer;
272
+ rb_encoding *encoding;
273
+ rbs_parser_t *parser;
274
+ VALUE module_type_params;
275
+ };
276
+
277
+ static VALUE parse_type_params_try(VALUE a) {
278
+ struct parse_type_params_arg *arg = (struct parse_type_params_arg *) a;
279
+ rbs_parser_t *parser = arg->parser;
280
+
281
+ if (parser->next_token.type == pEOF) {
282
+ return Qnil;
283
+ }
284
+
285
+ rbs_node_list_t *params = NULL;
286
+ rbs_parse_type_params(parser, arg->module_type_params, &params);
287
+
288
+ raise_error_if_any(parser, arg->buffer);
289
+
290
+ rbs_translation_context_t ctx = rbs_translation_context_create(
291
+ &parser->constant_pool,
292
+ arg->buffer,
293
+ arg->encoding
294
+ );
295
+
296
+ return rbs_node_list_to_ruby_array(ctx, params);
297
+ }
298
+
299
+ static VALUE rbsparser_parse_type_params(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos, VALUE module_type_params) {
300
+ VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
301
+ StringValue(string);
302
+ rb_encoding *encoding = rb_enc_get(string);
303
+
304
+ rbs_parser_t *parser = alloc_parser_from_buffer(buffer, FIX2INT(start_pos), FIX2INT(end_pos));
305
+ struct parse_type_params_arg arg = {
306
+ .buffer = buffer,
307
+ .encoding = encoding,
308
+ .parser = parser,
309
+ .module_type_params = module_type_params
310
+ };
311
+
312
+ VALUE result = rb_ensure(parse_type_params_try, (VALUE) &arg, ensure_free_parser, (VALUE) parser);
313
+
314
+ RB_GC_GUARD(string);
315
+
316
+ return result;
317
+ }
318
+
319
+ static VALUE rbsparser_lex(VALUE self, VALUE buffer, VALUE end_pos) {
320
+ VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
321
+ StringValue(string);
322
+ rb_encoding *encoding = rb_enc_get(string);
323
+
324
+ rbs_allocator_t *allocator = rbs_allocator_init();
325
+ rbs_lexer_t *lexer = alloc_lexer_from_buffer(allocator, string, encoding, 0, FIX2INT(end_pos));
326
+
327
+ VALUE results = rb_ary_new();
328
+ rbs_token_t token = NullToken;
329
+ while (token.type != pEOF) {
330
+ token = rbs_lexer_next_token(lexer);
331
+ VALUE type = ID2SYM(rb_intern(rbs_token_type_str(token.type)));
332
+ VALUE location = rbs_new_location(buffer, token.range);
333
+ VALUE pair = rb_ary_new3(2, type, location);
334
+ rb_ary_push(results, pair);
335
+ }
336
+
337
+ rbs_allocator_free(allocator);
338
+ RB_GC_GUARD(string);
339
+
340
+ return results;
341
+ }
342
+
343
+ void rbs__init_parser(void) {
344
+ RBS_Parser = rb_define_class_under(RBS, "Parser", rb_cObject);
345
+ rb_gc_register_mark_object(RBS_Parser);
346
+
347
+ EMPTY_ARRAY = rb_obj_freeze(rb_ary_new());
348
+ rb_gc_register_mark_object(EMPTY_ARRAY);
349
+
350
+ EMPTY_HASH = rb_obj_freeze(rb_hash_new());
351
+ rb_gc_register_mark_object(EMPTY_HASH);
352
+
353
+ rb_define_singleton_method(RBS_Parser, "_parse_type", rbsparser_parse_type, 5);
354
+ rb_define_singleton_method(RBS_Parser, "_parse_method_type", rbsparser_parse_method_type, 5);
355
+ rb_define_singleton_method(RBS_Parser, "_parse_signature", rbsparser_parse_signature, 3);
356
+ rb_define_singleton_method(RBS_Parser, "_parse_type_params", rbsparser_parse_type_params, 4);
357
+ rb_define_singleton_method(RBS_Parser, "_lex", rbsparser_lex, 2);
358
+ }
359
+
360
+ static void Deinit_rbs_extension(ruby_vm_t *_) {
361
+ rbs_constant_pool_free(RBS_GLOBAL_CONSTANT_POOL);
9
362
  }
10
363
 
11
- void
12
- Init_rbs_extension(void)
13
- {
364
+ void Init_rbs_extension(void) {
14
365
  #ifdef HAVE_RB_EXT_RACTOR_SAFE
15
- rb_ext_ractor_safe(true);
366
+ rb_ext_ractor_safe(true);
16
367
  #endif
17
- rbs__init_constants();
18
- rbs__init_location();
19
- rbs__init_parser();
368
+ rbs__init_constants();
369
+ rbs__init_location();
370
+ rbs__init_parser();
20
371
 
21
- /* Calculated based on the number of unique strings used with the `INTERN` macro in `parser.c`.
372
+ /* Calculated based on the number of unique strings used with the `INTERN` macro in `parser.c`.
22
373
  *
23
374
  * ```bash
24
375
  * grep -o 'INTERN("\([^"]*\)")' ext/rbs_extension/parser.c \
@@ -27,8 +378,8 @@ Init_rbs_extension(void)
27
378
  * | wc -l
28
379
  * ```
29
380
  */
30
- const size_t num_uniquely_interned_strings = 26;
31
- rbs_constant_pool_init(RBS_GLOBAL_CONSTANT_POOL, num_uniquely_interned_strings);
381
+ const size_t num_uniquely_interned_strings = 26;
382
+ rbs_constant_pool_init(RBS_GLOBAL_CONSTANT_POOL, num_uniquely_interned_strings);
32
383
 
33
- ruby_vm_at_exit(Deinit_rbs_extension);
384
+ ruby_vm_at_exit(Deinit_rbs_extension);
34
385
  }
@@ -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
- * Receives `parserstate` and `range`, which represents a string token or symbol token, and returns a string VALUE.
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 rbs_unquote_string(parserstate *state, range rg, int offset_bytes);
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;
@@ -0,0 +1,9 @@
1
+ #include "rbs_string_bridging.h"
2
+
3
+ rbs_string_t rbs_string_from_ruby_string(VALUE ruby_string) {
4
+ return rbs_string_new(StringValueCStr(ruby_string), RSTRING_END(ruby_string));
5
+ }
6
+
7
+ VALUE rbs_string_to_ruby_string(rbs_string_t *self, rb_encoding *encoding) {
8
+ return rb_enc_str_new(self->start, rbs_string_len(*self), encoding);
9
+ }
@@ -0,0 +1,24 @@
1
+ #ifndef RBS__RBS_STRING_BRIDGING_H
2
+ #define RBS__RBS_STRING_BRIDGING_H
3
+
4
+ #include "compat.h"
5
+
6
+ SUPPRESS_RUBY_HEADER_DIAGNOSTICS_BEGIN
7
+ #include "ruby.h"
8
+ #include "ruby/encoding.h"
9
+ SUPPRESS_RUBY_HEADER_DIAGNOSTICS_END
10
+
11
+ #include "rbs/string.h"
12
+
13
+ /**
14
+ * @returns A new shared rbs_string_t from the given Ruby string, which points into the given Ruby String's memory,
15
+ * and does not need to be `free()`ed. However, the Ruby String needs to be kept alive for the duration of the rbs_string_t.
16
+ */
17
+ rbs_string_t rbs_string_from_ruby_string(VALUE ruby_string);
18
+
19
+ /**
20
+ * Returns a new Ruby string from the given rbs_string_t.
21
+ */
22
+ VALUE rbs_string_to_ruby_string(rbs_string_t *self, rb_encoding *encoding);
23
+
24
+ #endif