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.
Files changed (354) hide show
  1. checksums.yaml +4 -4
  2. data/.clang-format +75 -0
  3. data/.clangd +2 -0
  4. data/.dockerignore +37 -0
  5. data/.gitattributes +1 -0
  6. data/.github/dependabot.yml +16 -14
  7. data/.github/workflows/bundle-update.yml +63 -0
  8. data/.github/workflows/c-check.yml +66 -0
  9. data/.github/workflows/changelog.yml +121 -0
  10. data/.github/workflows/comments.yml +6 -4
  11. data/.github/workflows/dependabot.yml +2 -2
  12. data/.github/workflows/jruby.yml +74 -0
  13. data/.github/workflows/release-gems.yml +234 -0
  14. data/.github/workflows/ruby.yml +86 -32
  15. data/.github/workflows/rust.yml +186 -0
  16. data/.github/workflows/truffleruby.yml +54 -0
  17. data/.github/workflows/typecheck.yml +6 -3
  18. data/.github/workflows/wasm.yml +55 -0
  19. data/.github/workflows/windows.yml +10 -4
  20. data/.gitignore +16 -0
  21. data/.rubocop.yml +1 -2
  22. data/CHANGELOG.md +502 -0
  23. data/Dockerfile.jruby +53 -0
  24. data/README.md +42 -5
  25. data/Rakefile +892 -111
  26. data/Steepfile +11 -0
  27. data/config.yml +665 -62
  28. data/core/array.rbs +541 -398
  29. data/core/basic_object.rbs +9 -8
  30. data/core/binding.rbs +0 -2
  31. data/core/builtin.rbs +9 -8
  32. data/core/class.rbs +11 -8
  33. data/core/comparable.rbs +55 -34
  34. data/core/complex.rbs +104 -78
  35. data/core/dir.rbs +61 -49
  36. data/core/encoding.rbs +12 -15
  37. data/core/enumerable.rbs +297 -196
  38. data/core/enumerator/arithmetic_sequence.rbs +70 -0
  39. data/core/enumerator/product.rbs +5 -5
  40. data/core/enumerator.rbs +91 -28
  41. data/core/errno.rbs +11 -2
  42. data/core/errors.rbs +58 -29
  43. data/core/exception.rbs +13 -13
  44. data/core/fiber.rbs +74 -54
  45. data/core/file.rbs +260 -1151
  46. data/core/file_constants.rbs +463 -0
  47. data/core/file_stat.rbs +534 -0
  48. data/core/file_test.rbs +3 -3
  49. data/core/float.rbs +257 -116
  50. data/core/gc.rbs +425 -281
  51. data/core/hash.rbs +1151 -829
  52. data/core/integer.rbs +156 -195
  53. data/core/io/buffer.rbs +53 -42
  54. data/core/io/wait.rbs +13 -35
  55. data/core/io.rbs +222 -155
  56. data/core/kernel.rbs +239 -163
  57. data/core/marshal.rbs +4 -4
  58. data/core/match_data.rbs +16 -14
  59. data/core/math.rbs +107 -66
  60. data/core/method.rbs +69 -33
  61. data/core/module.rbs +302 -150
  62. data/core/nil_class.rbs +7 -6
  63. data/core/numeric.rbs +77 -63
  64. data/core/object.rbs +9 -11
  65. data/core/object_space/weak_key_map.rbs +7 -7
  66. data/core/object_space.rbs +30 -23
  67. data/core/pathname.rbs +1312 -0
  68. data/core/proc.rbs +95 -58
  69. data/core/process.rbs +222 -202
  70. data/core/ractor.rbs +364 -518
  71. data/core/random.rbs +21 -3
  72. data/core/range.rbs +181 -79
  73. data/core/rational.rbs +60 -89
  74. data/core/rbs/ops.rbs +154 -0
  75. data/core/rbs/unnamed/argf.rbs +63 -56
  76. data/core/rbs/unnamed/env_class.rbs +19 -14
  77. data/core/rbs/unnamed/main_class.rbs +123 -0
  78. data/core/rbs/unnamed/random.rbs +11 -118
  79. data/core/regexp.rbs +258 -214
  80. data/core/ruby.rbs +53 -0
  81. data/core/ruby_vm.rbs +78 -34
  82. data/core/rubygems/config_file.rbs +5 -5
  83. data/core/rubygems/errors.rbs +6 -70
  84. data/core/rubygems/requirement.rbs +5 -15
  85. data/core/rubygems/rubygems.rbs +18 -81
  86. data/core/rubygems/specification.rbs +8 -0
  87. data/core/rubygems/version.rbs +2 -163
  88. data/core/set.rbs +493 -363
  89. data/core/signal.rbs +26 -16
  90. data/core/string.rbs +3234 -1285
  91. data/core/struct.rbs +43 -42
  92. data/core/symbol.rbs +41 -34
  93. data/core/thread.rbs +139 -83
  94. data/core/time.rbs +81 -50
  95. data/core/trace_point.rbs +41 -35
  96. data/core/true_class.rbs +2 -2
  97. data/core/unbound_method.rbs +24 -16
  98. data/core/warning.rbs +7 -7
  99. data/docs/CONTRIBUTING.md +3 -2
  100. data/docs/aliases.md +79 -0
  101. data/docs/collection.md +3 -3
  102. data/docs/config.md +171 -0
  103. data/docs/encoding.md +56 -0
  104. data/docs/gem.md +0 -1
  105. data/docs/inline.md +634 -0
  106. data/docs/rbs_by_example.md +20 -20
  107. data/docs/release.md +303 -0
  108. data/docs/rust.md +96 -0
  109. data/docs/sigs.md +3 -3
  110. data/docs/stdlib.md +8 -0
  111. data/docs/syntax.md +60 -18
  112. data/docs/type_fingerprint.md +21 -0
  113. data/docs/wasm_serialization.md +80 -0
  114. data/exe/rbs +1 -1
  115. data/ext/rbs_extension/ast_translation.c +1870 -0
  116. data/ext/rbs_extension/ast_translation.h +41 -0
  117. data/ext/rbs_extension/class_constants.c +189 -0
  118. data/{include/rbs/constants.h → ext/rbs_extension/class_constants.h} +24 -1
  119. data/ext/rbs_extension/compat.h +10 -0
  120. data/ext/rbs_extension/extconf.rb +26 -1
  121. data/ext/rbs_extension/legacy_location.c +299 -0
  122. data/ext/rbs_extension/legacy_location.h +82 -0
  123. data/ext/rbs_extension/main.c +639 -23
  124. data/ext/rbs_extension/rbs_extension.h +6 -21
  125. data/ext/rbs_extension/rbs_string_bridging.c +9 -0
  126. data/ext/rbs_extension/rbs_string_bridging.h +24 -0
  127. data/include/rbs/ast.h +1055 -0
  128. data/include/rbs/defines.h +104 -0
  129. data/include/rbs/lexer.h +208 -0
  130. data/include/rbs/location.h +40 -0
  131. data/include/rbs/parser.h +187 -0
  132. data/include/rbs/serialize.h +39 -0
  133. data/include/rbs/string.h +47 -0
  134. data/include/rbs/util/rbs_allocator.h +59 -0
  135. data/include/rbs/util/rbs_assert.h +20 -0
  136. data/include/rbs/util/rbs_buffer.h +83 -0
  137. data/include/rbs/util/rbs_constant_pool.h +6 -70
  138. data/include/rbs/util/rbs_encoding.h +282 -0
  139. data/include/rbs/util/rbs_unescape.h +24 -0
  140. data/include/rbs.h +9 -2
  141. data/lib/rbs/annotate/formatter.rb +3 -13
  142. data/lib/rbs/annotate/rdoc_annotator.rb +30 -32
  143. data/lib/rbs/annotate/rdoc_source.rb +1 -1
  144. data/lib/rbs/ast/annotation.rb +1 -1
  145. data/lib/rbs/ast/comment.rb +1 -1
  146. data/lib/rbs/ast/declarations.rb +11 -11
  147. data/lib/rbs/ast/members.rb +14 -14
  148. data/lib/rbs/ast/ruby/annotations.rb +451 -0
  149. data/lib/rbs/ast/ruby/comment_block.rb +247 -0
  150. data/lib/rbs/ast/ruby/declarations.rb +291 -0
  151. data/lib/rbs/ast/ruby/helpers/constant_helper.rb +28 -0
  152. data/lib/rbs/ast/ruby/helpers/location_helper.rb +15 -0
  153. data/lib/rbs/ast/ruby/members.rb +762 -0
  154. data/lib/rbs/ast/type_param.rb +25 -5
  155. data/lib/rbs/buffer.rb +142 -20
  156. data/lib/rbs/cli/diff.rb +16 -15
  157. data/lib/rbs/cli/validate.rb +63 -126
  158. data/lib/rbs/cli.rb +59 -29
  159. data/lib/rbs/collection/config/lockfile_generator.rb +28 -3
  160. data/lib/rbs/collection/sources/git.rb +7 -0
  161. data/lib/rbs/definition.rb +6 -1
  162. data/lib/rbs/definition_builder/ancestor_builder.rb +129 -70
  163. data/lib/rbs/definition_builder/method_builder.rb +74 -33
  164. data/lib/rbs/definition_builder.rb +181 -21
  165. data/lib/rbs/diff.rb +7 -1
  166. data/lib/rbs/environment/class_entry.rb +81 -0
  167. data/lib/rbs/environment/module_entry.rb +91 -0
  168. data/lib/rbs/environment.rb +410 -215
  169. data/lib/rbs/environment_loader.rb +2 -8
  170. data/lib/rbs/errors.rb +31 -21
  171. data/lib/rbs/inline_parser/comment_association.rb +117 -0
  172. data/lib/rbs/inline_parser.rb +568 -0
  173. data/lib/rbs/location_aux.rb +36 -4
  174. data/lib/rbs/locator.rb +5 -1
  175. data/lib/rbs/method_type.rb +5 -3
  176. data/lib/rbs/namespace.rb +47 -18
  177. data/lib/rbs/parser_aux.rb +37 -7
  178. data/lib/rbs/prototype/helpers.rb +24 -0
  179. data/lib/rbs/prototype/rb.rb +3 -28
  180. data/lib/rbs/prototype/rbi.rb +196 -45
  181. data/lib/rbs/prototype/runtime/value_object_generator.rb +0 -1
  182. data/lib/rbs/prototype/runtime.rb +13 -3
  183. data/lib/rbs/resolver/constant_resolver.rb +2 -2
  184. data/lib/rbs/resolver/type_name_resolver.rb +120 -44
  185. data/lib/rbs/rewriter.rb +70 -0
  186. data/lib/rbs/source.rb +99 -0
  187. data/lib/rbs/subtractor.rb +7 -4
  188. data/lib/rbs/test/type_check.rb +25 -3
  189. data/lib/rbs/type_name.rb +34 -21
  190. data/lib/rbs/types.rb +135 -81
  191. data/lib/rbs/unit_test/convertibles.rb +1 -0
  192. data/lib/rbs/unit_test/type_assertions.rb +47 -11
  193. data/lib/rbs/validator.rb +2 -2
  194. data/lib/rbs/version.rb +1 -1
  195. data/lib/rbs/wasm/deserializer.rb +213 -0
  196. data/lib/rbs/wasm/location.rb +61 -0
  197. data/lib/rbs/wasm/parser.rb +174 -0
  198. data/lib/rbs/wasm/runtime.rb +211 -0
  199. data/lib/rbs/wasm/serialization_schema.rb +111 -0
  200. data/lib/rbs.rb +25 -2
  201. data/lib/rbs_jars.rb +39 -0
  202. data/lib/rdoc/discover.rb +1 -1
  203. data/lib/rdoc_plugin/parser.rb +8 -3
  204. data/rbs.gemspec +34 -6
  205. data/schema/function.json +12 -1
  206. data/schema/typeParam.json +17 -1
  207. data/sig/ancestor_builder.rbs +1 -1
  208. data/sig/annotate/formatter.rbs +2 -2
  209. data/sig/annotate/rdoc_annotater.rbs +13 -10
  210. data/sig/ast/ruby/annotations.rbs +470 -0
  211. data/sig/ast/ruby/comment_block.rbs +127 -0
  212. data/sig/ast/ruby/declarations.rbs +158 -0
  213. data/sig/ast/ruby/helpers/constant_helper.rbs +11 -0
  214. data/sig/ast/ruby/helpers/location_helper.rbs +15 -0
  215. data/sig/ast/ruby/members.rbs +198 -0
  216. data/sig/buffer.rbs +81 -5
  217. data/sig/cli/diff.rbs +5 -11
  218. data/sig/cli/validate.rbs +12 -8
  219. data/sig/cli.rbs +18 -18
  220. data/sig/collection/config/lockfile_generator.rbs +2 -0
  221. data/sig/definition.rbs +6 -0
  222. data/sig/definition_builder.rbs +3 -1
  223. data/sig/environment/class_entry.rbs +56 -0
  224. data/sig/environment/module_entry.rbs +65 -0
  225. data/sig/environment.rbs +94 -87
  226. data/sig/errors.rbs +26 -20
  227. data/sig/inline_parser/comment_association.rbs +71 -0
  228. data/sig/inline_parser.rbs +126 -0
  229. data/sig/location.rbs +32 -7
  230. data/sig/locator.rbs +0 -2
  231. data/sig/manifest.yaml +0 -2
  232. data/sig/method_builder.rbs +9 -4
  233. data/sig/namespace.rbs +20 -5
  234. data/sig/parser.rbs +79 -15
  235. data/sig/prototype/helpers.rbs +2 -0
  236. data/sig/prototype/rbi.rbs +33 -4
  237. data/sig/resolver/type_name_resolver.rbs +36 -10
  238. data/sig/rewriter.rbs +45 -0
  239. data/sig/source.rbs +48 -0
  240. data/sig/type_param.rbs +13 -8
  241. data/sig/typename.rbs +15 -5
  242. data/sig/types.rbs +21 -9
  243. data/sig/unit_test/spy.rbs +0 -8
  244. data/sig/unit_test/type_assertions.rbs +17 -2
  245. data/sig/wasm/deserializer.rbs +66 -0
  246. data/sig/wasm/serialization_schema.rbs +13 -0
  247. data/src/ast.c +1644 -0
  248. data/src/lexer.c +3223 -0
  249. data/src/lexer.re +187 -0
  250. data/src/lexstate.c +222 -0
  251. data/src/location.c +31 -0
  252. data/src/parser.c +4318 -0
  253. data/src/serialize.c +965 -0
  254. data/src/string.c +41 -0
  255. data/src/util/rbs_allocator.c +171 -0
  256. data/src/util/rbs_assert.c +19 -0
  257. data/src/util/rbs_buffer.c +54 -0
  258. data/src/util/rbs_constant_pool.c +18 -92
  259. data/src/util/rbs_encoding.c +21454 -0
  260. data/src/util/rbs_unescape.c +167 -0
  261. data/stdlib/abbrev/0/array.rbs +1 -1
  262. data/stdlib/bigdecimal/0/big_decimal.rbs +116 -98
  263. data/stdlib/bigdecimal-math/0/big_math.rbs +169 -8
  264. data/stdlib/cgi/0/core.rbs +9 -393
  265. data/stdlib/cgi/0/manifest.yaml +1 -0
  266. data/stdlib/cgi-escape/0/escape.rbs +171 -0
  267. data/stdlib/coverage/0/coverage.rbs +7 -4
  268. data/stdlib/csv/0/csv.rbs +5 -5
  269. data/stdlib/date/0/date.rbs +92 -79
  270. data/stdlib/date/0/date_time.rbs +25 -24
  271. data/stdlib/delegate/0/delegator.rbs +11 -7
  272. data/stdlib/did_you_mean/0/did_you_mean.rbs +17 -16
  273. data/stdlib/digest/0/digest.rbs +117 -1
  274. data/stdlib/erb/0/erb.rbs +754 -353
  275. data/stdlib/etc/0/etc.rbs +73 -54
  276. data/stdlib/fileutils/0/fileutils.rbs +179 -160
  277. data/stdlib/forwardable/0/forwardable.rbs +13 -10
  278. data/stdlib/io-console/0/io-console.rbs +2 -2
  279. data/stdlib/ipaddr/0/ipaddr.rbs +0 -5
  280. data/stdlib/json/0/json.rbs +232 -185
  281. data/stdlib/monitor/0/monitor.rbs +9 -9
  282. data/stdlib/net-http/0/net-http.rbs +162 -134
  283. data/stdlib/objspace/0/objspace.rbs +17 -34
  284. data/stdlib/open-uri/0/open-uri.rbs +48 -8
  285. data/stdlib/open3/0/open3.rbs +469 -10
  286. data/stdlib/openssl/0/openssl.rbs +521 -397
  287. data/stdlib/optparse/0/optparse.rbs +26 -17
  288. data/stdlib/pathname/0/pathname.rbs +11 -1381
  289. data/stdlib/pp/0/pp.rbs +9 -8
  290. data/stdlib/prettyprint/0/prettyprint.rbs +7 -7
  291. data/stdlib/pstore/0/pstore.rbs +35 -30
  292. data/stdlib/psych/0/psych.rbs +65 -12
  293. data/stdlib/psych/0/store.rbs +2 -4
  294. data/stdlib/pty/0/pty.rbs +9 -6
  295. data/stdlib/random-formatter/0/random-formatter.rbs +277 -0
  296. data/stdlib/rdoc/0/code_object.rbs +4 -3
  297. data/stdlib/rdoc/0/comment.rbs +2 -0
  298. data/stdlib/rdoc/0/options.rbs +76 -0
  299. data/stdlib/rdoc/0/parser.rbs +1 -1
  300. data/stdlib/rdoc/0/rdoc.rbs +7 -5
  301. data/stdlib/rdoc/0/store.rbs +2 -2
  302. data/stdlib/resolv/0/resolv.rbs +26 -69
  303. data/stdlib/ripper/0/ripper.rbs +25 -19
  304. data/stdlib/securerandom/0/manifest.yaml +2 -0
  305. data/stdlib/securerandom/0/securerandom.rbs +7 -20
  306. data/stdlib/shellwords/0/shellwords.rbs +3 -3
  307. data/stdlib/singleton/0/singleton.rbs +6 -0
  308. data/stdlib/socket/0/addrinfo.rbs +9 -9
  309. data/stdlib/socket/0/basic_socket.rbs +3 -3
  310. data/stdlib/socket/0/ip_socket.rbs +10 -8
  311. data/stdlib/socket/0/socket.rbs +23 -10
  312. data/stdlib/socket/0/tcp_server.rbs +1 -1
  313. data/stdlib/socket/0/tcp_socket.rbs +11 -3
  314. data/stdlib/socket/0/udp_socket.rbs +1 -1
  315. data/stdlib/socket/0/unix_server.rbs +1 -1
  316. data/stdlib/stringio/0/stringio.rbs +1211 -96
  317. data/stdlib/strscan/0/string_scanner.rbs +101 -80
  318. data/stdlib/tempfile/0/manifest.yaml +3 -0
  319. data/stdlib/tempfile/0/tempfile.rbs +25 -21
  320. data/stdlib/time/0/time.rbs +8 -6
  321. data/stdlib/timeout/0/timeout.rbs +58 -7
  322. data/stdlib/tsort/0/cyclic.rbs +4 -1
  323. data/stdlib/tsort/0/interfaces.rbs +8 -8
  324. data/stdlib/tsort/0/tsort.rbs +16 -15
  325. data/stdlib/uri/0/common.rbs +42 -20
  326. data/stdlib/uri/0/file.rbs +3 -3
  327. data/stdlib/uri/0/generic.rbs +21 -18
  328. data/stdlib/uri/0/http.rbs +2 -2
  329. data/stdlib/uri/0/ldap.rbs +2 -2
  330. data/stdlib/uri/0/mailto.rbs +3 -3
  331. data/stdlib/uri/0/rfc2396_parser.rbs +12 -12
  332. data/stdlib/zlib/0/deflate.rbs +4 -3
  333. data/stdlib/zlib/0/gzip_file.rbs +1 -1
  334. data/stdlib/zlib/0/gzip_reader.rbs +8 -8
  335. data/stdlib/zlib/0/gzip_writer.rbs +16 -13
  336. data/stdlib/zlib/0/inflate.rbs +1 -1
  337. data/stdlib/zlib/0/need_dict.rbs +1 -1
  338. data/wasm/README.md +109 -0
  339. data/wasm/rbs_wasm.c +479 -0
  340. metadata +133 -19
  341. data/ext/rbs_extension/lexer.c +0 -2728
  342. data/ext/rbs_extension/lexer.h +0 -179
  343. data/ext/rbs_extension/lexer.re +0 -147
  344. data/ext/rbs_extension/lexstate.c +0 -175
  345. data/ext/rbs_extension/location.c +0 -325
  346. data/ext/rbs_extension/location.h +0 -85
  347. data/ext/rbs_extension/parser.c +0 -2982
  348. data/ext/rbs_extension/parser.h +0 -18
  349. data/ext/rbs_extension/parserstate.c +0 -411
  350. data/ext/rbs_extension/parserstate.h +0 -163
  351. data/ext/rbs_extension/unescape.c +0 -32
  352. data/include/rbs/ruby_objs.h +0 -72
  353. data/src/constants.c +0 -153
  354. data/src/ruby_objs.c +0 -799
@@ -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
- static
7
- void Deinit_rbs_extension(ruby_vm_t *_) {
8
- rbs_constant_pool_free(RBS_GLOBAL_CONSTANT_POOL);
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, &params);
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
- void
12
- Init_rbs_extension(void)
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
- rb_ext_ractor_safe(true);
642
+ rb_ext_ractor_safe(true);
16
643
  #endif
17
- rbs__init_constants();
18
- rbs__init_location();
19
- rbs__init_parser();
20
-
21
- // Calculated based on the number of unique strings used with the `INTERN` macro in `parser.c`.
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
- * 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;