rbs 3.9.5 → 4.0.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 (330) hide show
  1. checksums.yaml +4 -4
  2. data/.clang-format +74 -0
  3. data/.clangd +2 -0
  4. data/.github/dependabot.yml +14 -14
  5. data/.github/workflows/bundle-update.yml +60 -0
  6. data/.github/workflows/c-check.yml +58 -0
  7. data/.github/workflows/comments.yml +5 -3
  8. data/.github/workflows/dependabot.yml +2 -2
  9. data/.github/workflows/ruby.yml +40 -32
  10. data/.github/workflows/rust.yml +95 -0
  11. data/.github/workflows/typecheck.yml +2 -2
  12. data/.github/workflows/windows.yml +3 -3
  13. data/.gitignore +4 -0
  14. data/.rubocop.yml +2 -2
  15. data/.vscode/extensions.json +5 -0
  16. data/.vscode/settings.json +19 -0
  17. data/CHANGELOG.md +284 -0
  18. data/README.md +38 -1
  19. data/Rakefile +144 -29
  20. data/Steepfile +2 -0
  21. data/config.yml +634 -62
  22. data/core/array.rbs +307 -227
  23. data/core/basic_object.rbs +9 -8
  24. data/core/binding.rbs +0 -2
  25. data/core/builtin.rbs +2 -2
  26. data/core/class.rbs +6 -5
  27. data/core/comparable.rbs +55 -34
  28. data/core/complex.rbs +104 -78
  29. data/core/dir.rbs +61 -49
  30. data/core/encoding.rbs +12 -15
  31. data/core/enumerable.rbs +188 -87
  32. data/core/enumerator/arithmetic_sequence.rbs +70 -0
  33. data/core/enumerator.rbs +65 -2
  34. data/core/errno.rbs +11 -2
  35. data/core/errors.rbs +58 -29
  36. data/core/exception.rbs +13 -13
  37. data/core/fiber.rbs +74 -54
  38. data/core/file.rbs +280 -177
  39. data/core/file_test.rbs +3 -3
  40. data/core/float.rbs +257 -92
  41. data/core/gc.rbs +425 -281
  42. data/core/hash.rbs +1045 -739
  43. data/core/integer.rbs +135 -137
  44. data/core/io/buffer.rbs +53 -42
  45. data/core/io/wait.rbs +13 -35
  46. data/core/io.rbs +196 -148
  47. data/core/kernel.rbs +216 -155
  48. data/core/marshal.rbs +4 -4
  49. data/core/match_data.rbs +15 -13
  50. data/core/math.rbs +107 -66
  51. data/core/method.rbs +69 -33
  52. data/core/module.rbs +244 -106
  53. data/core/nil_class.rbs +7 -6
  54. data/core/numeric.rbs +74 -63
  55. data/core/object.rbs +9 -11
  56. data/core/object_space.rbs +30 -23
  57. data/core/pathname.rbs +1322 -0
  58. data/core/proc.rbs +95 -58
  59. data/core/process.rbs +222 -202
  60. data/core/ractor.rbs +371 -515
  61. data/core/random.rbs +21 -3
  62. data/core/range.rbs +159 -57
  63. data/core/rational.rbs +60 -89
  64. data/core/rbs/unnamed/argf.rbs +60 -53
  65. data/core/rbs/unnamed/env_class.rbs +19 -14
  66. data/core/rbs/unnamed/main_class.rbs +123 -0
  67. data/core/rbs/unnamed/random.rbs +11 -118
  68. data/core/regexp.rbs +258 -214
  69. data/core/ruby.rbs +53 -0
  70. data/core/ruby_vm.rbs +38 -34
  71. data/core/rubygems/config_file.rbs +5 -5
  72. data/core/rubygems/errors.rbs +4 -71
  73. data/core/rubygems/requirement.rbs +5 -5
  74. data/core/rubygems/rubygems.rbs +16 -82
  75. data/core/rubygems/version.rbs +2 -3
  76. data/core/set.rbs +490 -360
  77. data/core/signal.rbs +26 -16
  78. data/core/string.rbs +3303 -1365
  79. data/core/struct.rbs +27 -26
  80. data/core/symbol.rbs +41 -34
  81. data/core/thread.rbs +135 -74
  82. data/core/time.rbs +81 -50
  83. data/core/trace_point.rbs +41 -35
  84. data/core/true_class.rbs +2 -2
  85. data/core/unbound_method.rbs +24 -16
  86. data/core/warning.rbs +7 -7
  87. data/docs/aliases.md +79 -0
  88. data/docs/collection.md +3 -3
  89. data/docs/config.md +171 -0
  90. data/docs/encoding.md +56 -0
  91. data/docs/gem.md +0 -1
  92. data/docs/inline.md +576 -0
  93. data/docs/sigs.md +3 -3
  94. data/docs/syntax.md +46 -16
  95. data/docs/type_fingerprint.md +21 -0
  96. data/exe/rbs +1 -1
  97. data/ext/rbs_extension/ast_translation.c +1513 -0
  98. data/ext/rbs_extension/ast_translation.h +37 -0
  99. data/ext/rbs_extension/class_constants.c +185 -0
  100. data/{include/rbs/constants.h → ext/rbs_extension/class_constants.h} +22 -1
  101. data/ext/rbs_extension/compat.h +10 -0
  102. data/ext/rbs_extension/extconf.rb +25 -1
  103. data/ext/rbs_extension/legacy_location.c +294 -0
  104. data/ext/rbs_extension/legacy_location.h +82 -0
  105. data/ext/rbs_extension/main.c +468 -24
  106. data/ext/rbs_extension/rbs_extension.h +6 -21
  107. data/ext/rbs_extension/rbs_string_bridging.c +9 -0
  108. data/ext/rbs_extension/rbs_string_bridging.h +24 -0
  109. data/include/rbs/ast.h +1022 -0
  110. data/include/rbs/defines.h +86 -0
  111. data/include/rbs/lexer.h +206 -0
  112. data/include/rbs/location.h +40 -0
  113. data/include/rbs/parser.h +153 -0
  114. data/include/rbs/string.h +47 -0
  115. data/include/rbs/util/rbs_allocator.h +59 -0
  116. data/include/rbs/util/rbs_assert.h +20 -0
  117. data/include/rbs/util/rbs_buffer.h +83 -0
  118. data/include/rbs/util/rbs_constant_pool.h +6 -70
  119. data/include/rbs/util/rbs_encoding.h +282 -0
  120. data/include/rbs/util/rbs_unescape.h +24 -0
  121. data/include/rbs.h +9 -2
  122. data/lib/rbs/annotate/formatter.rb +3 -13
  123. data/lib/rbs/annotate/rdoc_annotator.rb +3 -1
  124. data/lib/rbs/annotate/rdoc_source.rb +1 -1
  125. data/lib/rbs/ast/annotation.rb +1 -1
  126. data/lib/rbs/ast/comment.rb +1 -1
  127. data/lib/rbs/ast/declarations.rb +10 -10
  128. data/lib/rbs/ast/members.rb +14 -14
  129. data/lib/rbs/ast/ruby/annotations.rb +409 -0
  130. data/lib/rbs/ast/ruby/comment_block.rb +245 -0
  131. data/lib/rbs/ast/ruby/declarations.rb +281 -0
  132. data/lib/rbs/ast/ruby/helpers/constant_helper.rb +28 -0
  133. data/lib/rbs/ast/ruby/helpers/location_helper.rb +15 -0
  134. data/lib/rbs/ast/ruby/members.rb +723 -0
  135. data/lib/rbs/ast/type_param.rb +24 -4
  136. data/lib/rbs/buffer.rb +105 -20
  137. data/lib/rbs/cli/diff.rb +16 -15
  138. data/lib/rbs/cli/validate.rb +63 -126
  139. data/lib/rbs/cli.rb +56 -24
  140. data/lib/rbs/collection/config/lockfile_generator.rb +14 -2
  141. data/lib/rbs/collection/sources/git.rb +1 -0
  142. data/lib/rbs/collection.rb +0 -1
  143. data/lib/rbs/definition.rb +6 -1
  144. data/lib/rbs/definition_builder/ancestor_builder.rb +121 -65
  145. data/lib/rbs/definition_builder/method_builder.rb +65 -30
  146. data/lib/rbs/definition_builder.rb +177 -20
  147. data/lib/rbs/diff.rb +7 -1
  148. data/lib/rbs/environment/class_entry.rb +69 -0
  149. data/lib/rbs/environment/module_entry.rb +66 -0
  150. data/lib/rbs/environment.rb +402 -214
  151. data/lib/rbs/environment_loader.rb +2 -8
  152. data/lib/rbs/errors.rb +31 -21
  153. data/lib/rbs/inline_parser/comment_association.rb +117 -0
  154. data/lib/rbs/inline_parser.rb +542 -0
  155. data/lib/rbs/location_aux.rb +36 -4
  156. data/lib/rbs/locator.rb +5 -1
  157. data/lib/rbs/method_type.rb +5 -3
  158. data/lib/rbs/namespace.rb +0 -7
  159. data/lib/rbs/parser_aux.rb +35 -7
  160. data/lib/rbs/prototype/helpers.rb +57 -0
  161. data/lib/rbs/prototype/rb.rb +3 -28
  162. data/lib/rbs/prototype/rbi.rb +3 -20
  163. data/lib/rbs/prototype/runtime.rb +10 -2
  164. data/lib/rbs/resolver/constant_resolver.rb +2 -2
  165. data/lib/rbs/resolver/type_name_resolver.rb +116 -38
  166. data/lib/rbs/source.rb +99 -0
  167. data/lib/rbs/subtractor.rb +7 -4
  168. data/lib/rbs/test/type_check.rb +19 -2
  169. data/lib/rbs/type_name.rb +1 -8
  170. data/lib/rbs/types.rb +91 -79
  171. data/lib/rbs/unit_test/convertibles.rb +1 -0
  172. data/lib/rbs/unit_test/type_assertions.rb +35 -8
  173. data/lib/rbs/validator.rb +2 -2
  174. data/lib/rbs/version.rb +1 -1
  175. data/lib/rbs.rb +13 -2
  176. data/lib/rdoc/discover.rb +1 -1
  177. data/lib/rdoc_plugin/parser.rb +3 -3
  178. data/rbs.gemspec +4 -2
  179. data/rust/.gitignore +1 -0
  180. data/rust/Cargo.lock +378 -0
  181. data/rust/Cargo.toml +7 -0
  182. data/rust/ruby-rbs/Cargo.toml +22 -0
  183. data/rust/ruby-rbs/build.rs +764 -0
  184. data/rust/ruby-rbs/examples/locations.rs +60 -0
  185. data/rust/ruby-rbs/src/lib.rs +1 -0
  186. data/rust/ruby-rbs/src/node/mod.rs +742 -0
  187. data/rust/ruby-rbs/tests/sanity.rs +47 -0
  188. data/rust/ruby-rbs/vendor/rbs/config.yml +1 -0
  189. data/rust/ruby-rbs-sys/Cargo.toml +23 -0
  190. data/rust/ruby-rbs-sys/build.rs +204 -0
  191. data/rust/ruby-rbs-sys/src/lib.rs +50 -0
  192. data/rust/ruby-rbs-sys/vendor/rbs/include +1 -0
  193. data/rust/ruby-rbs-sys/vendor/rbs/src +1 -0
  194. data/rust/ruby-rbs-sys/wrapper.h +1 -0
  195. data/schema/typeParam.json +17 -1
  196. data/sig/ancestor_builder.rbs +1 -1
  197. data/sig/annotate/formatter.rbs +2 -2
  198. data/sig/annotate/rdoc_annotater.rbs +1 -1
  199. data/sig/ast/ruby/annotations.rbs +421 -0
  200. data/sig/ast/ruby/comment_block.rbs +127 -0
  201. data/sig/ast/ruby/declarations.rbs +158 -0
  202. data/sig/ast/ruby/helpers/constant_helper.rbs +11 -0
  203. data/sig/ast/ruby/helpers/location_helper.rbs +15 -0
  204. data/sig/ast/ruby/members.rbs +178 -0
  205. data/sig/buffer.rbs +63 -5
  206. data/sig/cli/diff.rbs +5 -11
  207. data/sig/cli/validate.rbs +12 -8
  208. data/sig/cli.rbs +18 -18
  209. data/sig/definition.rbs +6 -0
  210. data/sig/definition_builder.rbs +3 -1
  211. data/sig/environment/class_entry.rbs +50 -0
  212. data/sig/environment/module_entry.rbs +50 -0
  213. data/sig/environment.rbs +94 -87
  214. data/sig/errors.rbs +26 -20
  215. data/sig/inline_parser/comment_association.rbs +71 -0
  216. data/sig/inline_parser.rbs +124 -0
  217. data/sig/location.rbs +32 -7
  218. data/sig/locator.rbs +0 -2
  219. data/sig/manifest.yaml +0 -1
  220. data/sig/method_builder.rbs +9 -4
  221. data/sig/namespace.rbs +0 -5
  222. data/sig/parser.rbs +67 -13
  223. data/sig/prototype/helpers.rbs +2 -0
  224. data/sig/resolver/type_name_resolver.rbs +35 -7
  225. data/sig/source.rbs +48 -0
  226. data/sig/type_param.rbs +13 -8
  227. data/sig/typename.rbs +0 -5
  228. data/sig/types.rbs +10 -8
  229. data/sig/unit_test/spy.rbs +0 -8
  230. data/sig/unit_test/type_assertions.rbs +11 -0
  231. data/src/ast.c +1604 -0
  232. data/src/lexer.c +3194 -0
  233. data/src/lexer.re +154 -0
  234. data/src/lexstate.c +212 -0
  235. data/src/location.c +31 -0
  236. data/src/parser.c +4205 -0
  237. data/src/string.c +41 -0
  238. data/src/util/rbs_allocator.c +165 -0
  239. data/src/util/rbs_assert.c +19 -0
  240. data/src/util/rbs_buffer.c +54 -0
  241. data/src/util/rbs_constant_pool.c +18 -92
  242. data/src/util/rbs_encoding.c +21308 -0
  243. data/src/util/rbs_unescape.c +167 -0
  244. data/stdlib/bigdecimal/0/big_decimal.rbs +116 -98
  245. data/stdlib/bigdecimal-math/0/big_math.rbs +169 -8
  246. data/stdlib/cgi/0/core.rbs +9 -393
  247. data/stdlib/cgi/0/manifest.yaml +1 -0
  248. data/stdlib/cgi-escape/0/escape.rbs +171 -0
  249. data/stdlib/coverage/0/coverage.rbs +7 -4
  250. data/stdlib/date/0/date.rbs +92 -79
  251. data/stdlib/date/0/date_time.rbs +25 -24
  252. data/stdlib/delegate/0/delegator.rbs +10 -7
  253. data/stdlib/did_you_mean/0/did_you_mean.rbs +17 -16
  254. data/stdlib/digest/0/digest.rbs +110 -0
  255. data/stdlib/erb/0/erb.rbs +748 -347
  256. data/stdlib/etc/0/etc.rbs +55 -50
  257. data/stdlib/fileutils/0/fileutils.rbs +158 -139
  258. data/stdlib/forwardable/0/forwardable.rbs +13 -10
  259. data/stdlib/io-console/0/io-console.rbs +2 -2
  260. data/stdlib/json/0/json.rbs +226 -179
  261. data/stdlib/monitor/0/monitor.rbs +3 -3
  262. data/stdlib/net-http/0/net-http.rbs +162 -134
  263. data/stdlib/objspace/0/objspace.rbs +17 -34
  264. data/stdlib/open-uri/0/open-uri.rbs +48 -8
  265. data/stdlib/open3/0/open3.rbs +469 -10
  266. data/stdlib/openssl/0/openssl.rbs +475 -357
  267. data/stdlib/optparse/0/optparse.rbs +26 -17
  268. data/stdlib/pathname/0/pathname.rbs +11 -1381
  269. data/stdlib/pp/0/pp.rbs +9 -8
  270. data/stdlib/prettyprint/0/prettyprint.rbs +7 -7
  271. data/stdlib/pstore/0/pstore.rbs +35 -30
  272. data/stdlib/psych/0/psych.rbs +65 -12
  273. data/stdlib/psych/0/store.rbs +2 -4
  274. data/stdlib/pty/0/pty.rbs +9 -6
  275. data/stdlib/random-formatter/0/random-formatter.rbs +277 -0
  276. data/stdlib/rdoc/0/code_object.rbs +4 -3
  277. data/stdlib/rdoc/0/comment.rbs +2 -0
  278. data/stdlib/rdoc/0/options.rbs +76 -0
  279. data/stdlib/rdoc/0/parser.rbs +1 -1
  280. data/stdlib/rdoc/0/rdoc.rbs +7 -5
  281. data/stdlib/rdoc/0/store.rbs +2 -2
  282. data/stdlib/resolv/0/resolv.rbs +25 -68
  283. data/stdlib/ripper/0/ripper.rbs +25 -19
  284. data/stdlib/securerandom/0/manifest.yaml +2 -0
  285. data/stdlib/securerandom/0/securerandom.rbs +7 -20
  286. data/stdlib/shellwords/0/shellwords.rbs +2 -2
  287. data/stdlib/singleton/0/singleton.rbs +3 -0
  288. data/stdlib/socket/0/addrinfo.rbs +9 -9
  289. data/stdlib/socket/0/basic_socket.rbs +3 -3
  290. data/stdlib/socket/0/ip_socket.rbs +10 -8
  291. data/stdlib/socket/0/socket.rbs +23 -10
  292. data/stdlib/socket/0/tcp_server.rbs +1 -1
  293. data/stdlib/socket/0/tcp_socket.rbs +11 -3
  294. data/stdlib/socket/0/udp_socket.rbs +1 -1
  295. data/stdlib/socket/0/unix_server.rbs +1 -1
  296. data/stdlib/stringio/0/stringio.rbs +1177 -85
  297. data/stdlib/strscan/0/string_scanner.rbs +27 -25
  298. data/stdlib/tempfile/0/tempfile.rbs +25 -21
  299. data/stdlib/time/0/time.rbs +8 -6
  300. data/stdlib/timeout/0/timeout.rbs +63 -7
  301. data/stdlib/tsort/0/cyclic.rbs +3 -0
  302. data/stdlib/tsort/0/tsort.rbs +7 -6
  303. data/stdlib/uri/0/common.rbs +42 -20
  304. data/stdlib/uri/0/file.rbs +3 -3
  305. data/stdlib/uri/0/generic.rbs +26 -18
  306. data/stdlib/uri/0/http.rbs +2 -2
  307. data/stdlib/uri/0/ldap.rbs +2 -2
  308. data/stdlib/uri/0/mailto.rbs +3 -3
  309. data/stdlib/uri/0/rfc2396_parser.rbs +12 -12
  310. data/stdlib/zlib/0/deflate.rbs +4 -3
  311. data/stdlib/zlib/0/gzip_reader.rbs +6 -6
  312. data/stdlib/zlib/0/gzip_writer.rbs +14 -12
  313. data/stdlib/zlib/0/inflate.rbs +1 -1
  314. data/stdlib/zlib/0/need_dict.rbs +1 -1
  315. data/stdlib/zlib/0/zstream.rbs +1 -0
  316. metadata +121 -18
  317. data/ext/rbs_extension/lexer.c +0 -2728
  318. data/ext/rbs_extension/lexer.h +0 -179
  319. data/ext/rbs_extension/lexer.re +0 -147
  320. data/ext/rbs_extension/lexstate.c +0 -175
  321. data/ext/rbs_extension/location.c +0 -325
  322. data/ext/rbs_extension/location.h +0 -85
  323. data/ext/rbs_extension/parser.c +0 -2982
  324. data/ext/rbs_extension/parser.h +0 -18
  325. data/ext/rbs_extension/parserstate.c +0 -411
  326. data/ext/rbs_extension/parserstate.h +0 -163
  327. data/ext/rbs_extension/unescape.c +0 -32
  328. data/include/rbs/ruby_objs.h +0 -72
  329. data/src/constants.c +0 -153
  330. data/src/ruby_objs.c +0 -799
@@ -1,34 +1,478 @@
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
+ uint8_t *copied_name = (uint8_t *) malloc((size_t) RSTRING_LEN(name_str));
71
+ memcpy((void *) copied_name, RSTRING_PTR(name_str), RSTRING_LEN(name_str));
72
+
73
+ rbs_constant_id_t id = rbs_constant_pool_insert_owned(
74
+ &parser->constant_pool,
75
+ copied_name,
76
+ RSTRING_LEN(name_str)
77
+ );
78
+
79
+ if (!rbs_parser_insert_typevar(parser, id)) {
80
+ raise_error(parser->error, buffer);
81
+ }
82
+ }
83
+ }
84
+
85
+ struct parse_type_arg {
86
+ VALUE buffer;
87
+ rb_encoding *encoding;
88
+ rbs_parser_t *parser;
89
+ VALUE require_eof;
90
+ VALUE void_allowed;
91
+ VALUE self_allowed;
92
+ VALUE classish_allowed;
93
+ };
94
+
95
+ struct parse_method_type_arg {
96
+ VALUE buffer;
97
+ rb_encoding *encoding;
98
+ rbs_parser_t *parser;
99
+ VALUE require_eof;
100
+ };
101
+
102
+ struct parse_signature_arg {
103
+ VALUE buffer;
104
+ rb_encoding *encoding;
105
+ rbs_parser_t *parser;
106
+ VALUE require_eof;
107
+ };
108
+
109
+ static VALUE ensure_free_parser(VALUE parser) {
110
+ rbs_parser_free((rbs_parser_t *) parser);
111
+ return Qnil;
112
+ }
113
+
114
+ static VALUE parse_type_try(VALUE a) {
115
+ struct parse_type_arg *arg = (struct parse_type_arg *) a;
116
+ rbs_parser_t *parser = arg->parser;
117
+
118
+ if (parser->next_token.type == pEOF) {
119
+ return Qnil;
120
+ }
121
+
122
+ bool void_allowed = RTEST(arg->void_allowed);
123
+ bool self_allowed = RTEST(arg->self_allowed);
124
+ bool classish_allowed = RTEST(arg->classish_allowed);
125
+
126
+ rbs_node_t *type;
127
+ rbs_parse_type(parser, &type, void_allowed, self_allowed, classish_allowed);
128
+
129
+ raise_error_if_any(parser, arg->buffer);
130
+
131
+ if (RB_TEST(arg->require_eof)) {
132
+ rbs_parser_advance(parser);
133
+ if (parser->current_token.type != pEOF) {
134
+ rbs_parser_set_error(parser, parser->current_token, true, "expected a token `%s`", rbs_token_type_str(pEOF));
135
+ raise_error(parser->error, arg->buffer);
136
+ }
137
+ }
138
+
139
+ rbs_translation_context_t ctx = rbs_translation_context_create(
140
+ &parser->constant_pool,
141
+ arg->buffer,
142
+ arg->encoding
143
+ );
144
+
145
+ return rbs_struct_to_ruby_value(ctx, type);
146
+ }
147
+
148
+ static rbs_lexer_t *alloc_lexer_from_buffer(rbs_allocator_t *allocator, VALUE string, rb_encoding *encoding, int start_pos, int end_pos) {
149
+ if (start_pos < 0 || end_pos < 0) {
150
+ rb_raise(rb_eArgError, "negative position range: %d...%d", start_pos, end_pos);
151
+ }
152
+
153
+ const char *encoding_name = rb_enc_name(encoding);
154
+
155
+ return rbs_lexer_new(
156
+ allocator,
157
+ rbs_string_from_ruby_string(string),
158
+ rbs_encoding_find((const uint8_t *) encoding_name, (const uint8_t *) (encoding_name + strlen(encoding_name))),
159
+ start_pos,
160
+ end_pos
161
+ );
162
+ }
163
+
164
+ static rbs_parser_t *alloc_parser_from_buffer(VALUE buffer, int start_pos, int end_pos) {
165
+ if (start_pos < 0 || end_pos < 0) {
166
+ rb_raise(rb_eArgError, "negative position range: %d...%d", start_pos, end_pos);
167
+ }
168
+
169
+ VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
170
+ StringValue(string);
171
+
172
+ rb_encoding *encoding = rb_enc_get(string);
173
+ const char *encoding_name = rb_enc_name(encoding);
174
+
175
+ return rbs_parser_new(
176
+ rbs_string_from_ruby_string(string),
177
+ rbs_encoding_find((const uint8_t *) encoding_name, (const uint8_t *) (encoding_name + strlen(encoding_name))),
178
+ start_pos,
179
+ end_pos
180
+ );
181
+ }
182
+
183
+ 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) {
184
+ VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
185
+ StringValue(string);
186
+ rb_encoding *encoding = rb_enc_get(string);
187
+
188
+ rbs_parser_t *parser = alloc_parser_from_buffer(buffer, FIX2INT(start_pos), FIX2INT(end_pos));
189
+ declare_type_variables(parser, variables, buffer);
190
+ struct parse_type_arg arg = {
191
+ .buffer = buffer,
192
+ .encoding = encoding,
193
+ .parser = parser,
194
+ .require_eof = require_eof,
195
+ .void_allowed = void_allowed,
196
+ .self_allowed = self_allowed,
197
+ .classish_allowed = classish_allowed
198
+ };
199
+
200
+ VALUE result = rb_ensure(parse_type_try, (VALUE) &arg, ensure_free_parser, (VALUE) parser);
201
+
202
+ RB_GC_GUARD(string);
203
+
204
+ return result;
9
205
  }
10
206
 
11
- void
12
- Init_rbs_extension(void)
13
- {
207
+ static VALUE parse_method_type_try(VALUE a) {
208
+ struct parse_method_type_arg *arg = (struct parse_method_type_arg *) a;
209
+ rbs_parser_t *parser = arg->parser;
210
+
211
+ if (parser->next_token.type == pEOF) {
212
+ return Qnil;
213
+ }
214
+
215
+ rbs_method_type_t *method_type = NULL;
216
+ rbs_parse_method_type(parser, &method_type, RB_TEST(arg->require_eof), true);
217
+
218
+ raise_error_if_any(parser, arg->buffer);
219
+
220
+ rbs_translation_context_t ctx = rbs_translation_context_create(
221
+ &parser->constant_pool,
222
+ arg->buffer,
223
+ arg->encoding
224
+ );
225
+
226
+ return rbs_struct_to_ruby_value(ctx, (rbs_node_t *) method_type);
227
+ }
228
+
229
+ static VALUE rbsparser_parse_method_type(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos, VALUE variables, VALUE require_eof) {
230
+ VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
231
+ StringValue(string);
232
+ rb_encoding *encoding = rb_enc_get(string);
233
+
234
+ rbs_parser_t *parser = alloc_parser_from_buffer(buffer, FIX2INT(start_pos), FIX2INT(end_pos));
235
+ declare_type_variables(parser, variables, buffer);
236
+ struct parse_method_type_arg arg = {
237
+ .buffer = buffer,
238
+ .encoding = encoding,
239
+ .parser = parser,
240
+ .require_eof = require_eof
241
+ };
242
+
243
+ VALUE result = rb_ensure(parse_method_type_try, (VALUE) &arg, ensure_free_parser, (VALUE) parser);
244
+
245
+ RB_GC_GUARD(string);
246
+
247
+ return result;
248
+ }
249
+
250
+ static VALUE parse_signature_try(VALUE a) {
251
+ struct parse_signature_arg *arg = (struct parse_signature_arg *) a;
252
+ rbs_parser_t *parser = arg->parser;
253
+
254
+ rbs_signature_t *signature = NULL;
255
+ rbs_parse_signature(parser, &signature);
256
+
257
+ raise_error_if_any(parser, arg->buffer);
258
+
259
+ rbs_translation_context_t ctx = rbs_translation_context_create(
260
+ &parser->constant_pool,
261
+ arg->buffer,
262
+ arg->encoding
263
+ );
264
+
265
+ return rbs_struct_to_ruby_value(ctx, (rbs_node_t *) signature);
266
+ }
267
+
268
+ static VALUE rbsparser_parse_signature(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos) {
269
+ VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
270
+ StringValue(string);
271
+ rb_encoding *encoding = rb_enc_get(string);
272
+
273
+ rbs_parser_t *parser = alloc_parser_from_buffer(buffer, FIX2INT(start_pos), FIX2INT(end_pos));
274
+ struct parse_signature_arg arg = {
275
+ .buffer = buffer,
276
+ .encoding = encoding,
277
+ .parser = parser,
278
+ .require_eof = false
279
+ };
280
+
281
+ VALUE result = rb_ensure(parse_signature_try, (VALUE) &arg, ensure_free_parser, (VALUE) parser);
282
+
283
+ RB_GC_GUARD(string);
284
+
285
+ return result;
286
+ }
287
+
288
+ struct parse_type_params_arg {
289
+ VALUE buffer;
290
+ rb_encoding *encoding;
291
+ rbs_parser_t *parser;
292
+ VALUE module_type_params;
293
+ };
294
+
295
+ static VALUE parse_type_params_try(VALUE a) {
296
+ struct parse_type_params_arg *arg = (struct parse_type_params_arg *) a;
297
+ rbs_parser_t *parser = arg->parser;
298
+
299
+ if (parser->next_token.type == pEOF) {
300
+ return Qnil;
301
+ }
302
+
303
+ rbs_node_list_t *params = NULL;
304
+ rbs_parse_type_params(parser, arg->module_type_params, &params);
305
+
306
+ raise_error_if_any(parser, arg->buffer);
307
+
308
+ rbs_translation_context_t ctx = rbs_translation_context_create(
309
+ &parser->constant_pool,
310
+ arg->buffer,
311
+ arg->encoding
312
+ );
313
+
314
+ return rbs_node_list_to_ruby_array(ctx, params);
315
+ }
316
+
317
+ static VALUE rbsparser_parse_type_params(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos, VALUE module_type_params) {
318
+ VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
319
+ StringValue(string);
320
+ rb_encoding *encoding = rb_enc_get(string);
321
+
322
+ rbs_parser_t *parser = alloc_parser_from_buffer(buffer, FIX2INT(start_pos), FIX2INT(end_pos));
323
+ struct parse_type_params_arg arg = {
324
+ .buffer = buffer,
325
+ .encoding = encoding,
326
+ .parser = parser,
327
+ .module_type_params = module_type_params
328
+ };
329
+
330
+ VALUE result = rb_ensure(parse_type_params_try, (VALUE) &arg, ensure_free_parser, (VALUE) parser);
331
+
332
+ RB_GC_GUARD(string);
333
+
334
+ return result;
335
+ }
336
+
337
+ static VALUE parse_inline_leading_annotation_try(VALUE a) {
338
+ struct parse_type_arg *arg = (struct parse_type_arg *) a;
339
+ rbs_parser_t *parser = arg->parser;
340
+
341
+ rbs_ast_ruby_annotations_t *annotation = NULL;
342
+ bool success = rbs_parse_inline_leading_annotation(parser, &annotation);
343
+
344
+ raise_error_if_any(parser, arg->buffer);
345
+
346
+ if (!success || annotation == NULL) {
347
+ return Qnil;
348
+ }
349
+
350
+ rbs_translation_context_t ctx = rbs_translation_context_create(
351
+ &parser->constant_pool,
352
+ arg->buffer,
353
+ arg->encoding
354
+ );
355
+
356
+ return rbs_struct_to_ruby_value(ctx, (rbs_node_t *) annotation);
357
+ }
358
+
359
+ static VALUE rbsparser_parse_inline_leading_annotation(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos, VALUE variables) {
360
+ VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
361
+ StringValue(string);
362
+ rb_encoding *encoding = rb_enc_get(string);
363
+
364
+ rbs_parser_t *parser = alloc_parser_from_buffer(buffer, FIX2INT(start_pos), FIX2INT(end_pos));
365
+ declare_type_variables(parser, variables, buffer);
366
+ struct parse_type_arg arg = {
367
+ .buffer = buffer,
368
+ .encoding = encoding,
369
+ .parser = parser,
370
+ .require_eof = Qfalse
371
+ };
372
+
373
+ VALUE result = rb_ensure(parse_inline_leading_annotation_try, (VALUE) &arg, ensure_free_parser, (VALUE) parser);
374
+
375
+ RB_GC_GUARD(string);
376
+
377
+ return result;
378
+ }
379
+
380
+ static VALUE parse_inline_trailing_annotation_try(VALUE a) {
381
+ struct parse_type_arg *arg = (struct parse_type_arg *) a;
382
+ rbs_parser_t *parser = arg->parser;
383
+
384
+ rbs_ast_ruby_annotations_t *annotation = NULL;
385
+ bool success = rbs_parse_inline_trailing_annotation(parser, &annotation);
386
+
387
+ raise_error_if_any(parser, arg->buffer);
388
+
389
+ if (!success || annotation == NULL) {
390
+ return Qnil;
391
+ }
392
+
393
+ rbs_translation_context_t ctx = rbs_translation_context_create(
394
+ &parser->constant_pool,
395
+ arg->buffer,
396
+ arg->encoding
397
+ );
398
+
399
+ return rbs_struct_to_ruby_value(ctx, (rbs_node_t *) annotation);
400
+ }
401
+
402
+ static VALUE rbsparser_parse_inline_trailing_annotation(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos, VALUE variables) {
403
+ VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
404
+ StringValue(string);
405
+ rb_encoding *encoding = rb_enc_get(string);
406
+
407
+ rbs_parser_t *parser = alloc_parser_from_buffer(buffer, FIX2INT(start_pos), FIX2INT(end_pos));
408
+ declare_type_variables(parser, variables, buffer);
409
+ struct parse_type_arg arg = {
410
+ .buffer = buffer,
411
+ .encoding = encoding,
412
+ .parser = parser,
413
+ .require_eof = Qfalse
414
+ };
415
+
416
+ VALUE result = rb_ensure(parse_inline_trailing_annotation_try, (VALUE) &arg, ensure_free_parser, (VALUE) parser);
417
+
418
+ RB_GC_GUARD(string);
419
+
420
+ return result;
421
+ }
422
+
423
+ static VALUE rbsparser_lex(VALUE self, VALUE buffer, VALUE end_pos) {
424
+ VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
425
+ StringValue(string);
426
+ rb_encoding *encoding = rb_enc_get(string);
427
+
428
+ rbs_allocator_t *allocator = rbs_allocator_init();
429
+ rbs_lexer_t *lexer = alloc_lexer_from_buffer(allocator, string, encoding, 0, FIX2INT(end_pos));
430
+
431
+ VALUE results = rb_ary_new();
432
+ rbs_token_t token = NullToken;
433
+ while (token.type != pEOF) {
434
+ token = rbs_lexer_next_token(lexer);
435
+ VALUE type = ID2SYM(rb_intern(rbs_token_type_str(token.type)));
436
+ VALUE location = rbs_new_location(buffer, token.range);
437
+ VALUE pair = rb_ary_new3(2, type, location);
438
+ rb_ary_push(results, pair);
439
+ }
440
+
441
+ rbs_allocator_free(allocator);
442
+ RB_GC_GUARD(string);
443
+
444
+ return results;
445
+ }
446
+
447
+ void rbs__init_parser(void) {
448
+ RBS_Parser = rb_define_class_under(RBS, "Parser", rb_cObject);
449
+ rb_gc_register_mark_object(RBS_Parser);
450
+
451
+ EMPTY_ARRAY = rb_obj_freeze(rb_ary_new());
452
+ rb_gc_register_mark_object(EMPTY_ARRAY);
453
+
454
+ EMPTY_HASH = rb_obj_freeze(rb_hash_new());
455
+ rb_gc_register_mark_object(EMPTY_HASH);
456
+
457
+ rb_define_singleton_method(RBS_Parser, "_parse_type", rbsparser_parse_type, 8);
458
+ rb_define_singleton_method(RBS_Parser, "_parse_method_type", rbsparser_parse_method_type, 5);
459
+ rb_define_singleton_method(RBS_Parser, "_parse_signature", rbsparser_parse_signature, 3);
460
+ rb_define_singleton_method(RBS_Parser, "_parse_type_params", rbsparser_parse_type_params, 4);
461
+ rb_define_singleton_method(RBS_Parser, "_parse_inline_leading_annotation", rbsparser_parse_inline_leading_annotation, 4);
462
+ rb_define_singleton_method(RBS_Parser, "_parse_inline_trailing_annotation", rbsparser_parse_inline_trailing_annotation, 4);
463
+ rb_define_singleton_method(RBS_Parser, "_lex", rbsparser_lex, 2);
464
+ }
465
+
466
+ static void Deinit_rbs_extension(ruby_vm_t *_) {
467
+ }
468
+
469
+ void Init_rbs_extension(void) {
14
470
  #ifdef HAVE_RB_EXT_RACTOR_SAFE
15
- rb_ext_ractor_safe(true);
471
+ rb_ext_ractor_safe(true);
16
472
  #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
- */
30
- const size_t num_uniquely_interned_strings = 26;
31
- rbs_constant_pool_init(RBS_GLOBAL_CONSTANT_POOL, num_uniquely_interned_strings);
32
-
33
- ruby_vm_at_exit(Deinit_rbs_extension);
473
+ rbs__init_constants();
474
+ rbs__init_location();
475
+ rbs__init_parser();
476
+
477
+ ruby_vm_at_exit(Deinit_rbs_extension);
34
478
  }
@@ -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