rbs 2.8.4 → 3.8.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (434) hide show
  1. checksums.yaml +4 -4
  2. data/.github/dependabot.yml +12 -4
  3. data/.github/workflows/comments.yml +11 -11
  4. data/.github/workflows/dependabot.yml +30 -0
  5. data/.github/workflows/ruby.yml +40 -49
  6. data/.github/workflows/typecheck.yml +36 -0
  7. data/.github/workflows/windows.yml +28 -0
  8. data/.gitignore +1 -0
  9. data/.rubocop.yml +42 -2
  10. data/CHANGELOG.md +845 -1
  11. data/README.md +64 -4
  12. data/Rakefile +198 -18
  13. data/Steepfile +11 -11
  14. data/config.yml +311 -0
  15. data/core/array.rbs +2189 -1914
  16. data/core/basic_object.rbs +59 -84
  17. data/core/binding.rbs +7 -69
  18. data/core/builtin.rbs +210 -11
  19. data/core/class.rbs +37 -0
  20. data/core/comparable.rbs +23 -25
  21. data/core/complex.rbs +449 -227
  22. data/core/constants.rbs +29 -21
  23. data/core/data.rbs +415 -0
  24. data/core/dir.rbs +698 -415
  25. data/core/encoding.rbs +468 -843
  26. data/core/enumerable.rbs +495 -455
  27. data/core/enumerator/product.rbs +92 -0
  28. data/core/enumerator.rbs +106 -9
  29. data/core/env.rbs +1 -1
  30. data/core/errno.rbs +506 -605
  31. data/core/errors.rbs +15 -17
  32. data/core/exception.rbs +361 -145
  33. data/core/false_class.rbs +39 -26
  34. data/core/fiber.rbs +121 -14
  35. data/core/file.rbs +1262 -320
  36. data/core/file_test.rbs +62 -45
  37. data/core/float.rbs +187 -208
  38. data/core/gc.rbs +446 -196
  39. data/core/global_variables.rbs +29 -29
  40. data/core/hash.rbs +242 -349
  41. data/core/integer.rbs +246 -308
  42. data/core/io/buffer.rbs +373 -122
  43. data/core/io/wait.rbs +29 -17
  44. data/core/io.rbs +1881 -1518
  45. data/core/kernel.rbs +2116 -1538
  46. data/core/marshal.rbs +24 -14
  47. data/core/match_data.rbs +413 -166
  48. data/core/math.rbs +531 -291
  49. data/core/method.rbs +101 -32
  50. data/core/module.rbs +228 -64
  51. data/core/nil_class.rbs +106 -47
  52. data/core/numeric.rbs +206 -292
  53. data/core/object.rbs +73 -1168
  54. data/core/object_space/weak_key_map.rbs +166 -0
  55. data/core/object_space.rbs +5 -3
  56. data/core/proc.rbs +280 -39
  57. data/core/process.rbs +1318 -658
  58. data/core/ractor.rbs +200 -134
  59. data/core/random.rbs +21 -4
  60. data/core/range.rbs +309 -153
  61. data/core/rational.rbs +4 -12
  62. data/core/rb_config.rbs +64 -43
  63. data/core/rbs/unnamed/argf.rbs +411 -147
  64. data/core/rbs/unnamed/env_class.rbs +137 -253
  65. data/core/rbs/unnamed/random.rbs +49 -26
  66. data/core/refinement.rbs +16 -1
  67. data/core/regexp.rbs +1568 -862
  68. data/core/ruby_vm.rbs +719 -7
  69. data/core/rubygems/config_file.rbs +3 -0
  70. data/core/rubygems/errors.rbs +69 -6
  71. data/core/rubygems/rubygems.rbs +71 -17
  72. data/core/rubygems/version.rbs +11 -7
  73. data/{stdlib/set/0 → core}/set.rbs +80 -91
  74. data/core/signal.rbs +14 -8
  75. data/core/string.rbs +1732 -1607
  76. data/core/struct.rbs +467 -95
  77. data/core/symbol.rbs +215 -245
  78. data/core/thread.rbs +133 -89
  79. data/core/thread_group.rbs +9 -9
  80. data/core/time.rbs +1141 -841
  81. data/core/trace_point.rbs +181 -121
  82. data/core/true_class.rbs +58 -32
  83. data/core/unbound_method.rbs +103 -30
  84. data/core/warning.rbs +50 -5
  85. data/docs/CONTRIBUTING.md +1 -1
  86. data/docs/architecture.md +110 -0
  87. data/docs/collection.md +59 -5
  88. data/docs/data_and_struct.md +86 -0
  89. data/docs/gem.md +57 -0
  90. data/docs/rbs_by_example.md +16 -35
  91. data/docs/repo.md +1 -1
  92. data/docs/sigs.md +7 -7
  93. data/docs/stdlib.md +63 -5
  94. data/docs/syntax.md +255 -61
  95. data/docs/tools.md +1 -0
  96. data/ext/rbs_extension/extconf.rb +10 -0
  97. data/ext/rbs_extension/lexer.c +1741 -1548
  98. data/ext/rbs_extension/lexer.h +11 -1
  99. data/ext/rbs_extension/lexer.re +12 -6
  100. data/ext/rbs_extension/lexstate.c +26 -3
  101. data/ext/rbs_extension/location.c +119 -111
  102. data/ext/rbs_extension/location.h +32 -7
  103. data/ext/rbs_extension/main.c +3 -0
  104. data/ext/rbs_extension/parser.c +883 -481
  105. data/ext/rbs_extension/parserstate.c +65 -25
  106. data/ext/rbs_extension/parserstate.h +13 -3
  107. data/ext/rbs_extension/rbs_extension.h +1 -10
  108. data/ext/rbs_extension/unescape.c +7 -47
  109. data/goodcheck.yml +2 -2
  110. data/{ext/rbs_extension → include/rbs}/constants.h +26 -15
  111. data/include/rbs/ruby_objs.h +72 -0
  112. data/include/rbs.h +7 -0
  113. data/lib/rbs/annotate/annotations.rb +3 -3
  114. data/lib/rbs/annotate/formatter.rb +13 -3
  115. data/lib/rbs/annotate/rdoc_annotator.rb +1 -1
  116. data/lib/rbs/annotate/rdoc_source.rb +12 -3
  117. data/lib/rbs/ast/declarations.rb +85 -2
  118. data/lib/rbs/ast/directives.rb +39 -0
  119. data/lib/rbs/ast/members.rb +49 -15
  120. data/lib/rbs/ast/type_param.rb +104 -15
  121. data/lib/rbs/ast/visitor.rb +137 -0
  122. data/lib/rbs/buffer.rb +5 -0
  123. data/lib/rbs/cli/colored_io.rb +48 -0
  124. data/lib/rbs/cli/diff.rb +83 -0
  125. data/lib/rbs/cli/validate.rb +356 -0
  126. data/lib/rbs/cli.rb +253 -143
  127. data/lib/rbs/collection/cleaner.rb +8 -1
  128. data/lib/rbs/collection/config/lockfile.rb +92 -0
  129. data/lib/rbs/collection/config/lockfile_generator.rb +154 -65
  130. data/lib/rbs/collection/config.rb +19 -46
  131. data/lib/rbs/collection/installer.rb +12 -13
  132. data/lib/rbs/collection/sources/base.rb +2 -2
  133. data/lib/rbs/collection/sources/git.rb +146 -69
  134. data/lib/rbs/collection/sources/local.rb +81 -0
  135. data/lib/rbs/collection/sources/rubygems.rb +10 -12
  136. data/lib/rbs/collection/sources/stdlib.rb +14 -13
  137. data/lib/rbs/collection/sources.rb +15 -2
  138. data/lib/rbs/collection.rb +2 -1
  139. data/lib/rbs/definition.rb +13 -16
  140. data/lib/rbs/definition_builder/ancestor_builder.rb +100 -24
  141. data/lib/rbs/definition_builder/method_builder.rb +4 -4
  142. data/lib/rbs/definition_builder.rb +489 -584
  143. data/lib/rbs/diff.rb +125 -0
  144. data/lib/rbs/environment/use_map.rb +77 -0
  145. data/lib/rbs/environment.rb +406 -105
  146. data/lib/rbs/environment_loader.rb +48 -44
  147. data/lib/rbs/environment_walker.rb +1 -1
  148. data/lib/rbs/errors.rb +175 -56
  149. data/lib/rbs/file_finder.rb +28 -0
  150. data/lib/rbs/location_aux.rb +8 -7
  151. data/lib/rbs/locator.rb +37 -15
  152. data/lib/rbs/method_type.rb +23 -0
  153. data/lib/rbs/namespace.rb +1 -0
  154. data/lib/rbs/parser/lex_result.rb +15 -0
  155. data/lib/rbs/parser/token.rb +23 -0
  156. data/lib/rbs/parser_aux.rb +22 -13
  157. data/lib/rbs/prototype/helpers.rb +48 -22
  158. data/lib/rbs/prototype/node_usage.rb +99 -0
  159. data/lib/rbs/prototype/rb.rb +125 -31
  160. data/lib/rbs/prototype/rbi.rb +49 -36
  161. data/lib/rbs/prototype/runtime/helpers.rb +59 -0
  162. data/lib/rbs/prototype/runtime/reflection.rb +19 -0
  163. data/lib/rbs/prototype/runtime/value_object_generator.rb +279 -0
  164. data/lib/rbs/prototype/runtime.rb +273 -159
  165. data/lib/rbs/resolver/constant_resolver.rb +24 -8
  166. data/lib/rbs/resolver/type_name_resolver.rb +41 -7
  167. data/lib/rbs/sorter.rb +153 -123
  168. data/lib/rbs/substitution.rb +19 -0
  169. data/lib/rbs/subtractor.rb +201 -0
  170. data/lib/rbs/test/errors.rb +24 -11
  171. data/lib/rbs/test/guaranteed.rb +30 -0
  172. data/lib/rbs/test/hook.rb +45 -40
  173. data/lib/rbs/test/setup.rb +1 -1
  174. data/lib/rbs/test/tester.rb +1 -1
  175. data/lib/rbs/test/type_check.rb +120 -23
  176. data/lib/rbs/test.rb +6 -3
  177. data/lib/rbs/type_alias_dependency.rb +13 -3
  178. data/lib/rbs/type_alias_regularity.rb +21 -14
  179. data/lib/rbs/type_name.rb +18 -13
  180. data/lib/rbs/types.rb +352 -18
  181. data/lib/rbs/unit_test/convertibles.rb +176 -0
  182. data/lib/rbs/unit_test/spy.rb +136 -0
  183. data/lib/rbs/unit_test/type_assertions.rb +341 -0
  184. data/lib/rbs/unit_test/with_aliases.rb +143 -0
  185. data/lib/rbs/unit_test.rb +6 -0
  186. data/lib/rbs/validator.rb +55 -30
  187. data/lib/rbs/variance_calculator.rb +26 -23
  188. data/lib/rbs/vendorer.rb +3 -3
  189. data/lib/rbs/version.rb +1 -1
  190. data/lib/rbs/writer.rb +69 -22
  191. data/lib/rbs.rb +7 -2
  192. data/lib/rdoc/discover.rb +1 -1
  193. data/lib/rdoc_plugin/parser.rb +5 -5
  194. data/rbs.gemspec +12 -2
  195. data/schema/decls.json +1 -1
  196. data/schema/members.json +15 -10
  197. data/sig/ancestor_builder.rbs +4 -0
  198. data/sig/ancestor_graph.rbs +22 -2
  199. data/sig/annotate/formatter.rbs +2 -2
  200. data/sig/annotate/rdoc_annotater.rbs +1 -1
  201. data/sig/cli/colored_io.rbs +15 -0
  202. data/sig/cli/diff.rbs +21 -0
  203. data/sig/cli/validate.rbs +43 -0
  204. data/sig/cli.rbs +4 -0
  205. data/sig/collection/config/lockfile.rbs +74 -0
  206. data/sig/collection/config/lockfile_generator.rbs +66 -0
  207. data/sig/collection/config.rbs +5 -48
  208. data/sig/collection/installer.rbs +1 -1
  209. data/sig/collection/sources.rbs +105 -33
  210. data/sig/constant.rbs +1 -1
  211. data/sig/declarations.rbs +42 -3
  212. data/sig/definition.rbs +26 -10
  213. data/sig/definition_builder.rbs +103 -81
  214. data/sig/diff.rbs +28 -0
  215. data/sig/directives.rbs +61 -0
  216. data/sig/environment.rbs +175 -29
  217. data/sig/environment_loader.rbs +20 -18
  218. data/sig/errors.rbs +123 -2
  219. data/sig/file_finder.rbs +28 -0
  220. data/sig/location.rbs +0 -3
  221. data/sig/locator.rbs +14 -2
  222. data/sig/manifest.yaml +0 -1
  223. data/sig/members.rbs +32 -9
  224. data/sig/method_types.rbs +10 -4
  225. data/sig/namespace.rbs +2 -3
  226. data/sig/parser.rbs +55 -16
  227. data/sig/prototype/helpers.rbs +4 -0
  228. data/sig/prototype/node_usage.rbs +20 -0
  229. data/sig/prototype/rb.rbs +10 -2
  230. data/sig/prototype/rbi.rbs +2 -0
  231. data/sig/prototype/runtime.rbs +182 -0
  232. data/sig/rbs.rbs +1 -1
  233. data/sig/rdoc/rbs.rbs +4 -0
  234. data/sig/repository.rbs +7 -5
  235. data/sig/resolver/constant_resolver.rbs +3 -4
  236. data/sig/resolver/context.rbs +1 -1
  237. data/sig/resolver/type_name_resolver.rbs +5 -1
  238. data/sig/shims/bundler.rbs +38 -0
  239. data/sig/shims/rubygems.rbs +19 -0
  240. data/sig/sorter.rbs +23 -5
  241. data/sig/substitution.rbs +6 -0
  242. data/sig/subtractor.rbs +37 -0
  243. data/sig/test/errors.rbs +52 -0
  244. data/sig/test/guranteed.rbs +9 -0
  245. data/sig/test/type_check.rbs +19 -0
  246. data/sig/test.rbs +82 -0
  247. data/sig/type_alias_dependency.rbs +31 -0
  248. data/sig/type_alias_regularity.rbs +12 -6
  249. data/sig/type_param.rbs +45 -9
  250. data/sig/typename.rbs +8 -5
  251. data/sig/types.rbs +119 -12
  252. data/sig/unit_test/convertibles.rbs +154 -0
  253. data/sig/unit_test/spy.rbs +28 -0
  254. data/sig/unit_test/type_assertions.rbs +194 -0
  255. data/sig/unit_test/with_aliases.rbs +136 -0
  256. data/sig/use_map.rbs +35 -0
  257. data/sig/validator.rbs +12 -5
  258. data/sig/variance_calculator.rbs +3 -1
  259. data/sig/vendorer.rbs +1 -1
  260. data/sig/visitor.rbs +47 -0
  261. data/sig/writer.rbs +6 -2
  262. data/src/constants.c +153 -0
  263. data/src/ruby_objs.c +793 -0
  264. data/stdlib/base64/0/base64.rbs +298 -45
  265. data/stdlib/benchmark/0/benchmark.rbs +12 -3
  266. data/stdlib/bigdecimal/0/big_decimal.rbs +62 -198
  267. data/stdlib/cgi/0/core.rbs +68 -15
  268. data/stdlib/cgi/0/manifest.yaml +1 -0
  269. data/stdlib/coverage/0/coverage.rbs +50 -11
  270. data/stdlib/csv/0/csv.rbs +90 -119
  271. data/stdlib/csv/0/manifest.yaml +1 -0
  272. data/stdlib/date/0/date.rbs +806 -735
  273. data/stdlib/date/0/date_time.rbs +70 -211
  274. data/stdlib/dbm/0/dbm.rbs +0 -2
  275. data/stdlib/delegate/0/delegator.rbs +184 -0
  276. data/stdlib/delegate/0/kernel.rbs +47 -0
  277. data/stdlib/delegate/0/simple_delegator.rbs +96 -0
  278. data/stdlib/did_you_mean/0/did_you_mean.rbs +3 -8
  279. data/stdlib/digest/0/digest.rbs +48 -35
  280. data/stdlib/erb/0/erb.rbs +15 -39
  281. data/stdlib/etc/0/etc.rbs +174 -54
  282. data/stdlib/fileutils/0/fileutils.rbs +1234 -385
  283. data/stdlib/forwardable/0/forwardable.rbs +4 -4
  284. data/stdlib/io-console/0/io-console.rbs +82 -17
  285. data/stdlib/ipaddr/0/ipaddr.rbs +11 -6
  286. data/stdlib/json/0/json.rbs +434 -151
  287. data/stdlib/kconv/0/kconv.rbs +166 -0
  288. data/stdlib/logger/0/formatter.rbs +0 -2
  289. data/stdlib/logger/0/log_device.rbs +1 -3
  290. data/stdlib/logger/0/logger.rbs +465 -328
  291. data/stdlib/minitest/0/kernel.rbs +2 -2
  292. data/stdlib/minitest/0/minitest/abstract_reporter.rbs +4 -1
  293. data/stdlib/minitest/0/minitest/assertion.rbs +1 -0
  294. data/stdlib/minitest/0/minitest/assertions.rbs +58 -13
  295. data/stdlib/minitest/0/minitest/backtrace_filter.rbs +7 -0
  296. data/stdlib/minitest/0/minitest/bench_spec.rbs +8 -8
  297. data/stdlib/minitest/0/minitest/benchmark.rbs +17 -16
  298. data/stdlib/minitest/0/minitest/compress.rbs +13 -0
  299. data/stdlib/minitest/0/minitest/error_on_warning.rbs +3 -0
  300. data/stdlib/minitest/0/minitest/mock.rbs +9 -5
  301. data/stdlib/minitest/0/minitest/parallel/executor.rbs +4 -0
  302. data/stdlib/minitest/0/minitest/parallel/test/class_methods.rbs +0 -1
  303. data/stdlib/minitest/0/minitest/pride_io.rbs +8 -0
  304. data/stdlib/minitest/0/minitest/pride_lol.rbs +2 -0
  305. data/stdlib/minitest/0/minitest/progress_reporter.rbs +1 -1
  306. data/stdlib/minitest/0/minitest/reportable.rbs +2 -0
  307. data/stdlib/minitest/0/minitest/runnable.rbs +33 -1
  308. data/stdlib/minitest/0/minitest/spec/dsl/instance_methods.rbs +1 -1
  309. data/stdlib/minitest/0/minitest/spec/dsl.rbs +10 -6
  310. data/stdlib/minitest/0/minitest/spec.rbs +1 -1
  311. data/stdlib/minitest/0/minitest/statistics_reporter.rbs +5 -0
  312. data/stdlib/minitest/0/minitest/summary_reporter.rbs +0 -7
  313. data/stdlib/minitest/0/minitest/test/lifecycle_hooks.rbs +7 -7
  314. data/stdlib/minitest/0/minitest/test.rbs +7 -14
  315. data/stdlib/minitest/0/minitest/unexpected_error.rbs +2 -0
  316. data/stdlib/minitest/0/minitest/unexpected_warning.rbs +6 -0
  317. data/stdlib/minitest/0/minitest/unit.rbs +1 -2
  318. data/stdlib/minitest/0/minitest.rbs +41 -892
  319. data/stdlib/monitor/0/monitor.rbs +91 -10
  320. data/stdlib/mutex_m/0/mutex_m.rbs +0 -2
  321. data/stdlib/net-http/0/manifest.yaml +1 -1
  322. data/stdlib/net-http/0/net-http.rbs +3858 -964
  323. data/stdlib/net-protocol/0/manifest.yaml +2 -0
  324. data/stdlib/net-protocol/0/net-protocol.rbs +56 -0
  325. data/stdlib/net-smtp/0/manifest.yaml +2 -0
  326. data/stdlib/net-smtp/0/net-smtp.rbs +55 -0
  327. data/stdlib/nkf/0/nkf.rbs +35 -5
  328. data/stdlib/objspace/0/objspace.rbs +40 -18
  329. data/stdlib/observable/0/observable.rbs +217 -0
  330. data/stdlib/open-uri/0/manifest.yaml +4 -0
  331. data/stdlib/open-uri/0/open-uri.rbs +393 -0
  332. data/stdlib/open3/0/open3.rbs +147 -0
  333. data/stdlib/openssl/0/manifest.yaml +1 -0
  334. data/stdlib/openssl/0/openssl.rbs +681 -316
  335. data/stdlib/optparse/0/optparse.rbs +100 -65
  336. data/stdlib/pathname/0/pathname.rbs +24 -15
  337. data/stdlib/pp/0/manifest.yaml +2 -0
  338. data/stdlib/pp/0/pp.rbs +300 -0
  339. data/stdlib/prettyprint/0/prettyprint.rbs +2 -6
  340. data/stdlib/pstore/0/pstore.rbs +370 -156
  341. data/stdlib/psych/0/core_ext.rbs +12 -0
  342. data/stdlib/{yaml → psych}/0/dbm.rbs +3 -3
  343. data/stdlib/psych/0/manifest.yaml +3 -0
  344. data/stdlib/psych/0/psych.rbs +402 -0
  345. data/stdlib/{yaml → psych}/0/store.rbs +2 -2
  346. data/stdlib/pty/0/pty.rbs +63 -11
  347. data/stdlib/rdoc/0/code_object.rbs +51 -0
  348. data/stdlib/rdoc/0/comment.rbs +59 -0
  349. data/stdlib/rdoc/0/context.rbs +153 -0
  350. data/stdlib/rdoc/0/markup.rbs +117 -0
  351. data/stdlib/rdoc/0/parser.rbs +56 -0
  352. data/stdlib/rdoc/0/rdoc.rbs +13 -380
  353. data/stdlib/rdoc/0/ri.rbs +17 -0
  354. data/stdlib/rdoc/0/store.rbs +48 -0
  355. data/stdlib/rdoc/0/top_level.rbs +97 -0
  356. data/stdlib/resolv/0/resolv.rbs +16 -79
  357. data/stdlib/ripper/0/ripper.rbs +1648 -0
  358. data/stdlib/securerandom/0/securerandom.rbs +7 -2
  359. data/stdlib/shellwords/0/shellwords.rbs +11 -12
  360. data/stdlib/singleton/0/singleton.rbs +0 -3
  361. data/stdlib/socket/0/addrinfo.rbs +13 -18
  362. data/stdlib/socket/0/basic_socket.rbs +5 -10
  363. data/stdlib/socket/0/ip_socket.rbs +0 -2
  364. data/stdlib/socket/0/socket.rbs +77 -46
  365. data/stdlib/socket/0/tcp_server.rbs +0 -5
  366. data/stdlib/socket/0/tcp_socket.rbs +36 -3
  367. data/stdlib/socket/0/udp_socket.rbs +4 -5
  368. data/stdlib/socket/0/unix_server.rbs +0 -5
  369. data/stdlib/socket/0/unix_socket.rbs +2 -4
  370. data/{core/string_io.rbs → stdlib/stringio/0/stringio.rbs} +188 -107
  371. data/stdlib/strscan/0/string_scanner.rbs +1269 -425
  372. data/stdlib/tempfile/0/tempfile.rbs +224 -61
  373. data/stdlib/time/0/time.rbs +48 -35
  374. data/stdlib/timeout/0/timeout.rbs +17 -8
  375. data/stdlib/tmpdir/0/tmpdir.rbs +10 -3
  376. data/stdlib/tsort/0/tsort.rbs +0 -4
  377. data/stdlib/uri/0/common.rbs +271 -144
  378. data/stdlib/uri/0/file.rbs +5 -0
  379. data/stdlib/uri/0/ftp.rbs +1 -1
  380. data/stdlib/uri/0/generic.rbs +26 -22
  381. data/stdlib/uri/0/http.rbs +4 -4
  382. data/stdlib/uri/0/ldap.rbs +1 -1
  383. data/stdlib/uri/0/mailto.rbs +84 -0
  384. data/stdlib/uri/0/rfc2396_parser.rbs +3 -0
  385. data/stdlib/yaml/0/manifest.yaml +1 -2
  386. data/stdlib/yaml/0/yaml.rbs +1 -199
  387. data/stdlib/zlib/0/buf_error.rbs +10 -0
  388. data/stdlib/zlib/0/data_error.rbs +10 -0
  389. data/stdlib/zlib/0/deflate.rbs +210 -0
  390. data/stdlib/zlib/0/error.rbs +20 -0
  391. data/stdlib/zlib/0/gzip_file/crc_error.rbs +12 -0
  392. data/stdlib/zlib/0/gzip_file/error.rbs +23 -0
  393. data/stdlib/zlib/0/gzip_file/length_error.rbs +12 -0
  394. data/stdlib/zlib/0/gzip_file/no_footer.rbs +11 -0
  395. data/stdlib/zlib/0/gzip_file.rbs +156 -0
  396. data/stdlib/zlib/0/gzip_reader.rbs +293 -0
  397. data/stdlib/zlib/0/gzip_writer.rbs +166 -0
  398. data/stdlib/zlib/0/inflate.rbs +180 -0
  399. data/stdlib/zlib/0/mem_error.rbs +10 -0
  400. data/stdlib/zlib/0/need_dict.rbs +13 -0
  401. data/stdlib/zlib/0/stream_end.rbs +11 -0
  402. data/stdlib/zlib/0/stream_error.rbs +11 -0
  403. data/stdlib/zlib/0/version_error.rbs +11 -0
  404. data/stdlib/zlib/0/zlib.rbs +1 -3
  405. data/stdlib/zlib/0/zstream.rbs +200 -0
  406. data/templates/include/rbs/constants.h.erb +20 -0
  407. data/templates/include/rbs/ruby_objs.h.erb +10 -0
  408. data/templates/src/constants.c.erb +36 -0
  409. data/templates/src/ruby_objs.c.erb +27 -0
  410. data/templates/template.rb +122 -0
  411. metadata +136 -36
  412. data/Gemfile +0 -33
  413. data/Gemfile.lock +0 -118
  414. data/core/deprecated.rbs +0 -9
  415. data/ext/rbs_extension/constants.c +0 -135
  416. data/ext/rbs_extension/ruby_objs.c +0 -525
  417. data/ext/rbs_extension/ruby_objs.h +0 -43
  418. data/lib/rbs/constant_table.rb +0 -167
  419. data/lib/rbs/parser_compat/lexer_error.rb +0 -6
  420. data/lib/rbs/parser_compat/located_value.rb +0 -7
  421. data/lib/rbs/parser_compat/semantics_error.rb +0 -6
  422. data/lib/rbs/parser_compat/syntax_error.rb +0 -6
  423. data/lib/rbs/test/spy.rb +0 -6
  424. data/lib/rbs/type_name_resolver.rb +0 -67
  425. data/sig/constant_table.rbs +0 -30
  426. data/sig/shims/abstract_syntax_tree.rbs +0 -25
  427. data/sig/shims/pp.rbs +0 -3
  428. data/sig/shims/ripper.rbs +0 -8
  429. data/sig/shims.rbs +0 -69
  430. data/sig/type_name_resolver.rbs +0 -26
  431. data/stdlib/minitest/0/manifest.yaml +0 -2
  432. data/stdlib/prime/0/integer-extension.rbs +0 -41
  433. data/stdlib/prime/0/manifest.yaml +0 -2
  434. data/stdlib/prime/0/prime.rbs +0 -372
@@ -37,6 +37,9 @@
37
37
  case kPUBLIC: \
38
38
  case kPRIVATE: \
39
39
  case kUNTYPED: \
40
+ case kUSE: \
41
+ case kAS: \
42
+ case k__TODO__: \
40
43
  /* nop */
41
44
 
42
45
  typedef struct {
@@ -49,6 +52,18 @@ typedef struct {
49
52
  VALUE rest_keywords;
50
53
  } method_params;
51
54
 
55
+ static VALUE EMPTY_ARRAY;
56
+
57
+ static inline void melt_array(VALUE *array) {
58
+ if (*array == EMPTY_ARRAY) {
59
+ *array = rb_ary_new();
60
+ }
61
+ }
62
+
63
+ static bool rbs_is_untyped_params(method_params *params) {
64
+ return NIL_P(params->required_positionals);
65
+ }
66
+
52
67
  // /**
53
68
  // * Returns RBS::Location object of `current_token` of a parser state.
54
69
  // *
@@ -75,7 +90,7 @@ static VALUE string_of_loc(parserstate *state, position start, position end) {
75
90
  }
76
91
 
77
92
  /**
78
- * Raises RuntimeError with "Unexpected error " messsage.
93
+ * Raises RuntimeError with "Unexpected error " message.
79
94
  * */
80
95
  static NORETURN(void) rbs_abort(void) {
81
96
  rb_raise(
@@ -128,10 +143,9 @@ void parser_advance_no_gap(parserstate *state) {
128
143
  | {(tUIDENT `::`)*} <tXIDENT>
129
144
  | {<tXIDENT>}
130
145
  */
131
- VALUE parse_type_name(parserstate *state, TypeNameKind kind, range *rg) {
146
+ static VALUE parse_type_name(parserstate *state, TypeNameKind kind, range *rg) {
132
147
  VALUE absolute = Qfalse;
133
- VALUE path = rb_ary_new();
134
- VALUE namespace;
148
+ VALUE path = EMPTY_ARRAY;
135
149
 
136
150
  if (rg) {
137
151
  rg->start = state->current_token.range.start;
@@ -148,12 +162,14 @@ VALUE parse_type_name(parserstate *state, TypeNameKind kind, range *rg) {
148
162
  && state->current_token.range.end.byte_pos == state->next_token.range.start.byte_pos
149
163
  && state->next_token.range.end.byte_pos == state->next_token2.range.start.byte_pos
150
164
  ) {
165
+ melt_array(&path);
151
166
  rb_ary_push(path, ID2SYM(INTERN_TOKEN(state, state->current_token)));
152
167
 
153
168
  parser_advance(state);
154
169
  parser_advance(state);
155
170
  }
156
- namespace = rbs_namespace(path, absolute);
171
+
172
+ VALUE namespace = rbs_namespace(path, absolute);
157
173
 
158
174
  switch (state->current_token.type) {
159
175
  case tLIDENT:
@@ -204,9 +220,10 @@ VALUE parse_type_name(parserstate *state, TypeNameKind kind, range *rg) {
204
220
  type_list ::= {} type `,` ... <`,`> eol
205
221
  | {} type `,` ... `,` <type> eol
206
222
  */
207
- static VALUE parse_type_list(parserstate *state, enum TokenType eol, VALUE types) {
223
+ static void parse_type_list(parserstate *state, enum TokenType eol, VALUE *types) {
208
224
  while (true) {
209
- rb_ary_push(types, parse_type(state));
225
+ melt_array(types);
226
+ rb_ary_push(*types, parse_type(state));
210
227
 
211
228
  if (state->next_token.type == pCOMMA) {
212
229
  parser_advance(state);
@@ -226,8 +243,6 @@ static VALUE parse_type_list(parserstate *state, enum TokenType eol, VALUE types
226
243
  }
227
244
  }
228
245
  }
229
-
230
- return types;
231
246
  }
232
247
 
233
248
  static bool is_keyword_token(enum TokenType type) {
@@ -252,7 +267,6 @@ static bool is_keyword_token(enum TokenType type) {
252
267
  */
253
268
  static VALUE parse_function_param(parserstate *state) {
254
269
  range type_range;
255
-
256
270
  type_range.start = state->next_token.range.start;
257
271
  VALUE type = parse_type(state);
258
272
  type_range.end = state->current_token.range.end;
@@ -262,20 +276,32 @@ static VALUE parse_function_param(parserstate *state) {
262
276
 
263
277
  VALUE location = rbs_new_location(state->buffer, param_range);
264
278
  rbs_loc *loc = rbs_check_location(location);
279
+ rbs_loc_alloc_children(loc, 1);
265
280
  rbs_loc_add_optional_child(loc, rb_intern("name"), NULL_RANGE);
266
281
 
267
282
  return rbs_function_param(type, Qnil, location);
268
283
  } else {
269
284
  range name_range = state->next_token.range;
270
- range param_range;
271
285
 
272
286
  parser_advance(state);
273
- param_range.start = type_range.start;
274
- param_range.end = name_range.end;
287
+
288
+ range param_range = {
289
+ .start = type_range.start,
290
+ .end = name_range.end,
291
+ };
292
+
293
+ if (!is_keyword_token(state->current_token.type)) {
294
+ raise_syntax_error(
295
+ state,
296
+ state->current_token,
297
+ "unexpected token for function parameter name"
298
+ );
299
+ }
275
300
 
276
301
  VALUE name = rb_to_symbol(rbs_unquote_string(state, state->current_token.range, 0));
277
302
  VALUE location = rbs_new_location(state->buffer, param_range);
278
303
  rbs_loc *loc = rbs_check_location(location);
304
+ rbs_loc_alloc_children(loc, 1);
279
305
  rbs_loc_add_optional_child(loc, rb_intern("name"), name_range);
280
306
 
281
307
  return rbs_function_param(type, name, location);
@@ -295,30 +321,35 @@ static ID intern_token_start_end(parserstate *state, token start_token, token en
295
321
  | {} keyword <`?`> `:`
296
322
  */
297
323
  static VALUE parse_keyword_key(parserstate *state) {
298
- VALUE key;
299
-
300
324
  parser_advance(state);
301
325
 
302
326
  if (state->next_token.type == pQUESTION) {
303
- key = ID2SYM(intern_token_start_end(state, state->current_token, state->next_token));
327
+ VALUE key = ID2SYM(intern_token_start_end(state, state->current_token, state->next_token));
304
328
  parser_advance(state);
329
+ return key;
305
330
  } else {
306
- key = ID2SYM(INTERN_TOKEN(state, state->current_token));
331
+ return ID2SYM(INTERN_TOKEN(state, state->current_token));
307
332
  }
308
-
309
- return key;
310
333
  }
311
334
 
312
335
  /*
313
336
  keyword ::= {} keyword `:` <function_param>
314
337
  */
315
- static void parse_keyword(parserstate *state, VALUE keywords) {
316
- VALUE key;
317
- VALUE param;
338
+ static void parse_keyword(parserstate *state, VALUE keywords, VALUE memo) {
339
+ VALUE key = parse_keyword_key(state);
340
+
341
+ if (!NIL_P(rb_hash_aref(memo, key))) {
342
+ raise_syntax_error(
343
+ state,
344
+ state->current_token,
345
+ "duplicated keyword argument"
346
+ );
347
+ } else {
348
+ rb_hash_aset(memo, key, Qtrue);
349
+ }
318
350
 
319
- key = parse_keyword_key(state);
320
351
  parser_advance_assert(state, pCOLON);
321
- param = parse_function_param(state);
352
+ VALUE param = parse_function_param(state);
322
353
 
323
354
  rb_hash_aset(keywords, key, param);
324
355
 
@@ -349,6 +380,7 @@ static bool is_keyword(parserstate *state) {
349
380
 
350
381
  /*
351
382
  params ::= {} `)`
383
+ | {} `?` `)` -- Untyped function params (assign params.required = nil)
352
384
  | <required_params> `)`
353
385
  | <required_params> `,` `)`
354
386
 
@@ -376,10 +408,17 @@ static bool is_keyword(parserstate *state) {
376
408
  | {} `**` <function_param>
377
409
  */
378
410
  static void parse_params(parserstate *state, method_params *params) {
411
+ if (state->next_token.type == pQUESTION && state->next_token2.type == pRPAREN) {
412
+ params->required_positionals = Qnil;
413
+ parser_advance(state);
414
+ return;
415
+ }
379
416
  if (state->next_token.type == pRPAREN) {
380
417
  return;
381
418
  }
382
419
 
420
+ VALUE memo = rb_hash_new();
421
+
383
422
  while (true) {
384
423
  VALUE param;
385
424
 
@@ -399,6 +438,7 @@ static void parse_params(parserstate *state, method_params *params) {
399
438
  }
400
439
 
401
440
  param = parse_function_param(state);
441
+ melt_array(&params->required_positionals);
402
442
  rb_ary_push(params->required_positionals, param);
403
443
 
404
444
  break;
@@ -418,12 +458,13 @@ PARSE_OPTIONAL_PARAMS:
418
458
  parser_advance(state);
419
459
 
420
460
  if (is_keyword(state)) {
421
- parse_keyword(state, params->optional_keywords);
461
+ parse_keyword(state, params->optional_keywords, memo);
422
462
  parser_advance_if(state, pCOMMA);
423
463
  goto PARSE_KEYWORDS;
424
464
  }
425
465
 
426
466
  param = parse_function_param(state);
467
+ melt_array(&params->optional_positionals);
427
468
  rb_ary_push(params->optional_positionals, param);
428
469
 
429
470
  break;
@@ -467,6 +508,7 @@ PARSE_TRAILING_PARAMS:
467
508
  }
468
509
 
469
510
  param = parse_function_param(state);
511
+ melt_array(&params->trailing_positionals);
470
512
  rb_ary_push(params->trailing_positionals, param);
471
513
 
472
514
  break;
@@ -483,7 +525,7 @@ PARSE_KEYWORDS:
483
525
  case pQUESTION:
484
526
  parser_advance(state);
485
527
  if (is_keyword(state)) {
486
- parse_keyword(state, params->optional_keywords);
528
+ parse_keyword(state, params->optional_keywords, memo);
487
529
  } else {
488
530
  raise_syntax_error(
489
531
  state,
@@ -500,12 +542,13 @@ PARSE_KEYWORDS:
500
542
 
501
543
  case tUIDENT:
502
544
  case tLIDENT:
545
+ case tQIDENT:
503
546
  case tULIDENT:
504
547
  case tULLIDENT:
505
548
  case tBANGIDENT:
506
549
  KEYWORD_CASES
507
550
  if (is_keyword(state)) {
508
- parse_keyword(state, params->required_keywords);
551
+ parse_keyword(state, params->required_keywords, memo);
509
552
  } else {
510
553
  raise_syntax_error(
511
554
  state,
@@ -543,6 +586,7 @@ EOP:
543
586
  static VALUE parse_optional(parserstate *state) {
544
587
  range rg;
545
588
  rg.start = state->next_token.range.start;
589
+
546
590
  VALUE type = parse_simple(state);
547
591
 
548
592
  if (state->next_token.type == pQUESTION) {
@@ -556,13 +600,15 @@ static VALUE parse_optional(parserstate *state) {
556
600
  }
557
601
 
558
602
  static void initialize_method_params(method_params *params){
559
- params->required_positionals = rb_ary_new();
560
- params->optional_positionals = rb_ary_new();
561
- params->rest_positionals = Qnil;
562
- params->trailing_positionals = rb_ary_new();
563
- params->required_keywords = rb_hash_new();
564
- params->optional_keywords = rb_hash_new();
565
- params->rest_keywords = Qnil;
603
+ *params = (method_params) {
604
+ .required_positionals = EMPTY_ARRAY,
605
+ .optional_positionals = EMPTY_ARRAY,
606
+ .rest_positionals = Qnil,
607
+ .trailing_positionals = EMPTY_ARRAY,
608
+ .required_keywords = rb_hash_new(),
609
+ .optional_keywords = rb_hash_new(),
610
+ .rest_keywords = Qnil,
611
+ };
566
612
  }
567
613
 
568
614
  /*
@@ -599,6 +645,13 @@ static void parse_function(parserstate *state, VALUE *function, VALUE *block, VA
599
645
  parser_advance_assert(state, pRPAREN);
600
646
  }
601
647
 
648
+ // Untyped method parameter means it cannot have block
649
+ if (rbs_is_untyped_params(&params)) {
650
+ if (state->next_token.type != pARROW) {
651
+ raise_syntax_error(state, state->next_token2, "A method type with untyped method parameter cannot have block");
652
+ }
653
+ }
654
+
602
655
  // Passing NULL to function_self_type means the function itself doesn't accept self type binding. (== method type)
603
656
  if (function_self_type) {
604
657
  *function_self_type = parse_self_type_binding(state);
@@ -627,8 +680,11 @@ static void parse_function(parserstate *state, VALUE *function, VALUE *block, VA
627
680
  parser_advance_assert(state, pARROW);
628
681
  VALUE block_return_type = parse_optional(state);
629
682
 
630
- *block = rbs_block(
631
- rbs_function(
683
+ VALUE block_function = Qnil;
684
+ if (rbs_is_untyped_params(&block_params)) {
685
+ block_function = rbs_untyped_function(block_return_type);
686
+ } else {
687
+ block_function = rbs_function(
632
688
  block_params.required_positionals,
633
689
  block_params.optional_positionals,
634
690
  block_params.rest_positionals,
@@ -637,10 +693,10 @@ static void parse_function(parserstate *state, VALUE *function, VALUE *block, VA
637
693
  block_params.optional_keywords,
638
694
  block_params.rest_keywords,
639
695
  block_return_type
640
- ),
641
- required,
642
- block_self_type
643
- );
696
+ );
697
+ }
698
+
699
+ *block = rbs_block(block_function, required, block_self_type);
644
700
 
645
701
  parser_advance_assert(state, pRBRACE);
646
702
  }
@@ -648,16 +704,20 @@ static void parse_function(parserstate *state, VALUE *function, VALUE *block, VA
648
704
  parser_advance_assert(state, pARROW);
649
705
  VALUE type = parse_optional(state);
650
706
 
651
- *function = rbs_function(
652
- params.required_positionals,
653
- params.optional_positionals,
654
- params.rest_positionals,
655
- params.trailing_positionals,
656
- params.required_keywords,
657
- params.optional_keywords,
658
- params.rest_keywords,
659
- type
660
- );
707
+ if (rbs_is_untyped_params(&params)) {
708
+ *function = rbs_untyped_function(type);
709
+ } else {
710
+ *function = rbs_function(
711
+ params.required_positionals,
712
+ params.optional_positionals,
713
+ params.rest_positionals,
714
+ params.trailing_positionals,
715
+ params.required_keywords,
716
+ params.optional_keywords,
717
+ params.rest_keywords,
718
+ type
719
+ );
720
+ }
661
721
  }
662
722
 
663
723
  /*
@@ -675,6 +735,16 @@ static VALUE parse_proc_type(parserstate *state) {
675
735
  return rbs_proc(function, block, loc, proc_self);
676
736
  }
677
737
 
738
+ static void check_key_duplication(parserstate *state, VALUE fields, VALUE key) {
739
+ if (!NIL_P(rb_hash_aref(fields, key))) {
740
+ raise_syntax_error(
741
+ state,
742
+ state->current_token,
743
+ "duplicated record key"
744
+ );
745
+ }
746
+ }
747
+
678
748
  /**
679
749
  * ... `{` ... `}` ...
680
750
  * > >
@@ -685,20 +755,28 @@ static VALUE parse_proc_type(parserstate *state) {
685
755
  record_attribute ::= {} keyword_token `:` <type>
686
756
  | {} literal_type `=>` <type>
687
757
  */
688
- VALUE parse_record_attributes(parserstate *state) {
689
- VALUE hash = rb_hash_new();
758
+ static VALUE parse_record_attributes(parserstate *state) {
759
+ VALUE fields = rb_hash_new();
690
760
 
691
761
  if (state->next_token.type == pRBRACE) {
692
- return hash;
762
+ return fields;
693
763
  }
694
764
 
695
765
  while (true) {
696
- VALUE key;
697
- VALUE type;
766
+ VALUE key, type,
767
+ value = rb_ary_new(),
768
+ required = Qtrue;
769
+
770
+ if (state->next_token.type == pQUESTION) {
771
+ // { ?foo: type } syntax
772
+ required = Qfalse;
773
+ parser_advance(state);
774
+ }
698
775
 
699
776
  if (is_keyword(state)) {
700
777
  // { foo: type } syntax
701
778
  key = parse_keyword_key(state);
779
+ check_key_duplication(state, fields, key);
702
780
  parser_advance_assert(state, pCOLON);
703
781
  } else {
704
782
  // { key => type } syntax
@@ -710,9 +788,10 @@ VALUE parse_record_attributes(parserstate *state) {
710
788
  case tDQSTRING:
711
789
  case tINTEGER:
712
790
  case kTRUE:
713
- case kFALSE:
714
- key = rb_funcall(parse_type(state), rb_intern("literal"), 0);
791
+ case kFALSE: {
792
+ key = rb_funcall(parse_simple(state), rb_intern("literal"), 0);
715
793
  break;
794
+ }
716
795
  default:
717
796
  raise_syntax_error(
718
797
  state,
@@ -720,10 +799,13 @@ VALUE parse_record_attributes(parserstate *state) {
720
799
  "unexpected record key token"
721
800
  );
722
801
  }
802
+ check_key_duplication(state, fields, key);
723
803
  parser_advance_assert(state, pFATARROW);
724
804
  }
725
805
  type = parse_type(state);
726
- rb_hash_aset(hash, key, type);
806
+ rb_ary_push(value, type);
807
+ rb_ary_push(value, required);
808
+ rb_hash_aset(fields, key, value);
727
809
 
728
810
  if (parser_advance_if(state, pCOMMA)) {
729
811
  if (state->next_token.type == pRBRACE) {
@@ -733,8 +815,7 @@ VALUE parse_record_attributes(parserstate *state) {
733
815
  break;
734
816
  }
735
817
  }
736
-
737
- return hash;
818
+ return fields;
738
819
  }
739
820
 
740
821
  /*
@@ -782,17 +863,14 @@ static VALUE parse_symbol(parserstate *state) {
782
863
  | {} `[` type_list <`]`>
783
864
  */
784
865
  static VALUE parse_instance_type(parserstate *state, bool parse_alias) {
785
- range name_range;
786
- range args_range;
787
- range type_range;
788
-
789
866
  TypeNameKind expected_kind = INTERFACE_NAME | CLASS_NAME;
790
867
  if (parse_alias) {
791
868
  expected_kind |= ALIAS_NAME;
792
869
  }
793
870
 
871
+ range name_range;
794
872
  VALUE typename = parse_type_name(state, expected_kind, &name_range);
795
- VALUE types = rb_ary_new();
873
+ VALUE types = EMPTY_ARRAY;
796
874
 
797
875
  TypeNameKind kind;
798
876
  if (state->current_token.type == tUIDENT) {
@@ -805,21 +883,25 @@ static VALUE parse_instance_type(parserstate *state, bool parse_alias) {
805
883
  rbs_abort();
806
884
  }
807
885
 
886
+ range args_range;
808
887
  if (state->next_token.type == pLBRACKET) {
809
888
  parser_advance(state);
810
889
  args_range.start = state->current_token.range.start;
811
- parse_type_list(state, pRBRACKET, types);
890
+ parse_type_list(state, pRBRACKET, &types);
812
891
  parser_advance_assert(state, pRBRACKET);
813
892
  args_range.end = state->current_token.range.end;
814
893
  } else {
815
894
  args_range = NULL_RANGE;
816
895
  }
817
896
 
818
- type_range.start = name_range.start;
819
- type_range.end = nonnull_pos_or(args_range.end, name_range.end);
897
+ range type_range = {
898
+ .start = name_range.start,
899
+ .end = nonnull_pos_or(args_range.end, name_range.end),
900
+ };
820
901
 
821
902
  VALUE location = rbs_new_location(state->buffer, type_range);
822
903
  rbs_loc *loc = rbs_check_location(location);
904
+ rbs_loc_alloc_children(loc, 2);
823
905
  rbs_loc_add_required_child(loc, rb_intern("name"), name_range);
824
906
  rbs_loc_add_optional_child(loc, rb_intern("args"), args_range);
825
907
 
@@ -838,15 +920,14 @@ static VALUE parse_instance_type(parserstate *state, bool parse_alias) {
838
920
  singleton_type ::= {`singleton`} `(` type_name <`)`>
839
921
  */
840
922
  static VALUE parse_singleton_type(parserstate *state) {
841
- range name_range;
842
- range type_range;
843
-
844
923
  parser_assert(state, kSINGLETON);
845
924
 
925
+ range type_range;
846
926
  type_range.start = state->current_token.range.start;
847
927
  parser_advance_assert(state, pLPAREN);
848
928
  parser_advance(state);
849
929
 
930
+ range name_range;
850
931
  VALUE typename = parse_type_name(state, CLASS_NAME, &name_range);
851
932
 
852
933
  parser_advance_assert(state, pRPAREN);
@@ -854,6 +935,7 @@ static VALUE parse_singleton_type(parserstate *state) {
854
935
 
855
936
  VALUE location = rbs_new_location(state->buffer, type_range);
856
937
  rbs_loc *loc = rbs_check_location(location);
938
+ rbs_loc_alloc_children(loc, 1);
857
939
  rbs_loc_add_required_child(loc, rb_intern("name"), name_range);
858
940
 
859
941
  return rbs_class_singleton(typename, location);
@@ -878,24 +960,38 @@ static VALUE parse_simple(parserstate *state) {
878
960
  parser_advance_assert(state, pRPAREN);
879
961
  return type;
880
962
  }
881
- case kBOOL:
882
- return rbs_base_type(RBS_Types_Bases_Bool, rbs_location_current_token(state));
883
- case kBOT:
884
- return rbs_base_type(RBS_Types_Bases_Bottom, rbs_location_current_token(state));
885
- case kCLASS:
886
- return rbs_base_type(RBS_Types_Bases_Class, rbs_location_current_token(state));
887
- case kINSTANCE:
888
- return rbs_base_type(RBS_Types_Bases_Instance, rbs_location_current_token(state));
889
- case kNIL:
890
- return rbs_base_type(RBS_Types_Bases_Nil, rbs_location_current_token(state));
891
- case kSELF:
892
- return rbs_base_type(RBS_Types_Bases_Self, rbs_location_current_token(state));
893
- case kTOP:
894
- return rbs_base_type(RBS_Types_Bases_Top, rbs_location_current_token(state));
895
- case kVOID:
896
- return rbs_base_type(RBS_Types_Bases_Void, rbs_location_current_token(state));
897
- case kUNTYPED:
898
- return rbs_base_type(RBS_Types_Bases_Any, rbs_location_current_token(state));
963
+ case kBOOL: {
964
+ return rbs_bases_bool(rbs_location_current_token(state));
965
+ }
966
+ case kBOT: {
967
+ return rbs_bases_bottom(rbs_location_current_token(state));
968
+ }
969
+ case kCLASS: {
970
+ return rbs_bases_class(rbs_location_current_token(state));
971
+ }
972
+ case kINSTANCE: {
973
+ return rbs_bases_instance(rbs_location_current_token(state));
974
+ }
975
+ case kNIL: {
976
+ return rbs_bases_nil(rbs_location_current_token(state));
977
+ }
978
+ case kSELF: {
979
+ return rbs_bases_self(rbs_location_current_token(state));
980
+ }
981
+ case kTOP: {
982
+ return rbs_bases_top(rbs_location_current_token(state));
983
+ }
984
+ case kVOID: {
985
+ return rbs_bases_void(rbs_location_current_token(state));
986
+ }
987
+ case kUNTYPED: {
988
+ return rbs_bases_any(rbs_location_current_token(state));
989
+ }
990
+ case k__TODO__: {
991
+ VALUE type = rbs_bases_any(rbs_location_current_token(state));
992
+ rb_funcall(type, rb_intern("todo!"), 0);
993
+ return type;
994
+ }
899
995
  case tINTEGER: {
900
996
  VALUE literal = rb_funcall(
901
997
  string_of_loc(state, state->current_token.range.start, state->current_token.range.end),
@@ -907,10 +1003,12 @@ static VALUE parse_simple(parserstate *state) {
907
1003
  rbs_location_current_token(state)
908
1004
  );
909
1005
  }
910
- case kTRUE:
1006
+ case kTRUE: {
911
1007
  return rbs_literal(Qtrue, rbs_location_current_token(state));
912
- case kFALSE:
1008
+ }
1009
+ case kFALSE: {
913
1010
  return rbs_literal(Qfalse, rbs_location_current_token(state));
1011
+ }
914
1012
  case tSQSTRING:
915
1013
  case tDQSTRING: {
916
1014
  VALUE literal = rbs_unquote_string(state, state->current_token.range, 0);
@@ -933,16 +1031,18 @@ static VALUE parse_simple(parserstate *state) {
933
1031
  }
934
1032
  case tULIDENT: // fallthrough
935
1033
  case tLIDENT: // fallthrough
936
- case pCOLON2:
1034
+ case pCOLON2: {
937
1035
  return parse_instance_type(state, true);
938
- case kSINGLETON:
1036
+ }
1037
+ case kSINGLETON: {
939
1038
  return parse_singleton_type(state);
1039
+ }
940
1040
  case pLBRACKET: {
941
1041
  range rg;
942
1042
  rg.start = state->current_token.range.start;
943
- VALUE types = rb_ary_new();
1043
+ VALUE types = EMPTY_ARRAY;
944
1044
  if (state->next_token.type != pRBRACKET) {
945
- parse_type_list(state, pRBRACKET, types);
1045
+ parse_type_list(state, pRBRACKET, &types);
946
1046
  }
947
1047
  parser_advance_assert(state, pRBRACKET);
948
1048
  rg.end = state->current_token.range.end;
@@ -950,7 +1050,7 @@ static VALUE parse_simple(parserstate *state) {
950
1050
  return rbs_tuple(types, rbs_new_location(state->buffer, rg));
951
1051
  }
952
1052
  case pAREF_OPR: {
953
- return rbs_tuple(rb_ary_new(), rbs_new_location(state->buffer, state->current_token.range));
1053
+ return rbs_tuple(EMPTY_ARRAY, rbs_new_location(state->buffer, state->current_token.range));
954
1054
  }
955
1055
  case pLBRACE: {
956
1056
  position start = state->current_token.range.start;
@@ -978,8 +1078,8 @@ static VALUE parse_simple(parserstate *state) {
978
1078
  */
979
1079
  static VALUE parse_intersection(parserstate *state) {
980
1080
  range rg;
981
-
982
1081
  rg.start = state->next_token.range.start;
1082
+
983
1083
  VALUE type = parse_optional(state);
984
1084
  VALUE intersection_types = rb_ary_new();
985
1085
 
@@ -1005,8 +1105,8 @@ static VALUE parse_intersection(parserstate *state) {
1005
1105
  */
1006
1106
  VALUE parse_type(parserstate *state) {
1007
1107
  range rg;
1008
-
1009
1108
  rg.start = state->next_token.range.start;
1109
+
1010
1110
  VALUE type = parse_intersection(state);
1011
1111
  VALUE union_types = rb_ary_new();
1012
1112
 
@@ -1030,13 +1130,14 @@ VALUE parse_type(parserstate *state) {
1030
1130
  type_params ::= {} `[` type_param `,` ... <`]`>
1031
1131
  | {<>}
1032
1132
 
1033
- type_param ::= kUNCHECKED? (kIN|kOUT|) tUIDENT (module_type_params == true)
1133
+ type_param ::= kUNCHECKED? (kIN|kOUT|) tUIDENT upper_bound? default_type? (module_type_params == true)
1034
1134
 
1035
- type_param ::= tUIDENT (module_type_params == false)
1135
+ type_param ::= tUIDENT upper_bound? default_type? (module_type_params == false)
1036
1136
  */
1137
+ static VALUE parse_type_params(parserstate *state, range *rg, bool module_type_params) {
1138
+ VALUE params = EMPTY_ARRAY;
1037
1139
 
1038
- VALUE parse_type_params(parserstate *state, range *rg, bool module_type_params) {
1039
- VALUE params = rb_ary_new();
1140
+ bool required_param_allowed = true;
1040
1141
 
1041
1142
  if (state->next_token.type == pLBRACKET) {
1042
1143
  parser_advance(state);
@@ -1044,19 +1145,16 @@ VALUE parse_type_params(parserstate *state, range *rg, bool module_type_params)
1044
1145
  rg->start = state->current_token.range.start;
1045
1146
 
1046
1147
  while (true) {
1047
- VALUE name;
1048
1148
  bool unchecked = false;
1049
1149
  VALUE variance = ID2SYM(rb_intern("invariant"));
1050
1150
  VALUE upper_bound = Qnil;
1151
+ VALUE default_type = Qnil;
1051
1152
 
1052
- range param_range = NULL_RANGE;
1053
- range name_range;
1054
- range variance_range = NULL_RANGE;
1055
- range unchecked_range = NULL_RANGE;
1056
- range upper_bound_range = NULL_RANGE;
1057
-
1153
+ range param_range;
1058
1154
  param_range.start = state->next_token.range.start;
1059
1155
 
1156
+ range variance_range = NULL_RANGE;
1157
+ range unchecked_range = NULL_RANGE;
1060
1158
  if (module_type_params) {
1061
1159
  if (state->next_token.type == kUNCHECKED) {
1062
1160
  unchecked = true;
@@ -1082,22 +1180,39 @@ VALUE parse_type_params(parserstate *state, range *rg, bool module_type_params)
1082
1180
  }
1083
1181
 
1084
1182
  parser_advance_assert(state, tUIDENT);
1085
- name_range = state->current_token.range;
1183
+ range name_range = state->current_token.range;
1086
1184
 
1087
1185
  ID id = INTERN_TOKEN(state, state->current_token);
1088
- name = ID2SYM(id);
1186
+ VALUE name = ID2SYM(id);
1089
1187
 
1090
1188
  parser_insert_typevar(state, id);
1091
1189
 
1190
+ range upper_bound_range = NULL_RANGE;
1092
1191
  if (state->next_token.type == pLT) {
1093
1192
  parser_advance(state);
1193
+ upper_bound_range.start = state->current_token.range.start;
1194
+ upper_bound = parse_type(state);
1195
+ upper_bound_range.end = state->current_token.range.end;
1196
+ }
1094
1197
 
1095
- if (state->next_token.type == kSINGLETON) {
1198
+ range default_type_range = NULL_RANGE;
1199
+ if (module_type_params) {
1200
+ if (state->next_token.type == pEQ) {
1096
1201
  parser_advance(state);
1097
- upper_bound = parse_singleton_type(state);
1202
+
1203
+ default_type_range.start = state->current_token.range.start;
1204
+ default_type = parse_type(state);
1205
+ default_type_range.end = state->current_token.range.end;
1206
+
1207
+ required_param_allowed = false;
1098
1208
  } else {
1099
- parser_advance(state);
1100
- upper_bound = parse_instance_type(state, false);
1209
+ if (!required_param_allowed) {
1210
+ raise_syntax_error(
1211
+ state,
1212
+ state->current_token,
1213
+ "required type parameter is not allowed after optional type parameter"
1214
+ );
1215
+ }
1101
1216
  }
1102
1217
  }
1103
1218
 
@@ -1105,12 +1220,20 @@ VALUE parse_type_params(parserstate *state, range *rg, bool module_type_params)
1105
1220
 
1106
1221
  VALUE location = rbs_new_location(state->buffer, param_range);
1107
1222
  rbs_loc *loc = rbs_check_location(location);
1223
+ rbs_loc_alloc_children(loc, 5);
1108
1224
  rbs_loc_add_required_child(loc, rb_intern("name"), name_range);
1109
1225
  rbs_loc_add_optional_child(loc, rb_intern("variance"), variance_range);
1110
1226
  rbs_loc_add_optional_child(loc, rb_intern("unchecked"), unchecked_range);
1111
1227
  rbs_loc_add_optional_child(loc, rb_intern("upper_bound"), upper_bound_range);
1228
+ rbs_loc_add_optional_child(loc, rb_intern("default"), default_type_range);
1229
+
1230
+ VALUE param = rbs_ast_type_param(name, variance, upper_bound, default_type, location);
1231
+
1232
+ if (unchecked) {
1233
+ rb_funcall(param, rb_intern("unchecked!"), 0);
1234
+ }
1112
1235
 
1113
- VALUE param = rbs_ast_type_param(name, variance, unchecked, upper_bound, location);
1236
+ melt_array(&params);
1114
1237
  rb_ary_push(params, param);
1115
1238
 
1116
1239
  if (state->next_token.type == pCOMMA) {
@@ -1142,20 +1265,19 @@ VALUE parse_type_params(parserstate *state, range *rg, bool module_type_params)
1142
1265
  method_type ::= {} type_params <function>
1143
1266
  */
1144
1267
  VALUE parse_method_type(parserstate *state) {
1145
- range rg;
1146
- range params_range = NULL_RANGE;
1147
- range type_range;
1148
-
1149
- VALUE function = Qnil;
1150
- VALUE block = Qnil;
1151
1268
  parser_push_typevar_table(state, false);
1152
1269
 
1270
+ range rg;
1153
1271
  rg.start = state->next_token.range.start;
1154
1272
 
1273
+ range params_range = NULL_RANGE;
1155
1274
  VALUE type_params = parse_type_params(state, &params_range, false);
1156
1275
 
1276
+ range type_range;
1157
1277
  type_range.start = state->next_token.range.start;
1158
1278
 
1279
+ VALUE function = Qnil;
1280
+ VALUE block = Qnil;
1159
1281
  parse_function(state, &function, &block, NULL);
1160
1282
 
1161
1283
  rg.end = state->current_token.range.end;
@@ -1165,6 +1287,7 @@ VALUE parse_method_type(parserstate *state) {
1165
1287
 
1166
1288
  VALUE location = rbs_new_location(state->buffer, rg);
1167
1289
  rbs_loc *loc = rbs_check_location(location);
1290
+ rbs_loc_alloc_children(loc, 2);
1168
1291
  rbs_loc_add_required_child(loc, rb_intern("type"), type_range);
1169
1292
  rbs_loc_add_optional_child(loc, rb_intern("type_params"), params_range);
1170
1293
 
@@ -1179,31 +1302,23 @@ VALUE parse_method_type(parserstate *state) {
1179
1302
  /*
1180
1303
  global_decl ::= {tGIDENT} `:` <type>
1181
1304
  */
1182
- VALUE parse_global_decl(parserstate *state) {
1305
+ static VALUE parse_global_decl(parserstate *state) {
1183
1306
  range decl_range;
1184
- range name_range, colon_range;
1185
-
1186
- VALUE typename;
1187
- VALUE type;
1188
- VALUE location;
1189
- VALUE comment;
1190
-
1191
- rbs_loc *loc;
1192
-
1193
1307
  decl_range.start = state->current_token.range.start;
1194
- comment = get_comment(state, decl_range.start.line);
1195
1308
 
1196
- name_range = state->current_token.range;
1197
- typename = ID2SYM(INTERN_TOKEN(state, state->current_token));
1309
+ VALUE comment = get_comment(state, decl_range.start.line);
1310
+ range name_range = state->current_token.range;
1311
+ VALUE typename = ID2SYM(INTERN_TOKEN(state, state->current_token));
1198
1312
 
1199
1313
  parser_advance_assert(state, pCOLON);
1200
- colon_range = state->current_token.range;
1314
+ range colon_range = state->current_token.range;
1201
1315
 
1202
- type = parse_type(state);
1316
+ VALUE type = parse_type(state);
1203
1317
  decl_range.end = state->current_token.range.end;
1204
1318
 
1205
- location = rbs_new_location(state->buffer, decl_range);
1206
- loc = rbs_check_location(location);
1319
+ VALUE location = rbs_new_location(state->buffer, decl_range);
1320
+ rbs_loc *loc = rbs_check_location(location);
1321
+ rbs_loc_alloc_children(loc, 2);
1207
1322
  rbs_loc_add_required_child(loc, rb_intern("name"), name_range);
1208
1323
  rbs_loc_add_required_child(loc, rb_intern("colon"), colon_range);
1209
1324
 
@@ -1213,30 +1328,24 @@ VALUE parse_global_decl(parserstate *state) {
1213
1328
  /*
1214
1329
  const_decl ::= {const_name} `:` <type>
1215
1330
  */
1216
- VALUE parse_const_decl(parserstate *state) {
1331
+ static VALUE parse_const_decl(parserstate *state) {
1217
1332
  range decl_range;
1218
- range name_range, colon_range;
1219
-
1220
- VALUE typename;
1221
- VALUE type;
1222
- VALUE location;
1223
- VALUE comment;
1224
-
1225
- rbs_loc *loc;
1226
1333
 
1227
1334
  decl_range.start = state->current_token.range.start;
1228
- comment = get_comment(state, decl_range.start.line);
1335
+ VALUE comment = get_comment(state, decl_range.start.line);
1229
1336
 
1230
- typename = parse_type_name(state, CLASS_NAME, &name_range);
1337
+ range name_range;
1338
+ VALUE typename = parse_type_name(state, CLASS_NAME, &name_range);
1231
1339
 
1232
1340
  parser_advance_assert(state, pCOLON);
1233
- colon_range = state->current_token.range;
1341
+ range colon_range = state->current_token.range;
1234
1342
 
1235
- type = parse_type(state);
1343
+ VALUE type = parse_type(state);
1236
1344
  decl_range.end = state->current_token.range.end;
1237
1345
 
1238
- location = rbs_new_location(state->buffer, decl_range);
1239
- loc = rbs_check_location(location);
1346
+ VALUE location = rbs_new_location(state->buffer, decl_range);
1347
+ rbs_loc *loc = rbs_check_location(location);
1348
+ rbs_loc_alloc_children(loc, 2);
1240
1349
  rbs_loc_add_required_child(loc, rb_intern("name"), name_range);
1241
1350
  rbs_loc_add_required_child(loc, rb_intern("colon"), colon_range);
1242
1351
 
@@ -1246,30 +1355,32 @@ VALUE parse_const_decl(parserstate *state) {
1246
1355
  /*
1247
1356
  type_decl ::= {kTYPE} alias_name `=` <type>
1248
1357
  */
1249
- VALUE parse_type_decl(parserstate *state, position comment_pos, VALUE annotations) {
1250
- range decl_range;
1251
- range keyword_range, name_range, params_range, eq_range;
1252
-
1358
+ static VALUE parse_type_decl(parserstate *state, position comment_pos, VALUE annotations) {
1253
1359
  parser_push_typevar_table(state, true);
1254
1360
 
1361
+ range decl_range;
1255
1362
  decl_range.start = state->current_token.range.start;
1256
1363
  comment_pos = nonnull_pos_or(comment_pos, decl_range.start);
1257
1364
 
1258
- keyword_range = state->current_token.range;
1365
+ range keyword_range = state->current_token.range;
1259
1366
 
1260
1367
  parser_advance(state);
1368
+
1369
+ range name_range;
1261
1370
  VALUE typename = parse_type_name(state, ALIAS_NAME, &name_range);
1262
1371
 
1372
+ range params_range;
1263
1373
  VALUE type_params = parse_type_params(state, &params_range, true);
1264
1374
 
1265
1375
  parser_advance_assert(state, pEQ);
1266
- eq_range = state->current_token.range;
1376
+ range eq_range = state->current_token.range;
1267
1377
 
1268
1378
  VALUE type = parse_type(state);
1269
1379
  decl_range.end = state->current_token.range.end;
1270
1380
 
1271
1381
  VALUE location = rbs_new_location(state->buffer, decl_range);
1272
1382
  rbs_loc *loc = rbs_check_location(location);
1383
+ rbs_loc_alloc_children(loc, 4);
1273
1384
  rbs_loc_add_required_child(loc, rb_intern("keyword"), keyword_range);
1274
1385
  rbs_loc_add_required_child(loc, rb_intern("name"), name_range);
1275
1386
  rbs_loc_add_optional_child(loc, rb_intern("type_params"), params_range);
@@ -1277,7 +1388,7 @@ VALUE parse_type_decl(parserstate *state, position comment_pos, VALUE annotation
1277
1388
 
1278
1389
  parser_pop_typevar_table(state);
1279
1390
 
1280
- return rbs_ast_decl_alias(
1391
+ return rbs_ast_decl_type_alias(
1281
1392
  typename,
1282
1393
  type_params,
1283
1394
  type,
@@ -1290,7 +1401,7 @@ VALUE parse_type_decl(parserstate *state, position comment_pos, VALUE annotation
1290
1401
  /*
1291
1402
  annotation ::= {<tANNOTATION>}
1292
1403
  */
1293
- VALUE parse_annotation(parserstate *state) {
1404
+ static VALUE parse_annotation(parserstate *state) {
1294
1405
  VALUE content = rb_funcall(state->buffer, rb_intern("content"), 0);
1295
1406
  rb_encoding *enc = rb_enc_get(content);
1296
1407
 
@@ -1346,7 +1457,7 @@ VALUE parse_annotation(parserstate *state) {
1346
1457
  annotations ::= {} annotation ... <annotation>
1347
1458
  | {<>}
1348
1459
  */
1349
- void parse_annotations(parserstate *state, VALUE annotations, position *annot_pos) {
1460
+ static void parse_annotations(parserstate *state, VALUE *annotations, position *annot_pos) {
1350
1461
  *annot_pos = NullPosition;
1351
1462
 
1352
1463
  while (true) {
@@ -1357,7 +1468,8 @@ void parse_annotations(parserstate *state, VALUE annotations, position *annot_po
1357
1468
  *annot_pos = state->current_token.range.start;
1358
1469
  }
1359
1470
 
1360
- rb_ary_push(annotations, parse_annotation(state));
1471
+ melt_array(annotations);
1472
+ rb_ary_push(*annotations, parse_annotation(state));
1361
1473
  } else {
1362
1474
  break;
1363
1475
  }
@@ -1368,7 +1480,7 @@ void parse_annotations(parserstate *state, VALUE annotations, position *annot_po
1368
1480
  method_name ::= {} <IDENT | keyword>
1369
1481
  | {} (IDENT | keyword)~<`?`>
1370
1482
  */
1371
- VALUE parse_method_name(parserstate *state, range *range) {
1483
+ static VALUE parse_method_name(parserstate *state, range *range) {
1372
1484
  parser_advance(state);
1373
1485
 
1374
1486
  switch (state->current_token.type)
@@ -1400,8 +1512,9 @@ VALUE parse_method_name(parserstate *state, range *range) {
1400
1512
  *range = state->current_token.range;
1401
1513
  return ID2SYM(INTERN_TOKEN(state, state->current_token));
1402
1514
 
1403
- case tQIDENT:
1515
+ case tQIDENT: {
1404
1516
  return rb_to_symbol(rbs_unquote_string(state, state->current_token.range, 0));
1517
+ }
1405
1518
 
1406
1519
  case pBAR:
1407
1520
  case pHAT:
@@ -1436,7 +1549,7 @@ typedef enum {
1436
1549
 
1437
1550
  @param allow_selfq `true` to accept `self?` kind.
1438
1551
  */
1439
- InstanceSingletonKind parse_instance_singleton_kind(parserstate *state, bool allow_selfq, range *rg) {
1552
+ static InstanceSingletonKind parse_instance_singleton_kind(parserstate *state, bool allow_selfq, range *rg) {
1440
1553
  InstanceSingletonKind kind = INSTANCE_KIND;
1441
1554
 
1442
1555
  if (state->next_token.type == kSELF) {
@@ -1446,8 +1559,6 @@ InstanceSingletonKind parse_instance_singleton_kind(parserstate *state, bool all
1446
1559
  parser_advance(state);
1447
1560
  parser_advance(state);
1448
1561
  kind = SINGLETON_KIND;
1449
- rg->start = self_range.start;
1450
- rg->end = state->current_token.range.end;
1451
1562
  } else if (
1452
1563
  state->next_token2.type == pQUESTION
1453
1564
  && state->next_token.range.end.char_pos == state->next_token2.range.start.char_pos
@@ -1457,9 +1568,12 @@ InstanceSingletonKind parse_instance_singleton_kind(parserstate *state, bool all
1457
1568
  parser_advance(state);
1458
1569
  parser_advance(state);
1459
1570
  kind = INSTANCE_SINGLETON_KIND;
1460
- rg->start = self_range.start;
1461
- rg->end = state->current_token.range.end;
1462
1571
  }
1572
+
1573
+ *rg = (range) {
1574
+ .start = self_range.start,
1575
+ .end = state->current_token.range.end,
1576
+ };
1463
1577
  } else {
1464
1578
  *rg = NULL_RANGE;
1465
1579
  }
@@ -1469,8 +1583,8 @@ InstanceSingletonKind parse_instance_singleton_kind(parserstate *state, bool all
1469
1583
 
1470
1584
  /**
1471
1585
  * def_member ::= {kDEF} method_name `:` <method_types>
1472
- * | {kPRIVATE2} kDEF method_name `:` <method_types>
1473
- * | {kPUBLIC2} kDEF method_name `:` <method_types>
1586
+ * | {kPRIVATE} kDEF method_name `:` <method_types>
1587
+ * | {kPUBLIC} kDEF method_name `:` <method_types>
1474
1588
  *
1475
1589
  * method_types ::= {} <method_type>
1476
1590
  * | {} <`...`>
@@ -1479,42 +1593,40 @@ InstanceSingletonKind parse_instance_singleton_kind(parserstate *state, bool all
1479
1593
  * @param instance_only `true` to reject singleton method definition.
1480
1594
  * @param accept_overload `true` to accept overloading (...) definition.
1481
1595
  * */
1482
- VALUE parse_member_def(parserstate *state, bool instance_only, bool accept_overload, position comment_pos, VALUE annotations) {
1596
+ static VALUE parse_member_def(parserstate *state, bool instance_only, bool accept_overload, position comment_pos, VALUE annotations) {
1483
1597
  range member_range;
1484
- range visibility_range;
1485
- range keyword_range;
1486
- range name_range;
1487
- range kind_range;
1488
- range overload_range = NULL_RANGE;
1489
-
1490
- VALUE visibility;
1491
-
1492
1598
  member_range.start = state->current_token.range.start;
1493
1599
  comment_pos = nonnull_pos_or(comment_pos, member_range.start);
1600
+
1494
1601
  VALUE comment = get_comment(state, comment_pos.line);
1495
1602
 
1603
+ range visibility_range;
1604
+ VALUE visibility;
1496
1605
  switch (state->current_token.type)
1497
1606
  {
1498
- case kPRIVATE:
1607
+ case kPRIVATE: {
1499
1608
  visibility_range = state->current_token.range;
1500
1609
  visibility = ID2SYM(rb_intern("private"));
1501
1610
  member_range.start = visibility_range.start;
1502
1611
  parser_advance(state);
1503
1612
  break;
1504
- case kPUBLIC:
1613
+ }
1614
+ case kPUBLIC: {
1505
1615
  visibility_range = state->current_token.range;
1506
1616
  visibility = ID2SYM(rb_intern("public"));
1507
1617
  member_range.start = visibility_range.start;
1508
1618
  parser_advance(state);
1509
1619
  break;
1620
+ }
1510
1621
  default:
1511
1622
  visibility_range = NULL_RANGE;
1512
1623
  visibility = Qnil;
1513
1624
  break;
1514
1625
  }
1515
1626
 
1516
- keyword_range = state->current_token.range;
1627
+ range keyword_range = state->current_token.range;
1517
1628
 
1629
+ range kind_range;
1518
1630
  InstanceSingletonKind kind;
1519
1631
  if (instance_only) {
1520
1632
  kind_range = NULL_RANGE;
@@ -1523,9 +1635,10 @@ VALUE parse_member_def(parserstate *state, bool instance_only, bool accept_overl
1523
1635
  kind = parse_instance_singleton_kind(state, NIL_P(visibility), &kind_range);
1524
1636
  }
1525
1637
 
1638
+ range name_range;
1526
1639
  VALUE name = parse_method_name(state, &name_range);
1527
- VALUE method_types = rb_ary_new();
1528
- VALUE overload = Qfalse;
1640
+ VALUE overloads = rb_ary_new();
1641
+ VALUE overloading = Qfalse;
1529
1642
 
1530
1643
  if (state->next_token.type == pDOT && RB_SYM2ID(name) == rb_intern("self?")) {
1531
1644
  raise_syntax_error(
@@ -1539,25 +1652,36 @@ VALUE parse_member_def(parserstate *state, bool instance_only, bool accept_overl
1539
1652
 
1540
1653
  parser_push_typevar_table(state, kind != INSTANCE_KIND);
1541
1654
 
1655
+ range overloading_range = NULL_RANGE;
1542
1656
  bool loop = true;
1543
1657
  while (loop) {
1658
+ VALUE annotations = EMPTY_ARRAY;
1659
+ position overload_annot_pos = NullPosition;
1660
+
1661
+ if (state->next_token.type == tANNOTATION) {
1662
+ parse_annotations(state, &annotations, &overload_annot_pos);
1663
+ }
1664
+
1544
1665
  switch (state->next_token.type) {
1545
1666
  case pLPAREN:
1546
1667
  case pARROW:
1547
1668
  case pLBRACE:
1548
1669
  case pLBRACKET:
1549
1670
  case pQUESTION:
1550
- rb_ary_push(method_types, parse_method_type(state));
1551
- member_range.end = state->current_token.range.end;
1552
- break;
1671
+ {
1672
+ VALUE method_type = parse_method_type(state);
1673
+ rb_ary_push(overloads, rbs_ast_members_method_definition_overload(annotations, method_type));
1674
+ member_range.end = state->current_token.range.end;
1675
+ break;
1676
+ }
1553
1677
 
1554
1678
  case pDOT3:
1555
1679
  if (accept_overload) {
1556
- overload = Qtrue;
1680
+ overloading = Qtrue;
1557
1681
  parser_advance(state);
1558
1682
  loop = false;
1559
- overload_range = state->current_token.range;
1560
- member_range.end = overload_range.end;
1683
+ overloading_range = state->current_token.range;
1684
+ member_range.end = overloading_range.end;
1561
1685
  break;
1562
1686
  } else {
1563
1687
  raise_syntax_error(
@@ -1601,20 +1725,21 @@ VALUE parse_member_def(parserstate *state, bool instance_only, bool accept_overl
1601
1725
 
1602
1726
  VALUE location = rbs_new_location(state->buffer, member_range);
1603
1727
  rbs_loc *loc = rbs_check_location(location);
1728
+ rbs_loc_alloc_children(loc, 5);
1604
1729
  rbs_loc_add_required_child(loc, rb_intern("keyword"), keyword_range);
1605
1730
  rbs_loc_add_required_child(loc, rb_intern("name"), name_range);
1606
1731
  rbs_loc_add_optional_child(loc, rb_intern("kind"), kind_range);
1607
- rbs_loc_add_optional_child(loc, rb_intern("overload"), overload_range);
1732
+ rbs_loc_add_optional_child(loc, rb_intern("overloading"), overloading_range);
1608
1733
  rbs_loc_add_optional_child(loc, rb_intern("visibility"), visibility_range);
1609
1734
 
1610
1735
  return rbs_ast_members_method_definition(
1611
1736
  name,
1612
1737
  k,
1613
- method_types,
1738
+ overloads,
1614
1739
  annotations,
1615
1740
  location,
1616
1741
  comment,
1617
- overload,
1742
+ overloading,
1618
1743
  visibility
1619
1744
  );
1620
1745
  }
@@ -1625,7 +1750,7 @@ VALUE parse_member_def(parserstate *state, bool instance_only, bool accept_overl
1625
1750
  *
1626
1751
  * @param kind
1627
1752
  * */
1628
- void class_instance_name(parserstate *state, TypeNameKind kind, VALUE *name, VALUE args, range *name_range, range *args_range) {
1753
+ void class_instance_name(parserstate *state, TypeNameKind kind, VALUE *name, VALUE *args, range *name_range, range *args_range) {
1629
1754
  parser_advance(state);
1630
1755
 
1631
1756
  *name = parse_type_name(state, kind, name_range);
@@ -1648,31 +1773,24 @@ void class_instance_name(parserstate *state, TypeNameKind kind, VALUE *name, VAL
1648
1773
  *
1649
1774
  * @param from_interface `true` when the member is in an interface.
1650
1775
  * */
1651
- VALUE parse_mixin_member(parserstate *state, bool from_interface, position comment_pos, VALUE annotations) {
1776
+ static VALUE parse_mixin_member(parserstate *state, bool from_interface, position comment_pos, VALUE annotations) {
1652
1777
  range member_range;
1653
- range name_range;
1654
- range keyword_range;
1655
- range args_range = NULL_RANGE;
1656
- bool reset_typevar_scope;
1657
-
1658
1778
  member_range.start = state->current_token.range.start;
1659
1779
  comment_pos = nonnull_pos_or(comment_pos, member_range.start);
1660
1780
 
1661
- keyword_range = state->current_token.range;
1781
+ enum TokenType type = state->current_token.type;
1782
+ range keyword_range = state->current_token.range;
1662
1783
 
1663
- VALUE klass = Qnil;
1664
- switch (state->current_token.type)
1784
+ bool reset_typevar_scope;
1785
+ switch (type)
1665
1786
  {
1666
1787
  case kINCLUDE:
1667
- klass = RBS_AST_Members_Include;
1668
1788
  reset_typevar_scope = false;
1669
1789
  break;
1670
1790
  case kEXTEND:
1671
- klass = RBS_AST_Members_Extend;
1672
1791
  reset_typevar_scope = true;
1673
1792
  break;
1674
1793
  case kPREPEND:
1675
- klass = RBS_AST_Members_Prepend;
1676
1794
  reset_typevar_scope = false;
1677
1795
  break;
1678
1796
  default:
@@ -1692,11 +1810,13 @@ VALUE parse_mixin_member(parserstate *state, bool from_interface, position comme
1692
1810
  parser_push_typevar_table(state, reset_typevar_scope);
1693
1811
 
1694
1812
  VALUE name;
1695
- VALUE args = rb_ary_new();
1813
+ VALUE args = EMPTY_ARRAY;
1814
+ range name_range;
1815
+ range args_range = NULL_RANGE;
1696
1816
  class_instance_name(
1697
1817
  state,
1698
1818
  from_interface ? INTERFACE_NAME : (INTERFACE_NAME | CLASS_NAME),
1699
- &name, args, &name_range, &args_range
1819
+ &name, &args, &name_range, &args_range
1700
1820
  );
1701
1821
 
1702
1822
  parser_pop_typevar_table(state);
@@ -1705,18 +1825,23 @@ VALUE parse_mixin_member(parserstate *state, bool from_interface, position comme
1705
1825
 
1706
1826
  VALUE location = rbs_new_location(state->buffer, member_range);
1707
1827
  rbs_loc *loc = rbs_check_location(location);
1828
+ rbs_loc_alloc_children(loc, 3);
1708
1829
  rbs_loc_add_required_child(loc, rb_intern("name"), name_range);
1709
1830
  rbs_loc_add_required_child(loc, rb_intern("keyword"), keyword_range);
1710
1831
  rbs_loc_add_optional_child(loc, rb_intern("args"), args_range);
1711
1832
 
1712
- return rbs_ast_members_mixin(
1713
- klass,
1714
- name,
1715
- args,
1716
- annotations,
1717
- location,
1718
- get_comment(state, comment_pos.line)
1719
- );
1833
+ VALUE comment = get_comment(state, comment_pos.line);
1834
+ switch (type)
1835
+ {
1836
+ case kINCLUDE:
1837
+ return rbs_ast_members_include(name, args, annotations, location, comment);
1838
+ case kEXTEND:
1839
+ return rbs_ast_members_extend(name, args, annotations, location, comment);
1840
+ case kPREPEND:
1841
+ return rbs_ast_members_prepend(name, args, annotations, location, comment);
1842
+ default:
1843
+ rbs_abort();
1844
+ }
1720
1845
  }
1721
1846
 
1722
1847
  /**
@@ -1727,21 +1852,16 @@ VALUE parse_mixin_member(parserstate *state, bool from_interface, position comme
1727
1852
  *
1728
1853
  * @param[in] instance_only `true` to reject `self.` alias.
1729
1854
  * */
1730
- VALUE parse_alias_member(parserstate *state, bool instance_only, position comment_pos, VALUE annotations) {
1855
+ static VALUE parse_alias_member(parserstate *state, bool instance_only, position comment_pos, VALUE annotations) {
1731
1856
  range member_range;
1732
- range keyword_range, new_name_range, old_name_range;
1733
- range new_kind_range, old_kind_range;
1734
-
1735
1857
  member_range.start = state->current_token.range.start;
1736
- keyword_range = state->current_token.range;
1858
+ range keyword_range = state->current_token.range;
1737
1859
 
1738
1860
  comment_pos = nonnull_pos_or(comment_pos, member_range.start);
1739
1861
  VALUE comment = get_comment(state, comment_pos.line);
1740
1862
 
1741
- VALUE new_name;
1742
- VALUE old_name;
1743
- VALUE kind;
1744
-
1863
+ VALUE kind, new_name, old_name;
1864
+ range new_kind_range, old_kind_range, new_name_range, old_name_range;
1745
1865
  if (!instance_only && state->next_token.type == kSELF) {
1746
1866
  kind = ID2SYM(rb_intern("singleton"));
1747
1867
 
@@ -1768,6 +1888,7 @@ VALUE parse_alias_member(parserstate *state, bool instance_only, position commen
1768
1888
  member_range.end = state->current_token.range.end;
1769
1889
  VALUE location = rbs_new_location(state->buffer, member_range);
1770
1890
  rbs_loc *loc = rbs_check_location(location);
1891
+ rbs_loc_alloc_children(loc, 5);
1771
1892
  rbs_loc_add_required_child(loc, rb_intern("keyword"), keyword_range);
1772
1893
  rbs_loc_add_required_child(loc, rb_intern("new_name"), new_name_range);
1773
1894
  rbs_loc_add_required_child(loc, rb_intern("old_name"), old_name_range);
@@ -1789,11 +1910,7 @@ VALUE parse_alias_member(parserstate *state, bool instance_only, position commen
1789
1910
  | {kSELF} `.` tAIDENT `:` <type>
1790
1911
  | {tA2IDENT} `:` <type>
1791
1912
  */
1792
- VALUE parse_variable_member(parserstate *state, position comment_pos, VALUE annotations) {
1793
- range member_range;
1794
- range name_range, colon_range;
1795
- range kind_range = NULL_RANGE;
1796
-
1913
+ static VALUE parse_variable_member(parserstate *state, position comment_pos, VALUE annotations) {
1797
1914
  if (rb_array_len(annotations) > 0) {
1798
1915
  raise_syntax_error(
1799
1916
  state,
@@ -1802,87 +1919,92 @@ VALUE parse_variable_member(parserstate *state, position comment_pos, VALUE anno
1802
1919
  );
1803
1920
  }
1804
1921
 
1922
+ range member_range;
1805
1923
  member_range.start = state->current_token.range.start;
1806
1924
  comment_pos = nonnull_pos_or(comment_pos, member_range.start);
1807
1925
  VALUE comment = get_comment(state, comment_pos.line);
1808
1926
 
1809
- VALUE klass;
1810
- VALUE location;
1811
- VALUE name;
1812
- VALUE type;
1813
-
1814
1927
  switch (state->current_token.type)
1815
1928
  {
1816
- case tAIDENT:
1817
- klass = RBS_AST_Members_InstanceVariable;
1818
-
1819
- name_range = state->current_token.range;
1820
- name = ID2SYM(INTERN_TOKEN(state, state->current_token));
1929
+ case tAIDENT: {
1930
+ range name_range = state->current_token.range;
1931
+ VALUE name = ID2SYM(INTERN_TOKEN(state, state->current_token));
1821
1932
 
1822
1933
  parser_advance_assert(state, pCOLON);
1823
- colon_range = state->current_token.range;
1934
+ range colon_range = state->current_token.range;
1824
1935
 
1825
- type = parse_type(state);
1936
+ VALUE type = parse_type(state);
1826
1937
  member_range.end = state->current_token.range.end;
1827
1938
 
1828
- break;
1829
-
1830
- case tA2IDENT:
1831
- klass = RBS_AST_Members_ClassVariable;
1939
+ VALUE location = rbs_new_location(state->buffer, member_range);
1940
+ rbs_loc *loc = rbs_check_location(location);
1941
+ rbs_loc_alloc_children(loc, 3);
1942
+ rbs_loc_add_required_child(loc, rb_intern("name"), name_range);
1943
+ rbs_loc_add_required_child(loc, rb_intern("colon"), colon_range);
1944
+ rbs_loc_add_optional_child(loc, rb_intern("kind"), NULL_RANGE);
1832
1945
 
1833
- name_range = state->current_token.range;
1834
- name = ID2SYM(INTERN_TOKEN(state, state->current_token));
1946
+ return rbs_ast_members_instance_variable(name, type, location, comment);
1947
+ }
1948
+ case tA2IDENT: {
1949
+ range name_range = state->current_token.range;
1950
+ VALUE name = ID2SYM(INTERN_TOKEN(state, state->current_token));
1835
1951
 
1836
1952
  parser_advance_assert(state, pCOLON);
1837
- colon_range = state->current_token.range;
1953
+ range colon_range = state->current_token.range;
1838
1954
 
1839
1955
  parser_push_typevar_table(state, true);
1840
- type = parse_type(state);
1956
+ VALUE type = parse_type(state);
1841
1957
  parser_pop_typevar_table(state);
1842
1958
  member_range.end = state->current_token.range.end;
1843
1959
 
1844
- break;
1845
-
1846
- case kSELF:
1847
- klass = RBS_AST_Members_ClassInstanceVariable;
1960
+ VALUE location = rbs_new_location(state->buffer, member_range);
1961
+ rbs_loc *loc = rbs_check_location(location);
1962
+ rbs_loc_alloc_children(loc, 3);
1963
+ rbs_loc_add_required_child(loc, rb_intern("name"), name_range);
1964
+ rbs_loc_add_required_child(loc, rb_intern("colon"), colon_range);
1965
+ rbs_loc_add_optional_child(loc, rb_intern("kind"), NULL_RANGE);
1848
1966
 
1849
- kind_range.start = state->current_token.range.start;
1850
- kind_range.end = state->next_token.range.end;
1967
+ return rbs_ast_members_class_variable(name, type, location, comment);
1968
+ }
1969
+ case kSELF: {
1970
+ range kind_range = {
1971
+ .start = state->current_token.range.start,
1972
+ .end = state->next_token.range.end
1973
+ };
1851
1974
 
1852
1975
  parser_advance_assert(state, pDOT);
1853
1976
  parser_advance_assert(state, tAIDENT);
1854
1977
 
1855
- name_range = state->current_token.range;
1856
- name = ID2SYM(INTERN_TOKEN(state, state->current_token));
1978
+ range name_range = state->current_token.range;
1979
+ VALUE name = ID2SYM(INTERN_TOKEN(state, state->current_token));
1857
1980
 
1858
1981
  parser_advance_assert(state, pCOLON);
1859
- colon_range = state->current_token.range;
1982
+ range colon_range = state->current_token.range;
1860
1983
 
1861
1984
  parser_push_typevar_table(state, true);
1862
- type = parse_type(state);
1985
+ VALUE type = parse_type(state);
1863
1986
  parser_pop_typevar_table(state);
1864
1987
  member_range.end = state->current_token.range.end;
1865
1988
 
1866
- break;
1989
+ VALUE location = rbs_new_location(state->buffer, member_range);
1990
+ rbs_loc *loc = rbs_check_location(location);
1991
+ rbs_loc_alloc_children(loc, 3);
1992
+ rbs_loc_add_required_child(loc, rb_intern("name"), name_range);
1993
+ rbs_loc_add_required_child(loc, rb_intern("colon"), colon_range);
1994
+ rbs_loc_add_optional_child(loc, rb_intern("kind"), kind_range);
1867
1995
 
1996
+ return rbs_ast_members_class_instance_variable(name, type, location, comment);
1997
+ }
1868
1998
  default:
1869
1999
  rbs_abort();
1870
2000
  }
1871
-
1872
- location = rbs_new_location(state->buffer, member_range);
1873
- rbs_loc *loc = rbs_check_location(location);
1874
- rbs_loc_add_required_child(loc, rb_intern("name"), name_range);
1875
- rbs_loc_add_required_child(loc, rb_intern("colon"), colon_range);
1876
- rbs_loc_add_optional_child(loc, rb_intern("kind"), kind_range);
1877
-
1878
- return rbs_ast_members_variable(klass, name, type, location, comment);
1879
2001
  }
1880
2002
 
1881
2003
  /*
1882
2004
  visibility_member ::= {<`public`>}
1883
2005
  | {<`private`>}
1884
2006
  */
1885
- VALUE parse_visibility_member(parserstate *state, VALUE annotations) {
2007
+ static VALUE parse_visibility_member(parserstate *state, VALUE annotations) {
1886
2008
  if (rb_array_len(annotations) > 0) {
1887
2009
  raise_syntax_error(
1888
2010
  state,
@@ -1891,24 +2013,17 @@ VALUE parse_visibility_member(parserstate *state, VALUE annotations) {
1891
2013
  );
1892
2014
  }
1893
2015
 
1894
- VALUE klass;
2016
+ VALUE location = rbs_new_location(state->buffer, state->current_token.range);
1895
2017
 
1896
2018
  switch (state->current_token.type)
1897
2019
  {
1898
2020
  case kPUBLIC:
1899
- klass = RBS_AST_Members_Public;
1900
- break;
2021
+ return rbs_ast_members_public(location);
1901
2022
  case kPRIVATE:
1902
- klass = RBS_AST_Members_Private;
1903
- break;
2023
+ return rbs_ast_members_private(location);
1904
2024
  default:
1905
2025
  rbs_abort();
1906
2026
  }
1907
-
1908
- return rbs_ast_members_visibility(
1909
- klass,
1910
- rbs_new_location(state->buffer, state->current_token.range)
1911
- );
1912
2027
  }
1913
2028
 
1914
2029
  /*
@@ -1925,26 +2040,14 @@ VALUE parse_visibility_member(parserstate *state, VALUE annotations) {
1925
2040
  | `(` tAIDENT `)` # Ivar name
1926
2041
  | `(` `)` # No variable
1927
2042
  */
1928
- VALUE parse_attribute_member(parserstate *state, position comment_pos, VALUE annotations) {
2043
+ static VALUE parse_attribute_member(parserstate *state, position comment_pos, VALUE annotations) {
1929
2044
  range member_range;
1930
- range keyword_range, name_range, colon_range;
1931
- range kind_range = NULL_RANGE, ivar_range = NULL_RANGE, ivar_name_range = NULL_RANGE, visibility_range = NULL_RANGE;
1932
-
1933
- InstanceSingletonKind is_kind;
1934
- VALUE klass;
1935
- VALUE kind;
1936
- VALUE attr_name;
1937
- VALUE ivar_name;
1938
- VALUE type;
1939
- VALUE comment;
1940
- VALUE location;
1941
- VALUE visibility;
1942
- rbs_loc *loc;
1943
-
1944
2045
  member_range.start = state->current_token.range.start;
1945
2046
  comment_pos = nonnull_pos_or(comment_pos, member_range.start);
1946
- comment = get_comment(state, comment_pos.line);
2047
+ VALUE comment = get_comment(state, comment_pos.line);
1947
2048
 
2049
+ VALUE visibility;
2050
+ range visibility_range;
1948
2051
  switch (state->current_token.type)
1949
2052
  {
1950
2053
  case kPRIVATE:
@@ -1963,31 +2066,18 @@ VALUE parse_attribute_member(parserstate *state, position comment_pos, VALUE ann
1963
2066
  break;
1964
2067
  }
1965
2068
 
1966
- keyword_range = state->current_token.range;
1967
- switch (state->current_token.type)
1968
- {
1969
- case kATTRREADER:
1970
- klass = RBS_AST_Members_AttrReader;
1971
- break;
1972
- case kATTRWRITER:
1973
- klass = RBS_AST_Members_AttrWriter;
1974
- break;
1975
- case kATTRACCESSOR:
1976
- klass = RBS_AST_Members_AttrAccessor;
1977
- break;
1978
- default:
1979
- rbs_abort();
1980
- }
2069
+ enum TokenType attr_type = state->current_token.type;
2070
+ range keyword_range = state->current_token.range;
1981
2071
 
1982
- is_kind = parse_instance_singleton_kind(state, false, &kind_range);
1983
- if (is_kind == INSTANCE_KIND) {
1984
- kind = ID2SYM(rb_intern("instance"));
1985
- } else {
1986
- kind = ID2SYM(rb_intern("singleton"));
1987
- }
2072
+ range kind_range;
2073
+ InstanceSingletonKind is_kind = parse_instance_singleton_kind(state, false, &kind_range);
2074
+ VALUE kind = ID2SYM(rb_intern((is_kind == INSTANCE_KIND) ? "instance" : "singleton"));
1988
2075
 
1989
- attr_name = parse_method_name(state, &name_range);
2076
+ range name_range;
2077
+ VALUE attr_name = parse_method_name(state, &name_range);
1990
2078
 
2079
+ VALUE ivar_name;
2080
+ range ivar_range, ivar_name_range;
1991
2081
  if (state->next_token.type == pLPAREN) {
1992
2082
  parser_advance_assert(state, pLPAREN);
1993
2083
  ivar_range.start = state->current_token.range.start;
@@ -1997,24 +2087,28 @@ VALUE parse_attribute_member(parserstate *state, position comment_pos, VALUE ann
1997
2087
  ivar_name_range = state->current_token.range;
1998
2088
  } else {
1999
2089
  ivar_name = Qfalse;
2090
+ ivar_name_range = NULL_RANGE;
2000
2091
  }
2001
2092
 
2002
2093
  parser_advance_assert(state, pRPAREN);
2003
2094
  ivar_range.end = state->current_token.range.end;
2004
2095
  } else {
2096
+ ivar_range = NULL_RANGE;
2005
2097
  ivar_name = Qnil;
2098
+ ivar_name_range = NULL_RANGE;
2006
2099
  }
2007
2100
 
2008
2101
  parser_advance_assert(state, pCOLON);
2009
- colon_range = state->current_token.range;
2102
+ range colon_range = state->current_token.range;
2010
2103
 
2011
2104
  parser_push_typevar_table(state, is_kind == SINGLETON_KIND);
2012
- type = parse_type(state);
2105
+ VALUE type = parse_type(state);
2013
2106
  parser_pop_typevar_table(state);
2014
2107
  member_range.end = state->current_token.range.end;
2015
2108
 
2016
- location = rbs_new_location(state->buffer, member_range);
2017
- loc = rbs_check_location(location);
2109
+ VALUE location = rbs_new_location(state->buffer, member_range);
2110
+ rbs_loc *loc = rbs_check_location(location);
2111
+ rbs_loc_alloc_children(loc, 7);
2018
2112
  rbs_loc_add_required_child(loc, rb_intern("keyword"), keyword_range);
2019
2113
  rbs_loc_add_required_child(loc, rb_intern("name"), name_range);
2020
2114
  rbs_loc_add_required_child(loc, rb_intern("colon"), colon_range);
@@ -2023,17 +2117,17 @@ VALUE parse_attribute_member(parserstate *state, position comment_pos, VALUE ann
2023
2117
  rbs_loc_add_optional_child(loc, rb_intern("ivar_name"), ivar_name_range);
2024
2118
  rbs_loc_add_optional_child(loc, rb_intern("visibility"), visibility_range);
2025
2119
 
2026
- return rbs_ast_members_attribute(
2027
- klass,
2028
- attr_name,
2029
- type,
2030
- ivar_name,
2031
- kind,
2032
- annotations,
2033
- location,
2034
- comment,
2035
- visibility
2036
- );
2120
+ switch (attr_type)
2121
+ {
2122
+ case kATTRREADER:
2123
+ return rbs_ast_members_attr_reader(attr_name, type, ivar_name, kind, annotations, location, comment, visibility);
2124
+ case kATTRWRITER:
2125
+ return rbs_ast_members_attr_writer(attr_name, type, ivar_name, kind, annotations, location, comment, visibility);
2126
+ case kATTRACCESSOR:
2127
+ return rbs_ast_members_attr_accessor(attr_name, type, ivar_name, kind, annotations, location, comment, visibility);
2128
+ default:
2129
+ rbs_abort();
2130
+ }
2037
2131
  }
2038
2132
 
2039
2133
  /*
@@ -2043,32 +2137,35 @@ VALUE parse_attribute_member(parserstate *state, position comment_pos, VALUE ann
2043
2137
  | mixin_member (interface only)
2044
2138
  | alias_member (instance only)
2045
2139
  */
2046
- VALUE parse_interface_members(parserstate *state) {
2047
- VALUE members = rb_ary_new();
2140
+ static VALUE parse_interface_members(parserstate *state) {
2141
+ VALUE members = EMPTY_ARRAY;
2048
2142
 
2049
2143
  while (state->next_token.type != kEND) {
2050
- VALUE annotations = rb_ary_new();
2144
+ VALUE annotations = EMPTY_ARRAY;
2051
2145
  position annot_pos = NullPosition;
2052
2146
 
2053
- parse_annotations(state, annotations, &annot_pos);
2147
+ parse_annotations(state, &annotations, &annot_pos);
2054
2148
 
2055
2149
  parser_advance(state);
2056
2150
 
2057
2151
  VALUE member;
2058
2152
  switch (state->current_token.type) {
2059
- case kDEF:
2153
+ case kDEF: {
2060
2154
  member = parse_member_def(state, true, true, annot_pos, annotations);
2061
2155
  break;
2156
+ }
2062
2157
 
2063
2158
  case kINCLUDE:
2064
2159
  case kEXTEND:
2065
- case kPREPEND:
2160
+ case kPREPEND: {
2066
2161
  member = parse_mixin_member(state, true, annot_pos, annotations);
2067
2162
  break;
2163
+ }
2068
2164
 
2069
- case kALIAS:
2165
+ case kALIAS: {
2070
2166
  member = parse_alias_member(state, true, annot_pos, annotations);
2071
2167
  break;
2168
+ }
2072
2169
 
2073
2170
  default:
2074
2171
  raise_syntax_error(
@@ -2078,6 +2175,7 @@ VALUE parse_interface_members(parserstate *state) {
2078
2175
  );
2079
2176
  }
2080
2177
 
2178
+ melt_array(&members);
2081
2179
  rb_ary_push(members, member);
2082
2180
  }
2083
2181
 
@@ -2087,31 +2185,32 @@ VALUE parse_interface_members(parserstate *state) {
2087
2185
  /*
2088
2186
  interface_decl ::= {`interface`} interface_name module_type_params interface_members <kEND>
2089
2187
  */
2090
- VALUE parse_interface_decl(parserstate *state, position comment_pos, VALUE annotations) {
2091
- range member_range;
2092
- range name_range, keyword_range, end_range;
2093
- range type_params_range = NULL_RANGE;
2188
+ static VALUE parse_interface_decl(parserstate *state, position comment_pos, VALUE annotations) {
2189
+ parser_push_typevar_table(state, true);
2094
2190
 
2191
+ range member_range;
2095
2192
  member_range.start = state->current_token.range.start;
2096
2193
  comment_pos = nonnull_pos_or(comment_pos, member_range.start);
2097
2194
 
2098
- parser_push_typevar_table(state, true);
2099
- keyword_range = state->current_token.range;
2195
+ range keyword_range = state->current_token.range;
2100
2196
 
2101
2197
  parser_advance(state);
2102
2198
 
2199
+ range name_range;
2103
2200
  VALUE name = parse_type_name(state, INTERFACE_NAME, &name_range);
2201
+ range type_params_range;
2104
2202
  VALUE params = parse_type_params(state, &type_params_range, true);
2105
2203
  VALUE members = parse_interface_members(state);
2106
2204
 
2107
2205
  parser_advance_assert(state, kEND);
2108
- end_range = state->current_token.range;
2206
+ range end_range = state->current_token.range;
2109
2207
  member_range.end = end_range.end;
2110
2208
 
2111
2209
  parser_pop_typevar_table(state);
2112
2210
 
2113
2211
  VALUE location = rbs_new_location(state->buffer, member_range);
2114
2212
  rbs_loc *loc = rbs_check_location(location);
2213
+ rbs_loc_alloc_children(loc, 4);
2115
2214
  rbs_loc_add_required_child(loc, rb_intern("keyword"), keyword_range);
2116
2215
  rbs_loc_add_required_child(loc, rb_intern("name"), name_range);
2117
2216
  rbs_loc_add_required_child(loc, rb_intern("end"), end_range);
@@ -2133,35 +2232,35 @@ VALUE parse_interface_decl(parserstate *state, position comment_pos, VALUE annot
2133
2232
  module_self_type ::= <module_name>
2134
2233
  | module_name `[` type_list <`]`>
2135
2234
  */
2136
- void parse_module_self_types(parserstate *state, VALUE array) {
2235
+ static void parse_module_self_types(parserstate *state, VALUE *array) {
2137
2236
  while (true) {
2138
- range self_range;
2139
- range name_range;
2140
- range args_range = NULL_RANGE;
2141
-
2142
2237
  parser_advance(state);
2143
2238
 
2239
+ range self_range;
2144
2240
  self_range.start = state->current_token.range.start;
2145
-
2241
+ range name_range;
2146
2242
  VALUE module_name = parse_type_name(state, CLASS_NAME | INTERFACE_NAME, &name_range);
2147
2243
  self_range.end = name_range.end;
2148
2244
 
2149
- VALUE args = rb_ary_new();
2245
+ VALUE args = EMPTY_ARRAY;
2246
+ range args_range = NULL_RANGE;
2150
2247
  if (state->next_token.type == pLBRACKET) {
2151
2248
  parser_advance(state);
2152
2249
  args_range.start = state->current_token.range.start;
2153
- parse_type_list(state, pRBRACKET, args);
2250
+ parse_type_list(state, pRBRACKET, &args);
2154
2251
  parser_advance(state);
2155
2252
  self_range.end = args_range.end = state->current_token.range.end;
2156
2253
  }
2157
2254
 
2158
2255
  VALUE location = rbs_new_location(state->buffer, self_range);
2159
2256
  rbs_loc *loc = rbs_check_location(location);
2257
+ rbs_loc_alloc_children(loc, 2);
2160
2258
  rbs_loc_add_required_child(loc, rb_intern("name"), name_range);
2161
2259
  rbs_loc_add_optional_child(loc, rb_intern("args"), args_range);
2162
2260
 
2163
2261
  VALUE self_type = rbs_ast_decl_module_self(module_name, args, location);
2164
- rb_ary_push(array, self_type);
2262
+ melt_array(array);
2263
+ rb_ary_push(*array, self_type);
2165
2264
 
2166
2265
  if (state->next_token.type == pCOMMA) {
2167
2266
  parser_advance(state);
@@ -2171,7 +2270,7 @@ void parse_module_self_types(parserstate *state, VALUE array) {
2171
2270
  }
2172
2271
  }
2173
2272
 
2174
- VALUE parse_nested_decl(parserstate *state, const char *nested_in, position annot_pos, VALUE annotations);
2273
+ static VALUE parse_nested_decl(parserstate *state, const char *nested_in, position annot_pos, VALUE annotations);
2175
2274
 
2176
2275
  /*
2177
2276
  module_members ::= {} ...<module_member> kEND
@@ -2184,59 +2283,65 @@ VALUE parse_nested_decl(parserstate *state, const char *nested_in, position anno
2184
2283
  | `public`
2185
2284
  | `private`
2186
2285
  */
2187
- VALUE parse_module_members(parserstate *state) {
2188
- VALUE members = rb_ary_new();
2286
+ static VALUE parse_module_members(parserstate *state) {
2287
+ VALUE members = EMPTY_ARRAY;
2189
2288
 
2190
2289
  while (state->next_token.type != kEND) {
2191
- VALUE member;
2192
- VALUE annotations = rb_ary_new();
2290
+ VALUE annotations = EMPTY_ARRAY;
2193
2291
  position annot_pos = NullPosition;
2194
-
2195
- parse_annotations(state, annotations, &annot_pos);
2292
+ parse_annotations(state, &annotations, &annot_pos);
2196
2293
 
2197
2294
  parser_advance(state);
2198
2295
 
2296
+ VALUE member;
2199
2297
  switch (state->current_token.type)
2200
2298
  {
2201
- case kDEF:
2299
+ case kDEF: {
2202
2300
  member = parse_member_def(state, false, true, annot_pos, annotations);
2203
2301
  break;
2302
+ }
2204
2303
 
2205
2304
  case kINCLUDE:
2206
2305
  case kEXTEND:
2207
- case kPREPEND:
2306
+ case kPREPEND: {
2208
2307
  member = parse_mixin_member(state, false, annot_pos, annotations);
2209
2308
  break;
2309
+ }
2210
2310
 
2211
- case kALIAS:
2311
+ case kALIAS: {
2212
2312
  member = parse_alias_member(state, false, annot_pos, annotations);
2213
2313
  break;
2314
+ }
2214
2315
 
2215
2316
  case tAIDENT:
2216
2317
  case tA2IDENT:
2217
- case kSELF:
2318
+ case kSELF: {
2218
2319
  member = parse_variable_member(state, annot_pos, annotations);
2219
2320
  break;
2321
+ }
2220
2322
 
2221
2323
  case kATTRREADER:
2222
2324
  case kATTRWRITER:
2223
- case kATTRACCESSOR:
2325
+ case kATTRACCESSOR: {
2224
2326
  member = parse_attribute_member(state, annot_pos, annotations);
2225
2327
  break;
2328
+ }
2226
2329
 
2227
2330
  case kPUBLIC:
2228
2331
  case kPRIVATE:
2229
2332
  if (state->next_token.range.start.line == state->current_token.range.start.line) {
2230
2333
  switch (state->next_token.type)
2231
2334
  {
2232
- case kDEF:
2335
+ case kDEF: {
2233
2336
  member = parse_member_def(state, false, true, annot_pos, annotations);
2234
2337
  break;
2338
+ }
2235
2339
  case kATTRREADER:
2236
2340
  case kATTRWRITER:
2237
- case kATTRACCESSOR:
2341
+ case kATTRACCESSOR: {
2238
2342
  member = parse_attribute_member(state, annot_pos, annotations);
2239
2343
  break;
2344
+ }
2240
2345
  default:
2241
2346
  raise_syntax_error(state, state->next_token, "method or attribute definition is expected after visibility modifier");
2242
2347
  }
@@ -2250,6 +2355,7 @@ VALUE parse_module_members(parserstate *state) {
2250
2355
  break;
2251
2356
  }
2252
2357
 
2358
+ melt_array(&members);
2253
2359
  rb_ary_push(members, member);
2254
2360
  }
2255
2361
 
@@ -2257,37 +2363,25 @@ VALUE parse_module_members(parserstate *state) {
2257
2363
  }
2258
2364
 
2259
2365
  /*
2260
- module_decl ::= {`module`} module_name module_type_params module_members <kEND>
2261
- | {`module`} module_name module_type_params `:` module_self_types module_members <kEND>
2366
+ module_decl ::= {module_name} module_type_params module_members <kEND>
2367
+ | {module_name} module_name module_type_params `:` module_self_types module_members <kEND>
2262
2368
  */
2263
- VALUE parse_module_decl(parserstate *state, position comment_pos, VALUE annotations) {
2264
- range decl_range;
2265
- range keyword_range;
2266
- range name_range;
2267
- range end_range;
2268
- range type_params_range;
2269
- range colon_range;
2270
- range self_types_range;
2271
-
2369
+ static VALUE parse_module_decl0(parserstate *state, range keyword_range, VALUE module_name, range name_range, VALUE comment, VALUE annotations) {
2272
2370
  parser_push_typevar_table(state, true);
2273
2371
 
2274
- position start = state->current_token.range.start;
2275
- comment_pos = nonnull_pos_or(comment_pos, start);
2276
- VALUE comment = get_comment(state, comment_pos.line);
2277
-
2278
- keyword_range = state->current_token.range;
2279
- decl_range.start = state->current_token.range.start;
2280
-
2281
- parser_advance(state);
2282
- VALUE module_name = parse_type_name(state, CLASS_NAME, &name_range);
2372
+ range decl_range;
2373
+ decl_range.start = keyword_range.start;
2374
+ range type_params_range;
2283
2375
  VALUE type_params = parse_type_params(state, &type_params_range, true);
2284
- VALUE self_types = rb_ary_new();
2285
2376
 
2377
+ VALUE self_types = EMPTY_ARRAY;
2378
+ range colon_range;
2379
+ range self_types_range;
2286
2380
  if (state->next_token.type == pCOLON) {
2287
2381
  parser_advance(state);
2288
2382
  colon_range = state->current_token.range;
2289
2383
  self_types_range.start = state->next_token.range.start;
2290
- parse_module_self_types(state, self_types);
2384
+ parse_module_self_types(state, &self_types);
2291
2385
  self_types_range.end = state->current_token.range.end;
2292
2386
  } else {
2293
2387
  colon_range = NULL_RANGE;
@@ -2297,11 +2391,12 @@ VALUE parse_module_decl(parserstate *state, position comment_pos, VALUE annotati
2297
2391
  VALUE members = parse_module_members(state);
2298
2392
 
2299
2393
  parser_advance_assert(state, kEND);
2300
- end_range = state->current_token.range;
2394
+ range end_range = state->current_token.range;
2301
2395
  decl_range.end = state->current_token.range.end;
2302
2396
 
2303
2397
  VALUE location = rbs_new_location(state->buffer, decl_range);
2304
2398
  rbs_loc *loc = rbs_check_location(location);
2399
+ rbs_loc_alloc_children(loc, 6);
2305
2400
  rbs_loc_add_required_child(loc, rb_intern("keyword"), keyword_range);
2306
2401
  rbs_loc_add_required_child(loc, rb_intern("name"), name_range);
2307
2402
  rbs_loc_add_required_child(loc, rb_intern("end"), end_range);
@@ -2322,31 +2417,69 @@ VALUE parse_module_decl(parserstate *state, position comment_pos, VALUE annotati
2322
2417
  );
2323
2418
  }
2324
2419
 
2420
+ /*
2421
+ module_decl ::= {`module`} module_name `=` old_module_name <kEND>
2422
+ | {`module`} module_name module_decl0 <kEND>
2423
+
2424
+ */
2425
+ static VALUE parse_module_decl(parserstate *state, position comment_pos, VALUE annotations) {
2426
+ range keyword_range = state->current_token.range;
2427
+
2428
+ comment_pos = nonnull_pos_or(comment_pos, state->current_token.range.start);
2429
+ VALUE comment = get_comment(state, comment_pos.line);
2430
+
2431
+ parser_advance(state);
2432
+ range module_name_range;
2433
+ VALUE module_name = parse_type_name(state, CLASS_NAME, &module_name_range);
2434
+
2435
+ if (state->next_token.type == pEQ) {
2436
+ range eq_range = state->next_token.range;
2437
+ parser_advance(state);
2438
+ parser_advance(state);
2439
+
2440
+ range old_name_range;
2441
+ VALUE old_name = parse_type_name(state, CLASS_NAME, &old_name_range);
2442
+
2443
+ range decl_range = {
2444
+ .start = keyword_range.start,
2445
+ .end = old_name_range.end
2446
+ };
2447
+
2448
+ VALUE location = rbs_new_location(state->buffer, decl_range);
2449
+ rbs_loc *loc = rbs_check_location(location);
2450
+ rbs_loc_alloc_children(loc, 4);
2451
+ rbs_loc_add_required_child(loc, rb_intern("keyword"), keyword_range);
2452
+ rbs_loc_add_required_child(loc, rb_intern("new_name"), module_name_range);
2453
+ rbs_loc_add_required_child(loc, rb_intern("eq"), eq_range);
2454
+ rbs_loc_add_optional_child(loc, rb_intern("old_name"), old_name_range);
2455
+
2456
+ return rbs_ast_decl_module_alias(module_name, old_name, location, comment);
2457
+ } else {
2458
+ return parse_module_decl0(state, keyword_range, module_name, module_name_range, comment, annotations);
2459
+ }
2460
+ }
2461
+
2325
2462
  /*
2326
2463
  class_decl_super ::= {} `<` <class_instance_name>
2327
2464
  | {<>}
2328
2465
  */
2329
- VALUE parse_class_decl_super(parserstate *state, range *lt_range) {
2466
+ static VALUE parse_class_decl_super(parserstate *state, range *lt_range) {
2330
2467
  if (parser_advance_if(state, pLT)) {
2331
- range super_range;
2332
- range name_range;
2333
- range args_range = NULL_RANGE;
2334
-
2335
- VALUE name;
2336
- VALUE args;
2337
- VALUE location;
2338
- rbs_loc *loc;
2339
-
2340
2468
  *lt_range = state->current_token.range;
2469
+
2470
+ range super_range;
2341
2471
  super_range.start = state->next_token.range.start;
2342
2472
 
2343
- args = rb_ary_new();
2344
- class_instance_name(state, CLASS_NAME, &name, args, &name_range, &args_range);
2473
+ VALUE name;
2474
+ VALUE args = EMPTY_ARRAY;
2475
+ range name_range, args_range;
2476
+ class_instance_name(state, CLASS_NAME, &name, &args, &name_range, &args_range);
2345
2477
 
2346
- super_range.end = args_range.end;
2478
+ super_range.end = state->current_token.range.end;
2347
2479
 
2348
- location = rbs_new_location(state->buffer, super_range);
2349
- loc = rbs_check_location(location);
2480
+ VALUE location = rbs_new_location(state->buffer, super_range);
2481
+ rbs_loc *loc = rbs_check_location(location);
2482
+ rbs_loc_alloc_children(loc, 2);
2350
2483
  rbs_loc_add_required_child(loc, rb_intern("name"), name_range);
2351
2484
  rbs_loc_add_optional_child(loc, rb_intern("args"), args_range);
2352
2485
 
@@ -2358,47 +2491,33 @@ VALUE parse_class_decl_super(parserstate *state, range *lt_range) {
2358
2491
  }
2359
2492
 
2360
2493
  /*
2361
- class_decl ::= {`class`} class_name type_params class_decl_super class_members <`end`>
2494
+ class_decl ::= {class_name} type_params class_decl_super class_members <`end`>
2362
2495
  */
2363
- VALUE parse_class_decl(parserstate *state, position comment_pos, VALUE annotations) {
2364
- range decl_range;
2365
- range keyword_range;
2366
- range name_range;
2367
- range end_range;
2368
- range type_params_range;
2369
- range lt_range;
2370
-
2371
- VALUE name;
2372
- VALUE type_params;
2373
- VALUE super;
2374
- VALUE members;
2375
- VALUE comment;
2376
- VALUE location;
2496
+ static VALUE parse_class_decl0(parserstate *state, range keyword_range, VALUE name, range name_range, VALUE comment, VALUE annotations) {
2497
+ parser_push_typevar_table(state, true);
2377
2498
 
2378
- rbs_loc *loc;
2499
+ range decl_range;
2500
+ decl_range.start = keyword_range.start;
2379
2501
 
2380
- parser_push_typevar_table(state, true);
2502
+ range type_params_range;
2503
+ VALUE type_params = parse_type_params(state, &type_params_range, true);
2381
2504
 
2382
- decl_range.start = state->current_token.range.start;
2383
- keyword_range = state->current_token.range;
2505
+ range lt_range;
2506
+ VALUE super = parse_class_decl_super(state, &lt_range);
2384
2507
 
2385
- comment_pos = nonnull_pos_or(comment_pos, decl_range.start);
2386
- comment = get_comment(state, comment_pos.line);
2508
+ VALUE members = parse_module_members(state);
2387
2509
 
2388
- parser_advance(state);
2389
- name = parse_type_name(state, CLASS_NAME, &name_range);
2390
- type_params = parse_type_params(state, &type_params_range, true);
2391
- super = parse_class_decl_super(state, &lt_range);
2392
- members = parse_module_members(state);
2393
2510
  parser_advance_assert(state, kEND);
2394
- end_range = state->current_token.range;
2511
+
2512
+ range end_range = state->current_token.range;
2395
2513
 
2396
2514
  decl_range.end = end_range.end;
2397
2515
 
2398
2516
  parser_pop_typevar_table(state);
2399
2517
 
2400
- location = rbs_new_location(state->buffer, decl_range);
2401
- loc = rbs_check_location(location);
2518
+ VALUE location = rbs_new_location(state->buffer, decl_range);
2519
+ rbs_loc *loc = rbs_check_location(location);
2520
+ rbs_loc_alloc_children(loc, 5);
2402
2521
  rbs_loc_add_required_child(loc, rb_intern("keyword"), keyword_range);
2403
2522
  rbs_loc_add_required_child(loc, rb_intern("name"), name_range);
2404
2523
  rbs_loc_add_required_child(loc, rb_intern("end"), end_range);
@@ -2416,6 +2535,47 @@ VALUE parse_class_decl(parserstate *state, position comment_pos, VALUE annotatio
2416
2535
  );
2417
2536
  }
2418
2537
 
2538
+ /*
2539
+ class_decl ::= {`class`} class_name `=` <class_name>
2540
+ | {`class`} class_name <class_decl0>
2541
+ */
2542
+ static VALUE parse_class_decl(parserstate *state, position comment_pos, VALUE annotations) {
2543
+ range keyword_range = state->current_token.range;
2544
+
2545
+ comment_pos = nonnull_pos_or(comment_pos, state->current_token.range.start);
2546
+ VALUE comment = get_comment(state, comment_pos.line);
2547
+
2548
+ parser_advance(state);
2549
+ range class_name_range;
2550
+ VALUE class_name = parse_type_name(state, CLASS_NAME, &class_name_range);
2551
+
2552
+ if (state->next_token.type == pEQ) {
2553
+ range eq_range = state->next_token.range;
2554
+ parser_advance(state);
2555
+ parser_advance(state);
2556
+
2557
+ range old_name_range;
2558
+ VALUE old_name = parse_type_name(state, CLASS_NAME, &old_name_range);
2559
+
2560
+ range decl_range = {
2561
+ .start = keyword_range.start,
2562
+ .end = old_name_range.end,
2563
+ };
2564
+
2565
+ VALUE location = rbs_new_location(state->buffer, decl_range);
2566
+ rbs_loc *loc = rbs_check_location(location);
2567
+ rbs_loc_alloc_children(loc, 4);
2568
+ rbs_loc_add_required_child(loc, rb_intern("keyword"), keyword_range);
2569
+ rbs_loc_add_required_child(loc, rb_intern("new_name"), class_name_range);
2570
+ rbs_loc_add_required_child(loc, rb_intern("eq"), eq_range);
2571
+ rbs_loc_add_optional_child(loc, rb_intern("old_name"), old_name_range);
2572
+
2573
+ return rbs_ast_decl_class_alias(class_name, old_name, location, comment);
2574
+ } else {
2575
+ return parse_class_decl0(state, keyword_range, class_name, class_name_range, comment, annotations);
2576
+ }
2577
+ }
2578
+
2419
2579
  /*
2420
2580
  nested_decl ::= {<const_decl>}
2421
2581
  | {<class_decl>}
@@ -2423,31 +2583,36 @@ VALUE parse_class_decl(parserstate *state, position comment_pos, VALUE annotatio
2423
2583
  | {<module_decl>}
2424
2584
  | {<class_decl>}
2425
2585
  */
2426
- VALUE parse_nested_decl(parserstate *state, const char *nested_in, position annot_pos, VALUE annotations) {
2427
- VALUE decl;
2428
-
2586
+ static VALUE parse_nested_decl(parserstate *state, const char *nested_in, position annot_pos, VALUE annotations) {
2429
2587
  parser_push_typevar_table(state, true);
2430
2588
 
2589
+ VALUE decl;
2431
2590
  switch (state->current_token.type) {
2432
2591
  case tUIDENT:
2433
- case pCOLON2:
2592
+ case pCOLON2: {
2434
2593
  decl = parse_const_decl(state);
2435
2594
  break;
2436
- case tGIDENT:
2595
+ }
2596
+ case tGIDENT: {
2437
2597
  decl = parse_global_decl(state);
2438
2598
  break;
2439
- case kTYPE:
2599
+ }
2600
+ case kTYPE: {
2440
2601
  decl = parse_type_decl(state, annot_pos, annotations);
2441
2602
  break;
2442
- case kINTERFACE:
2603
+ }
2604
+ case kINTERFACE: {
2443
2605
  decl = parse_interface_decl(state, annot_pos, annotations);
2444
2606
  break;
2445
- case kMODULE:
2607
+ }
2608
+ case kMODULE: {
2446
2609
  decl = parse_module_decl(state, annot_pos, annotations);
2447
2610
  break;
2448
- case kCLASS:
2611
+ }
2612
+ case kCLASS: {
2449
2613
  decl = parse_class_decl(state, annot_pos, annotations);
2450
2614
  break;
2615
+ }
2451
2616
  default:
2452
2617
  raise_syntax_error(
2453
2618
  state,
@@ -2461,27 +2626,33 @@ VALUE parse_nested_decl(parserstate *state, const char *nested_in, position anno
2461
2626
  return decl;
2462
2627
  }
2463
2628
 
2464
- VALUE parse_decl(parserstate *state) {
2465
- VALUE annotations = rb_ary_new();
2629
+ static VALUE parse_decl(parserstate *state) {
2630
+ VALUE annotations = EMPTY_ARRAY;
2466
2631
  position annot_pos = NullPosition;
2467
2632
 
2468
- parse_annotations(state, annotations, &annot_pos);
2633
+ parse_annotations(state, &annotations, &annot_pos);
2469
2634
 
2470
2635
  parser_advance(state);
2471
2636
  switch (state->current_token.type) {
2472
2637
  case tUIDENT:
2473
- case pCOLON2:
2638
+ case pCOLON2: {
2474
2639
  return parse_const_decl(state);
2475
- case tGIDENT:
2640
+ }
2641
+ case tGIDENT: {
2476
2642
  return parse_global_decl(state);
2477
- case kTYPE:
2643
+ }
2644
+ case kTYPE: {
2478
2645
  return parse_type_decl(state, annot_pos, annotations);
2479
- case kINTERFACE:
2646
+ }
2647
+ case kINTERFACE: {
2480
2648
  return parse_interface_decl(state, annot_pos, annotations);
2481
- case kMODULE:
2649
+ }
2650
+ case kMODULE: {
2482
2651
  return parse_module_decl(state, annot_pos, annotations);
2483
- case kCLASS:
2652
+ }
2653
+ case kCLASS: {
2484
2654
  return parse_class_decl(state, annot_pos, annotations);
2655
+ }
2485
2656
  default:
2486
2657
  raise_syntax_error(
2487
2658
  state,
@@ -2491,69 +2662,300 @@ VALUE parse_decl(parserstate *state) {
2491
2662
  }
2492
2663
  }
2493
2664
 
2665
+ /*
2666
+ namespace ::= {} (`::`)? (`tUIDENT` `::`)* `tUIDENT` <`::`>
2667
+ | {} <> (empty -- returns empty namespace)
2668
+ */
2669
+ static VALUE parse_namespace(parserstate *state, range *rg) {
2670
+ bool is_absolute = false;
2671
+
2672
+ if (state->next_token.type == pCOLON2) {
2673
+ *rg = (range) {
2674
+ .start = state->next_token.range.start,
2675
+ .end = state->next_token.range.end,
2676
+ };
2677
+ is_absolute = true;
2678
+
2679
+ parser_advance(state);
2680
+ }
2681
+
2682
+ VALUE path = EMPTY_ARRAY;
2683
+
2684
+ while (true) {
2685
+ if (state->next_token.type == tUIDENT && state->next_token2.type == pCOLON2) {
2686
+ melt_array(&path);
2687
+ rb_ary_push(path, ID2SYM(INTERN_TOKEN(state, state->next_token)));
2688
+ if (null_position_p(rg->start)) {
2689
+ rg->start = state->next_token.range.start;
2690
+ }
2691
+ rg->end = state->next_token2.range.end;
2692
+ parser_advance(state);
2693
+ parser_advance(state);
2694
+ } else {
2695
+ break;
2696
+ }
2697
+ }
2698
+
2699
+ return rbs_namespace(path, is_absolute ? Qtrue : Qfalse);
2700
+ }
2701
+
2702
+ /*
2703
+ use_clauses ::= {} use_clause `,` ... `,` <use_clause>
2704
+
2705
+ use_clause ::= {} namespace <tUIDENT>
2706
+ | {} namespace tUIDENT `as` <tUIDENT>
2707
+ | {} namespace <tSTAR>
2708
+ */
2709
+ static void parse_use_clauses(parserstate *state, VALUE clauses) {
2710
+ while (true) {
2711
+ range namespace_range = NULL_RANGE;
2712
+ VALUE namespace = parse_namespace(state, &namespace_range);
2713
+
2714
+ switch (state->next_token.type)
2715
+ {
2716
+ case tLIDENT:
2717
+ case tULIDENT:
2718
+ case tUIDENT: {
2719
+ parser_advance(state);
2720
+
2721
+ enum TokenType ident_type = state->current_token.type;
2722
+
2723
+ range type_name_range = null_range_p(namespace_range)
2724
+ ? state->current_token.range
2725
+ : (range) { .start = namespace_range.start, .end = state->current_token.range.end };
2726
+
2727
+ VALUE type_name = rbs_type_name(namespace, ID2SYM(INTERN_TOKEN(state, state->current_token)));
2728
+
2729
+ range keyword_range = NULL_RANGE;
2730
+ range new_name_range = NULL_RANGE;
2731
+
2732
+ VALUE new_name = Qnil;
2733
+ range clause_range = type_name_range;
2734
+ if (state->next_token.type == kAS) {
2735
+ parser_advance(state);
2736
+ keyword_range = state->current_token.range;
2737
+
2738
+ if (ident_type == tUIDENT) parser_advance_assert(state, tUIDENT);
2739
+ if (ident_type == tLIDENT) parser_advance_assert(state, tLIDENT);
2740
+ if (ident_type == tULIDENT) parser_advance_assert(state, tULIDENT);
2741
+
2742
+ new_name = ID2SYM(INTERN_TOKEN(state, state->current_token));
2743
+ new_name_range = state->current_token.range;
2744
+ clause_range.end = new_name_range.end;
2745
+ }
2746
+
2747
+ VALUE location = rbs_new_location(state->buffer, clause_range);
2748
+ rbs_loc *loc = rbs_check_location(location);
2749
+ rbs_loc_alloc_children(loc, 3);
2750
+ rbs_loc_add_required_child(loc, rb_intern("type_name"), type_name_range);
2751
+ rbs_loc_add_optional_child(loc, rb_intern("keyword"), keyword_range);
2752
+ rbs_loc_add_optional_child(loc, rb_intern("new_name"), new_name_range);
2753
+
2754
+ rb_ary_push(clauses, rbs_ast_directives_use_single_clause(type_name, new_name, location));
2755
+
2756
+ break;
2757
+ }
2758
+ case pSTAR:
2759
+ {
2760
+ range clause_range = namespace_range;
2761
+ parser_advance(state);
2762
+
2763
+ range star_range = state->current_token.range;
2764
+ clause_range.end = star_range.end;
2765
+
2766
+ VALUE location = rbs_new_location(state->buffer, clause_range);
2767
+ rbs_loc *loc = rbs_check_location(location);
2768
+ rbs_loc_alloc_children(loc, 2);
2769
+ rbs_loc_add_required_child(loc, rb_intern("namespace"), namespace_range);
2770
+ rbs_loc_add_required_child(loc, rb_intern("star"), star_range);
2771
+
2772
+ rb_ary_push(clauses, rbs_ast_directives_use_wildcard_clause(namespace, location));
2773
+
2774
+ break;
2775
+ }
2776
+ default:
2777
+ raise_syntax_error(
2778
+ state,
2779
+ state->next_token,
2780
+ "use clause is expected"
2781
+ );
2782
+ }
2783
+
2784
+ if (state->next_token.type == pCOMMA) {
2785
+ parser_advance(state);
2786
+ } else {
2787
+ break;
2788
+ }
2789
+ }
2790
+
2791
+ return;
2792
+ }
2793
+
2794
+ /*
2795
+ use_directive ::= {} `use` <clauses>
2796
+ */
2797
+ static VALUE parse_use_directive(parserstate *state) {
2798
+ if (state->next_token.type == kUSE) {
2799
+ parser_advance(state);
2800
+
2801
+ range keyword_range = state->current_token.range;
2802
+
2803
+ VALUE clauses = rb_ary_new();
2804
+ parse_use_clauses(state, clauses);
2805
+
2806
+ range directive_range = keyword_range;
2807
+ directive_range.end = state->current_token.range.end;
2808
+
2809
+ VALUE location = rbs_new_location(state->buffer, directive_range);
2810
+ rbs_loc *loc = rbs_check_location(location);
2811
+ rbs_loc_alloc_children(loc, 1);
2812
+ rbs_loc_add_required_child(loc, rb_intern("keyword"), keyword_range);
2813
+
2814
+ return rbs_ast_directives_use(clauses, location);
2815
+ } else {
2816
+ return Qnil;
2817
+ }
2818
+ }
2819
+
2494
2820
  VALUE parse_signature(parserstate *state) {
2495
- VALUE decls = rb_ary_new();
2821
+ VALUE dirs = EMPTY_ARRAY;
2822
+ VALUE decls = EMPTY_ARRAY;
2823
+
2824
+ while (state->next_token.type == kUSE) {
2825
+ melt_array(&dirs);
2826
+ rb_ary_push(dirs, parse_use_directive(state));
2827
+ }
2496
2828
 
2497
2829
  while (state->next_token.type != pEOF) {
2830
+ melt_array(&decls);
2498
2831
  rb_ary_push(decls, parse_decl(state));
2499
2832
  }
2500
2833
 
2501
- return decls;
2834
+ VALUE ret = rb_ary_new();
2835
+ rb_ary_push(ret, dirs);
2836
+ rb_ary_push(ret, decls);
2837
+ return ret;
2502
2838
  }
2503
2839
 
2840
+ struct parse_type_arg {
2841
+ parserstate *parser;
2842
+ VALUE require_eof;
2843
+ };
2844
+
2504
2845
  static VALUE
2505
- rbsparser_parse_type(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos, VALUE variables, VALUE requires_eof)
2506
- {
2507
- parserstate *parser = alloc_parser(buffer, FIX2INT(start_pos), FIX2INT(end_pos), variables);
2846
+ ensure_free_parser(VALUE parser) {
2847
+ free_parser((parserstate *)parser);
2848
+ return Qnil;
2849
+ }
2850
+
2851
+ static VALUE
2852
+ parse_type_try(VALUE a) {
2853
+ struct parse_type_arg *arg = (struct parse_type_arg *)a;
2508
2854
 
2509
- if (parser->next_token.type == pEOF) {
2855
+ if (arg->parser->next_token.type == pEOF) {
2510
2856
  return Qnil;
2511
2857
  }
2512
2858
 
2513
- VALUE type = parse_type(parser);
2859
+ VALUE type = parse_type(arg->parser);
2514
2860
 
2515
- if (RTEST(requires_eof)) {
2516
- parser_advance_assert(parser, pEOF);
2861
+ if (RB_TEST(arg->require_eof)) {
2862
+ parser_advance_assert(arg->parser, pEOF);
2517
2863
  }
2518
2864
 
2519
- free_parser(parser);
2520
-
2521
2865
  return type;
2522
2866
  }
2523
2867
 
2524
2868
  static VALUE
2525
- rbsparser_parse_method_type(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos, VALUE variables, VALUE requires_eof)
2869
+ rbsparser_parse_type(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos, VALUE variables, VALUE require_eof)
2526
2870
  {
2527
- parserstate *parser = alloc_parser(buffer, FIX2INT(start_pos), FIX2INT(end_pos), variables);
2871
+ VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
2872
+ StringValue(string);
2873
+ lexstate *lexer = alloc_lexer(string, FIX2INT(start_pos), FIX2INT(end_pos));
2874
+ parserstate *parser = alloc_parser(buffer, lexer, FIX2INT(start_pos), FIX2INT(end_pos), variables);
2875
+ struct parse_type_arg arg = {
2876
+ parser,
2877
+ require_eof
2878
+ };
2879
+ return rb_ensure(parse_type_try, (VALUE)&arg, ensure_free_parser, (VALUE)parser);
2880
+ }
2881
+
2882
+ static VALUE
2883
+ parse_method_type_try(VALUE a) {
2884
+ struct parse_type_arg *arg = (struct parse_type_arg *)a;
2528
2885
 
2529
- if (parser->next_token.type == pEOF) {
2886
+ if (arg->parser->next_token.type == pEOF) {
2530
2887
  return Qnil;
2531
2888
  }
2532
2889
 
2533
- VALUE method_type = parse_method_type(parser);
2890
+ VALUE method_type = parse_method_type(arg->parser);
2534
2891
 
2535
- if (RTEST(requires_eof)) {
2536
- parser_advance_assert(parser, pEOF);
2892
+ if (RB_TEST(arg->require_eof)) {
2893
+ parser_advance_assert(arg->parser, pEOF);
2537
2894
  }
2538
2895
 
2539
- free_parser(parser);
2540
-
2541
2896
  return method_type;
2542
2897
  }
2543
2898
 
2899
+ static VALUE
2900
+ rbsparser_parse_method_type(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos, VALUE variables, VALUE require_eof)
2901
+ {
2902
+ VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
2903
+ StringValue(string);
2904
+ lexstate *lexer = alloc_lexer(string, FIX2INT(start_pos), FIX2INT(end_pos));
2905
+ parserstate *parser = alloc_parser(buffer, lexer, FIX2INT(start_pos), FIX2INT(end_pos), variables);
2906
+ struct parse_type_arg arg = {
2907
+ parser,
2908
+ require_eof
2909
+ };
2910
+ return rb_ensure(parse_method_type_try, (VALUE)&arg, ensure_free_parser, (VALUE)parser);
2911
+ }
2912
+
2913
+ static VALUE
2914
+ parse_signature_try(VALUE a) {
2915
+ parserstate *parser = (parserstate *)a;
2916
+ return parse_signature(parser);
2917
+ }
2918
+
2544
2919
  static VALUE
2545
2920
  rbsparser_parse_signature(VALUE self, VALUE buffer, VALUE end_pos)
2546
2921
  {
2547
- parserstate *parser = alloc_parser(buffer, 0, FIX2INT(end_pos), Qnil);
2548
- VALUE signature = parse_signature(parser);
2549
- free_parser(parser);
2922
+ VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
2923
+ StringValue(string);
2924
+ lexstate *lexer = alloc_lexer(string, 0, FIX2INT(end_pos));
2925
+ parserstate *parser = alloc_parser(buffer, lexer, 0, FIX2INT(end_pos), Qnil);
2926
+ return rb_ensure(parse_signature_try, (VALUE)parser, ensure_free_parser, (VALUE)parser);
2927
+ }
2928
+
2929
+ static VALUE
2930
+ rbsparser_lex(VALUE self, VALUE buffer, VALUE end_pos) {
2931
+ VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
2932
+ StringValue(string);
2933
+ lexstate *lexer = alloc_lexer(string, 0, FIX2INT(end_pos));
2934
+ VALUE results = rb_ary_new();
2550
2935
 
2551
- return signature;
2936
+ token token = NullToken;
2937
+ while (token.type != pEOF) {
2938
+ token = rbsparser_next_token(lexer);
2939
+ VALUE type = ID2SYM(rb_intern(token_type_str(token.type)));
2940
+ VALUE location = rbs_new_location(buffer, token.range);
2941
+ VALUE pair = rb_ary_new3(2, type, location);
2942
+ rb_ary_push(results, pair);
2943
+ }
2944
+
2945
+ free(lexer);
2946
+
2947
+ return results;
2552
2948
  }
2553
2949
 
2554
2950
  void rbs__init_parser(void) {
2555
2951
  RBS_Parser = rb_define_class_under(RBS, "Parser", rb_cObject);
2952
+ rb_gc_register_mark_object(RBS_Parser);
2953
+ VALUE empty_array = rb_obj_freeze(rb_ary_new());
2954
+ rb_gc_register_mark_object(empty_array);
2955
+ EMPTY_ARRAY = empty_array;
2956
+
2556
2957
  rb_define_singleton_method(RBS_Parser, "_parse_type", rbsparser_parse_type, 5);
2557
2958
  rb_define_singleton_method(RBS_Parser, "_parse_method_type", rbsparser_parse_method_type, 5);
2558
2959
  rb_define_singleton_method(RBS_Parser, "_parse_signature", rbsparser_parse_signature, 2);
2960
+ rb_define_singleton_method(RBS_Parser, "_lex", rbsparser_lex, 2);
2559
2961
  }