rbs 3.9.4 → 4.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (354) hide show
  1. checksums.yaml +4 -4
  2. data/.clang-format +75 -0
  3. data/.clangd +2 -0
  4. data/.dockerignore +37 -0
  5. data/.gitattributes +1 -0
  6. data/.github/dependabot.yml +16 -14
  7. data/.github/workflows/bundle-update.yml +63 -0
  8. data/.github/workflows/c-check.yml +66 -0
  9. data/.github/workflows/changelog.yml +121 -0
  10. data/.github/workflows/comments.yml +6 -4
  11. data/.github/workflows/dependabot.yml +2 -2
  12. data/.github/workflows/jruby.yml +74 -0
  13. data/.github/workflows/release-gems.yml +234 -0
  14. data/.github/workflows/ruby.yml +86 -32
  15. data/.github/workflows/rust.yml +186 -0
  16. data/.github/workflows/truffleruby.yml +54 -0
  17. data/.github/workflows/typecheck.yml +6 -3
  18. data/.github/workflows/wasm.yml +55 -0
  19. data/.github/workflows/windows.yml +10 -4
  20. data/.gitignore +16 -0
  21. data/.rubocop.yml +1 -2
  22. data/CHANGELOG.md +502 -0
  23. data/Dockerfile.jruby +53 -0
  24. data/README.md +42 -5
  25. data/Rakefile +892 -111
  26. data/Steepfile +11 -0
  27. data/config.yml +665 -62
  28. data/core/array.rbs +541 -398
  29. data/core/basic_object.rbs +9 -8
  30. data/core/binding.rbs +0 -2
  31. data/core/builtin.rbs +9 -8
  32. data/core/class.rbs +11 -8
  33. data/core/comparable.rbs +55 -34
  34. data/core/complex.rbs +104 -78
  35. data/core/dir.rbs +61 -49
  36. data/core/encoding.rbs +12 -15
  37. data/core/enumerable.rbs +297 -196
  38. data/core/enumerator/arithmetic_sequence.rbs +70 -0
  39. data/core/enumerator/product.rbs +5 -5
  40. data/core/enumerator.rbs +91 -28
  41. data/core/errno.rbs +11 -2
  42. data/core/errors.rbs +58 -29
  43. data/core/exception.rbs +13 -13
  44. data/core/fiber.rbs +74 -54
  45. data/core/file.rbs +260 -1151
  46. data/core/file_constants.rbs +463 -0
  47. data/core/file_stat.rbs +534 -0
  48. data/core/file_test.rbs +3 -3
  49. data/core/float.rbs +257 -116
  50. data/core/gc.rbs +425 -281
  51. data/core/hash.rbs +1151 -829
  52. data/core/integer.rbs +156 -195
  53. data/core/io/buffer.rbs +53 -42
  54. data/core/io/wait.rbs +13 -35
  55. data/core/io.rbs +222 -155
  56. data/core/kernel.rbs +239 -163
  57. data/core/marshal.rbs +4 -4
  58. data/core/match_data.rbs +16 -14
  59. data/core/math.rbs +107 -66
  60. data/core/method.rbs +69 -33
  61. data/core/module.rbs +302 -150
  62. data/core/nil_class.rbs +7 -6
  63. data/core/numeric.rbs +77 -63
  64. data/core/object.rbs +9 -11
  65. data/core/object_space/weak_key_map.rbs +7 -7
  66. data/core/object_space.rbs +30 -23
  67. data/core/pathname.rbs +1312 -0
  68. data/core/proc.rbs +95 -58
  69. data/core/process.rbs +222 -202
  70. data/core/ractor.rbs +364 -518
  71. data/core/random.rbs +21 -3
  72. data/core/range.rbs +181 -79
  73. data/core/rational.rbs +60 -89
  74. data/core/rbs/ops.rbs +154 -0
  75. data/core/rbs/unnamed/argf.rbs +63 -56
  76. data/core/rbs/unnamed/env_class.rbs +19 -14
  77. data/core/rbs/unnamed/main_class.rbs +123 -0
  78. data/core/rbs/unnamed/random.rbs +11 -118
  79. data/core/regexp.rbs +258 -214
  80. data/core/ruby.rbs +53 -0
  81. data/core/ruby_vm.rbs +78 -34
  82. data/core/rubygems/config_file.rbs +5 -5
  83. data/core/rubygems/errors.rbs +6 -70
  84. data/core/rubygems/requirement.rbs +5 -15
  85. data/core/rubygems/rubygems.rbs +18 -81
  86. data/core/rubygems/specification.rbs +8 -0
  87. data/core/rubygems/version.rbs +2 -163
  88. data/core/set.rbs +493 -363
  89. data/core/signal.rbs +26 -16
  90. data/core/string.rbs +3234 -1285
  91. data/core/struct.rbs +43 -42
  92. data/core/symbol.rbs +41 -34
  93. data/core/thread.rbs +139 -83
  94. data/core/time.rbs +81 -50
  95. data/core/trace_point.rbs +41 -35
  96. data/core/true_class.rbs +2 -2
  97. data/core/unbound_method.rbs +24 -16
  98. data/core/warning.rbs +7 -7
  99. data/docs/CONTRIBUTING.md +3 -2
  100. data/docs/aliases.md +79 -0
  101. data/docs/collection.md +3 -3
  102. data/docs/config.md +171 -0
  103. data/docs/encoding.md +56 -0
  104. data/docs/gem.md +0 -1
  105. data/docs/inline.md +634 -0
  106. data/docs/rbs_by_example.md +20 -20
  107. data/docs/release.md +303 -0
  108. data/docs/rust.md +96 -0
  109. data/docs/sigs.md +3 -3
  110. data/docs/stdlib.md +8 -0
  111. data/docs/syntax.md +60 -18
  112. data/docs/type_fingerprint.md +21 -0
  113. data/docs/wasm_serialization.md +80 -0
  114. data/exe/rbs +1 -1
  115. data/ext/rbs_extension/ast_translation.c +1870 -0
  116. data/ext/rbs_extension/ast_translation.h +41 -0
  117. data/ext/rbs_extension/class_constants.c +189 -0
  118. data/{include/rbs/constants.h → ext/rbs_extension/class_constants.h} +24 -1
  119. data/ext/rbs_extension/compat.h +10 -0
  120. data/ext/rbs_extension/extconf.rb +26 -1
  121. data/ext/rbs_extension/legacy_location.c +299 -0
  122. data/ext/rbs_extension/legacy_location.h +82 -0
  123. data/ext/rbs_extension/main.c +639 -23
  124. data/ext/rbs_extension/rbs_extension.h +6 -21
  125. data/ext/rbs_extension/rbs_string_bridging.c +9 -0
  126. data/ext/rbs_extension/rbs_string_bridging.h +24 -0
  127. data/include/rbs/ast.h +1055 -0
  128. data/include/rbs/defines.h +104 -0
  129. data/include/rbs/lexer.h +208 -0
  130. data/include/rbs/location.h +40 -0
  131. data/include/rbs/parser.h +187 -0
  132. data/include/rbs/serialize.h +39 -0
  133. data/include/rbs/string.h +47 -0
  134. data/include/rbs/util/rbs_allocator.h +59 -0
  135. data/include/rbs/util/rbs_assert.h +20 -0
  136. data/include/rbs/util/rbs_buffer.h +83 -0
  137. data/include/rbs/util/rbs_constant_pool.h +6 -70
  138. data/include/rbs/util/rbs_encoding.h +282 -0
  139. data/include/rbs/util/rbs_unescape.h +24 -0
  140. data/include/rbs.h +9 -2
  141. data/lib/rbs/annotate/formatter.rb +3 -13
  142. data/lib/rbs/annotate/rdoc_annotator.rb +30 -32
  143. data/lib/rbs/annotate/rdoc_source.rb +1 -1
  144. data/lib/rbs/ast/annotation.rb +1 -1
  145. data/lib/rbs/ast/comment.rb +1 -1
  146. data/lib/rbs/ast/declarations.rb +11 -11
  147. data/lib/rbs/ast/members.rb +14 -14
  148. data/lib/rbs/ast/ruby/annotations.rb +451 -0
  149. data/lib/rbs/ast/ruby/comment_block.rb +247 -0
  150. data/lib/rbs/ast/ruby/declarations.rb +291 -0
  151. data/lib/rbs/ast/ruby/helpers/constant_helper.rb +28 -0
  152. data/lib/rbs/ast/ruby/helpers/location_helper.rb +15 -0
  153. data/lib/rbs/ast/ruby/members.rb +762 -0
  154. data/lib/rbs/ast/type_param.rb +25 -5
  155. data/lib/rbs/buffer.rb +142 -20
  156. data/lib/rbs/cli/diff.rb +16 -15
  157. data/lib/rbs/cli/validate.rb +63 -126
  158. data/lib/rbs/cli.rb +59 -29
  159. data/lib/rbs/collection/config/lockfile_generator.rb +28 -3
  160. data/lib/rbs/collection/sources/git.rb +7 -0
  161. data/lib/rbs/definition.rb +6 -1
  162. data/lib/rbs/definition_builder/ancestor_builder.rb +129 -70
  163. data/lib/rbs/definition_builder/method_builder.rb +74 -33
  164. data/lib/rbs/definition_builder.rb +181 -21
  165. data/lib/rbs/diff.rb +7 -1
  166. data/lib/rbs/environment/class_entry.rb +81 -0
  167. data/lib/rbs/environment/module_entry.rb +91 -0
  168. data/lib/rbs/environment.rb +410 -215
  169. data/lib/rbs/environment_loader.rb +2 -8
  170. data/lib/rbs/errors.rb +31 -21
  171. data/lib/rbs/inline_parser/comment_association.rb +117 -0
  172. data/lib/rbs/inline_parser.rb +568 -0
  173. data/lib/rbs/location_aux.rb +36 -4
  174. data/lib/rbs/locator.rb +5 -1
  175. data/lib/rbs/method_type.rb +5 -3
  176. data/lib/rbs/namespace.rb +47 -18
  177. data/lib/rbs/parser_aux.rb +37 -7
  178. data/lib/rbs/prototype/helpers.rb +24 -0
  179. data/lib/rbs/prototype/rb.rb +3 -28
  180. data/lib/rbs/prototype/rbi.rb +196 -45
  181. data/lib/rbs/prototype/runtime/value_object_generator.rb +0 -1
  182. data/lib/rbs/prototype/runtime.rb +13 -3
  183. data/lib/rbs/resolver/constant_resolver.rb +2 -2
  184. data/lib/rbs/resolver/type_name_resolver.rb +120 -44
  185. data/lib/rbs/rewriter.rb +70 -0
  186. data/lib/rbs/source.rb +99 -0
  187. data/lib/rbs/subtractor.rb +7 -4
  188. data/lib/rbs/test/type_check.rb +25 -3
  189. data/lib/rbs/type_name.rb +34 -21
  190. data/lib/rbs/types.rb +135 -81
  191. data/lib/rbs/unit_test/convertibles.rb +1 -0
  192. data/lib/rbs/unit_test/type_assertions.rb +47 -11
  193. data/lib/rbs/validator.rb +2 -2
  194. data/lib/rbs/version.rb +1 -1
  195. data/lib/rbs/wasm/deserializer.rb +213 -0
  196. data/lib/rbs/wasm/location.rb +61 -0
  197. data/lib/rbs/wasm/parser.rb +174 -0
  198. data/lib/rbs/wasm/runtime.rb +211 -0
  199. data/lib/rbs/wasm/serialization_schema.rb +111 -0
  200. data/lib/rbs.rb +25 -2
  201. data/lib/rbs_jars.rb +39 -0
  202. data/lib/rdoc/discover.rb +1 -1
  203. data/lib/rdoc_plugin/parser.rb +8 -3
  204. data/rbs.gemspec +34 -6
  205. data/schema/function.json +12 -1
  206. data/schema/typeParam.json +17 -1
  207. data/sig/ancestor_builder.rbs +1 -1
  208. data/sig/annotate/formatter.rbs +2 -2
  209. data/sig/annotate/rdoc_annotater.rbs +13 -10
  210. data/sig/ast/ruby/annotations.rbs +470 -0
  211. data/sig/ast/ruby/comment_block.rbs +127 -0
  212. data/sig/ast/ruby/declarations.rbs +158 -0
  213. data/sig/ast/ruby/helpers/constant_helper.rbs +11 -0
  214. data/sig/ast/ruby/helpers/location_helper.rbs +15 -0
  215. data/sig/ast/ruby/members.rbs +198 -0
  216. data/sig/buffer.rbs +81 -5
  217. data/sig/cli/diff.rbs +5 -11
  218. data/sig/cli/validate.rbs +12 -8
  219. data/sig/cli.rbs +18 -18
  220. data/sig/collection/config/lockfile_generator.rbs +2 -0
  221. data/sig/definition.rbs +6 -0
  222. data/sig/definition_builder.rbs +3 -1
  223. data/sig/environment/class_entry.rbs +56 -0
  224. data/sig/environment/module_entry.rbs +65 -0
  225. data/sig/environment.rbs +94 -87
  226. data/sig/errors.rbs +26 -20
  227. data/sig/inline_parser/comment_association.rbs +71 -0
  228. data/sig/inline_parser.rbs +126 -0
  229. data/sig/location.rbs +32 -7
  230. data/sig/locator.rbs +0 -2
  231. data/sig/manifest.yaml +0 -2
  232. data/sig/method_builder.rbs +9 -4
  233. data/sig/namespace.rbs +20 -5
  234. data/sig/parser.rbs +79 -15
  235. data/sig/prototype/helpers.rbs +2 -0
  236. data/sig/prototype/rbi.rbs +33 -4
  237. data/sig/resolver/type_name_resolver.rbs +36 -10
  238. data/sig/rewriter.rbs +45 -0
  239. data/sig/source.rbs +48 -0
  240. data/sig/type_param.rbs +13 -8
  241. data/sig/typename.rbs +15 -5
  242. data/sig/types.rbs +21 -9
  243. data/sig/unit_test/spy.rbs +0 -8
  244. data/sig/unit_test/type_assertions.rbs +17 -2
  245. data/sig/wasm/deserializer.rbs +66 -0
  246. data/sig/wasm/serialization_schema.rbs +13 -0
  247. data/src/ast.c +1644 -0
  248. data/src/lexer.c +3223 -0
  249. data/src/lexer.re +187 -0
  250. data/src/lexstate.c +222 -0
  251. data/src/location.c +31 -0
  252. data/src/parser.c +4318 -0
  253. data/src/serialize.c +965 -0
  254. data/src/string.c +41 -0
  255. data/src/util/rbs_allocator.c +171 -0
  256. data/src/util/rbs_assert.c +19 -0
  257. data/src/util/rbs_buffer.c +54 -0
  258. data/src/util/rbs_constant_pool.c +18 -92
  259. data/src/util/rbs_encoding.c +21454 -0
  260. data/src/util/rbs_unescape.c +167 -0
  261. data/stdlib/abbrev/0/array.rbs +1 -1
  262. data/stdlib/bigdecimal/0/big_decimal.rbs +116 -98
  263. data/stdlib/bigdecimal-math/0/big_math.rbs +169 -8
  264. data/stdlib/cgi/0/core.rbs +9 -393
  265. data/stdlib/cgi/0/manifest.yaml +1 -0
  266. data/stdlib/cgi-escape/0/escape.rbs +171 -0
  267. data/stdlib/coverage/0/coverage.rbs +7 -4
  268. data/stdlib/csv/0/csv.rbs +5 -5
  269. data/stdlib/date/0/date.rbs +92 -79
  270. data/stdlib/date/0/date_time.rbs +25 -24
  271. data/stdlib/delegate/0/delegator.rbs +11 -7
  272. data/stdlib/did_you_mean/0/did_you_mean.rbs +17 -16
  273. data/stdlib/digest/0/digest.rbs +117 -1
  274. data/stdlib/erb/0/erb.rbs +754 -353
  275. data/stdlib/etc/0/etc.rbs +73 -54
  276. data/stdlib/fileutils/0/fileutils.rbs +179 -160
  277. data/stdlib/forwardable/0/forwardable.rbs +13 -10
  278. data/stdlib/io-console/0/io-console.rbs +2 -2
  279. data/stdlib/ipaddr/0/ipaddr.rbs +0 -5
  280. data/stdlib/json/0/json.rbs +232 -185
  281. data/stdlib/monitor/0/monitor.rbs +9 -9
  282. data/stdlib/net-http/0/net-http.rbs +162 -134
  283. data/stdlib/objspace/0/objspace.rbs +17 -34
  284. data/stdlib/open-uri/0/open-uri.rbs +48 -8
  285. data/stdlib/open3/0/open3.rbs +469 -10
  286. data/stdlib/openssl/0/openssl.rbs +521 -397
  287. data/stdlib/optparse/0/optparse.rbs +26 -17
  288. data/stdlib/pathname/0/pathname.rbs +11 -1381
  289. data/stdlib/pp/0/pp.rbs +9 -8
  290. data/stdlib/prettyprint/0/prettyprint.rbs +7 -7
  291. data/stdlib/pstore/0/pstore.rbs +35 -30
  292. data/stdlib/psych/0/psych.rbs +65 -12
  293. data/stdlib/psych/0/store.rbs +2 -4
  294. data/stdlib/pty/0/pty.rbs +9 -6
  295. data/stdlib/random-formatter/0/random-formatter.rbs +277 -0
  296. data/stdlib/rdoc/0/code_object.rbs +4 -3
  297. data/stdlib/rdoc/0/comment.rbs +2 -0
  298. data/stdlib/rdoc/0/options.rbs +76 -0
  299. data/stdlib/rdoc/0/parser.rbs +1 -1
  300. data/stdlib/rdoc/0/rdoc.rbs +7 -5
  301. data/stdlib/rdoc/0/store.rbs +2 -2
  302. data/stdlib/resolv/0/resolv.rbs +26 -69
  303. data/stdlib/ripper/0/ripper.rbs +25 -19
  304. data/stdlib/securerandom/0/manifest.yaml +2 -0
  305. data/stdlib/securerandom/0/securerandom.rbs +7 -20
  306. data/stdlib/shellwords/0/shellwords.rbs +3 -3
  307. data/stdlib/singleton/0/singleton.rbs +6 -0
  308. data/stdlib/socket/0/addrinfo.rbs +9 -9
  309. data/stdlib/socket/0/basic_socket.rbs +3 -3
  310. data/stdlib/socket/0/ip_socket.rbs +10 -8
  311. data/stdlib/socket/0/socket.rbs +23 -10
  312. data/stdlib/socket/0/tcp_server.rbs +1 -1
  313. data/stdlib/socket/0/tcp_socket.rbs +11 -3
  314. data/stdlib/socket/0/udp_socket.rbs +1 -1
  315. data/stdlib/socket/0/unix_server.rbs +1 -1
  316. data/stdlib/stringio/0/stringio.rbs +1211 -96
  317. data/stdlib/strscan/0/string_scanner.rbs +101 -80
  318. data/stdlib/tempfile/0/manifest.yaml +3 -0
  319. data/stdlib/tempfile/0/tempfile.rbs +25 -21
  320. data/stdlib/time/0/time.rbs +8 -6
  321. data/stdlib/timeout/0/timeout.rbs +58 -7
  322. data/stdlib/tsort/0/cyclic.rbs +4 -1
  323. data/stdlib/tsort/0/interfaces.rbs +8 -8
  324. data/stdlib/tsort/0/tsort.rbs +16 -15
  325. data/stdlib/uri/0/common.rbs +42 -20
  326. data/stdlib/uri/0/file.rbs +3 -3
  327. data/stdlib/uri/0/generic.rbs +21 -18
  328. data/stdlib/uri/0/http.rbs +2 -2
  329. data/stdlib/uri/0/ldap.rbs +2 -2
  330. data/stdlib/uri/0/mailto.rbs +3 -3
  331. data/stdlib/uri/0/rfc2396_parser.rbs +12 -12
  332. data/stdlib/zlib/0/deflate.rbs +4 -3
  333. data/stdlib/zlib/0/gzip_file.rbs +1 -1
  334. data/stdlib/zlib/0/gzip_reader.rbs +8 -8
  335. data/stdlib/zlib/0/gzip_writer.rbs +16 -13
  336. data/stdlib/zlib/0/inflate.rbs +1 -1
  337. data/stdlib/zlib/0/need_dict.rbs +1 -1
  338. data/wasm/README.md +109 -0
  339. data/wasm/rbs_wasm.c +479 -0
  340. metadata +133 -19
  341. data/ext/rbs_extension/lexer.c +0 -2728
  342. data/ext/rbs_extension/lexer.h +0 -179
  343. data/ext/rbs_extension/lexer.re +0 -147
  344. data/ext/rbs_extension/lexstate.c +0 -175
  345. data/ext/rbs_extension/location.c +0 -325
  346. data/ext/rbs_extension/location.h +0 -85
  347. data/ext/rbs_extension/parser.c +0 -2982
  348. data/ext/rbs_extension/parser.h +0 -18
  349. data/ext/rbs_extension/parserstate.c +0 -411
  350. data/ext/rbs_extension/parserstate.h +0 -163
  351. data/ext/rbs_extension/unescape.c +0 -32
  352. data/include/rbs/ruby_objs.h +0 -72
  353. data/src/constants.c +0 -153
  354. data/src/ruby_objs.c +0 -799
data/src/parser.c ADDED
@@ -0,0 +1,4318 @@
1
+ #include "rbs/parser.h"
2
+
3
+ #include <stdlib.h>
4
+ #include <stdarg.h>
5
+ #include <stdio.h>
6
+ #include <ctype.h>
7
+ #include <string.h>
8
+
9
+ #include "rbs/ast.h"
10
+ #include "rbs/defines.h"
11
+ #include "rbs/lexer.h"
12
+ #include "rbs/location.h"
13
+ #include "rbs/string.h"
14
+ #include "rbs/util/rbs_unescape.h"
15
+ #include "rbs/util/rbs_buffer.h"
16
+ #include "rbs/util/rbs_assert.h"
17
+
18
+ #define INTERN_TOKEN(parser, tok) \
19
+ rbs_constant_pool_insert_shared_with_encoding( \
20
+ &parser->constant_pool, \
21
+ (const uint8_t *) rbs_peek_token(parser->lexer, tok), \
22
+ rbs_token_bytes(tok), \
23
+ parser->lexer->encoding \
24
+ )
25
+
26
+ #define KEYWORD_CASES \
27
+ case kBOOL: \
28
+ case kBOT: \
29
+ case kCLASS: \
30
+ case kFALSE: \
31
+ case kINSTANCE: \
32
+ case kINTERFACE: \
33
+ case kNIL: \
34
+ case kSELF: \
35
+ case kSINGLETON: \
36
+ case kTOP: \
37
+ case kTRUE: \
38
+ case kVOID: \
39
+ case kTYPE: \
40
+ case kUNCHECKED: \
41
+ case kIN: \
42
+ case kOUT: \
43
+ case kEND: \
44
+ case kDEF: \
45
+ case kINCLUDE: \
46
+ case kEXTEND: \
47
+ case kPREPEND: \
48
+ case kALIAS: \
49
+ case kMODULE: \
50
+ case kATTRREADER: \
51
+ case kATTRWRITER: \
52
+ case kATTRACCESSOR: \
53
+ case kPUBLIC: \
54
+ case kPRIVATE: \
55
+ case kUNTYPED: \
56
+ case kUSE: \
57
+ case kAS: \
58
+ case k__TODO__: \
59
+ case kSKIP: \
60
+ case kRETURN: \
61
+ /* nop */
62
+
63
+ #define PARAM_NAME_CASES \
64
+ case kBOOL: \
65
+ case kBOT: \
66
+ case kCLASS: \
67
+ case kFALSE: \
68
+ case kINSTANCE: \
69
+ case kINTERFACE: \
70
+ case kNIL: \
71
+ case kSELF: \
72
+ case kSINGLETON: \
73
+ case kTOP: \
74
+ case kTRUE: \
75
+ case kVOID: \
76
+ case kTYPE: \
77
+ case kUNCHECKED: \
78
+ case kIN: \
79
+ case kOUT: \
80
+ case kEND: \
81
+ case kDEF: \
82
+ case kINCLUDE: \
83
+ case kEXTEND: \
84
+ case kPREPEND: \
85
+ case kALIAS: \
86
+ case kMODULE: \
87
+ case kATTRREADER: \
88
+ case kATTRWRITER: \
89
+ case kATTRACCESSOR: \
90
+ case kPUBLIC: \
91
+ case kPRIVATE: \
92
+ case kUNTYPED: \
93
+ case kUSE: \
94
+ case kAS: \
95
+ case k__TODO__: \
96
+ /* nop */
97
+
98
+ #define CHECK_PARSE(call) \
99
+ if (!call) { \
100
+ return false; \
101
+ }
102
+
103
+ #define ASSERT_TOKEN(parser, expected_type) \
104
+ if (parser->current_token.type != expected_type) { \
105
+ rbs_parser_set_error(parser, parser->current_token, true, "expected a token `%s`", rbs_token_type_str(expected_type)); \
106
+ return false; \
107
+ }
108
+
109
+ #define ADVANCE_ASSERT(parser, expected_type) \
110
+ do { \
111
+ rbs_parser_advance(parser); \
112
+ ASSERT_TOKEN(parser, expected_type) \
113
+ } while (0);
114
+
115
+ #define RESET_TABLE_P(table) (table->size == 0)
116
+
117
+ #define ALLOCATOR() parser->allocator
118
+
119
+ typedef struct {
120
+ rbs_node_list_t *required_positionals;
121
+ rbs_node_list_t *optional_positionals;
122
+ rbs_node_t *rest_positionals;
123
+ rbs_node_list_t *trailing_positionals;
124
+ rbs_hash_t *required_keywords;
125
+ rbs_hash_t *optional_keywords;
126
+ rbs_node_t *rest_keywords;
127
+ rbs_node_t *forwarding;
128
+ } method_params;
129
+
130
+ /**
131
+ * id_table represents a set of RBS constant IDs.
132
+ * This is used to manage the set of bound variables.
133
+ * */
134
+ typedef struct id_table {
135
+ size_t size;
136
+ size_t count;
137
+ rbs_constant_id_t *ids;
138
+ struct id_table *next;
139
+ } id_table;
140
+
141
+ static bool rbs_is_untyped_params(method_params *params) {
142
+ return params->required_positionals == NULL;
143
+ }
144
+
145
+ static rbs_location_range rbs_location_range_current_token(rbs_parser_t *parser) {
146
+ return RBS_RANGE_LEX2AST(parser->current_token.range);
147
+ }
148
+
149
+ static bool parse_optional(rbs_parser_t *parser, rbs_node_t **optional, bool void_allowed, bool self_allowed, bool classish_allowed);
150
+ static bool parse_simple(rbs_parser_t *parser, rbs_node_t **type, bool void_allowed, bool self_allowed, bool classish_allowed);
151
+
152
+ /**
153
+ * @returns A borrowed copy of the current token, which does *not* need to be freed.
154
+ */
155
+ static rbs_string_t rbs_parser_peek_current_token(rbs_parser_t *parser) {
156
+ rbs_range_t rg = parser->current_token.range;
157
+
158
+ const char *start = parser->lexer->string.start + rg.start.byte_pos;
159
+ size_t length = rg.end.byte_pos - rg.start.byte_pos;
160
+
161
+ return rbs_string_new(start, start + length);
162
+ }
163
+
164
+ static rbs_constant_id_t rbs_constant_pool_insert_string(rbs_constant_pool_t *self, rbs_string_t string) {
165
+ return rbs_constant_pool_insert_shared(self, (const uint8_t *) string.start, rbs_string_len(string));
166
+ }
167
+
168
+ typedef enum {
169
+ CLASS_NAME = 1,
170
+ INTERFACE_NAME = 2,
171
+ ALIAS_NAME = 4
172
+ } TypeNameKind;
173
+
174
+ static void parser_advance_no_gap(rbs_parser_t *parser) {
175
+ if (parser->current_token.range.end.byte_pos == parser->next_token.range.start.byte_pos) {
176
+ rbs_parser_advance(parser);
177
+ } else {
178
+ rbs_parser_set_error(parser, parser->next_token, true, "unexpected token");
179
+ }
180
+ }
181
+
182
+ /*
183
+ type_name ::= {`::`} (tUIDENT `::`)* <tXIDENT>
184
+ | {(tUIDENT `::`)*} <tXIDENT>
185
+ | {<tXIDENT>}
186
+ */
187
+ RBS_NODISCARD
188
+ static bool parse_type_name(rbs_parser_t *parser, TypeNameKind kind, rbs_range_t *rg, rbs_type_name_t **type_name) {
189
+ bool absolute = false;
190
+
191
+ if (rg) {
192
+ rg->start = parser->current_token.range.start;
193
+ }
194
+
195
+ if (parser->current_token.type == pCOLON2) {
196
+ absolute = true;
197
+ parser_advance_no_gap(parser);
198
+ }
199
+
200
+ rbs_node_list_t *path = rbs_node_list_new(ALLOCATOR());
201
+
202
+ while (
203
+ parser->current_token.type == tUIDENT && parser->next_token.type == pCOLON2 && parser->current_token.range.end.byte_pos == parser->next_token.range.start.byte_pos && parser->next_token.range.end.byte_pos == parser->next_token2.range.start.byte_pos
204
+ ) {
205
+ rbs_constant_id_t symbol_value = INTERN_TOKEN(parser, parser->current_token);
206
+ rbs_location_range symbol_range = RBS_RANGE_LEX2AST(parser->next_token.range);
207
+ rbs_ast_symbol_t *symbol = rbs_ast_symbol_new(ALLOCATOR(), symbol_range, &parser->constant_pool, symbol_value);
208
+ rbs_node_list_append(path, (rbs_node_t *) symbol);
209
+
210
+ rbs_parser_advance(parser);
211
+ rbs_parser_advance(parser);
212
+ }
213
+
214
+ rbs_range_t namespace_range = {
215
+ .start = rg->start,
216
+ .end = parser->current_token.range.end
217
+ };
218
+ rbs_namespace_t *ns = rbs_namespace_new(ALLOCATOR(), RBS_RANGE_LEX2AST(namespace_range), path, absolute);
219
+
220
+ switch (parser->current_token.type) {
221
+ case tLIDENT:
222
+ case kSKIP:
223
+ case kRETURN:
224
+ if (kind & ALIAS_NAME) goto success;
225
+ goto error_handling;
226
+ case tULIDENT:
227
+ if (kind & INTERFACE_NAME) goto success;
228
+ goto error_handling;
229
+ case tUIDENT:
230
+ if (kind & CLASS_NAME) goto success;
231
+ goto error_handling;
232
+ default:
233
+ goto error_handling;
234
+ }
235
+
236
+ success: {
237
+ if (rg) {
238
+ rg->end = parser->current_token.range.end;
239
+ }
240
+
241
+ rbs_location_range symbol_range = rbs_location_range_current_token(parser);
242
+ rbs_constant_id_t name = INTERN_TOKEN(parser, parser->current_token);
243
+ rbs_ast_symbol_t *symbol = rbs_ast_symbol_new(ALLOCATOR(), symbol_range, &parser->constant_pool, name);
244
+ *type_name = rbs_type_name_new(ALLOCATOR(), RBS_RANGE_LEX2AST(*rg), ns, symbol);
245
+ return true;
246
+ }
247
+
248
+ error_handling: {
249
+ const char *ids = NULL;
250
+ if (kind & ALIAS_NAME) {
251
+ ids = "alias name";
252
+ }
253
+ if (kind & INTERFACE_NAME) {
254
+ ids = "interface name";
255
+ }
256
+ if (kind & CLASS_NAME) {
257
+ ids = "class/module/constant name";
258
+ }
259
+
260
+ RBS_ASSERT(ids != NULL, "Unknown kind of type: %i", kind);
261
+
262
+ rbs_parser_set_error(parser, parser->current_token, true, "expected one of %s", ids);
263
+ return false;
264
+ }
265
+ }
266
+
267
+ /*
268
+ type_list ::= {} type `,` ... <`,`> eol
269
+ | {} type `,` ... `,` <type> eol
270
+ */
271
+ RBS_NODISCARD
272
+ static bool parse_type_list(rbs_parser_t *parser, enum RBSTokenType eol, rbs_node_list_t *types, bool void_allowed, bool self_allowed, bool classish_allowed) {
273
+ while (true) {
274
+ rbs_node_t *type;
275
+ CHECK_PARSE(rbs_parse_type(parser, &type, void_allowed, self_allowed, classish_allowed));
276
+ rbs_node_list_append(types, type);
277
+
278
+ if (parser->next_token.type == pCOMMA) {
279
+ rbs_parser_advance(parser);
280
+
281
+ if (parser->next_token.type == eol) {
282
+ break;
283
+ }
284
+ } else {
285
+ if (parser->next_token.type == eol) {
286
+ break;
287
+ } else {
288
+ rbs_parser_set_error(parser, parser->next_token, true, "comma delimited type list is expected");
289
+ return false;
290
+ }
291
+ }
292
+ }
293
+
294
+ return true;
295
+ }
296
+
297
+ /*
298
+ type_list_with_commas ::= {} type `,` ... <`,`> eol
299
+ | {} type `,` ... `,` <type> eol
300
+ */
301
+ RBS_NODISCARD
302
+ static bool parse_type_list_with_commas(rbs_parser_t *parser, enum RBSTokenType eol, rbs_node_list_t *types, rbs_location_range_list_t *comma_locations, bool void_allowed, bool self_allowed, bool classish_allowed) {
303
+ while (true) {
304
+ rbs_node_t *type;
305
+ CHECK_PARSE(rbs_parse_type(parser, &type, void_allowed, self_allowed, classish_allowed));
306
+ rbs_node_list_append(types, type);
307
+
308
+ if (parser->next_token.type == pCOMMA) {
309
+ rbs_location_range comma_loc = RBS_RANGE_LEX2AST(parser->next_token.range);
310
+ rbs_location_range_list_append(comma_locations, comma_loc);
311
+ rbs_parser_advance(parser);
312
+
313
+ if (parser->next_token.type == eol) {
314
+ // Handle trailing comma - for type applications, this is an error
315
+ if (eol == pRBRACKET) {
316
+ rbs_parser_set_error(parser, parser->next_token, true, "unexpected trailing comma");
317
+ return false;
318
+ }
319
+ break;
320
+ }
321
+ } else {
322
+ if (parser->next_token.type == eol) {
323
+ break;
324
+ } else {
325
+ rbs_parser_set_error(parser, parser->next_token, true, "comma delimited type list is expected");
326
+ return false;
327
+ }
328
+ }
329
+ }
330
+
331
+ return true;
332
+ }
333
+
334
+ static bool is_keyword_token(enum RBSTokenType type) {
335
+ switch (type) {
336
+ case tLIDENT:
337
+ case tUIDENT:
338
+ case tULIDENT:
339
+ case tULLIDENT:
340
+ case tNONASCIIIDENT:
341
+ case tQIDENT:
342
+ case tBANGIDENT:
343
+ KEYWORD_CASES
344
+ return true;
345
+ default:
346
+ return false;
347
+ }
348
+ }
349
+
350
+ /**
351
+ * Ruby lets a local variable name open with a character outside ASCII, and a
352
+ * parameter name is a local variable name. Nothing here reads the case of that
353
+ * character, so both spellings are a name.
354
+ * */
355
+ static bool is_param_name_token(enum RBSTokenType type) {
356
+ return type == tLIDENT || type == tNONASCIIIDENT;
357
+ }
358
+
359
+ /*
360
+ function_param ::= {} <type>
361
+ | {} type <param>
362
+ */
363
+ RBS_NODISCARD
364
+ static bool parse_function_param(rbs_parser_t *parser, rbs_types_function_param_t **function_param, bool self_allowed, bool classish_allowed) {
365
+ rbs_range_t type_range;
366
+ type_range.start = parser->next_token.range.start;
367
+ rbs_node_t *type;
368
+ CHECK_PARSE(rbs_parse_type(parser, &type, false, self_allowed, classish_allowed));
369
+ type_range.end = parser->current_token.range.end;
370
+
371
+ if (parser->next_token.type == pCOMMA || parser->next_token.type == pRPAREN) {
372
+ rbs_range_t param_range = type_range;
373
+
374
+ *function_param = rbs_types_function_param_new(ALLOCATOR(), RBS_RANGE_LEX2AST(param_range), type, NULL);
375
+ return true;
376
+ } else {
377
+ rbs_range_t name_range = parser->next_token.range;
378
+
379
+ rbs_parser_advance(parser);
380
+
381
+ rbs_range_t param_range = {
382
+ .start = type_range.start,
383
+ .end = name_range.end,
384
+ };
385
+
386
+ if (!is_keyword_token(parser->current_token.type)) {
387
+ rbs_parser_set_error(parser, parser->current_token, true, "unexpected token for function parameter name");
388
+ return false;
389
+ }
390
+
391
+ rbs_string_t unquoted_str = rbs_unquote_string(ALLOCATOR(), rbs_parser_peek_current_token(parser), parser->lexer->encoding);
392
+ rbs_location_range symbol_range = rbs_location_range_current_token(parser);
393
+ rbs_constant_id_t constant_id = rbs_constant_pool_insert_string(&parser->constant_pool, unquoted_str);
394
+ rbs_ast_symbol_t *name = rbs_ast_symbol_new(ALLOCATOR(), symbol_range, &parser->constant_pool, constant_id);
395
+
396
+ *function_param = rbs_types_function_param_new(ALLOCATOR(), RBS_RANGE_LEX2AST(param_range), type, name);
397
+ (*function_param)->name_range = RBS_RANGE_LEX2AST(name_range);
398
+ return true;
399
+ }
400
+ }
401
+
402
+ static rbs_constant_id_t intern_token_start_end(rbs_parser_t *parser, rbs_token_t start_token, rbs_token_t end_token) {
403
+ return rbs_constant_pool_insert_shared_with_encoding(
404
+ &parser->constant_pool,
405
+ (const uint8_t *) rbs_peek_token(parser->lexer, start_token),
406
+ end_token.range.end.byte_pos - start_token.range.start.byte_pos,
407
+ parser->lexer->encoding
408
+ );
409
+ }
410
+
411
+ /*
412
+ keyword_key ::= {} <keyword> `:`
413
+ | {} keyword <`?`> `:`
414
+ */
415
+ RBS_NODISCARD
416
+ static bool parse_keyword_key(rbs_parser_t *parser, rbs_ast_symbol_t **key) {
417
+ rbs_parser_advance(parser);
418
+
419
+ rbs_range_t symbol_range = parser->current_token.range;
420
+
421
+ if (parser->next_token.type == pQUESTION) {
422
+ // The `?` is part of the key, so it is part of the location too.
423
+ symbol_range.end = parser->next_token.range.end;
424
+
425
+ *key = rbs_ast_symbol_new(
426
+ ALLOCATOR(),
427
+ RBS_RANGE_LEX2AST(symbol_range),
428
+ &parser->constant_pool,
429
+ intern_token_start_end(parser, parser->current_token, parser->next_token)
430
+ );
431
+ rbs_parser_advance(parser);
432
+ } else {
433
+ *key = rbs_ast_symbol_new(ALLOCATOR(), RBS_RANGE_LEX2AST(symbol_range), &parser->constant_pool, INTERN_TOKEN(parser, parser->current_token));
434
+ }
435
+
436
+ return true;
437
+ }
438
+
439
+ /*
440
+ keyword ::= {} keyword `:` <function_param>
441
+ */
442
+ RBS_NODISCARD
443
+ static bool parse_keyword(rbs_parser_t *parser, rbs_hash_t *keywords, rbs_hash_t *memo, bool self_allowed, bool classish_allowed) {
444
+ rbs_ast_symbol_t *key = NULL;
445
+ CHECK_PARSE(parse_keyword_key(parser, &key));
446
+
447
+ if (rbs_hash_find(memo, (rbs_node_t *) key)) {
448
+ rbs_parser_set_error(parser, parser->current_token, true, "duplicated keyword argument");
449
+ return false;
450
+ } else {
451
+ rbs_location_range symbol_range = rbs_location_range_current_token(parser);
452
+ rbs_hash_set(memo, (rbs_node_t *) key, (rbs_node_t *) rbs_ast_bool_new(ALLOCATOR(), symbol_range, true));
453
+ }
454
+
455
+ ADVANCE_ASSERT(parser, pCOLON);
456
+ rbs_types_function_param_t *param = NULL;
457
+ CHECK_PARSE(parse_function_param(parser, &param, self_allowed, classish_allowed));
458
+
459
+ rbs_hash_set(keywords, (rbs_node_t *) key, (rbs_node_t *) param);
460
+
461
+ return true;
462
+ }
463
+
464
+ /*
465
+ Returns true if keyword is given.
466
+
467
+ is_keyword === {} KEYWORD `:`
468
+ */
469
+ static bool is_keyword(rbs_parser_t *parser) {
470
+ if (is_keyword_token(parser->next_token.type)) {
471
+ if (parser->next_token2.type == pCOLON && parser->next_token.range.end.byte_pos == parser->next_token2.range.start.byte_pos) {
472
+ return true;
473
+ }
474
+
475
+ if (parser->next_token2.type == pQUESTION && parser->next_token3.type == pCOLON && parser->next_token.range.end.byte_pos == parser->next_token2.range.start.byte_pos && parser->next_token2.range.end.byte_pos == parser->next_token3.range.start.byte_pos) {
476
+ return true;
477
+ }
478
+ }
479
+
480
+ return false;
481
+ }
482
+
483
+ /**
484
+ * Advance token if _next_ token is `type`.
485
+ * Ensures one token advance and `parser->current_token.type == type`, or current token not changed.
486
+ *
487
+ * @returns true if token advances, false otherwise.
488
+ **/
489
+ static bool parser_advance_if(rbs_parser_t *parser, enum RBSTokenType type) {
490
+ if (parser->next_token.type == type) {
491
+ rbs_parser_advance(parser);
492
+ return true;
493
+ } else {
494
+ return false;
495
+ }
496
+ }
497
+
498
+ /*
499
+ params ::= {} `)`
500
+ | {} `?` `)` -- Untyped function params (assign params.required = nil)
501
+ | {} `...` `)` -- Forwarding params
502
+ | <required_params> `)`
503
+ | <required_params> `,` `)`
504
+
505
+ required_params ::= {} function_param `,` <required_params>
506
+ | {} function_param `,` `...`
507
+ | {} <function_param>
508
+ | {} <optional_params>
509
+
510
+ optional_params ::= {} `?` function_param `,` <optional_params>
511
+ | {} `?` <function_param>
512
+ | {} <rest_params>
513
+
514
+ rest_params ::= {} `*` function_param `,` <trailing_params>
515
+ | {} `*` <function_param>
516
+ | {} <trailing_params>
517
+
518
+ trailing_params ::= {} function_param `,` <trailing_params>
519
+ | {} <function_param>
520
+ | {} <keywords>
521
+
522
+ keywords ::= {} required_keyword `,` <keywords>
523
+ | {} `?` optional_keyword `,` <keywords>
524
+ | {} `**` function_param `,` <keywords>
525
+ | {} <required_keyword>
526
+ | {} `?` <optional_keyword>
527
+ | {} `**` <function_param>
528
+ */
529
+ RBS_NODISCARD
530
+ static bool parse_params(rbs_parser_t *parser, method_params *params, bool forwarding_allowed, bool self_allowed, bool classish_allowed) {
531
+ if (parser->next_token.type == pQUESTION && parser->next_token2.type == pRPAREN) {
532
+ params->required_positionals = NULL;
533
+ rbs_parser_advance(parser);
534
+ return true;
535
+ }
536
+ if (parser->next_token.type == pRPAREN) {
537
+ return true;
538
+ }
539
+
540
+ rbs_hash_t *memo = rbs_hash_new(ALLOCATOR());
541
+
542
+ while (true) {
543
+ switch (parser->next_token.type) {
544
+ case pQUESTION:
545
+ goto PARSE_OPTIONAL_PARAMS;
546
+ case pSTAR:
547
+ goto PARSE_REST_PARAM;
548
+ case pSTAR2:
549
+ goto PARSE_KEYWORDS;
550
+ case pRPAREN:
551
+ goto EOP;
552
+
553
+ case pDOT3: {
554
+ if (!forwarding_allowed) {
555
+ rbs_parser_set_error(parser, parser->next_token, true, "forwarding parameter is not allowed in this context");
556
+ return false;
557
+ }
558
+
559
+ if (!parser->options.enable_forwarding_params) {
560
+ rbs_parser_set_error(parser, parser->next_token, true, "forwarding parameter syntax is not enabled");
561
+ return false;
562
+ }
563
+
564
+ rbs_parser_advance(parser);
565
+ params->forwarding = (rbs_node_t *) rbs_types_function_forwarding_param_new(
566
+ ALLOCATOR(),
567
+ rbs_location_range_current_token(parser)
568
+ );
569
+ goto EOP;
570
+ }
571
+ default:
572
+ if (is_keyword(parser)) {
573
+ goto PARSE_KEYWORDS;
574
+ }
575
+
576
+ rbs_types_function_param_t *param = NULL;
577
+ CHECK_PARSE(parse_function_param(parser, &param, self_allowed, classish_allowed));
578
+ rbs_node_list_append(params->required_positionals, (rbs_node_t *) param);
579
+
580
+ break;
581
+ }
582
+
583
+ if (!parser_advance_if(parser, pCOMMA)) {
584
+ goto EOP;
585
+ }
586
+ }
587
+
588
+ PARSE_OPTIONAL_PARAMS:
589
+ while (true) {
590
+ switch (parser->next_token.type) {
591
+ case pQUESTION: {
592
+ rbs_parser_advance(parser);
593
+
594
+ if (is_keyword(parser)) {
595
+ CHECK_PARSE(parse_keyword(parser, params->optional_keywords, memo, self_allowed, classish_allowed));
596
+ parser_advance_if(parser, pCOMMA);
597
+ goto PARSE_KEYWORDS;
598
+ }
599
+
600
+ rbs_types_function_param_t *param = NULL;
601
+ CHECK_PARSE(parse_function_param(parser, &param, self_allowed, classish_allowed));
602
+ rbs_node_list_append(params->optional_positionals, (rbs_node_t *) param);
603
+
604
+ break;
605
+ }
606
+ default:
607
+ goto PARSE_REST_PARAM;
608
+ }
609
+
610
+ if (!parser_advance_if(parser, pCOMMA)) {
611
+ goto EOP;
612
+ }
613
+ }
614
+
615
+ PARSE_REST_PARAM:
616
+ if (parser->next_token.type == pSTAR) {
617
+ rbs_parser_advance(parser);
618
+ rbs_types_function_param_t *param = NULL;
619
+ CHECK_PARSE(parse_function_param(parser, &param, self_allowed, classish_allowed));
620
+ params->rest_positionals = (rbs_node_t *) param;
621
+
622
+ if (!parser_advance_if(parser, pCOMMA)) {
623
+ goto EOP;
624
+ }
625
+ }
626
+ goto PARSE_TRAILING_PARAMS;
627
+
628
+ PARSE_TRAILING_PARAMS:
629
+ while (true) {
630
+ switch (parser->next_token.type) {
631
+ case pQUESTION:
632
+ goto PARSE_KEYWORDS;
633
+ case pSTAR:
634
+ goto EOP;
635
+ case pSTAR2:
636
+ goto PARSE_KEYWORDS;
637
+ case pRPAREN:
638
+ goto EOP;
639
+
640
+ default:
641
+ if (is_keyword(parser)) {
642
+ goto PARSE_KEYWORDS;
643
+ }
644
+
645
+ rbs_types_function_param_t *param = NULL;
646
+ CHECK_PARSE(parse_function_param(parser, &param, self_allowed, classish_allowed));
647
+ rbs_node_list_append(params->trailing_positionals, (rbs_node_t *) param);
648
+
649
+ break;
650
+ }
651
+
652
+ if (!parser_advance_if(parser, pCOMMA)) {
653
+ goto EOP;
654
+ }
655
+ }
656
+
657
+ PARSE_KEYWORDS:
658
+ while (true) {
659
+ switch (parser->next_token.type) {
660
+ case pQUESTION:
661
+ rbs_parser_advance(parser);
662
+ if (is_keyword(parser)) {
663
+ CHECK_PARSE(parse_keyword(parser, params->optional_keywords, memo, self_allowed, classish_allowed));
664
+ } else {
665
+ rbs_parser_set_error(parser, parser->next_token, true, "optional keyword argument type is expected");
666
+ return false;
667
+ }
668
+ break;
669
+
670
+ case pSTAR2: {
671
+ rbs_parser_advance(parser);
672
+ rbs_types_function_param_t *param = NULL;
673
+ CHECK_PARSE(parse_function_param(parser, &param, self_allowed, classish_allowed));
674
+ params->rest_keywords = (rbs_node_t *) param;
675
+ break;
676
+ }
677
+ case tUIDENT:
678
+ case tLIDENT:
679
+ case tQIDENT:
680
+ case tULIDENT:
681
+ case tULLIDENT:
682
+ case tNONASCIIIDENT:
683
+ case tBANGIDENT:
684
+ KEYWORD_CASES
685
+ if (is_keyword(parser)) {
686
+ CHECK_PARSE(parse_keyword(parser, params->required_keywords, memo, self_allowed, classish_allowed));
687
+ } else {
688
+ rbs_parser_set_error(parser, parser->next_token, true, "required keyword argument type is expected");
689
+ return false;
690
+ }
691
+ break;
692
+
693
+ default:
694
+ goto EOP;
695
+ }
696
+
697
+ if (!parser_advance_if(parser, pCOMMA)) {
698
+ goto EOP;
699
+ }
700
+ }
701
+
702
+ EOP:
703
+ if (parser->next_token.type != pRPAREN) {
704
+ rbs_parser_set_error(parser, parser->next_token, true, "unexpected token for method type parameters");
705
+ return false;
706
+ }
707
+
708
+ return true;
709
+ }
710
+
711
+ /*
712
+ optional ::= {} <simple_type>
713
+ | {} simple_type <`?`>
714
+ */
715
+ RBS_NODISCARD
716
+ static bool parse_optional(rbs_parser_t *parser, rbs_node_t **optional, bool void_allowed, bool self_allowed, bool classish_allowed) {
717
+ rbs_range_t rg;
718
+ rg.start = parser->next_token.range.start;
719
+
720
+ rbs_node_t *type = NULL;
721
+ CHECK_PARSE(parse_simple(parser, &type, void_allowed, self_allowed, classish_allowed));
722
+
723
+ if (parser->next_token.type == pQUESTION) {
724
+ if (void_allowed && type->type == RBS_TYPES_BASES_VOID) {
725
+ rbs_parser_set_error(parser, parser->current_token, true, "void type is not allowed here");
726
+ return false;
727
+ }
728
+
729
+ rbs_parser_advance(parser);
730
+ rg.end = parser->current_token.range.end;
731
+ *optional = (rbs_node_t *) rbs_types_optional_new(ALLOCATOR(), RBS_RANGE_LEX2AST(rg), type);
732
+ } else {
733
+ *optional = type;
734
+ }
735
+
736
+ return true;
737
+ }
738
+
739
+ static void initialize_method_params(method_params *params, rbs_allocator_t *allocator) {
740
+ *params = (method_params) {
741
+ .required_positionals = rbs_node_list_new(allocator),
742
+ .optional_positionals = rbs_node_list_new(allocator),
743
+ .rest_positionals = NULL,
744
+ .trailing_positionals = rbs_node_list_new(allocator),
745
+ .required_keywords = rbs_hash_new(allocator),
746
+ .optional_keywords = rbs_hash_new(allocator),
747
+ .rest_keywords = NULL,
748
+ .forwarding = NULL,
749
+ };
750
+ }
751
+
752
+ /*
753
+ self_type_binding ::= {} <>
754
+ | {} `[` `self` `:` type <`]`>
755
+ */
756
+ RBS_NODISCARD
757
+ static bool parse_self_type_binding(rbs_parser_t *parser, rbs_node_t **self_type, bool self_allowed, bool classish_allowed) {
758
+ if (parser->next_token.type == pLBRACKET) {
759
+ rbs_parser_advance(parser);
760
+ ADVANCE_ASSERT(parser, kSELF);
761
+ ADVANCE_ASSERT(parser, pCOLON);
762
+ rbs_node_t *type;
763
+ CHECK_PARSE(rbs_parse_type(parser, &type, false, self_allowed, classish_allowed));
764
+ ADVANCE_ASSERT(parser, pRBRACKET);
765
+ *self_type = type;
766
+ }
767
+
768
+ return true;
769
+ }
770
+
771
+ typedef struct {
772
+ rbs_node_t *function;
773
+ rbs_types_block_t *block;
774
+ rbs_node_t *function_self_type;
775
+ } parse_function_result;
776
+
777
+ /*
778
+ function ::= {} `(` params `)` self_type_binding? `{` `(` params `)` self_type_binding? `->` optional `}` `->` <optional>
779
+ | {} `(` params `)` self_type_binding? `->` <optional>
780
+ | {} self_type_binding? `{` `(` params `)` self_type_binding? `->` optional `}` `->` <optional>
781
+ | {} self_type_binding? `{` self_type_binding `->` optional `}` `->` <optional>
782
+ | {} self_type_binding? `->` <optional>
783
+ */
784
+ RBS_NODISCARD
785
+ static bool parse_function(rbs_parser_t *parser, bool accept_type_binding, bool block_allowed, bool forwarding_allowed, parse_function_result **result, bool self_allowed, bool classish_allowed) {
786
+ rbs_node_t *function = NULL;
787
+ rbs_types_block_t *block = NULL;
788
+ rbs_node_t *function_self_type = NULL;
789
+ rbs_range_t function_range;
790
+ function_range.start = parser->current_token.range.start;
791
+
792
+ method_params params;
793
+ initialize_method_params(&params, ALLOCATOR());
794
+
795
+ if (parser->next_token.type == pLPAREN) {
796
+ rbs_parser_advance(parser);
797
+ CHECK_PARSE(parse_params(parser, &params, forwarding_allowed, self_allowed, classish_allowed));
798
+ ADVANCE_ASSERT(parser, pRPAREN);
799
+ }
800
+
801
+ // Passing NULL to function_self_type means the function itself doesn't accept self type binding. (== method type)
802
+ if (accept_type_binding) {
803
+ CHECK_PARSE(parse_self_type_binding(parser, &function_self_type, self_allowed, classish_allowed));
804
+ } else {
805
+ if (rbs_is_untyped_params(&params)) {
806
+ if (parser->next_token.type != pARROW) {
807
+ rbs_parser_set_error(parser, parser->next_token2, true, "A method type with untyped method parameter cannot have block");
808
+ return false;
809
+ }
810
+ }
811
+ }
812
+
813
+ bool required = true;
814
+ rbs_range_t block_range;
815
+ bool has_block = parser->next_token.type == pLBRACE || (parser->next_token.type == pQUESTION && parser->next_token2.type == pLBRACE);
816
+
817
+ if (params.forwarding && has_block) {
818
+ rbs_parser_set_error(parser, parser->next_token, true, "method type with forwarding parameter cannot have block");
819
+ return false;
820
+ }
821
+
822
+ if (!block_allowed && has_block) {
823
+ rbs_parser_set_error(parser, parser->next_token, true, "block is not allowed in this context");
824
+ return false;
825
+ }
826
+
827
+ if (parser->next_token.type == pQUESTION && parser->next_token2.type == pLBRACE) {
828
+ // Optional block
829
+ block_range.start = parser->next_token.range.start;
830
+ required = false;
831
+ rbs_parser_advance(parser);
832
+ } else if (parser->next_token.type == pLBRACE) {
833
+ block_range.start = parser->next_token.range.start;
834
+ }
835
+
836
+ if (parser->next_token.type == pLBRACE) {
837
+ rbs_parser_advance(parser);
838
+
839
+ method_params block_params;
840
+ initialize_method_params(&block_params, ALLOCATOR());
841
+
842
+ if (parser->next_token.type == pLPAREN) {
843
+ rbs_parser_advance(parser);
844
+ CHECK_PARSE(parse_params(parser, &block_params, false, self_allowed, classish_allowed));
845
+ ADVANCE_ASSERT(parser, pRPAREN);
846
+ }
847
+
848
+ rbs_node_t *self_type = NULL;
849
+ CHECK_PARSE(parse_self_type_binding(parser, &self_type, self_allowed, classish_allowed));
850
+
851
+ ADVANCE_ASSERT(parser, pARROW);
852
+ rbs_node_t *block_return_type = NULL;
853
+ CHECK_PARSE(parse_optional(parser, &block_return_type, true, self_allowed, classish_allowed));
854
+
855
+ ADVANCE_ASSERT(parser, pRBRACE);
856
+
857
+ block_range.end = parser->current_token.range.end;
858
+
859
+ rbs_node_t *block_function = NULL;
860
+ if (rbs_is_untyped_params(&block_params)) {
861
+ block_function = (rbs_node_t *) rbs_types_untyped_function_new(ALLOCATOR(), RBS_RANGE_LEX2AST(block_range), block_return_type);
862
+ } else {
863
+ block_function = (rbs_node_t *) rbs_types_function_new(
864
+ ALLOCATOR(),
865
+ RBS_RANGE_LEX2AST(block_range),
866
+ block_params.required_positionals,
867
+ block_params.optional_positionals,
868
+ block_params.rest_positionals,
869
+ block_params.trailing_positionals,
870
+ block_params.required_keywords,
871
+ block_params.optional_keywords,
872
+ block_params.rest_keywords,
873
+ block_params.forwarding,
874
+ block_return_type
875
+ );
876
+ }
877
+
878
+ block = rbs_types_block_new(ALLOCATOR(), RBS_RANGE_LEX2AST(block_range), block_function, required, self_type);
879
+ }
880
+
881
+ ADVANCE_ASSERT(parser, pARROW);
882
+ rbs_node_t *type = NULL;
883
+ CHECK_PARSE(parse_optional(parser, &type, true, self_allowed, classish_allowed));
884
+
885
+ function_range.end = parser->current_token.range.end;
886
+ if (rbs_is_untyped_params(&params)) {
887
+ function = (rbs_node_t *) rbs_types_untyped_function_new(ALLOCATOR(), RBS_RANGE_LEX2AST(function_range), type);
888
+ } else {
889
+ function = (rbs_node_t *) rbs_types_function_new(
890
+ ALLOCATOR(),
891
+ RBS_RANGE_LEX2AST(function_range),
892
+ params.required_positionals,
893
+ params.optional_positionals,
894
+ params.rest_positionals,
895
+ params.trailing_positionals,
896
+ params.required_keywords,
897
+ params.optional_keywords,
898
+ params.rest_keywords,
899
+ params.forwarding,
900
+ type
901
+ );
902
+ }
903
+
904
+ (*result)->function = function;
905
+ (*result)->block = block;
906
+ (*result)->function_self_type = function_self_type;
907
+ return true;
908
+ }
909
+
910
+ /*
911
+ proc_type ::= {`^`} <function>
912
+ */
913
+ RBS_NODISCARD
914
+ static bool parse_proc_type(rbs_parser_t *parser, rbs_types_proc_t **proc, bool self_allowed, bool classish_allowed) {
915
+ rbs_position_t start = parser->current_token.range.start;
916
+ parse_function_result *result = rbs_allocator_alloc(ALLOCATOR(), parse_function_result);
917
+ CHECK_PARSE(parse_function(parser, true, true, false, &result, self_allowed, classish_allowed));
918
+
919
+ rbs_position_t end = parser->current_token.range.end;
920
+ rbs_location_range range = { .start_char = start.char_pos, .start_byte = start.byte_pos, .end_char = end.char_pos, .end_byte = end.byte_pos };
921
+ *proc = rbs_types_proc_new(ALLOCATOR(), range, result->function, result->block, result->function_self_type);
922
+ return true;
923
+ }
924
+
925
+ static void check_key_duplication(rbs_parser_t *parser, rbs_hash_t *fields, rbs_node_t *key) {
926
+ if (rbs_hash_find(fields, ((rbs_node_t *) key))) {
927
+ rbs_parser_set_error(parser, parser->current_token, true, "duplicated record key");
928
+ }
929
+ }
930
+
931
+ /**
932
+ * ... `{` ... `}` ...
933
+ * > >
934
+ * */
935
+ /*
936
+ record_attributes ::= {`{`} record_attribute... <record_attribute> `}`
937
+
938
+ record_attribute ::= {} keyword_token `:` <type>
939
+ | {} literal_type `=>` <type>
940
+ */
941
+ RBS_NODISCARD
942
+ static bool parse_record_attributes(rbs_parser_t *parser, rbs_hash_t **fields, bool self_allowed, bool classish_allowed) {
943
+ *fields = rbs_hash_new(ALLOCATOR());
944
+
945
+ if (parser->next_token.type == pRBRACE) return true;
946
+
947
+ while (true) {
948
+ rbs_ast_symbol_t *key = NULL;
949
+ bool required = true;
950
+
951
+ if (parser->next_token.type == pQUESTION) {
952
+ // { ?foo: type } syntax
953
+ required = false;
954
+ rbs_parser_advance(parser);
955
+ }
956
+
957
+ if (is_keyword(parser)) {
958
+ // { foo: type } syntax
959
+ CHECK_PARSE(parse_keyword_key(parser, &key));
960
+
961
+ check_key_duplication(parser, *fields, (rbs_node_t *) key);
962
+ ADVANCE_ASSERT(parser, pCOLON);
963
+ } else {
964
+ // { key => type } syntax
965
+ switch (parser->next_token.type) {
966
+ case tSYMBOL:
967
+ case tSQSYMBOL:
968
+ case tDQSYMBOL:
969
+ case tSQSTRING:
970
+ case tDQSTRING:
971
+ case tINTEGER:
972
+ case kTRUE:
973
+ case kFALSE: {
974
+ rbs_node_t *type = NULL;
975
+ CHECK_PARSE(parse_simple(parser, &type, false, self_allowed, classish_allowed));
976
+
977
+ key = (rbs_ast_symbol_t *) ((rbs_types_literal_t *) type)->literal;
978
+ break;
979
+ }
980
+ default:
981
+ rbs_parser_set_error(parser, parser->next_token, true, "unexpected record key token");
982
+ return false;
983
+ }
984
+ check_key_duplication(parser, *fields, (rbs_node_t *) key);
985
+ ADVANCE_ASSERT(parser, pFATARROW);
986
+ }
987
+
988
+ rbs_range_t field_range;
989
+ field_range.start = parser->current_token.range.end;
990
+
991
+ rbs_node_t *type;
992
+ CHECK_PARSE(rbs_parse_type(parser, &type, false, self_allowed, classish_allowed));
993
+
994
+ field_range.end = parser->current_token.range.end;
995
+ rbs_hash_set(*fields, (rbs_node_t *) key, (rbs_node_t *) rbs_types_record_field_type_new(ALLOCATOR(), RBS_RANGE_LEX2AST(field_range), type, required));
996
+
997
+ if (parser_advance_if(parser, pCOMMA)) {
998
+ if (parser->next_token.type == pRBRACE) {
999
+ break;
1000
+ }
1001
+ } else {
1002
+ break;
1003
+ }
1004
+ }
1005
+ return true;
1006
+ }
1007
+
1008
+ /*
1009
+ symbol ::= {<tSYMBOL>}
1010
+ */
1011
+ RBS_NODISCARD
1012
+ static bool parse_symbol(rbs_parser_t *parser, rbs_location_range location, rbs_types_literal_t **symbol) {
1013
+ size_t offset_bytes = parser->lexer->encoding->char_width((const uint8_t *) ":", (size_t) 1);
1014
+ size_t bytes = rbs_token_bytes(parser->current_token) - offset_bytes;
1015
+
1016
+ rbs_ast_symbol_t *literal;
1017
+
1018
+ switch (parser->current_token.type) {
1019
+ case tSYMBOL: {
1020
+ rbs_location_range symbol_range = rbs_location_range_current_token(parser);
1021
+
1022
+ char *buffer = rbs_peek_token(parser->lexer, parser->current_token);
1023
+ rbs_constant_id_t constant_id = rbs_constant_pool_insert_shared(
1024
+ &parser->constant_pool,
1025
+ (const uint8_t *) buffer + offset_bytes,
1026
+ bytes
1027
+ );
1028
+ literal = rbs_ast_symbol_new(ALLOCATOR(), symbol_range, &parser->constant_pool, constant_id);
1029
+ break;
1030
+ }
1031
+ case tDQSYMBOL:
1032
+ case tSQSYMBOL: {
1033
+ rbs_location_range symbol_range = rbs_location_range_current_token(parser);
1034
+ rbs_string_t current_token = rbs_parser_peek_current_token(parser);
1035
+
1036
+ rbs_string_t symbol = rbs_string_new(current_token.start + offset_bytes, current_token.end);
1037
+
1038
+ rbs_string_t unquoted_symbol = rbs_unquote_string(ALLOCATOR(), symbol, parser->lexer->encoding);
1039
+
1040
+ rbs_constant_id_t constant_id = rbs_constant_pool_insert_string(&parser->constant_pool, unquoted_symbol);
1041
+
1042
+ literal = rbs_ast_symbol_new(ALLOCATOR(), symbol_range, &parser->constant_pool, constant_id);
1043
+ break;
1044
+ }
1045
+ default:
1046
+ rbs_parser_set_error(parser, parser->current_token, false, "Unexpected error");
1047
+ return false;
1048
+ }
1049
+
1050
+ *symbol = rbs_types_literal_new(ALLOCATOR(), location, (rbs_node_t *) literal);
1051
+ return true;
1052
+ }
1053
+
1054
+ /*
1055
+ instance_type ::= {type_name} <type_args>
1056
+
1057
+ type_args ::= {} <> /empty/
1058
+ | {} `[` type_list <`]`>
1059
+ */
1060
+ RBS_NODISCARD
1061
+ static bool parse_instance_type(rbs_parser_t *parser, bool parse_alias, rbs_node_t **type) {
1062
+ TypeNameKind expected_kind = (TypeNameKind) (INTERFACE_NAME | CLASS_NAME);
1063
+ if (parse_alias) {
1064
+ expected_kind = (TypeNameKind) (expected_kind | ALIAS_NAME);
1065
+ }
1066
+
1067
+ rbs_range_t name_range;
1068
+ rbs_type_name_t *type_name = NULL;
1069
+ CHECK_PARSE(parse_type_name(parser, expected_kind, &name_range, &type_name));
1070
+
1071
+ rbs_node_list_t *types = rbs_node_list_new(ALLOCATOR());
1072
+
1073
+ TypeNameKind kind;
1074
+ switch (parser->current_token.type) {
1075
+ case tUIDENT: {
1076
+ kind = CLASS_NAME;
1077
+ break;
1078
+ }
1079
+ case tULIDENT: {
1080
+ kind = INTERFACE_NAME;
1081
+ break;
1082
+ }
1083
+ case kSKIP:
1084
+ case kRETURN:
1085
+ case tLIDENT: {
1086
+ kind = ALIAS_NAME;
1087
+ break;
1088
+ }
1089
+ default:
1090
+ rbs_parser_set_error(parser, parser->current_token, false, "unexpected token for type name");
1091
+ return false;
1092
+ }
1093
+
1094
+ rbs_range_t args_range;
1095
+ if (parser->next_token.type == pLBRACKET) {
1096
+ rbs_parser_advance(parser);
1097
+ args_range.start = parser->current_token.range.start;
1098
+ CHECK_PARSE(parse_type_list(parser, pRBRACKET, types, true, true, true));
1099
+ ADVANCE_ASSERT(parser, pRBRACKET);
1100
+ args_range.end = parser->current_token.range.end;
1101
+ } else {
1102
+ args_range = NULL_RANGE;
1103
+ }
1104
+
1105
+ rbs_range_t type_range = {
1106
+ .start = name_range.start,
1107
+ .end = rbs_nonnull_pos_or(args_range.end, name_range.end),
1108
+ };
1109
+
1110
+ rbs_location_range loc = RBS_RANGE_LEX2AST(type_range);
1111
+
1112
+ if (kind == CLASS_NAME) {
1113
+ rbs_types_class_instance_t *instance_type = rbs_types_class_instance_new(ALLOCATOR(), loc, type_name, types, RBS_RANGE_LEX2AST(name_range));
1114
+ instance_type->args_range = RBS_RANGE_LEX2AST(args_range);
1115
+ *type = (rbs_node_t *) instance_type;
1116
+ } else if (kind == INTERFACE_NAME) {
1117
+ rbs_types_interface_t *interface_type = rbs_types_interface_new(ALLOCATOR(), loc, type_name, types, RBS_RANGE_LEX2AST(name_range));
1118
+ interface_type->args_range = RBS_RANGE_LEX2AST(args_range);
1119
+ *type = (rbs_node_t *) interface_type;
1120
+ } else if (kind == ALIAS_NAME) {
1121
+ rbs_types_alias_t *type_alias = rbs_types_alias_new(ALLOCATOR(), loc, type_name, types, RBS_RANGE_LEX2AST(name_range));
1122
+ type_alias->args_range = RBS_RANGE_LEX2AST(args_range);
1123
+ *type = (rbs_node_t *) type_alias;
1124
+ }
1125
+
1126
+ return true;
1127
+ }
1128
+
1129
+ /*
1130
+ singleton_type ::= {`singleton`} `(` type_name <`)`> type_args?
1131
+ */
1132
+ RBS_NODISCARD
1133
+ static bool parse_singleton_type(rbs_parser_t *parser, rbs_types_class_singleton_t **singleton, bool self_allowed, bool classish_allowed) {
1134
+ ASSERT_TOKEN(parser, kSINGLETON);
1135
+
1136
+ rbs_range_t type_range;
1137
+ type_range.start = parser->current_token.range.start;
1138
+ ADVANCE_ASSERT(parser, pLPAREN);
1139
+ rbs_parser_advance(parser);
1140
+
1141
+ rbs_range_t name_range;
1142
+ rbs_type_name_t *type_name = NULL;
1143
+ CHECK_PARSE(parse_type_name(parser, CLASS_NAME, &name_range, &type_name));
1144
+
1145
+ ADVANCE_ASSERT(parser, pRPAREN);
1146
+
1147
+ rbs_node_list_t *types = rbs_node_list_new(ALLOCATOR());
1148
+
1149
+ rbs_location_range args_range = RBS_LOCATION_NULL_RANGE;
1150
+ if (parser->next_token.type == pLBRACKET) {
1151
+ rbs_parser_advance(parser);
1152
+ args_range.start_byte = parser->current_token.range.start.byte_pos;
1153
+ args_range.start_char = parser->current_token.range.start.char_pos;
1154
+ CHECK_PARSE(parse_type_list(parser, pRBRACKET, types, true, self_allowed, classish_allowed));
1155
+ ADVANCE_ASSERT(parser, pRBRACKET);
1156
+ args_range.end_byte = parser->current_token.range.end.byte_pos;
1157
+ args_range.end_char = parser->current_token.range.end.char_pos;
1158
+ }
1159
+
1160
+ type_range.end = parser->current_token.range.end;
1161
+ rbs_location_range loc = RBS_RANGE_LEX2AST(type_range);
1162
+
1163
+ *singleton = rbs_types_class_singleton_new(ALLOCATOR(), loc, type_name, types, RBS_RANGE_LEX2AST(name_range));
1164
+ (*singleton)->args_range = args_range;
1165
+
1166
+ return true;
1167
+ }
1168
+
1169
+ /**
1170
+ * Returns true if given type variable is recorded in the table.
1171
+ * If not found, it goes one table up, if it's not a reset table.
1172
+ * Or returns false, if it's a reset table.
1173
+ * */
1174
+ static bool parser_typevar_member(rbs_parser_t *parser, rbs_constant_id_t id) {
1175
+ id_table *table = parser->vars;
1176
+
1177
+ while (table && !RESET_TABLE_P(table)) {
1178
+ for (size_t i = 0; i < table->count; i++) {
1179
+ if (table->ids[i] == id) {
1180
+ return true;
1181
+ }
1182
+ }
1183
+
1184
+ table = table->next;
1185
+ }
1186
+
1187
+ return false;
1188
+ }
1189
+
1190
+ /*
1191
+ simple ::= {} `(` type <`)`>
1192
+ | {} <base type>
1193
+ | {} <type_name>
1194
+ | {} class_instance `[` type_list <`]`>
1195
+ | {} `singleton` `(` type_name <`)`>
1196
+ | {} `[` type_list <`]`>
1197
+ | {} `{` record_attributes <`}`>
1198
+ | {} `^` <function>
1199
+ */
1200
+ RBS_NODISCARD
1201
+ static bool parse_simple(rbs_parser_t *parser, rbs_node_t **type, bool void_allowed, bool self_allowed, bool classish_allowed) {
1202
+ rbs_parser_advance(parser);
1203
+
1204
+ switch (parser->current_token.type) {
1205
+ case pLPAREN: {
1206
+ rbs_node_t *lparen_type;
1207
+ CHECK_PARSE(rbs_parse_type(parser, &lparen_type, void_allowed, self_allowed, classish_allowed));
1208
+ ADVANCE_ASSERT(parser, pRPAREN);
1209
+ *type = lparen_type;
1210
+ return true;
1211
+ }
1212
+ case kBOOL: {
1213
+ rbs_location_range loc = rbs_location_range_current_token(parser);
1214
+ *type = (rbs_node_t *) rbs_types_bases_bool_new(ALLOCATOR(), loc);
1215
+ return true;
1216
+ }
1217
+ case kBOT: {
1218
+ rbs_location_range loc = rbs_location_range_current_token(parser);
1219
+ *type = (rbs_node_t *) rbs_types_bases_bottom_new(ALLOCATOR(), loc);
1220
+ return true;
1221
+ }
1222
+ case kCLASS: {
1223
+ if (!classish_allowed) {
1224
+ rbs_parser_set_error(parser, parser->current_token, true, "class type is not allowed here");
1225
+ return false;
1226
+ }
1227
+
1228
+ rbs_location_range loc = rbs_location_range_current_token(parser);
1229
+ *type = (rbs_node_t *) rbs_types_bases_class_new(ALLOCATOR(), loc);
1230
+ return true;
1231
+ }
1232
+ case kINSTANCE: {
1233
+ if (!classish_allowed) {
1234
+ rbs_parser_set_error(parser, parser->current_token, true, "instance type is not allowed here");
1235
+ return false;
1236
+ }
1237
+
1238
+ rbs_location_range loc = rbs_location_range_current_token(parser);
1239
+ *type = (rbs_node_t *) rbs_types_bases_instance_new(ALLOCATOR(), loc);
1240
+ return true;
1241
+ }
1242
+ case kNIL: {
1243
+ rbs_location_range loc = rbs_location_range_current_token(parser);
1244
+ *type = (rbs_node_t *) rbs_types_bases_nil_new(ALLOCATOR(), loc);
1245
+ return true;
1246
+ }
1247
+ case kSELF: {
1248
+ if (!self_allowed) {
1249
+ rbs_parser_set_error(parser, parser->current_token, true, "self type is not allowed here");
1250
+ return false;
1251
+ }
1252
+
1253
+ rbs_location_range loc = rbs_location_range_current_token(parser);
1254
+ *type = (rbs_node_t *) rbs_types_bases_self_new(ALLOCATOR(), loc);
1255
+ return true;
1256
+ }
1257
+ case kTOP: {
1258
+ rbs_location_range loc = rbs_location_range_current_token(parser);
1259
+ *type = (rbs_node_t *) rbs_types_bases_top_new(ALLOCATOR(), loc);
1260
+ return true;
1261
+ }
1262
+ case kVOID: {
1263
+ if (!void_allowed) {
1264
+ rbs_parser_set_error(parser, parser->current_token, true, "void type is not allowed here");
1265
+ return false;
1266
+ }
1267
+
1268
+ rbs_location_range loc = rbs_location_range_current_token(parser);
1269
+ *type = (rbs_node_t *) rbs_types_bases_void_new(ALLOCATOR(), loc);
1270
+ return true;
1271
+ }
1272
+ case kUNTYPED: {
1273
+ rbs_location_range loc = rbs_location_range_current_token(parser);
1274
+ *type = (rbs_node_t *) rbs_types_bases_any_new(ALLOCATOR(), loc, false);
1275
+ return true;
1276
+ }
1277
+ case k__TODO__: {
1278
+ rbs_location_range loc = rbs_location_range_current_token(parser);
1279
+ *type = (rbs_node_t *) rbs_types_bases_any_new(ALLOCATOR(), loc, true);
1280
+ return true;
1281
+ }
1282
+ case tINTEGER: {
1283
+ rbs_location_range loc = rbs_location_range_current_token(parser);
1284
+
1285
+ rbs_string_t string = rbs_parser_peek_current_token(parser);
1286
+ rbs_string_t stripped_string = rbs_string_strip_whitespace(&string);
1287
+
1288
+ rbs_node_t *literal = (rbs_node_t *) rbs_ast_integer_new(ALLOCATOR(), loc, stripped_string);
1289
+ *type = (rbs_node_t *) rbs_types_literal_new(ALLOCATOR(), loc, literal);
1290
+ return true;
1291
+ }
1292
+ case kTRUE: {
1293
+ rbs_location_range loc = rbs_location_range_current_token(parser);
1294
+ *type = (rbs_node_t *) rbs_types_literal_new(ALLOCATOR(), loc, (rbs_node_t *) rbs_ast_bool_new(ALLOCATOR(), loc, true));
1295
+ return true;
1296
+ }
1297
+ case kFALSE: {
1298
+ rbs_location_range loc = rbs_location_range_current_token(parser);
1299
+ *type = (rbs_node_t *) rbs_types_literal_new(ALLOCATOR(), loc, (rbs_node_t *) rbs_ast_bool_new(ALLOCATOR(), loc, false));
1300
+ return true;
1301
+ }
1302
+ case tSQSTRING:
1303
+ case tDQSTRING: {
1304
+ rbs_location_range loc = rbs_location_range_current_token(parser);
1305
+
1306
+ rbs_string_t unquoted_str = rbs_unquote_string(ALLOCATOR(), rbs_parser_peek_current_token(parser), parser->lexer->encoding);
1307
+ rbs_node_t *literal = (rbs_node_t *) rbs_ast_string_new(ALLOCATOR(), loc, unquoted_str);
1308
+ *type = (rbs_node_t *) rbs_types_literal_new(ALLOCATOR(), loc, literal);
1309
+ return true;
1310
+ }
1311
+ case tSYMBOL:
1312
+ case tSQSYMBOL:
1313
+ case tDQSYMBOL: {
1314
+ rbs_location_range loc = rbs_location_range_current_token(parser);
1315
+ rbs_types_literal_t *literal = NULL;
1316
+ CHECK_PARSE(parse_symbol(parser, loc, &literal));
1317
+ *type = (rbs_node_t *) literal;
1318
+ return true;
1319
+ }
1320
+ case tUIDENT: {
1321
+ const char *name_str = rbs_peek_token(parser->lexer, parser->current_token);
1322
+ size_t name_len = rbs_token_bytes(parser->current_token);
1323
+
1324
+ rbs_constant_id_t name = rbs_constant_pool_find(&parser->constant_pool, (const uint8_t *) name_str, name_len);
1325
+
1326
+ if (parser_typevar_member(parser, name)) {
1327
+ rbs_location_range loc = rbs_location_range_current_token(parser);
1328
+ rbs_ast_symbol_t *symbol = rbs_ast_symbol_new(ALLOCATOR(), loc, &parser->constant_pool, name);
1329
+ *type = (rbs_node_t *) rbs_types_variable_new(ALLOCATOR(), loc, symbol);
1330
+ return true;
1331
+ }
1332
+
1333
+ RBS_FALLTHROUGH // for type name
1334
+ }
1335
+ case tULIDENT:
1336
+ case tLIDENT:
1337
+ case kSKIP:
1338
+ case kRETURN:
1339
+ case pCOLON2: {
1340
+ rbs_node_t *instance_type = NULL;
1341
+ CHECK_PARSE(parse_instance_type(parser, true, &instance_type));
1342
+ *type = instance_type;
1343
+ return true;
1344
+ }
1345
+ case kSINGLETON: {
1346
+ rbs_types_class_singleton_t *singleton = NULL;
1347
+ CHECK_PARSE(parse_singleton_type(parser, &singleton, self_allowed, classish_allowed));
1348
+ *type = (rbs_node_t *) singleton;
1349
+ return true;
1350
+ }
1351
+ case pLBRACKET: {
1352
+ rbs_range_t rg;
1353
+ rg.start = parser->current_token.range.start;
1354
+ rbs_node_list_t *types = rbs_node_list_new(ALLOCATOR());
1355
+ if (parser->next_token.type != pRBRACKET) {
1356
+ CHECK_PARSE(parse_type_list(parser, pRBRACKET, types, false, self_allowed, classish_allowed));
1357
+ }
1358
+ ADVANCE_ASSERT(parser, pRBRACKET);
1359
+ rg.end = parser->current_token.range.end;
1360
+
1361
+ *type = (rbs_node_t *) rbs_types_tuple_new(ALLOCATOR(), RBS_RANGE_LEX2AST(rg), types);
1362
+ return true;
1363
+ }
1364
+ case pAREF_OPR: {
1365
+ rbs_location_range loc = rbs_location_range_current_token(parser);
1366
+ rbs_node_list_t *types = rbs_node_list_new(ALLOCATOR());
1367
+ *type = (rbs_node_t *) rbs_types_tuple_new(ALLOCATOR(), loc, types);
1368
+ return true;
1369
+ }
1370
+ case pLBRACE: {
1371
+ rbs_position_t start = parser->current_token.range.start;
1372
+ rbs_hash_t *fields = NULL;
1373
+ CHECK_PARSE(parse_record_attributes(parser, &fields, self_allowed, classish_allowed));
1374
+ ADVANCE_ASSERT(parser, pRBRACE);
1375
+ rbs_position_t end = parser->current_token.range.end;
1376
+ rbs_location_range loc = { .start_char = start.char_pos, .start_byte = start.byte_pos, .end_char = end.char_pos, .end_byte = end.byte_pos };
1377
+ *type = (rbs_node_t *) rbs_types_record_new(ALLOCATOR(), loc, fields);
1378
+ return true;
1379
+ }
1380
+ case pHAT: {
1381
+ rbs_types_proc_t *value = NULL;
1382
+ CHECK_PARSE(parse_proc_type(parser, &value, self_allowed, classish_allowed));
1383
+ *type = (rbs_node_t *) value;
1384
+ return true;
1385
+ }
1386
+ default:
1387
+ rbs_parser_set_error(parser, parser->current_token, true, "unexpected token for simple type");
1388
+ return false;
1389
+ }
1390
+ }
1391
+
1392
+ /*
1393
+ intersection ::= {} optional `&` ... '&' <optional>
1394
+ | {} <optional>
1395
+ */
1396
+ RBS_NODISCARD
1397
+ static bool parse_intersection(rbs_parser_t *parser, rbs_node_t **type, bool void_allowed, bool self_allowed, bool classish_allowed) {
1398
+ rbs_range_t rg;
1399
+ rg.start = parser->next_token.range.start;
1400
+
1401
+ rbs_node_t *optional = NULL;
1402
+ CHECK_PARSE(parse_optional(parser, &optional, void_allowed, self_allowed, classish_allowed));
1403
+ *type = optional;
1404
+
1405
+ rbs_node_list_t *intersection_types = rbs_node_list_new(ALLOCATOR());
1406
+
1407
+ rbs_node_list_append(intersection_types, optional);
1408
+ while (parser->next_token.type == pAMP) {
1409
+ if (void_allowed && (*type)->type == RBS_TYPES_BASES_VOID) {
1410
+ rbs_parser_set_error(parser, parser->current_token, true, "void type is not allowed here");
1411
+ return false;
1412
+ }
1413
+
1414
+ rbs_parser_advance(parser);
1415
+ rbs_node_t *type = NULL;
1416
+ CHECK_PARSE(parse_optional(parser, &type, false, self_allowed, classish_allowed));
1417
+ rbs_node_list_append(intersection_types, type);
1418
+ }
1419
+
1420
+ rg.end = parser->current_token.range.end;
1421
+
1422
+ if (intersection_types->length > 1) {
1423
+ *type = (rbs_node_t *) rbs_types_intersection_new(ALLOCATOR(), RBS_RANGE_LEX2AST(rg), intersection_types);
1424
+ }
1425
+
1426
+ return true;
1427
+ }
1428
+
1429
+ /*
1430
+ union ::= {} intersection '|' ... '|' <intersection>
1431
+ | {} <intersection>
1432
+ */
1433
+ bool rbs_parse_type(rbs_parser_t *parser, rbs_node_t **type, bool void_allowed, bool self_allowed, bool classish_allowed) {
1434
+ rbs_range_t rg;
1435
+ rg.start = parser->next_token.range.start;
1436
+ rbs_node_list_t *union_types = rbs_node_list_new(ALLOCATOR());
1437
+
1438
+ CHECK_PARSE(parse_intersection(parser, type, void_allowed, self_allowed, classish_allowed));
1439
+
1440
+ rbs_node_list_append(union_types, *type);
1441
+
1442
+ while (parser->next_token.type == pBAR) {
1443
+ if (void_allowed && (*type)->type == RBS_TYPES_BASES_VOID) {
1444
+ rbs_parser_set_error(parser, parser->current_token, true, "void type is not allowed here");
1445
+ return false;
1446
+ }
1447
+
1448
+ rbs_parser_advance(parser);
1449
+ rbs_node_t *intersection = NULL;
1450
+ CHECK_PARSE(parse_intersection(parser, &intersection, false, self_allowed, classish_allowed));
1451
+ rbs_node_list_append(union_types, intersection);
1452
+ }
1453
+
1454
+ rg.end = parser->current_token.range.end;
1455
+
1456
+ if (union_types->length > 1) {
1457
+ *type = (rbs_node_t *) rbs_types_union_new(ALLOCATOR(), RBS_RANGE_LEX2AST(rg), union_types);
1458
+ }
1459
+
1460
+ return true;
1461
+ }
1462
+
1463
+ /*
1464
+ type_params ::= {} `[` type_param `,` ... <`]`>
1465
+ | {<>}
1466
+
1467
+ type_param ::= kUNCHECKED? (kIN|kOUT|) tUIDENT upper_bound? lower_bound? default_type? (module_type_params == true)
1468
+
1469
+ type_param ::= tUIDENT upper_bound? lower_bound? default_type? (module_type_params == false)
1470
+ */
1471
+ RBS_NODISCARD
1472
+ static bool parse_type_params(rbs_parser_t *parser, rbs_range_t *rg, bool module_type_params, rbs_node_list_t **params) {
1473
+ *params = rbs_node_list_new(ALLOCATOR());
1474
+
1475
+ bool required_param_allowed = true;
1476
+
1477
+ if (parser->next_token.type == pLBRACKET) {
1478
+ rbs_parser_advance(parser);
1479
+
1480
+ rg->start = parser->current_token.range.start;
1481
+
1482
+ while (true) {
1483
+ bool unchecked = false;
1484
+ enum rbs_type_param_variance variance = RBS_TYPE_PARAM_VARIANCE_INVARIANT;
1485
+ rbs_node_t *upper_bound = NULL;
1486
+ rbs_node_t *lower_bound = NULL;
1487
+ rbs_node_t *default_type = NULL;
1488
+
1489
+ rbs_range_t param_range;
1490
+ param_range.start = parser->next_token.range.start;
1491
+
1492
+ rbs_range_t unchecked_range = NULL_RANGE;
1493
+ rbs_range_t variance_range = NULL_RANGE;
1494
+ if (module_type_params) {
1495
+ if (parser->next_token.type == kUNCHECKED) {
1496
+ unchecked = true;
1497
+ rbs_parser_advance(parser);
1498
+ unchecked_range = parser->current_token.range;
1499
+ }
1500
+
1501
+ if (parser->next_token.type == kIN || parser->next_token.type == kOUT) {
1502
+ switch (parser->next_token.type) {
1503
+ case kIN:
1504
+ variance = RBS_TYPE_PARAM_VARIANCE_CONTRAVARIANT;
1505
+ break;
1506
+ case kOUT:
1507
+ variance = RBS_TYPE_PARAM_VARIANCE_COVARIANT;
1508
+ break;
1509
+ default:
1510
+ rbs_parser_set_error(parser, parser->current_token, false, "Unexpected error");
1511
+ return false;
1512
+ }
1513
+
1514
+ rbs_parser_advance(parser);
1515
+ variance_range = parser->current_token.range;
1516
+ }
1517
+ }
1518
+
1519
+ ADVANCE_ASSERT(parser, tUIDENT);
1520
+ rbs_range_t name_range = parser->current_token.range;
1521
+
1522
+ rbs_string_t string = rbs_parser_peek_current_token(parser);
1523
+ rbs_location_range name_symbol_range = rbs_location_range_current_token(parser);
1524
+ rbs_constant_id_t id = rbs_constant_pool_insert_string(&parser->constant_pool, string);
1525
+ rbs_ast_symbol_t *name = rbs_ast_symbol_new(ALLOCATOR(), name_symbol_range, &parser->constant_pool, id);
1526
+
1527
+ CHECK_PARSE(rbs_parser_insert_typevar(parser, id));
1528
+
1529
+ rbs_range_t upper_bound_range = NULL_RANGE;
1530
+ rbs_range_t lower_bound_range = NULL_RANGE;
1531
+
1532
+ for (int bound_parse_attempt = 0; bound_parse_attempt < 2; bound_parse_attempt++) {
1533
+ switch (parser->next_token.type) {
1534
+ case pLT:
1535
+ if (upper_bound != NULL) {
1536
+ rbs_parser_set_error(parser, parser->next_token, true, "duplicate upper bound ('<') for type parameter");
1537
+ return false;
1538
+ }
1539
+
1540
+ rbs_parser_advance(parser);
1541
+ upper_bound_range.start = parser->current_token.range.start;
1542
+ CHECK_PARSE(rbs_parse_type(parser, &upper_bound, false, false, false));
1543
+ upper_bound_range.end = parser->current_token.range.end;
1544
+ break;
1545
+
1546
+ case pGT:
1547
+ if (lower_bound != NULL) {
1548
+ rbs_parser_set_error(parser, parser->next_token, true, "duplicate lower bound ('>') for type parameter");
1549
+ return false;
1550
+ }
1551
+
1552
+ rbs_parser_advance(parser);
1553
+ lower_bound_range.start = parser->current_token.range.start;
1554
+ CHECK_PARSE(rbs_parse_type(parser, &lower_bound, false, false, false));
1555
+ lower_bound_range.end = parser->current_token.range.end;
1556
+ break;
1557
+
1558
+ default:
1559
+ break;
1560
+ }
1561
+ }
1562
+
1563
+ rbs_range_t default_type_range = NULL_RANGE;
1564
+ if (module_type_params) {
1565
+ if (parser->next_token.type == pEQ) {
1566
+ rbs_parser_advance(parser);
1567
+
1568
+ default_type_range.start = parser->current_token.range.start;
1569
+ CHECK_PARSE(rbs_parse_type(parser, &default_type, true, false, false));
1570
+ default_type_range.end = parser->current_token.range.end;
1571
+
1572
+ required_param_allowed = false;
1573
+ } else {
1574
+ if (!required_param_allowed) {
1575
+ rbs_parser_set_error(parser, parser->current_token, true, "required type parameter is not allowed after optional type parameter");
1576
+ return false;
1577
+ }
1578
+ }
1579
+ }
1580
+
1581
+ param_range.end = parser->current_token.range.end;
1582
+
1583
+ rbs_ast_type_param_t *param = rbs_ast_type_param_new(ALLOCATOR(), RBS_RANGE_LEX2AST(param_range), name, variance, upper_bound, lower_bound, default_type, unchecked, RBS_RANGE_LEX2AST(name_range));
1584
+ param->variance_range = RBS_RANGE_LEX2AST(variance_range);
1585
+ param->unchecked_range = RBS_RANGE_LEX2AST(unchecked_range);
1586
+ param->upper_bound_range = RBS_RANGE_LEX2AST(upper_bound_range);
1587
+ param->lower_bound_range = RBS_RANGE_LEX2AST(lower_bound_range);
1588
+ param->default_range = RBS_RANGE_LEX2AST(default_type_range);
1589
+
1590
+ rbs_node_list_append(*params, (rbs_node_t *) param);
1591
+
1592
+ if (parser->next_token.type == pCOMMA) {
1593
+ rbs_parser_advance(parser);
1594
+ } else if (parser->next_token.type == pRBRACKET) {
1595
+ break;
1596
+ } else {
1597
+ rbs_parser_set_error(parser, parser->next_token, true, "expected ',' or ']' after type parameter, got %s", rbs_token_type_str(parser->next_token.type));
1598
+ return false;
1599
+ }
1600
+ }
1601
+
1602
+ ADVANCE_ASSERT(parser, pRBRACKET);
1603
+ rg->end = parser->current_token.range.end;
1604
+ } else {
1605
+ *rg = NULL_RANGE;
1606
+ }
1607
+
1608
+ return true;
1609
+ }
1610
+
1611
+ RBS_NODISCARD
1612
+ static bool parser_pop_typevar_table(rbs_parser_t *parser) {
1613
+ id_table *table;
1614
+
1615
+ if (parser->vars) {
1616
+ table = parser->vars;
1617
+ parser->vars = table->next;
1618
+ } else {
1619
+ rbs_parser_set_error(parser, parser->current_token, false, "Cannot pop empty table");
1620
+ return false;
1621
+ }
1622
+
1623
+ if (parser->vars && RESET_TABLE_P(parser->vars)) {
1624
+ table = parser->vars;
1625
+ parser->vars = table->next;
1626
+ }
1627
+
1628
+ return true;
1629
+ }
1630
+
1631
+ /*
1632
+ method_type ::= {} type_params <function>
1633
+ */
1634
+ // TODO: Should this be RBS_NODISCARD?
1635
+ bool rbs_parse_method_type(rbs_parser_t *parser, rbs_method_type_t **method_type, bool require_eof, bool classish_allowed) {
1636
+ rbs_parser_push_typevar_table(parser, false);
1637
+
1638
+ rbs_range_t rg;
1639
+ rg.start = parser->next_token.range.start;
1640
+
1641
+ rbs_range_t params_range = NULL_RANGE;
1642
+ rbs_node_list_t *type_params;
1643
+ CHECK_PARSE(parse_type_params(parser, &params_range, false, &type_params));
1644
+
1645
+ rbs_range_t type_range;
1646
+ type_range.start = parser->next_token.range.start;
1647
+
1648
+ parse_function_result *result = rbs_allocator_alloc(ALLOCATOR(), parse_function_result);
1649
+ CHECK_PARSE(parse_function(parser, false, true, true, &result, true, classish_allowed));
1650
+
1651
+ CHECK_PARSE(parser_pop_typevar_table(parser));
1652
+
1653
+ rg.end = parser->current_token.range.end;
1654
+ type_range.end = rg.end;
1655
+
1656
+ if (require_eof) {
1657
+ rbs_parser_advance(parser);
1658
+ if (parser->current_token.type != pEOF) {
1659
+ rbs_parser_set_error(parser, parser->current_token, true, "expected a token `%s`", rbs_token_type_str(pEOF));
1660
+ return false;
1661
+ }
1662
+ }
1663
+
1664
+ *method_type = rbs_method_type_new(ALLOCATOR(), RBS_RANGE_LEX2AST(rg), type_params, result->function, result->block, RBS_RANGE_LEX2AST(type_range));
1665
+ (*method_type)->type_params_range = RBS_RANGE_LEX2AST(params_range);
1666
+
1667
+ return true;
1668
+ }
1669
+
1670
+ /*
1671
+ global_decl ::= {tGIDENT} `:` <type>
1672
+ */
1673
+ RBS_NODISCARD
1674
+ static bool parse_global_decl(rbs_parser_t *parser, rbs_node_list_t *annotations, rbs_ast_declarations_global_t **global) {
1675
+ rbs_range_t decl_range;
1676
+ decl_range.start = parser->current_token.range.start;
1677
+
1678
+ rbs_ast_comment_t *comment = rbs_parser_get_comment(parser, decl_range.start.line);
1679
+
1680
+ rbs_range_t name_range = parser->current_token.range;
1681
+
1682
+ rbs_ast_symbol_t *type_name = rbs_ast_symbol_new(ALLOCATOR(), RBS_RANGE_LEX2AST(name_range), &parser->constant_pool, INTERN_TOKEN(parser, parser->current_token));
1683
+
1684
+ ADVANCE_ASSERT(parser, pCOLON);
1685
+ rbs_range_t colon_range = parser->current_token.range;
1686
+
1687
+ rbs_node_t *type;
1688
+ CHECK_PARSE(rbs_parse_type(parser, &type, false, false, false));
1689
+ decl_range.end = parser->current_token.range.end;
1690
+
1691
+ *global = rbs_ast_declarations_global_new(ALLOCATOR(), RBS_RANGE_LEX2AST(decl_range), type_name, type, comment, annotations, RBS_RANGE_LEX2AST(name_range), RBS_RANGE_LEX2AST(colon_range));
1692
+ return true;
1693
+ }
1694
+
1695
+ /*
1696
+ const_decl ::= {const_name} `:` <type>
1697
+ */
1698
+ RBS_NODISCARD
1699
+ static bool parse_const_decl(rbs_parser_t *parser, rbs_node_list_t *annotations, rbs_ast_declarations_constant_t **constant) {
1700
+ rbs_range_t decl_range;
1701
+
1702
+ decl_range.start = parser->current_token.range.start;
1703
+ rbs_ast_comment_t *comment = rbs_parser_get_comment(parser, decl_range.start.line);
1704
+
1705
+ rbs_range_t name_range;
1706
+ rbs_type_name_t *type_name = NULL;
1707
+ CHECK_PARSE(parse_type_name(parser, CLASS_NAME, &name_range, &type_name));
1708
+
1709
+ ADVANCE_ASSERT(parser, pCOLON);
1710
+ rbs_range_t colon_range = parser->current_token.range;
1711
+
1712
+ rbs_node_t *type;
1713
+ CHECK_PARSE(rbs_parse_type(parser, &type, false, false, false));
1714
+
1715
+ decl_range.end = parser->current_token.range.end;
1716
+
1717
+ *constant = rbs_ast_declarations_constant_new(ALLOCATOR(), RBS_RANGE_LEX2AST(decl_range), type_name, type, comment, annotations, RBS_RANGE_LEX2AST(name_range), RBS_RANGE_LEX2AST(colon_range));
1718
+ return true;
1719
+ }
1720
+
1721
+ /*
1722
+ type_decl ::= {kTYPE} alias_name `=` <type>
1723
+ */
1724
+ RBS_NODISCARD
1725
+ static bool parse_type_decl(rbs_parser_t *parser, rbs_position_t comment_pos, rbs_node_list_t *annotations, rbs_ast_declarations_type_alias_t **typealias) {
1726
+ rbs_parser_push_typevar_table(parser, true);
1727
+
1728
+ rbs_range_t decl_range;
1729
+ decl_range.start = parser->current_token.range.start;
1730
+ comment_pos = rbs_nonnull_pos_or(comment_pos, decl_range.start);
1731
+
1732
+ rbs_range_t keyword_range = parser->current_token.range;
1733
+
1734
+ rbs_parser_advance(parser);
1735
+
1736
+ rbs_range_t name_range;
1737
+ rbs_type_name_t *type_name = NULL;
1738
+ CHECK_PARSE(parse_type_name(parser, ALIAS_NAME, &name_range, &type_name));
1739
+
1740
+ rbs_range_t params_range;
1741
+ rbs_node_list_t *type_params;
1742
+ CHECK_PARSE(parse_type_params(parser, &params_range, true, &type_params));
1743
+
1744
+ ADVANCE_ASSERT(parser, pEQ);
1745
+ rbs_range_t eq_range = parser->current_token.range;
1746
+
1747
+ rbs_node_t *type;
1748
+ CHECK_PARSE(rbs_parse_type(parser, &type, false, false, false));
1749
+
1750
+ decl_range.end = parser->current_token.range.end;
1751
+
1752
+ CHECK_PARSE(parser_pop_typevar_table(parser));
1753
+
1754
+ rbs_ast_comment_t *comment = rbs_parser_get_comment(parser, comment_pos.line);
1755
+
1756
+ *typealias = rbs_ast_declarations_type_alias_new(ALLOCATOR(), RBS_RANGE_LEX2AST(decl_range), type_name, type_params, type, annotations, comment, RBS_RANGE_LEX2AST(keyword_range), RBS_RANGE_LEX2AST(name_range), RBS_RANGE_LEX2AST(eq_range));
1757
+ (*typealias)->type_params_range = RBS_RANGE_LEX2AST(params_range);
1758
+ return true;
1759
+ }
1760
+
1761
+ /*
1762
+ annotation ::= {<tANNOTATION>}
1763
+ */
1764
+ RBS_NODISCARD
1765
+ static bool parse_annotation(rbs_parser_t *parser, rbs_ast_annotation_t **annotation) {
1766
+ rbs_range_t rg = parser->current_token.range;
1767
+
1768
+ size_t offset_bytes =
1769
+ parser->lexer->encoding->char_width((const uint8_t *) "%", (size_t) 1) +
1770
+ parser->lexer->encoding->char_width((const uint8_t *) "a", (size_t) 1);
1771
+
1772
+ rbs_string_t str = rbs_string_new(
1773
+ parser->lexer->string.start + rg.start.byte_pos + offset_bytes,
1774
+ parser->lexer->string.end
1775
+ );
1776
+
1777
+ // Assumes the input is ASCII compatible
1778
+ unsigned int open_char = str.start[0];
1779
+
1780
+ unsigned int close_char;
1781
+
1782
+ switch (open_char) {
1783
+ case '{':
1784
+ close_char = '}';
1785
+ break;
1786
+ case '(':
1787
+ close_char = ')';
1788
+ break;
1789
+ case '[':
1790
+ close_char = ']';
1791
+ break;
1792
+ case '<':
1793
+ close_char = '>';
1794
+ break;
1795
+ case '|':
1796
+ close_char = '|';
1797
+ break;
1798
+ default:
1799
+ rbs_parser_set_error(parser, parser->current_token, false, "Unexpected error");
1800
+ return false;
1801
+ }
1802
+
1803
+ size_t open_bytes = parser->lexer->encoding->char_width((const uint8_t *) &open_char, (size_t) 1);
1804
+ size_t close_bytes = parser->lexer->encoding->char_width((const uint8_t *) &close_char, (size_t) 1);
1805
+
1806
+ rbs_string_t current_token = rbs_parser_peek_current_token(parser);
1807
+ size_t total_offset = offset_bytes + open_bytes;
1808
+
1809
+ rbs_string_t annotation_str = rbs_string_new(
1810
+ current_token.start + total_offset,
1811
+ current_token.end - close_bytes
1812
+ );
1813
+
1814
+ rbs_string_t stripped_annotation_str = rbs_string_strip_whitespace(&annotation_str);
1815
+
1816
+ *annotation = rbs_ast_annotation_new(ALLOCATOR(), RBS_RANGE_LEX2AST(rg), stripped_annotation_str);
1817
+ return true;
1818
+ }
1819
+
1820
+ /*
1821
+ annotations ::= {} annotation ... <annotation>
1822
+ | {<>}
1823
+ */
1824
+ RBS_NODISCARD
1825
+ static bool parse_annotations(rbs_parser_t *parser, rbs_node_list_t *annotations, rbs_position_t *annot_pos) {
1826
+ *annot_pos = NullPosition;
1827
+
1828
+ while (true) {
1829
+ if (parser->next_token.type == tANNOTATION) {
1830
+ rbs_parser_advance(parser);
1831
+
1832
+ if (rbs_null_position_p((*annot_pos))) {
1833
+ *annot_pos = parser->current_token.range.start;
1834
+ }
1835
+
1836
+ rbs_ast_annotation_t *annotation = NULL;
1837
+ CHECK_PARSE(parse_annotation(parser, &annotation));
1838
+ rbs_node_list_append(annotations, (rbs_node_t *) annotation);
1839
+ } else {
1840
+ break;
1841
+ }
1842
+ }
1843
+
1844
+ return true;
1845
+ }
1846
+
1847
+ /*
1848
+ method_name ::= {} <IDENT | keyword>
1849
+ | {} (IDENT | keyword)~<`?`>
1850
+ */
1851
+ RBS_NODISCARD
1852
+ static bool parse_method_name(rbs_parser_t *parser, rbs_range_t *range, rbs_ast_symbol_t **symbol) {
1853
+ rbs_parser_advance(parser);
1854
+
1855
+ switch (parser->current_token.type) {
1856
+ case tUIDENT:
1857
+ case tLIDENT:
1858
+ case tULIDENT:
1859
+ case tULLIDENT:
1860
+ case tNONASCIIIDENT:
1861
+ KEYWORD_CASES
1862
+ if (parser->next_token.type == pQUESTION && parser->current_token.range.end.byte_pos == parser->next_token.range.start.byte_pos) {
1863
+ range->start = parser->current_token.range.start;
1864
+ range->end = parser->next_token.range.end;
1865
+ rbs_parser_advance(parser);
1866
+
1867
+ rbs_constant_id_t constant_id = rbs_constant_pool_insert_shared_with_encoding(
1868
+ &parser->constant_pool,
1869
+ (const uint8_t *) parser->lexer->string.start + range->start.byte_pos,
1870
+ range->end.byte_pos - range->start.byte_pos,
1871
+ parser->lexer->encoding
1872
+ );
1873
+
1874
+ *symbol = rbs_ast_symbol_new(ALLOCATOR(), RBS_RANGE_LEX2AST(*range), &parser->constant_pool, constant_id);
1875
+ } else {
1876
+ *range = parser->current_token.range;
1877
+ *symbol = rbs_ast_symbol_new(ALLOCATOR(), RBS_RANGE_LEX2AST(*range), &parser->constant_pool, INTERN_TOKEN(parser, parser->current_token));
1878
+ }
1879
+ return true;
1880
+
1881
+ case tBANGIDENT:
1882
+ case tEQIDENT: {
1883
+ *range = parser->current_token.range;
1884
+ *symbol = rbs_ast_symbol_new(ALLOCATOR(), RBS_RANGE_LEX2AST(*range), &parser->constant_pool, INTERN_TOKEN(parser, parser->current_token));
1885
+ return true;
1886
+ }
1887
+ case tQIDENT: {
1888
+ rbs_string_t string = rbs_parser_peek_current_token(parser);
1889
+ rbs_string_t unquoted_str = rbs_unquote_string(ALLOCATOR(), string, parser->lexer->encoding);
1890
+ rbs_constant_id_t constant_id = rbs_constant_pool_insert_string(&parser->constant_pool, unquoted_str);
1891
+ *symbol = rbs_ast_symbol_new(ALLOCATOR(), rbs_location_range_current_token(parser), &parser->constant_pool, constant_id);
1892
+ return true;
1893
+ }
1894
+
1895
+ case pBAR:
1896
+ case pHAT:
1897
+ case pAMP:
1898
+ case pSTAR:
1899
+ case pSTAR2:
1900
+ case pLT:
1901
+ case pGT:
1902
+ case pAREF_OPR:
1903
+ case tOPERATOR: {
1904
+ *range = parser->current_token.range;
1905
+ *symbol = rbs_ast_symbol_new(ALLOCATOR(), RBS_RANGE_LEX2AST(*range), &parser->constant_pool, INTERN_TOKEN(parser, parser->current_token));
1906
+ return true;
1907
+ }
1908
+
1909
+ default:
1910
+ rbs_parser_set_error(parser, parser->current_token, true, "unexpected token for method name");
1911
+ return false;
1912
+ }
1913
+ }
1914
+
1915
+ typedef enum {
1916
+ INSTANCE_KIND,
1917
+ SINGLETON_KIND,
1918
+ INSTANCE_SINGLETON_KIND
1919
+ } InstanceSingletonKind;
1920
+
1921
+ /*
1922
+ instance_singleton_kind ::= {<>}
1923
+ | {} kSELF <`.`>
1924
+ | {} kSELF~`?` <`.`>
1925
+
1926
+ @param allow_selfq `true` to accept `self?` kind.
1927
+ */
1928
+ static InstanceSingletonKind parse_instance_singleton_kind(rbs_parser_t *parser, bool allow_selfq, rbs_range_t *rg) {
1929
+ InstanceSingletonKind kind = INSTANCE_KIND;
1930
+
1931
+ if (parser->next_token.type == kSELF) {
1932
+ rbs_range_t self_range = parser->next_token.range;
1933
+
1934
+ if (parser->next_token2.type == pDOT) {
1935
+ rbs_parser_advance(parser);
1936
+ rbs_parser_advance(parser);
1937
+ kind = SINGLETON_KIND;
1938
+ } else if (
1939
+ parser->next_token2.type == pQUESTION && parser->next_token.range.end.char_pos == parser->next_token2.range.start.char_pos && parser->next_token3.type == pDOT && allow_selfq
1940
+ ) {
1941
+ rbs_parser_advance(parser);
1942
+ rbs_parser_advance(parser);
1943
+ rbs_parser_advance(parser);
1944
+ kind = INSTANCE_SINGLETON_KIND;
1945
+ }
1946
+
1947
+ *rg = (rbs_range_t) {
1948
+ .start = self_range.start,
1949
+ .end = parser->current_token.range.end,
1950
+ };
1951
+ } else {
1952
+ *rg = NULL_RANGE;
1953
+ }
1954
+
1955
+ return kind;
1956
+ }
1957
+
1958
+ /**
1959
+ * def_member ::= {kDEF} method_name `:` <method_types>
1960
+ * | {kPRIVATE} kDEF method_name `:` <method_types>
1961
+ * | {kPUBLIC} kDEF method_name `:` <method_types>
1962
+ *
1963
+ * method_types ::= {} <method_type>
1964
+ * | {} <`...`>
1965
+ * | {} method_type `|` <method_types>
1966
+ *
1967
+ * @param instance_only `true` to reject singleton method definition.
1968
+ * @param accept_overload `true` to accept overloading (...) definition.
1969
+ * */
1970
+ RBS_NODISCARD
1971
+ static bool parse_member_def(rbs_parser_t *parser, bool instance_only, bool accept_overload, rbs_position_t comment_pos, rbs_node_list_t *annotations, rbs_ast_members_method_definition_t **method_definition) {
1972
+ rbs_range_t member_range;
1973
+ member_range.start = parser->current_token.range.start;
1974
+ comment_pos = rbs_nonnull_pos_or(comment_pos, member_range.start);
1975
+
1976
+ rbs_ast_comment_t *comment = rbs_parser_get_comment(parser, comment_pos.line);
1977
+
1978
+ rbs_range_t visibility_range;
1979
+ enum rbs_method_definition_visibility visibility;
1980
+ switch (parser->current_token.type) {
1981
+ case kPRIVATE: {
1982
+ visibility_range = parser->current_token.range;
1983
+ visibility = RBS_METHOD_DEFINITION_VISIBILITY_PRIVATE;
1984
+ member_range.start = visibility_range.start;
1985
+ rbs_parser_advance(parser);
1986
+ break;
1987
+ }
1988
+ case kPUBLIC: {
1989
+ visibility_range = parser->current_token.range;
1990
+ visibility = RBS_METHOD_DEFINITION_VISIBILITY_PUBLIC;
1991
+ member_range.start = visibility_range.start;
1992
+ rbs_parser_advance(parser);
1993
+ break;
1994
+ }
1995
+ default:
1996
+ visibility_range = NULL_RANGE;
1997
+ visibility = RBS_METHOD_DEFINITION_VISIBILITY_UNSPECIFIED;
1998
+ break;
1999
+ }
2000
+
2001
+ rbs_range_t keyword_range = parser->current_token.range;
2002
+
2003
+ rbs_range_t kind_range;
2004
+ InstanceSingletonKind kind;
2005
+ if (instance_only) {
2006
+ kind_range = NULL_RANGE;
2007
+ kind = INSTANCE_KIND;
2008
+ } else {
2009
+ kind = parse_instance_singleton_kind(parser, visibility == RBS_METHOD_DEFINITION_VISIBILITY_UNSPECIFIED, &kind_range);
2010
+ }
2011
+
2012
+ rbs_range_t name_range;
2013
+ rbs_ast_symbol_t *name = NULL;
2014
+ CHECK_PARSE(parse_method_name(parser, &name_range, &name));
2015
+
2016
+ #define SELF_ID rbs_constant_pool_insert_constant(&parser->constant_pool, (const unsigned char *) "self?", strlen("self?"))
2017
+
2018
+ if (parser->next_token.type == pDOT && name->constant_id == SELF_ID) {
2019
+ rbs_parser_set_error(parser, parser->next_token, true, "`self?` method cannot have visibility");
2020
+ return false;
2021
+ } else {
2022
+ ADVANCE_ASSERT(parser, pCOLON);
2023
+ }
2024
+
2025
+ rbs_parser_push_typevar_table(parser, kind != INSTANCE_KIND);
2026
+
2027
+ rbs_node_list_t *overloads = rbs_node_list_new(ALLOCATOR());
2028
+ bool overloading = false;
2029
+ rbs_range_t overloading_range = NULL_RANGE;
2030
+ bool loop = true;
2031
+ while (loop) {
2032
+ rbs_node_list_t *annotations = rbs_node_list_new(ALLOCATOR());
2033
+ rbs_position_t overload_annot_pos = NullPosition;
2034
+
2035
+ rbs_range_t overload_range;
2036
+ overload_range.start = parser->current_token.range.start;
2037
+
2038
+ if (parser->next_token.type == tANNOTATION) {
2039
+ CHECK_PARSE(parse_annotations(parser, annotations, &overload_annot_pos));
2040
+ }
2041
+
2042
+ switch (parser->next_token.type) {
2043
+ case pLPAREN:
2044
+ case pARROW:
2045
+ case pLBRACE:
2046
+ case pLBRACKET:
2047
+ case pQUESTION: {
2048
+ rbs_method_type_t *method_type = NULL;
2049
+ CHECK_PARSE(rbs_parse_method_type(parser, &method_type, false, !instance_only));
2050
+
2051
+ overload_range.end = parser->current_token.range.end;
2052
+ rbs_node_t *overload = (rbs_node_t *) rbs_ast_members_method_definition_overload_new(ALLOCATOR(), RBS_RANGE_LEX2AST(overload_range), annotations, (rbs_node_t *) method_type);
2053
+ rbs_node_list_append(overloads, overload);
2054
+ member_range.end = parser->current_token.range.end;
2055
+ break;
2056
+ }
2057
+
2058
+ case pDOT3:
2059
+ if (accept_overload) {
2060
+ overloading = true;
2061
+ rbs_parser_advance(parser);
2062
+ loop = false;
2063
+ overloading_range = parser->current_token.range;
2064
+ member_range.end = overloading_range.end;
2065
+ break;
2066
+ } else {
2067
+ rbs_parser_set_error(parser, parser->next_token, true, "unexpected overloading method definition");
2068
+ return false;
2069
+ }
2070
+
2071
+ default:
2072
+ rbs_parser_set_error(parser, parser->next_token, true, "unexpected token for method type");
2073
+ return false;
2074
+ }
2075
+
2076
+ if (parser->next_token.type == pBAR) {
2077
+ rbs_parser_advance(parser);
2078
+ } else {
2079
+ loop = false;
2080
+ }
2081
+ }
2082
+
2083
+ CHECK_PARSE(parser_pop_typevar_table(parser));
2084
+
2085
+ enum rbs_method_definition_kind k;
2086
+ switch (kind) {
2087
+ case INSTANCE_KIND: {
2088
+ k = RBS_METHOD_DEFINITION_KIND_INSTANCE;
2089
+ break;
2090
+ }
2091
+ case SINGLETON_KIND: {
2092
+ k = RBS_METHOD_DEFINITION_KIND_SINGLETON;
2093
+ break;
2094
+ }
2095
+ case INSTANCE_SINGLETON_KIND: {
2096
+ k = RBS_METHOD_DEFINITION_KIND_SINGLETON_INSTANCE;
2097
+ break;
2098
+ }
2099
+ default:
2100
+ rbs_parser_set_error(parser, parser->current_token, false, "Unexpected error");
2101
+ return false;
2102
+ }
2103
+
2104
+ *method_definition = rbs_ast_members_method_definition_new(ALLOCATOR(), RBS_RANGE_LEX2AST(member_range), name, k, overloads, annotations, comment, overloading, visibility, RBS_RANGE_LEX2AST(keyword_range), RBS_RANGE_LEX2AST(name_range));
2105
+ (*method_definition)->kind_range = RBS_RANGE_LEX2AST(kind_range);
2106
+ (*method_definition)->overloading_range = RBS_RANGE_LEX2AST(overloading_range);
2107
+ (*method_definition)->visibility_range = RBS_RANGE_LEX2AST(visibility_range);
2108
+
2109
+ return true;
2110
+ }
2111
+
2112
+ /**
2113
+ * class_instance_name ::= {} <class_name>
2114
+ * | {} class_name `[` type args <`]`>
2115
+ *
2116
+ * @param kind
2117
+ * */
2118
+ RBS_NODISCARD
2119
+ static bool class_instance_name(rbs_parser_t *parser, TypeNameKind kind, rbs_node_list_t *args, rbs_range_t *name_range, rbs_range_t *args_range, rbs_type_name_t **name, bool classish_allowed) {
2120
+ rbs_parser_advance(parser);
2121
+
2122
+ rbs_type_name_t *type_name = NULL;
2123
+ CHECK_PARSE(parse_type_name(parser, kind, name_range, &type_name));
2124
+ *name = type_name;
2125
+
2126
+ if (parser->next_token.type == pLBRACKET) {
2127
+ rbs_parser_advance(parser);
2128
+ args_range->start = parser->current_token.range.start;
2129
+ CHECK_PARSE(parse_type_list(parser, pRBRACKET, args, true, false, classish_allowed));
2130
+ ADVANCE_ASSERT(parser, pRBRACKET);
2131
+ args_range->end = parser->current_token.range.end;
2132
+ } else {
2133
+ *args_range = NULL_RANGE;
2134
+ }
2135
+
2136
+ return true;
2137
+ }
2138
+
2139
+ /**
2140
+ * mixin_member ::= {kINCLUDE} <class_instance_name>
2141
+ * | {kPREPEND} <class_instance_name>
2142
+ * | {kEXTEND} <class_instance_name>
2143
+ *
2144
+ * @param from_interface `true` when the member is in an interface.
2145
+ * */
2146
+ RBS_NODISCARD
2147
+ static bool parse_mixin_member(rbs_parser_t *parser, bool from_interface, rbs_position_t comment_pos, rbs_node_list_t *annotations, rbs_node_t **mixin_member) {
2148
+ rbs_range_t member_range;
2149
+ member_range.start = parser->current_token.range.start;
2150
+ comment_pos = rbs_nonnull_pos_or(comment_pos, member_range.start);
2151
+
2152
+ enum RBSTokenType type = parser->current_token.type;
2153
+ rbs_range_t keyword_range = parser->current_token.range;
2154
+
2155
+ bool reset_typevar_scope;
2156
+ switch (type) {
2157
+ case kINCLUDE:
2158
+ reset_typevar_scope = false;
2159
+ break;
2160
+ case kEXTEND:
2161
+ reset_typevar_scope = true;
2162
+ break;
2163
+ case kPREPEND:
2164
+ reset_typevar_scope = false;
2165
+ break;
2166
+ default:
2167
+ rbs_parser_set_error(parser, parser->current_token, false, "Unexpected error");
2168
+ return false;
2169
+ }
2170
+
2171
+ if (from_interface) {
2172
+ if (parser->current_token.type != kINCLUDE) {
2173
+ rbs_parser_set_error(parser, parser->current_token, true, "unexpected mixin in interface declaration");
2174
+ return false;
2175
+ }
2176
+ }
2177
+
2178
+ rbs_parser_push_typevar_table(parser, reset_typevar_scope);
2179
+
2180
+ rbs_node_list_t *args = rbs_node_list_new(ALLOCATOR());
2181
+ rbs_range_t name_range;
2182
+ rbs_range_t args_range = NULL_RANGE;
2183
+ rbs_type_name_t *name = NULL;
2184
+ CHECK_PARSE(class_instance_name(
2185
+ parser,
2186
+ from_interface ? INTERFACE_NAME : (TypeNameKind) (INTERFACE_NAME | CLASS_NAME),
2187
+ args,
2188
+ &name_range,
2189
+ &args_range,
2190
+ &name,
2191
+ !from_interface
2192
+ ));
2193
+
2194
+ CHECK_PARSE(parser_pop_typevar_table(parser));
2195
+
2196
+ member_range.end = parser->current_token.range.end;
2197
+
2198
+ rbs_location_range loc = RBS_RANGE_LEX2AST(member_range);
2199
+ rbs_location_range name_rg = RBS_RANGE_LEX2AST(name_range);
2200
+ rbs_location_range keyword_rg = RBS_RANGE_LEX2AST(keyword_range);
2201
+ rbs_location_range args_rg = RBS_RANGE_LEX2AST(args_range);
2202
+
2203
+ rbs_ast_comment_t *comment = rbs_parser_get_comment(parser, comment_pos.line);
2204
+ switch (type) {
2205
+ case kINCLUDE: {
2206
+ rbs_ast_members_include_t *include_member = rbs_ast_members_include_new(ALLOCATOR(), loc, name, args, annotations, comment, name_rg, keyword_rg);
2207
+ include_member->args_range = args_rg;
2208
+ *mixin_member = (rbs_node_t *) include_member;
2209
+ return true;
2210
+ }
2211
+ case kEXTEND: {
2212
+ rbs_ast_members_extend_t *extend_member = rbs_ast_members_extend_new(ALLOCATOR(), loc, name, args, annotations, comment, name_rg, keyword_rg);
2213
+ extend_member->args_range = args_rg;
2214
+ *mixin_member = (rbs_node_t *) extend_member;
2215
+ return true;
2216
+ }
2217
+ case kPREPEND: {
2218
+ rbs_ast_members_prepend_t *prepend_member = rbs_ast_members_prepend_new(ALLOCATOR(), loc, name, args, annotations, comment, name_rg, keyword_rg);
2219
+ prepend_member->args_range = args_rg;
2220
+ *mixin_member = (rbs_node_t *) prepend_member;
2221
+ return true;
2222
+ }
2223
+ default:
2224
+ rbs_parser_set_error(parser, parser->current_token, false, "Unexpected error");
2225
+ return false;
2226
+ }
2227
+ }
2228
+
2229
+ /**
2230
+ * @code
2231
+ * alias_member ::= {kALIAS} method_name <method_name>
2232
+ * | {kALIAS} kSELF `.` method_name kSELF `.` <method_name>
2233
+ * @endcode
2234
+ *
2235
+ * @param[in] instance_only `true` to reject `self.` alias.
2236
+ * */
2237
+ RBS_NODISCARD
2238
+ static bool parse_alias_member(rbs_parser_t *parser, bool instance_only, rbs_position_t comment_pos, rbs_node_list_t *annotations, rbs_ast_members_alias_t **alias_member) {
2239
+ rbs_range_t member_range;
2240
+ member_range.start = parser->current_token.range.start;
2241
+ rbs_range_t keyword_range = parser->current_token.range;
2242
+
2243
+ comment_pos = rbs_nonnull_pos_or(comment_pos, member_range.start);
2244
+ rbs_ast_comment_t *comment = rbs_parser_get_comment(parser, comment_pos.line);
2245
+
2246
+ enum rbs_alias_kind kind;
2247
+ rbs_ast_symbol_t *new_name, *old_name;
2248
+ rbs_range_t new_kind_range, old_kind_range, new_name_range, old_name_range;
2249
+
2250
+ if (!instance_only && parser->next_token.type == kSELF) {
2251
+ kind = RBS_ALIAS_KIND_SINGLETON;
2252
+
2253
+ new_kind_range.start = parser->next_token.range.start;
2254
+ new_kind_range.end = parser->next_token2.range.end;
2255
+ ADVANCE_ASSERT(parser, kSELF);
2256
+ ADVANCE_ASSERT(parser, pDOT);
2257
+ CHECK_PARSE(parse_method_name(parser, &new_name_range, &new_name));
2258
+
2259
+ old_kind_range.start = parser->next_token.range.start;
2260
+ old_kind_range.end = parser->next_token2.range.end;
2261
+ ADVANCE_ASSERT(parser, kSELF);
2262
+ ADVANCE_ASSERT(parser, pDOT);
2263
+ CHECK_PARSE(parse_method_name(parser, &old_name_range, &old_name));
2264
+ } else {
2265
+ kind = RBS_ALIAS_KIND_INSTANCE;
2266
+ CHECK_PARSE(parse_method_name(parser, &new_name_range, &new_name));
2267
+ CHECK_PARSE(parse_method_name(parser, &old_name_range, &old_name));
2268
+ new_kind_range = NULL_RANGE;
2269
+ old_kind_range = NULL_RANGE;
2270
+ }
2271
+
2272
+ member_range.end = parser->current_token.range.end;
2273
+
2274
+ rbs_location_range loc = RBS_RANGE_LEX2AST(member_range);
2275
+
2276
+ *alias_member = rbs_ast_members_alias_new(ALLOCATOR(), loc, new_name, old_name, kind, annotations, comment, RBS_RANGE_LEX2AST(keyword_range), RBS_RANGE_LEX2AST(new_name_range), RBS_RANGE_LEX2AST(old_name_range));
2277
+ (*alias_member)->new_kind_range = RBS_RANGE_LEX2AST(new_kind_range);
2278
+ (*alias_member)->old_kind_range = RBS_RANGE_LEX2AST(old_kind_range);
2279
+
2280
+ return true;
2281
+ }
2282
+
2283
+ /*
2284
+ variable_member ::= {tAIDENT} `:` <type>
2285
+ | {kSELF} `.` tAIDENT `:` <type>
2286
+ | {tA2IDENT} `:` <type>
2287
+ */
2288
+ RBS_NODISCARD
2289
+ static bool parse_variable_member(rbs_parser_t *parser, rbs_position_t comment_pos, rbs_node_list_t *annotations, rbs_node_t **variable_member) {
2290
+ if (annotations->length > 0) {
2291
+ rbs_parser_set_error(parser, parser->current_token, true, "annotation cannot be given to variable members");
2292
+ return false;
2293
+ }
2294
+
2295
+ rbs_range_t member_range;
2296
+ member_range.start = parser->current_token.range.start;
2297
+ comment_pos = rbs_nonnull_pos_or(comment_pos, member_range.start);
2298
+ rbs_ast_comment_t *comment = rbs_parser_get_comment(parser, comment_pos.line);
2299
+
2300
+ switch (parser->current_token.type) {
2301
+ case tAIDENT:
2302
+ case kATRBS: {
2303
+ rbs_range_t name_range = parser->current_token.range;
2304
+ rbs_location_range symbolLoc = RBS_RANGE_LEX2AST(name_range);
2305
+ rbs_ast_symbol_t *name = rbs_ast_symbol_new(ALLOCATOR(), symbolLoc, &parser->constant_pool, INTERN_TOKEN(parser, parser->current_token));
2306
+
2307
+ ADVANCE_ASSERT(parser, pCOLON);
2308
+ rbs_range_t colon_range = parser->current_token.range;
2309
+
2310
+ rbs_node_t *type;
2311
+ CHECK_PARSE(rbs_parse_type(parser, &type, false, true, true));
2312
+ member_range.end = parser->current_token.range.end;
2313
+
2314
+ rbs_location_range loc = RBS_RANGE_LEX2AST(member_range);
2315
+
2316
+ *variable_member = (rbs_node_t *) rbs_ast_members_instance_variable_new(ALLOCATOR(), loc, name, type, comment, RBS_RANGE_LEX2AST(name_range), RBS_RANGE_LEX2AST(colon_range));
2317
+
2318
+ return true;
2319
+ }
2320
+ case tA2IDENT: {
2321
+ rbs_range_t name_range = parser->current_token.range;
2322
+ rbs_location_range symbol_loc = RBS_RANGE_LEX2AST(name_range);
2323
+ rbs_ast_symbol_t *name = rbs_ast_symbol_new(ALLOCATOR(), symbol_loc, &parser->constant_pool, INTERN_TOKEN(parser, parser->current_token));
2324
+
2325
+ ADVANCE_ASSERT(parser, pCOLON);
2326
+ rbs_range_t colon_range = parser->current_token.range;
2327
+
2328
+ rbs_parser_push_typevar_table(parser, true);
2329
+
2330
+ rbs_node_t *type;
2331
+ CHECK_PARSE(rbs_parse_type(parser, &type, false, false, true));
2332
+
2333
+ CHECK_PARSE(parser_pop_typevar_table(parser));
2334
+
2335
+ member_range.end = parser->current_token.range.end;
2336
+
2337
+ rbs_location_range loc = RBS_RANGE_LEX2AST(member_range);
2338
+
2339
+ *variable_member = (rbs_node_t *) rbs_ast_members_class_variable_new(ALLOCATOR(), loc, name, type, comment, RBS_RANGE_LEX2AST(name_range), RBS_RANGE_LEX2AST(colon_range));
2340
+
2341
+ return true;
2342
+ }
2343
+ case kSELF: {
2344
+ rbs_range_t kind_range = {
2345
+ .start = parser->current_token.range.start,
2346
+ .end = parser->next_token.range.end
2347
+ };
2348
+
2349
+ ADVANCE_ASSERT(parser, pDOT);
2350
+ if (parser->next_token.type == tAIDENT || parser->next_token.type == kATRBS) {
2351
+ rbs_parser_advance(parser);
2352
+ } else {
2353
+ rbs_parser_set_error(parser, parser->current_token, false, "Unexpected error");
2354
+ return false;
2355
+ }
2356
+
2357
+ rbs_range_t name_range = parser->current_token.range;
2358
+ rbs_location_range symbol_loc = RBS_RANGE_LEX2AST(name_range);
2359
+ rbs_ast_symbol_t *name = rbs_ast_symbol_new(ALLOCATOR(), symbol_loc, &parser->constant_pool, INTERN_TOKEN(parser, parser->current_token));
2360
+
2361
+ ADVANCE_ASSERT(parser, pCOLON);
2362
+ rbs_range_t colon_range = parser->current_token.range;
2363
+
2364
+ rbs_parser_push_typevar_table(parser, true);
2365
+
2366
+ rbs_node_t *type;
2367
+ CHECK_PARSE(rbs_parse_type(parser, &type, false, true, true));
2368
+
2369
+ CHECK_PARSE(parser_pop_typevar_table(parser));
2370
+
2371
+ member_range.end = parser->current_token.range.end;
2372
+
2373
+ rbs_location_range loc = RBS_RANGE_LEX2AST(member_range);
2374
+
2375
+ rbs_ast_members_class_instance_variable_t *class_ivar_member = rbs_ast_members_class_instance_variable_new(ALLOCATOR(), loc, name, type, comment, RBS_RANGE_LEX2AST(name_range), RBS_RANGE_LEX2AST(colon_range));
2376
+ class_ivar_member->kind_range = RBS_RANGE_LEX2AST(kind_range);
2377
+
2378
+ *variable_member = (rbs_node_t *) class_ivar_member;
2379
+
2380
+ return true;
2381
+ }
2382
+ default:
2383
+ rbs_parser_set_error(parser, parser->current_token, false, "Unexpected error");
2384
+ return false;
2385
+ }
2386
+ }
2387
+
2388
+ /*
2389
+ visibility_member ::= {<`public`>}
2390
+ | {<`private`>}
2391
+ */
2392
+ RBS_NODISCARD
2393
+ static bool parse_visibility_member(rbs_parser_t *parser, rbs_node_list_t *annotations, rbs_node_t **visibility_member) {
2394
+ if (annotations->length > 0) {
2395
+ rbs_parser_set_error(parser, parser->current_token, true, "annotation cannot be given to visibility members");
2396
+ return false;
2397
+ }
2398
+
2399
+ rbs_location_range location = rbs_location_range_current_token(parser);
2400
+
2401
+ switch (parser->current_token.type) {
2402
+ case kPUBLIC: {
2403
+ *visibility_member = (rbs_node_t *) rbs_ast_members_public_new(ALLOCATOR(), location);
2404
+ return true;
2405
+ }
2406
+ case kPRIVATE: {
2407
+ *visibility_member = (rbs_node_t *) rbs_ast_members_private_new(ALLOCATOR(), location);
2408
+ return true;
2409
+ }
2410
+ default:
2411
+ rbs_parser_set_error(parser, parser->current_token, false, "Unexpected error");
2412
+ return false;
2413
+ }
2414
+ }
2415
+
2416
+ /*
2417
+ attribute_member ::= {attr_keyword} attr_name attr_var `:` <type>
2418
+ | {visibility} attr_keyword attr_name attr_var `:` <type>
2419
+ | {attr_keyword} `self` `.` attr_name attr_var `:` <type>
2420
+ | {visibility} attr_keyword `self` `.` attr_name attr_var `:` <type>
2421
+
2422
+ attr_keyword ::= `attr_reader` | `attr_writer` | `attr_accessor`
2423
+
2424
+ visibility ::= `public` | `private`
2425
+
2426
+ attr_var ::= # empty
2427
+ | `(` tAIDENT `)` # Ivar name
2428
+ | `(` `)` # No variable
2429
+ */
2430
+ RBS_NODISCARD
2431
+ static bool parse_attribute_member(rbs_parser_t *parser, rbs_position_t comment_pos, rbs_node_list_t *annotations, rbs_node_t **attribute_member) {
2432
+ rbs_range_t member_range;
2433
+
2434
+ member_range.start = parser->current_token.range.start;
2435
+ comment_pos = rbs_nonnull_pos_or(comment_pos, member_range.start);
2436
+ rbs_ast_comment_t *comment = rbs_parser_get_comment(parser, comment_pos.line);
2437
+
2438
+ rbs_range_t visibility_range;
2439
+ enum rbs_attribute_visibility visibility;
2440
+
2441
+ switch (parser->current_token.type) {
2442
+ case kPRIVATE: {
2443
+ visibility_range = parser->current_token.range;
2444
+ visibility = RBS_ATTRIBUTE_VISIBILITY_PRIVATE;
2445
+ rbs_parser_advance(parser);
2446
+ break;
2447
+ }
2448
+ case kPUBLIC: {
2449
+ visibility_range = parser->current_token.range;
2450
+ visibility = RBS_ATTRIBUTE_VISIBILITY_PUBLIC;
2451
+ rbs_parser_advance(parser);
2452
+ break;
2453
+ }
2454
+ default:
2455
+ visibility = RBS_ATTRIBUTE_VISIBILITY_UNSPECIFIED;
2456
+ visibility_range = NULL_RANGE;
2457
+ break;
2458
+ }
2459
+
2460
+ enum RBSTokenType attr_type = parser->current_token.type;
2461
+ rbs_range_t keyword_range = parser->current_token.range;
2462
+
2463
+ rbs_range_t kind_range;
2464
+ InstanceSingletonKind is_kind = parse_instance_singleton_kind(parser, false, &kind_range);
2465
+
2466
+ enum rbs_attribute_kind kind = (is_kind == INSTANCE_KIND) ? RBS_ATTRIBUTE_KIND_INSTANCE : RBS_ATTRIBUTE_KIND_SINGLETON;
2467
+
2468
+ rbs_range_t name_range;
2469
+ rbs_ast_symbol_t *attr_name;
2470
+ CHECK_PARSE(parse_method_name(parser, &name_range, &attr_name));
2471
+
2472
+ rbs_attr_ivar_name_t ivar_name = { .tag = RBS_ATTR_IVAR_NAME_TAG_UNSPECIFIED };
2473
+ rbs_location_range ivar_range = RBS_LOCATION_NULL_RANGE;
2474
+ rbs_location_range ivar_name_range = RBS_LOCATION_NULL_RANGE;
2475
+ if (parser->next_token.type == pLPAREN) {
2476
+ ADVANCE_ASSERT(parser, pLPAREN);
2477
+ ivar_range.start_char = parser->current_token.range.start.char_pos;
2478
+ ivar_range.start_byte = parser->current_token.range.start.byte_pos;
2479
+
2480
+ ivar_name.tag = RBS_ATTR_IVAR_NAME_TAG_EMPTY;
2481
+
2482
+ if (parser_advance_if(parser, tAIDENT) || parser_advance_if(parser, kATRBS)) {
2483
+ ivar_name.tag = RBS_ATTR_IVAR_NAME_TAG_NAME;
2484
+ ivar_name.name = INTERN_TOKEN(parser, parser->current_token);
2485
+ ivar_name_range = rbs_location_range_current_token(parser);
2486
+ }
2487
+
2488
+ ADVANCE_ASSERT(parser, pRPAREN);
2489
+ ivar_range.end_char = parser->current_token.range.end.char_pos;
2490
+ ivar_range.end_byte = parser->current_token.range.end.byte_pos;
2491
+ }
2492
+
2493
+ ADVANCE_ASSERT(parser, pCOLON);
2494
+ rbs_range_t colon_range = parser->current_token.range;
2495
+
2496
+ rbs_parser_push_typevar_table(parser, is_kind == SINGLETON_KIND);
2497
+
2498
+ rbs_node_t *type;
2499
+ CHECK_PARSE(rbs_parse_type(parser, &type, false, true, true));
2500
+
2501
+ CHECK_PARSE(parser_pop_typevar_table(parser));
2502
+
2503
+ member_range.end = parser->current_token.range.end;
2504
+
2505
+ rbs_location_range loc = RBS_RANGE_LEX2AST(member_range);
2506
+ rbs_location_range keyword_rg = RBS_RANGE_LEX2AST(keyword_range);
2507
+ rbs_location_range name_rg = RBS_RANGE_LEX2AST(name_range);
2508
+ rbs_location_range colon_rg = RBS_RANGE_LEX2AST(colon_range);
2509
+ rbs_location_range kind_rg = RBS_RANGE_LEX2AST(kind_range);
2510
+ rbs_location_range visibility_rg = RBS_RANGE_LEX2AST(visibility_range);
2511
+
2512
+ switch (attr_type) {
2513
+ case kATTRREADER: {
2514
+ rbs_ast_members_attr_reader_t *attr_reader = rbs_ast_members_attr_reader_new(ALLOCATOR(), loc, attr_name, type, ivar_name, kind, annotations, comment, visibility, keyword_rg, name_rg, colon_rg);
2515
+ attr_reader->kind_range = kind_rg;
2516
+ attr_reader->ivar_range = ivar_range;
2517
+ attr_reader->ivar_name_range = ivar_name_range;
2518
+ attr_reader->visibility_range = visibility_rg;
2519
+
2520
+ *attribute_member = (rbs_node_t *) attr_reader;
2521
+
2522
+ return true;
2523
+ }
2524
+ case kATTRWRITER: {
2525
+ rbs_ast_members_attr_writer_t *attr_writer = rbs_ast_members_attr_writer_new(ALLOCATOR(), loc, attr_name, type, ivar_name, kind, annotations, comment, visibility, keyword_rg, name_rg, colon_rg);
2526
+ attr_writer->kind_range = kind_rg;
2527
+ attr_writer->ivar_range = ivar_range;
2528
+ attr_writer->ivar_name_range = ivar_name_range;
2529
+ attr_writer->visibility_range = visibility_rg;
2530
+
2531
+ *attribute_member = (rbs_node_t *) attr_writer;
2532
+
2533
+ return true;
2534
+ }
2535
+ case kATTRACCESSOR: {
2536
+ rbs_ast_members_attr_accessor_t *attr_accessor = rbs_ast_members_attr_accessor_new(ALLOCATOR(), loc, attr_name, type, ivar_name, kind, annotations, comment, visibility, keyword_rg, name_rg, colon_rg);
2537
+ attr_accessor->kind_range = kind_rg;
2538
+ attr_accessor->ivar_range = ivar_range;
2539
+ attr_accessor->ivar_name_range = ivar_name_range;
2540
+ attr_accessor->visibility_range = visibility_rg;
2541
+
2542
+ *attribute_member = (rbs_node_t *) attr_accessor;
2543
+
2544
+ return true;
2545
+ }
2546
+ default:
2547
+ rbs_parser_set_error(parser, parser->current_token, false, "Unexpected error");
2548
+ return false;
2549
+ }
2550
+ }
2551
+
2552
+ /*
2553
+ interface_members ::= {} ...<interface_member> kEND
2554
+
2555
+ interface_member ::= def_member (instance method only && no overloading)
2556
+ | mixin_member (interface only)
2557
+ | alias_member (instance only)
2558
+ */
2559
+ RBS_NODISCARD
2560
+ static bool parse_interface_members(rbs_parser_t *parser, rbs_node_list_t **members) {
2561
+ *members = rbs_node_list_new(ALLOCATOR());
2562
+
2563
+ while (parser->next_token.type != kEND) {
2564
+ rbs_node_list_t *annotations = rbs_node_list_new(ALLOCATOR());
2565
+ rbs_position_t annot_pos = NullPosition;
2566
+
2567
+ CHECK_PARSE(parse_annotations(parser, annotations, &annot_pos));
2568
+ rbs_parser_advance(parser);
2569
+
2570
+ rbs_node_t *member;
2571
+ switch (parser->current_token.type) {
2572
+ case kDEF: {
2573
+ rbs_ast_members_method_definition_t *method_definition = NULL;
2574
+ CHECK_PARSE(parse_member_def(parser, true, true, annot_pos, annotations, &method_definition));
2575
+ member = (rbs_node_t *) method_definition;
2576
+ break;
2577
+ }
2578
+
2579
+ case kINCLUDE:
2580
+ case kEXTEND:
2581
+ case kPREPEND: {
2582
+ CHECK_PARSE(parse_mixin_member(parser, true, annot_pos, annotations, &member));
2583
+ break;
2584
+ }
2585
+
2586
+ case kALIAS: {
2587
+ rbs_ast_members_alias_t *alias_member = NULL;
2588
+ CHECK_PARSE(parse_alias_member(parser, true, annot_pos, annotations, &alias_member));
2589
+ member = (rbs_node_t *) alias_member;
2590
+ break;
2591
+ }
2592
+
2593
+ default:
2594
+ rbs_parser_set_error(parser, parser->current_token, true, "unexpected token for interface declaration member");
2595
+ return false;
2596
+ }
2597
+
2598
+ rbs_node_list_append(*members, member);
2599
+ }
2600
+
2601
+ return true;
2602
+ }
2603
+
2604
+ /*
2605
+ interface_decl ::= {`interface`} interface_name module_type_params interface_members <kEND>
2606
+ */
2607
+ RBS_NODISCARD
2608
+ static bool parse_interface_decl(rbs_parser_t *parser, rbs_position_t comment_pos, rbs_node_list_t *annotations, rbs_ast_declarations_interface_t **interface_decl) {
2609
+ rbs_parser_push_typevar_table(parser, true);
2610
+
2611
+ rbs_range_t member_range;
2612
+ member_range.start = parser->current_token.range.start;
2613
+ comment_pos = rbs_nonnull_pos_or(comment_pos, member_range.start);
2614
+
2615
+ rbs_range_t keyword_range = parser->current_token.range;
2616
+
2617
+ rbs_parser_advance(parser);
2618
+
2619
+ rbs_range_t name_range;
2620
+ rbs_type_name_t *name = NULL;
2621
+ CHECK_PARSE(parse_type_name(parser, INTERFACE_NAME, &name_range, &name));
2622
+
2623
+ rbs_range_t type_params_range;
2624
+ rbs_node_list_t *type_params;
2625
+ CHECK_PARSE(parse_type_params(parser, &type_params_range, true, &type_params));
2626
+
2627
+ rbs_node_list_t *members = NULL;
2628
+ CHECK_PARSE(parse_interface_members(parser, &members));
2629
+
2630
+ ADVANCE_ASSERT(parser, kEND);
2631
+ rbs_range_t end_range = parser->current_token.range;
2632
+ member_range.end = end_range.end;
2633
+
2634
+ CHECK_PARSE(parser_pop_typevar_table(parser));
2635
+
2636
+ rbs_location_range loc = RBS_RANGE_LEX2AST(member_range);
2637
+ rbs_ast_comment_t *comment = rbs_parser_get_comment(parser, comment_pos.line);
2638
+
2639
+ *interface_decl = rbs_ast_declarations_interface_new(ALLOCATOR(), loc, name, type_params, members, annotations, comment, RBS_RANGE_LEX2AST(keyword_range), RBS_RANGE_LEX2AST(name_range), RBS_RANGE_LEX2AST(end_range));
2640
+ (*interface_decl)->type_params_range = RBS_RANGE_LEX2AST(type_params_range);
2641
+
2642
+ return true;
2643
+ }
2644
+
2645
+ /*
2646
+ module_self_types ::= {`:`} module_self_type `,` ... `,` <module_self_type>
2647
+
2648
+ module_self_type ::= <module_name>
2649
+ | module_name `[` type_list <`]`>
2650
+ */
2651
+ RBS_NODISCARD
2652
+ static bool parse_module_self_types(rbs_parser_t *parser, rbs_node_list_t *array) {
2653
+ while (true) {
2654
+ rbs_parser_advance(parser);
2655
+
2656
+ rbs_range_t self_range;
2657
+ self_range.start = parser->current_token.range.start;
2658
+
2659
+ rbs_range_t name_range;
2660
+ rbs_type_name_t *module_name = NULL;
2661
+ CHECK_PARSE(parse_type_name(parser, (TypeNameKind) (CLASS_NAME | INTERFACE_NAME), &name_range, &module_name));
2662
+ self_range.end = name_range.end;
2663
+
2664
+ rbs_node_list_t *args = rbs_node_list_new(ALLOCATOR());
2665
+ rbs_range_t args_range = NULL_RANGE;
2666
+ if (parser->next_token.type == pLBRACKET) {
2667
+ rbs_parser_advance(parser);
2668
+ args_range.start = parser->current_token.range.start;
2669
+ CHECK_PARSE(parse_type_list(parser, pRBRACKET, args, true, false, false));
2670
+ rbs_parser_advance(parser);
2671
+ self_range.end = args_range.end = parser->current_token.range.end;
2672
+ }
2673
+
2674
+ rbs_location_range loc = RBS_RANGE_LEX2AST(self_range);
2675
+
2676
+ rbs_ast_declarations_module_self_t *self_type = rbs_ast_declarations_module_self_new(ALLOCATOR(), loc, module_name, args, RBS_RANGE_LEX2AST(name_range));
2677
+ self_type->args_range = RBS_RANGE_LEX2AST(args_range);
2678
+ rbs_node_list_append(array, (rbs_node_t *) self_type);
2679
+
2680
+ if (parser->next_token.type == pCOMMA) {
2681
+ rbs_parser_advance(parser);
2682
+ } else {
2683
+ break;
2684
+ }
2685
+ }
2686
+
2687
+ return true;
2688
+ }
2689
+
2690
+ RBS_NODISCARD
2691
+ static bool parse_nested_decl(rbs_parser_t *parser, const char *nested_in, rbs_position_t annot_pos, rbs_node_list_t *annotations, rbs_node_t **decl);
2692
+
2693
+ /*
2694
+ module_members ::= {} ...<module_member> kEND
2695
+
2696
+ module_member ::= def_member
2697
+ | variable_member
2698
+ | mixin_member
2699
+ | alias_member
2700
+ | attribute_member
2701
+ | `public`
2702
+ | `private`
2703
+ */
2704
+ RBS_NODISCARD
2705
+ static bool parse_module_members(rbs_parser_t *parser, rbs_node_list_t **members) {
2706
+ *members = rbs_node_list_new(ALLOCATOR());
2707
+
2708
+ while (parser->next_token.type != kEND) {
2709
+ rbs_node_list_t *annotations = rbs_node_list_new(ALLOCATOR());
2710
+ rbs_position_t annot_pos;
2711
+ CHECK_PARSE(parse_annotations(parser, annotations, &annot_pos));
2712
+
2713
+ rbs_parser_advance(parser);
2714
+
2715
+ rbs_node_t *member;
2716
+ switch (parser->current_token.type) {
2717
+ case kDEF: {
2718
+ rbs_ast_members_method_definition_t *method_definition;
2719
+ CHECK_PARSE(parse_member_def(parser, false, true, annot_pos, annotations, &method_definition));
2720
+ member = (rbs_node_t *) method_definition;
2721
+ break;
2722
+ }
2723
+
2724
+ case kINCLUDE:
2725
+ case kEXTEND:
2726
+ case kPREPEND: {
2727
+ CHECK_PARSE(parse_mixin_member(parser, false, annot_pos, annotations, &member));
2728
+ break;
2729
+ }
2730
+ case kALIAS: {
2731
+ rbs_ast_members_alias_t *alias_member = NULL;
2732
+ CHECK_PARSE(parse_alias_member(parser, false, annot_pos, annotations, &alias_member));
2733
+ member = (rbs_node_t *) alias_member;
2734
+ break;
2735
+ }
2736
+ case tAIDENT:
2737
+ case tA2IDENT:
2738
+ case kATRBS:
2739
+ case kSELF: {
2740
+ CHECK_PARSE(parse_variable_member(parser, annot_pos, annotations, &member));
2741
+ break;
2742
+ }
2743
+
2744
+ case kATTRREADER:
2745
+ case kATTRWRITER:
2746
+ case kATTRACCESSOR: {
2747
+ CHECK_PARSE(parse_attribute_member(parser, annot_pos, annotations, &member));
2748
+ break;
2749
+ }
2750
+
2751
+ case kPUBLIC:
2752
+ case kPRIVATE:
2753
+ if (parser->next_token.range.start.line == parser->current_token.range.start.line) {
2754
+ switch (parser->next_token.type) {
2755
+ case kDEF: {
2756
+ rbs_ast_members_method_definition_t *method_definition = NULL;
2757
+ CHECK_PARSE(parse_member_def(parser, false, true, annot_pos, annotations, &method_definition));
2758
+ member = (rbs_node_t *) method_definition;
2759
+ break;
2760
+ }
2761
+ case kATTRREADER:
2762
+ case kATTRWRITER:
2763
+ case kATTRACCESSOR: {
2764
+ CHECK_PARSE(parse_attribute_member(parser, annot_pos, annotations, &member));
2765
+ break;
2766
+ }
2767
+ default:
2768
+ rbs_parser_set_error(parser, parser->next_token, true, "method or attribute definition is expected after visibility modifier");
2769
+ return false;
2770
+ }
2771
+ } else {
2772
+ CHECK_PARSE(parse_visibility_member(parser, annotations, &member));
2773
+ }
2774
+ break;
2775
+
2776
+ default:
2777
+ CHECK_PARSE(parse_nested_decl(parser, "module", annot_pos, annotations, &member));
2778
+ break;
2779
+ }
2780
+
2781
+ rbs_node_list_append(*members, member);
2782
+ }
2783
+
2784
+ return true;
2785
+ }
2786
+
2787
+ /*
2788
+ module_decl ::= {module_name} module_type_params module_members <kEND>
2789
+ | {module_name} module_name module_type_params `:` module_self_types module_members <kEND>
2790
+ */
2791
+ RBS_NODISCARD
2792
+ static bool parse_module_decl0(rbs_parser_t *parser, rbs_range_t keyword_range, rbs_type_name_t *module_name, rbs_range_t name_range, rbs_ast_comment_t *comment, rbs_node_list_t *annotations, rbs_ast_declarations_module_t **module_decl) {
2793
+ rbs_parser_push_typevar_table(parser, true);
2794
+
2795
+ rbs_range_t decl_range;
2796
+ decl_range.start = keyword_range.start;
2797
+
2798
+ rbs_range_t type_params_range;
2799
+ rbs_node_list_t *type_params;
2800
+ CHECK_PARSE(parse_type_params(parser, &type_params_range, true, &type_params));
2801
+
2802
+ rbs_node_list_t *self_types = rbs_node_list_new(ALLOCATOR());
2803
+ rbs_range_t colon_range;
2804
+ rbs_range_t self_types_range;
2805
+ if (parser->next_token.type == pCOLON) {
2806
+ rbs_parser_advance(parser);
2807
+ colon_range = parser->current_token.range;
2808
+ self_types_range.start = parser->next_token.range.start;
2809
+ CHECK_PARSE(parse_module_self_types(parser, self_types));
2810
+ self_types_range.end = parser->current_token.range.end;
2811
+ } else {
2812
+ colon_range = NULL_RANGE;
2813
+ self_types_range = NULL_RANGE;
2814
+ }
2815
+
2816
+ rbs_node_list_t *members = NULL;
2817
+ CHECK_PARSE(parse_module_members(parser, &members));
2818
+
2819
+ ADVANCE_ASSERT(parser, kEND);
2820
+ rbs_range_t end_range = parser->current_token.range;
2821
+ decl_range.end = parser->current_token.range.end;
2822
+
2823
+ CHECK_PARSE(parser_pop_typevar_table(parser));
2824
+
2825
+ rbs_location_range loc = RBS_RANGE_LEX2AST(decl_range);
2826
+
2827
+ *module_decl = rbs_ast_declarations_module_new(ALLOCATOR(), loc, module_name, type_params, self_types, members, annotations, comment, RBS_RANGE_LEX2AST(keyword_range), RBS_RANGE_LEX2AST(name_range), RBS_RANGE_LEX2AST(end_range));
2828
+ (*module_decl)->type_params_range = RBS_RANGE_LEX2AST(type_params_range);
2829
+ (*module_decl)->colon_range = RBS_RANGE_LEX2AST(colon_range);
2830
+ (*module_decl)->self_types_range = RBS_RANGE_LEX2AST(self_types_range);
2831
+ return true;
2832
+ }
2833
+
2834
+ /*
2835
+ module_decl ::= {`module`} module_name `=` old_module_name <kEND>
2836
+ | {`module`} module_name module_decl0 <kEND>
2837
+
2838
+ */
2839
+ RBS_NODISCARD
2840
+ static bool parse_module_decl(rbs_parser_t *parser, rbs_position_t comment_pos, rbs_node_list_t *annotations, rbs_node_t **module_decl) {
2841
+ rbs_range_t keyword_range = parser->current_token.range;
2842
+
2843
+ comment_pos = rbs_nonnull_pos_or(comment_pos, parser->current_token.range.start);
2844
+ rbs_ast_comment_t *comment = rbs_parser_get_comment(parser, comment_pos.line);
2845
+
2846
+ rbs_parser_advance(parser);
2847
+
2848
+ rbs_range_t module_name_range;
2849
+ rbs_type_name_t *module_name;
2850
+ CHECK_PARSE(parse_type_name(parser, CLASS_NAME, &module_name_range, &module_name));
2851
+
2852
+ if (parser->next_token.type == pEQ) {
2853
+ rbs_range_t eq_range = parser->next_token.range;
2854
+ rbs_parser_advance(parser);
2855
+ rbs_parser_advance(parser);
2856
+
2857
+ rbs_range_t old_name_range;
2858
+ rbs_type_name_t *old_name = NULL;
2859
+ CHECK_PARSE(parse_type_name(parser, CLASS_NAME, &old_name_range, &old_name));
2860
+
2861
+ rbs_range_t decl_range = {
2862
+ .start = keyword_range.start,
2863
+ .end = old_name_range.end
2864
+ };
2865
+
2866
+ rbs_location_range loc = RBS_RANGE_LEX2AST(decl_range);
2867
+
2868
+ *module_decl = (rbs_node_t *) rbs_ast_declarations_module_alias_new(ALLOCATOR(), loc, module_name, old_name, comment, annotations, RBS_RANGE_LEX2AST(keyword_range), RBS_RANGE_LEX2AST(module_name_range), RBS_RANGE_LEX2AST(eq_range), RBS_RANGE_LEX2AST(old_name_range));
2869
+ } else {
2870
+ rbs_ast_declarations_module_t *module_decl0 = NULL;
2871
+ CHECK_PARSE(parse_module_decl0(parser, keyword_range, module_name, module_name_range, comment, annotations, &module_decl0));
2872
+ *module_decl = (rbs_node_t *) module_decl0;
2873
+ }
2874
+
2875
+ return true;
2876
+ }
2877
+
2878
+ /*
2879
+ class_decl_super ::= {} `<` <class_instance_name>
2880
+ | {<>}
2881
+ */
2882
+ RBS_NODISCARD
2883
+ static bool parse_class_decl_super(rbs_parser_t *parser, rbs_range_t *lt_range, rbs_ast_declarations_class_super_t **super) {
2884
+ if (parser_advance_if(parser, pLT)) {
2885
+ *lt_range = parser->current_token.range;
2886
+
2887
+ rbs_range_t super_range;
2888
+ super_range.start = parser->next_token.range.start;
2889
+
2890
+ rbs_node_list_t *args = rbs_node_list_new(ALLOCATOR());
2891
+ rbs_type_name_t *name = NULL;
2892
+ rbs_range_t name_range, args_range;
2893
+ CHECK_PARSE(class_instance_name(parser, CLASS_NAME, args, &name_range, &args_range, &name, false));
2894
+
2895
+ super_range.end = parser->current_token.range.end;
2896
+
2897
+ rbs_location_range loc = RBS_RANGE_LEX2AST(super_range);
2898
+ *super = rbs_ast_declarations_class_super_new(ALLOCATOR(), loc, name, args, RBS_RANGE_LEX2AST(name_range));
2899
+ (*super)->args_range = RBS_RANGE_LEX2AST(args_range);
2900
+ } else {
2901
+ *lt_range = NULL_RANGE;
2902
+ }
2903
+
2904
+ return true;
2905
+ }
2906
+
2907
+ /*
2908
+ class_decl ::= {class_name} type_params class_decl_super class_members <`end`>
2909
+ */
2910
+ RBS_NODISCARD
2911
+ static bool parse_class_decl0(rbs_parser_t *parser, rbs_range_t keyword_range, rbs_type_name_t *name, rbs_range_t name_range, rbs_ast_comment_t *comment, rbs_node_list_t *annotations, rbs_ast_declarations_class_t **class_decl) {
2912
+ rbs_parser_push_typevar_table(parser, true);
2913
+
2914
+ rbs_range_t decl_range;
2915
+ decl_range.start = keyword_range.start;
2916
+
2917
+ rbs_range_t type_params_range;
2918
+ rbs_node_list_t *type_params;
2919
+ CHECK_PARSE(parse_type_params(parser, &type_params_range, true, &type_params));
2920
+
2921
+ rbs_range_t lt_range;
2922
+ rbs_ast_declarations_class_super_t *super = NULL;
2923
+ CHECK_PARSE(parse_class_decl_super(parser, &lt_range, &super));
2924
+
2925
+ rbs_node_list_t *members = NULL;
2926
+ CHECK_PARSE(parse_module_members(parser, &members));
2927
+
2928
+ ADVANCE_ASSERT(parser, kEND);
2929
+
2930
+ rbs_range_t end_range = parser->current_token.range;
2931
+
2932
+ decl_range.end = end_range.end;
2933
+
2934
+ CHECK_PARSE(parser_pop_typevar_table(parser));
2935
+
2936
+ rbs_location_range loc = RBS_RANGE_LEX2AST(decl_range);
2937
+
2938
+ *class_decl = rbs_ast_declarations_class_new(ALLOCATOR(), loc, name, type_params, super, members, annotations, comment, RBS_RANGE_LEX2AST(keyword_range), RBS_RANGE_LEX2AST(name_range), RBS_RANGE_LEX2AST(end_range));
2939
+ (*class_decl)->type_params_range = RBS_RANGE_LEX2AST(type_params_range);
2940
+ (*class_decl)->lt_range = RBS_RANGE_LEX2AST(lt_range);
2941
+
2942
+ return true;
2943
+ }
2944
+
2945
+ /*
2946
+ class_decl ::= {`class`} class_name `=` <class_name>
2947
+ | {`class`} class_name <class_decl0>
2948
+ */
2949
+ RBS_NODISCARD
2950
+ static bool parse_class_decl(rbs_parser_t *parser, rbs_position_t comment_pos, rbs_node_list_t *annotations, rbs_node_t **class_decl) {
2951
+ rbs_range_t keyword_range = parser->current_token.range;
2952
+
2953
+ comment_pos = rbs_nonnull_pos_or(comment_pos, parser->current_token.range.start);
2954
+ rbs_ast_comment_t *comment = rbs_parser_get_comment(parser, comment_pos.line);
2955
+
2956
+ rbs_parser_advance(parser);
2957
+ rbs_range_t class_name_range;
2958
+ rbs_type_name_t *class_name = NULL;
2959
+ CHECK_PARSE(parse_type_name(parser, CLASS_NAME, &class_name_range, &class_name));
2960
+
2961
+ if (parser->next_token.type == pEQ) {
2962
+ rbs_range_t eq_range = parser->next_token.range;
2963
+ rbs_parser_advance(parser);
2964
+ rbs_parser_advance(parser);
2965
+
2966
+ rbs_range_t old_name_range;
2967
+ rbs_type_name_t *old_name = NULL;
2968
+ CHECK_PARSE(parse_type_name(parser, CLASS_NAME, &old_name_range, &old_name));
2969
+
2970
+ rbs_range_t decl_range = {
2971
+ .start = keyword_range.start,
2972
+ .end = old_name_range.end,
2973
+ };
2974
+
2975
+ rbs_location_range loc = RBS_RANGE_LEX2AST(decl_range);
2976
+
2977
+ *class_decl = (rbs_node_t *) rbs_ast_declarations_class_alias_new(ALLOCATOR(), loc, class_name, old_name, comment, annotations, RBS_RANGE_LEX2AST(keyword_range), RBS_RANGE_LEX2AST(class_name_range), RBS_RANGE_LEX2AST(eq_range), RBS_RANGE_LEX2AST(old_name_range));
2978
+ } else {
2979
+ rbs_ast_declarations_class_t *class_decl0 = NULL;
2980
+ CHECK_PARSE(parse_class_decl0(parser, keyword_range, class_name, class_name_range, comment, annotations, &class_decl0));
2981
+ *class_decl = (rbs_node_t *) class_decl0;
2982
+ }
2983
+
2984
+ return true;
2985
+ }
2986
+
2987
+ /*
2988
+ nested_decl ::= {<const_decl>}
2989
+ | {<class_decl>}
2990
+ | {<interface_decl>}
2991
+ | {<module_decl>}
2992
+ | {<class_decl>}
2993
+ */
2994
+ RBS_NODISCARD
2995
+ static bool parse_nested_decl(rbs_parser_t *parser, const char *nested_in, rbs_position_t annot_pos, rbs_node_list_t *annotations, rbs_node_t **decl) {
2996
+ rbs_parser_push_typevar_table(parser, true);
2997
+
2998
+ switch (parser->current_token.type) {
2999
+ case tUIDENT:
3000
+ case pCOLON2: {
3001
+ rbs_ast_declarations_constant_t *constant = NULL;
3002
+ CHECK_PARSE(parse_const_decl(parser, annotations, &constant));
3003
+ *decl = (rbs_node_t *) constant;
3004
+ break;
3005
+ }
3006
+ case tGIDENT: {
3007
+ rbs_ast_declarations_global_t *global = NULL;
3008
+ CHECK_PARSE(parse_global_decl(parser, annotations, &global));
3009
+ *decl = (rbs_node_t *) global;
3010
+ break;
3011
+ }
3012
+ case kTYPE: {
3013
+ rbs_ast_declarations_type_alias_t *typealias = NULL;
3014
+ CHECK_PARSE(parse_type_decl(parser, annot_pos, annotations, &typealias));
3015
+ *decl = (rbs_node_t *) typealias;
3016
+ break;
3017
+ }
3018
+ case kINTERFACE: {
3019
+ rbs_ast_declarations_interface_t *interface_decl = NULL;
3020
+ CHECK_PARSE(parse_interface_decl(parser, annot_pos, annotations, &interface_decl));
3021
+ *decl = (rbs_node_t *) interface_decl;
3022
+ break;
3023
+ }
3024
+ case kMODULE: {
3025
+ rbs_node_t *module_decl = NULL;
3026
+ CHECK_PARSE(parse_module_decl(parser, annot_pos, annotations, &module_decl));
3027
+ *decl = module_decl;
3028
+ break;
3029
+ }
3030
+ case kCLASS: {
3031
+ rbs_node_t *class_decl = NULL;
3032
+ CHECK_PARSE(parse_class_decl(parser, annot_pos, annotations, &class_decl));
3033
+ *decl = class_decl;
3034
+ break;
3035
+ }
3036
+ default:
3037
+ rbs_parser_set_error(parser, parser->current_token, true, "unexpected token for class/module declaration member");
3038
+ return false;
3039
+ }
3040
+
3041
+ CHECK_PARSE(parser_pop_typevar_table(parser));
3042
+
3043
+ return true;
3044
+ }
3045
+
3046
+ RBS_NODISCARD
3047
+ static bool parse_decl(rbs_parser_t *parser, rbs_node_t **decl) {
3048
+ rbs_node_list_t *annotations = rbs_node_list_new(ALLOCATOR());
3049
+ rbs_position_t annot_pos = NullPosition;
3050
+
3051
+ CHECK_PARSE(parse_annotations(parser, annotations, &annot_pos));
3052
+ rbs_parser_advance(parser);
3053
+
3054
+ switch (parser->current_token.type) {
3055
+ case tUIDENT:
3056
+ case pCOLON2: {
3057
+ rbs_ast_declarations_constant_t *constant = NULL;
3058
+ CHECK_PARSE(parse_const_decl(parser, annotations, &constant));
3059
+ *decl = (rbs_node_t *) constant;
3060
+ return true;
3061
+ }
3062
+ case tGIDENT: {
3063
+ rbs_ast_declarations_global_t *global = NULL;
3064
+ CHECK_PARSE(parse_global_decl(parser, annotations, &global));
3065
+ *decl = (rbs_node_t *) global;
3066
+ return true;
3067
+ }
3068
+ case kTYPE: {
3069
+ rbs_ast_declarations_type_alias_t *typealias = NULL;
3070
+ CHECK_PARSE(parse_type_decl(parser, annot_pos, annotations, &typealias));
3071
+ *decl = (rbs_node_t *) typealias;
3072
+ return true;
3073
+ }
3074
+ case kINTERFACE: {
3075
+ rbs_ast_declarations_interface_t *interface_decl = NULL;
3076
+ CHECK_PARSE(parse_interface_decl(parser, annot_pos, annotations, &interface_decl));
3077
+ *decl = (rbs_node_t *) interface_decl;
3078
+ return true;
3079
+ }
3080
+ case kMODULE: {
3081
+ rbs_node_t *module_decl = NULL;
3082
+ CHECK_PARSE(parse_module_decl(parser, annot_pos, annotations, &module_decl));
3083
+ *decl = module_decl;
3084
+ return true;
3085
+ }
3086
+ case kCLASS: {
3087
+ rbs_node_t *class_decl = NULL;
3088
+ CHECK_PARSE(parse_class_decl(parser, annot_pos, annotations, &class_decl));
3089
+ *decl = class_decl;
3090
+ return true;
3091
+ }
3092
+ default:
3093
+ rbs_parser_set_error(parser, parser->current_token, true, "cannot start a declaration");
3094
+ return false;
3095
+ }
3096
+ }
3097
+
3098
+ /*
3099
+ namespace ::= {} (`::`)? (`tUIDENT` `::`)* `tUIDENT` <`::`>
3100
+ | {} <> (empty -- returns empty namespace)
3101
+ */
3102
+ RBS_NODISCARD
3103
+ static bool parse_namespace(rbs_parser_t *parser, rbs_range_t *rg, rbs_namespace_t **out_ns) {
3104
+ bool is_absolute = false;
3105
+
3106
+ if (parser->next_token.type == pCOLON2) {
3107
+ *rg = (rbs_range_t) {
3108
+ .start = parser->next_token.range.start,
3109
+ .end = parser->next_token.range.end,
3110
+ };
3111
+ is_absolute = true;
3112
+
3113
+ rbs_parser_advance(parser);
3114
+ }
3115
+
3116
+ rbs_node_list_t *path = rbs_node_list_new(ALLOCATOR());
3117
+
3118
+ while (true) {
3119
+ if (parser->next_token.type == tUIDENT && parser->next_token2.type == pCOLON2) {
3120
+ rbs_location_range symbol_loc = RBS_RANGE_LEX2AST(parser->next_token.range);
3121
+ rbs_ast_symbol_t *symbol = rbs_ast_symbol_new(ALLOCATOR(), symbol_loc, &parser->constant_pool, INTERN_TOKEN(parser, parser->next_token));
3122
+ rbs_node_list_append(path, (rbs_node_t *) symbol);
3123
+ if (rbs_null_position_p(rg->start)) {
3124
+ rg->start = parser->next_token.range.start;
3125
+ }
3126
+ rg->end = parser->next_token2.range.end;
3127
+ rbs_parser_advance(parser);
3128
+ rbs_parser_advance(parser);
3129
+ } else {
3130
+ break;
3131
+ }
3132
+ }
3133
+
3134
+ *out_ns = rbs_namespace_new(ALLOCATOR(), RBS_RANGE_LEX2AST(*rg), path, is_absolute);
3135
+ return true;
3136
+ }
3137
+
3138
+ /*
3139
+ use_clauses ::= {} use_clause `,` ... `,` <use_clause>
3140
+
3141
+ use_clause ::= {} namespace <tUIDENT>
3142
+ | {} namespace tUIDENT `as` <tUIDENT>
3143
+ | {} namespace <tSTAR>
3144
+ */
3145
+ RBS_NODISCARD
3146
+ static bool parse_use_clauses(rbs_parser_t *parser, rbs_node_list_t *clauses) {
3147
+ while (true) {
3148
+ rbs_range_t namespace_range = NULL_RANGE;
3149
+ rbs_namespace_t *ns = NULL;
3150
+ CHECK_PARSE(parse_namespace(parser, &namespace_range, &ns));
3151
+
3152
+ switch (parser->next_token.type) {
3153
+ case tLIDENT:
3154
+ case tULIDENT:
3155
+ case tUIDENT: {
3156
+ rbs_parser_advance(parser);
3157
+
3158
+ enum RBSTokenType ident_type = parser->current_token.type;
3159
+
3160
+ rbs_range_t type_name_range = rbs_null_range_p(namespace_range) ? parser->current_token.range : (rbs_range_t) { .start = namespace_range.start, .end = parser->current_token.range.end };
3161
+
3162
+ rbs_location_range symbol_loc = rbs_location_range_current_token(parser);
3163
+ rbs_ast_symbol_t *symbol = rbs_ast_symbol_new(ALLOCATOR(), symbol_loc, &parser->constant_pool, INTERN_TOKEN(parser, parser->current_token));
3164
+ rbs_type_name_t *type_name = rbs_type_name_new(ALLOCATOR(), RBS_RANGE_LEX2AST(type_name_range), ns, symbol);
3165
+
3166
+ rbs_range_t keyword_range = NULL_RANGE;
3167
+ rbs_range_t new_name_range = NULL_RANGE;
3168
+ rbs_ast_symbol_t *new_name = NULL;
3169
+ rbs_range_t clause_range = type_name_range;
3170
+ if (parser->next_token.type == kAS) {
3171
+ rbs_parser_advance(parser);
3172
+ keyword_range = parser->current_token.range;
3173
+
3174
+ if (ident_type == tUIDENT) ADVANCE_ASSERT(parser, tUIDENT);
3175
+ if (ident_type == tLIDENT) ADVANCE_ASSERT(parser, tLIDENT);
3176
+ if (ident_type == tULIDENT) ADVANCE_ASSERT(parser, tULIDENT);
3177
+
3178
+ rbs_location_range symbol_loc = RBS_RANGE_LEX2AST(new_name_range);
3179
+ new_name = rbs_ast_symbol_new(ALLOCATOR(), symbol_loc, &parser->constant_pool, INTERN_TOKEN(parser, parser->current_token));
3180
+ new_name_range = parser->current_token.range;
3181
+ clause_range.end = new_name_range.end;
3182
+ }
3183
+
3184
+ rbs_location_range loc = RBS_RANGE_LEX2AST(clause_range);
3185
+
3186
+ rbs_ast_directives_use_single_clause_t *clause = rbs_ast_directives_use_single_clause_new(ALLOCATOR(), loc, type_name, new_name, RBS_RANGE_LEX2AST(type_name_range));
3187
+ clause->keyword_range = RBS_RANGE_LEX2AST(keyword_range);
3188
+ clause->new_name_range = RBS_RANGE_LEX2AST(new_name_range);
3189
+
3190
+ rbs_node_list_append(clauses, (rbs_node_t *) clause);
3191
+
3192
+ break;
3193
+ }
3194
+ case pSTAR: {
3195
+ rbs_range_t clause_range = namespace_range;
3196
+ rbs_parser_advance(parser);
3197
+
3198
+ rbs_range_t star_range = parser->current_token.range;
3199
+ clause_range.end = star_range.end;
3200
+
3201
+ rbs_location_range loc = RBS_RANGE_LEX2AST(clause_range);
3202
+
3203
+ rbs_ast_directives_use_wildcard_clause_t *clause = rbs_ast_directives_use_wildcard_clause_new(ALLOCATOR(), loc, ns, RBS_RANGE_LEX2AST(namespace_range), RBS_RANGE_LEX2AST(star_range));
3204
+ rbs_node_list_append(clauses, (rbs_node_t *) clause);
3205
+
3206
+ break;
3207
+ }
3208
+ default:
3209
+ rbs_parser_set_error(parser, parser->next_token, true, "use clause is expected");
3210
+ return false;
3211
+ }
3212
+
3213
+ if (parser->next_token.type == pCOMMA) {
3214
+ rbs_parser_advance(parser);
3215
+ } else {
3216
+ break;
3217
+ }
3218
+ }
3219
+
3220
+ return true;
3221
+ }
3222
+
3223
+ /*
3224
+ use_directive ::= {} `use` <clauses>
3225
+ */
3226
+ RBS_NODISCARD
3227
+ static bool parse_use_directive(rbs_parser_t *parser, rbs_ast_directives_use_t **use_directive) {
3228
+ if (parser->next_token.type == kUSE) {
3229
+ rbs_parser_advance(parser);
3230
+
3231
+ rbs_range_t keyword_range = parser->current_token.range;
3232
+
3233
+ rbs_node_list_t *clauses = rbs_node_list_new(ALLOCATOR());
3234
+ CHECK_PARSE(parse_use_clauses(parser, clauses));
3235
+
3236
+ rbs_range_t directive_range = keyword_range;
3237
+ directive_range.end = parser->current_token.range.end;
3238
+
3239
+ rbs_location_range loc = RBS_RANGE_LEX2AST(directive_range);
3240
+
3241
+ *use_directive = rbs_ast_directives_use_new(ALLOCATOR(), loc, clauses, RBS_RANGE_LEX2AST(keyword_range));
3242
+ }
3243
+
3244
+ return true;
3245
+ }
3246
+
3247
+ static rbs_ast_comment_t *parse_comment_lines(rbs_parser_t *parser, rbs_comment_t *com) {
3248
+ size_t hash_bytes = parser->lexer->encoding->char_width((const uint8_t *) "#", (size_t) 1);
3249
+ size_t space_bytes = parser->lexer->encoding->char_width((const uint8_t *) " ", (size_t) 1);
3250
+
3251
+ rbs_buffer_t rbs_buffer;
3252
+ rbs_buffer_init(ALLOCATOR(), &rbs_buffer);
3253
+
3254
+ for (size_t i = 0; i < com->line_tokens_count; i++) {
3255
+ rbs_token_t tok = com->line_tokens[i];
3256
+
3257
+ const char *comment_start = parser->lexer->string.start + tok.range.start.byte_pos + hash_bytes;
3258
+ size_t comment_bytes = RBS_RANGE_BYTES(tok.range) - hash_bytes;
3259
+
3260
+ rbs_string_t str = rbs_string_new(
3261
+ comment_start,
3262
+ parser->lexer->string.end
3263
+ );
3264
+
3265
+ // Assumes the input is ASCII compatible
3266
+ unsigned char c = str.start[0];
3267
+
3268
+ if (c == ' ') {
3269
+ comment_start += space_bytes;
3270
+ comment_bytes -= space_bytes;
3271
+ }
3272
+
3273
+ rbs_buffer_append_string(ALLOCATOR(), &rbs_buffer, comment_start, comment_bytes);
3274
+ rbs_buffer_append_cstr(ALLOCATOR(), &rbs_buffer, "\n");
3275
+ }
3276
+
3277
+ return rbs_ast_comment_new(
3278
+ ALLOCATOR(),
3279
+ (rbs_location_range) {
3280
+ .start_char = com->start.char_pos,
3281
+ .start_byte = com->start.byte_pos,
3282
+ .end_char = com->end.char_pos,
3283
+ .end_byte = com->end.byte_pos,
3284
+ },
3285
+ rbs_buffer_to_string(&rbs_buffer)
3286
+ );
3287
+ }
3288
+
3289
+ static rbs_comment_t *comment_get_comment(rbs_comment_t *com, int line) {
3290
+ if (com == NULL) {
3291
+ return NULL;
3292
+ }
3293
+
3294
+ if (com->end.line < line) {
3295
+ return NULL;
3296
+ }
3297
+
3298
+ if (com->end.line == line) {
3299
+ return com;
3300
+ }
3301
+
3302
+ return comment_get_comment(com->next_comment, line);
3303
+ }
3304
+
3305
+ static void comment_insert_new_line(rbs_allocator_t *allocator, rbs_comment_t *com, rbs_token_t comment_token) {
3306
+ if (com->line_tokens_count == com->line_tokens_capacity) {
3307
+ size_t old_size = com->line_tokens_capacity;
3308
+ size_t new_size = old_size * 2;
3309
+ com->line_tokens_capacity = new_size;
3310
+
3311
+ com->line_tokens = rbs_allocator_realloc(
3312
+ allocator,
3313
+ com->line_tokens,
3314
+ sizeof(rbs_token_t) * old_size,
3315
+ sizeof(rbs_token_t) * new_size,
3316
+ rbs_token_t
3317
+ );
3318
+ }
3319
+
3320
+ com->line_tokens[com->line_tokens_count++] = comment_token;
3321
+ com->end = comment_token.range.end;
3322
+ }
3323
+
3324
+ static rbs_comment_t *alloc_comment(rbs_allocator_t *allocator, rbs_token_t comment_token, rbs_comment_t *last_comment) {
3325
+ rbs_comment_t *new_comment = rbs_allocator_alloc(allocator, rbs_comment_t);
3326
+
3327
+ size_t initial_line_capacity = 10;
3328
+
3329
+ rbs_token_t *tokens = rbs_allocator_calloc(allocator, initial_line_capacity, rbs_token_t);
3330
+ tokens[0] = comment_token;
3331
+
3332
+ *new_comment = (rbs_comment_t) {
3333
+ .start = comment_token.range.start,
3334
+ .end = comment_token.range.end,
3335
+
3336
+ .line_tokens_capacity = initial_line_capacity,
3337
+ .line_tokens_count = 1,
3338
+ .line_tokens = tokens,
3339
+
3340
+ .next_comment = last_comment,
3341
+ };
3342
+
3343
+ return new_comment;
3344
+ }
3345
+
3346
+ /**
3347
+ * Insert new comment line token.
3348
+ * */
3349
+ static void insert_comment_line(rbs_parser_t *parser, rbs_token_t tok) {
3350
+ int prev_line = tok.range.start.line - 1;
3351
+
3352
+ rbs_comment_t *com = comment_get_comment(parser->last_comment, prev_line);
3353
+
3354
+ if (com) {
3355
+ comment_insert_new_line(ALLOCATOR(), com, tok);
3356
+ } else {
3357
+ parser->last_comment = alloc_comment(ALLOCATOR(), tok, parser->last_comment);
3358
+ }
3359
+ }
3360
+
3361
+ bool rbs_parse_signature(rbs_parser_t *parser, rbs_signature_t **signature) {
3362
+ rbs_range_t signature_range;
3363
+ signature_range.start = parser->current_token.range.start;
3364
+
3365
+ rbs_node_list_t *dirs = rbs_node_list_new(ALLOCATOR());
3366
+ rbs_node_list_t *decls = rbs_node_list_new(ALLOCATOR());
3367
+
3368
+ while (parser->next_token.type == kUSE) {
3369
+ rbs_ast_directives_use_t *use_node;
3370
+ CHECK_PARSE(parse_use_directive(parser, &use_node));
3371
+
3372
+ rbs_node_list_append(dirs, (rbs_node_t *) use_node);
3373
+ }
3374
+
3375
+ while (parser->next_token.type != pEOF) {
3376
+ rbs_node_t *decl = NULL;
3377
+ CHECK_PARSE(parse_decl(parser, &decl));
3378
+ rbs_node_list_append(decls, decl);
3379
+ }
3380
+
3381
+ signature_range.end = parser->current_token.range.end;
3382
+ *signature = rbs_signature_new(ALLOCATOR(), RBS_RANGE_LEX2AST(signature_range), dirs, decls);
3383
+ return true;
3384
+ }
3385
+
3386
+ bool rbs_parse_type_params(rbs_parser_t *parser, bool module_type_params, rbs_node_list_t **params) {
3387
+ if (parser->next_token.type != pLBRACKET) {
3388
+ rbs_parser_set_error(parser, parser->next_token, true, "expected a token `pLBRACKET`");
3389
+ return false;
3390
+ }
3391
+
3392
+ rbs_range_t rg = NULL_RANGE;
3393
+ rbs_parser_push_typevar_table(parser, true);
3394
+ bool res = parse_type_params(parser, &rg, module_type_params, params);
3395
+ rbs_parser_push_typevar_table(parser, false);
3396
+
3397
+ rbs_parser_advance(parser);
3398
+ if (parser->current_token.type != pEOF) {
3399
+ rbs_parser_set_error(parser, parser->current_token, true, "expected a token `%s`", rbs_token_type_str(pEOF));
3400
+ return false;
3401
+ }
3402
+
3403
+ return res;
3404
+ }
3405
+
3406
+ id_table *alloc_empty_table(rbs_allocator_t *allocator) {
3407
+ id_table *table = rbs_allocator_alloc(allocator, id_table);
3408
+
3409
+ *table = (id_table) {
3410
+ .size = 10,
3411
+ .count = 0,
3412
+ .ids = rbs_allocator_calloc(allocator, 10, rbs_constant_id_t),
3413
+ .next = NULL,
3414
+ };
3415
+
3416
+ return table;
3417
+ }
3418
+
3419
+ id_table *alloc_reset_table(rbs_allocator_t *allocator) {
3420
+ id_table *table = rbs_allocator_alloc(allocator, id_table);
3421
+
3422
+ *table = (id_table) {
3423
+ .size = 0,
3424
+ .count = 0,
3425
+ .ids = NULL,
3426
+ .next = NULL,
3427
+ };
3428
+
3429
+ return table;
3430
+ }
3431
+
3432
+ void rbs_parser_push_typevar_table(rbs_parser_t *parser, bool reset) {
3433
+ if (reset) {
3434
+ id_table *table = alloc_reset_table(ALLOCATOR());
3435
+ table->next = parser->vars;
3436
+ parser->vars = table;
3437
+ }
3438
+
3439
+ id_table *table = alloc_empty_table(ALLOCATOR());
3440
+ table->next = parser->vars;
3441
+ parser->vars = table;
3442
+ }
3443
+
3444
+ RBS_NODISCARD
3445
+ bool rbs_parser_insert_typevar(rbs_parser_t *parser, rbs_constant_id_t id) {
3446
+ id_table *table = parser->vars;
3447
+
3448
+ if (RESET_TABLE_P(table)) {
3449
+ rbs_parser_set_error(parser, parser->current_token, false, "Cannot insert to reset table");
3450
+ return false;
3451
+ }
3452
+
3453
+ if (table->size == table->count) {
3454
+ // expand
3455
+ rbs_constant_id_t *ptr = table->ids;
3456
+ table->size += 10;
3457
+ table->ids = rbs_allocator_calloc(ALLOCATOR(), table->size, rbs_constant_id_t);
3458
+ memcpy(table->ids, ptr, sizeof(rbs_constant_id_t) * table->count);
3459
+ }
3460
+
3461
+ table->ids[table->count++] = id;
3462
+
3463
+ return true;
3464
+ }
3465
+
3466
+ void rbs_parser_print(rbs_parser_t *parser) {
3467
+ printf(" current_token = %s (%d...%d)\n", rbs_token_type_str(parser->current_token.type), parser->current_token.range.start.char_pos, parser->current_token.range.end.char_pos);
3468
+ printf(" next_token = %s (%d...%d)\n", rbs_token_type_str(parser->next_token.type), parser->next_token.range.start.char_pos, parser->next_token.range.end.char_pos);
3469
+ printf(" next_token2 = %s (%d...%d)\n", rbs_token_type_str(parser->next_token2.type), parser->next_token2.range.start.char_pos, parser->next_token2.range.end.char_pos);
3470
+ printf(" next_token3 = %s (%d...%d)\n", rbs_token_type_str(parser->next_token3.type), parser->next_token3.range.start.char_pos, parser->next_token3.range.end.char_pos);
3471
+ }
3472
+
3473
+ void rbs_parser_advance(rbs_parser_t *parser) {
3474
+ parser->current_token = parser->next_token;
3475
+ parser->next_token = parser->next_token2;
3476
+ parser->next_token2 = parser->next_token3;
3477
+
3478
+ while (true) {
3479
+ if (parser->next_token3.type == pEOF) {
3480
+ break;
3481
+ }
3482
+
3483
+ parser->next_token3 = rbs_lexer_next_token(parser->lexer);
3484
+
3485
+ if (parser->next_token3.type == tCOMMENT) {
3486
+ // skip
3487
+ } else if (parser->next_token3.type == tLINECOMMENT) {
3488
+ insert_comment_line(parser, parser->next_token3);
3489
+ } else if (parser->next_token3.type == tTRIVIA) {
3490
+ //skip
3491
+ } else {
3492
+ break;
3493
+ }
3494
+ }
3495
+ }
3496
+
3497
+ void rbs_print_token(rbs_token_t tok) {
3498
+ printf(
3499
+ "%s char=%d...%d\n",
3500
+ rbs_token_type_str(tok.type),
3501
+ tok.range.start.char_pos,
3502
+ tok.range.end.char_pos
3503
+ );
3504
+ }
3505
+
3506
+ void rbs_print_lexer(rbs_lexer_t *lexer) {
3507
+ printf("Lexer: (range = %d...%d, encoding = %s\n", lexer->start_pos, lexer->end_pos, lexer->encoding->name);
3508
+ printf(" start = { char_pos = %d, byte_pos = %d }\n", lexer->start.char_pos, lexer->start.byte_pos);
3509
+ printf(" current = { char_pos = %d, byte_pos = %d }\n", lexer->current.char_pos, lexer->current.byte_pos);
3510
+ printf(" character = { code_point = %d (%c), bytes = %zu }\n", lexer->current_code_point, lexer->current_code_point < 256 ? lexer->current_code_point : '?', lexer->current_character_bytes);
3511
+ printf(" first_token_of_line = %s\n", lexer->first_token_of_line ? "true" : "false");
3512
+ }
3513
+
3514
+ rbs_ast_comment_t *rbs_parser_get_comment(rbs_parser_t *parser, int subject_line) {
3515
+ int comment_line = subject_line - 1;
3516
+
3517
+ rbs_comment_t *com = comment_get_comment(parser->last_comment, comment_line);
3518
+
3519
+ if (com) {
3520
+ return parse_comment_lines(parser, com);
3521
+ } else {
3522
+ return NULL;
3523
+ }
3524
+ }
3525
+
3526
+ rbs_lexer_t *rbs_lexer_new(rbs_allocator_t *allocator, rbs_string_t string, const rbs_encoding_t *encoding, int start_pos, int end_pos) {
3527
+ rbs_lexer_t *lexer = rbs_allocator_alloc(allocator, rbs_lexer_t);
3528
+
3529
+ rbs_position_t start_position = (rbs_position_t) {
3530
+ .byte_pos = 0,
3531
+ .char_pos = 0,
3532
+ .line = 1,
3533
+ .column = 0,
3534
+ };
3535
+
3536
+ *lexer = (rbs_lexer_t) {
3537
+ .string = string,
3538
+ .start_pos = start_pos,
3539
+ .end_pos = end_pos,
3540
+ .current = start_position,
3541
+ .start = { 0 },
3542
+ .first_token_of_line = true,
3543
+ .current_character_bytes = 0,
3544
+ .current_code_point = '\0',
3545
+ .encoding = encoding,
3546
+ };
3547
+
3548
+ unsigned int codepoint;
3549
+ size_t bytes;
3550
+
3551
+ if (rbs_next_char(lexer, &codepoint, &bytes)) {
3552
+ lexer->current_code_point = codepoint;
3553
+ lexer->current_character_bytes = bytes;
3554
+ } else {
3555
+ lexer->current_code_point = '\0';
3556
+ lexer->current_character_bytes = 1;
3557
+ }
3558
+
3559
+ // `rbs_skip` moves a whole character at a time, and moves nothing at all
3560
+ // once the input is spent, so this walk can only ever stand on the first
3561
+ // byte of a character.
3562
+ while (lexer->current.byte_pos < start_pos && lexer->current_code_point != '\0') {
3563
+ rbs_skip(lexer);
3564
+ }
3565
+
3566
+ // Stopping anywhere else means `start_pos` is a position the lexer cannot
3567
+ // start from: over it, and the walk stepped across a character that
3568
+ // straddles it; short of it, and the input ran out first.
3569
+ if (lexer->current.byte_pos != start_pos) return NULL;
3570
+
3571
+ lexer->start = lexer->current;
3572
+
3573
+ return lexer;
3574
+ }
3575
+
3576
+ rbs_parser_t *rbs_parser_new(rbs_string_t string, const rbs_encoding_t *encoding, int start_pos, int end_pos) {
3577
+ return rbs_parser_new_with_options(string, encoding, start_pos, end_pos, (rbs_parser_options_t) { 0 });
3578
+ }
3579
+
3580
+ rbs_parser_t *rbs_parser_new_with_options(rbs_string_t string, const rbs_encoding_t *encoding, int start_pos, int end_pos, rbs_parser_options_t options) {
3581
+ rbs_allocator_t *allocator = rbs_allocator_init();
3582
+
3583
+ rbs_lexer_t *lexer = rbs_lexer_new(allocator, string, encoding, start_pos, end_pos);
3584
+ if (lexer == NULL) {
3585
+ rbs_allocator_free(allocator);
3586
+ return NULL;
3587
+ }
3588
+
3589
+ rbs_parser_t *parser = rbs_allocator_alloc(allocator, rbs_parser_t);
3590
+
3591
+ *parser = (rbs_parser_t) {
3592
+ .lexer = lexer,
3593
+
3594
+ .current_token = NullToken,
3595
+ .next_token = NullToken,
3596
+ .next_token2 = NullToken,
3597
+ .next_token3 = NullToken,
3598
+
3599
+ .vars = NULL,
3600
+ .last_comment = NULL,
3601
+
3602
+ .constant_pool = { 0 },
3603
+ .allocator = allocator,
3604
+ .error = NULL,
3605
+
3606
+ .options = options,
3607
+ };
3608
+
3609
+ // The parser's constant pool is mainly used for storing the names of type variables, which usually aren't many.
3610
+ // Below are some statistics gathered from the current test suite. We can see that 56% of parsers never add to their
3611
+ // constant pool at all. The initial capacity needs to be a power of 2. Picking 2 means that we won't need to realloc
3612
+ // in 85% of cases.
3613
+ //
3614
+ // TODO: recalculate these statistics based on a real world codebase, rather than the test suite.
3615
+ //
3616
+ // | Size | Count | Cumulative | % Coverage |
3617
+ // |------|-------|------------|------------|
3618
+ // | 0 | 7,862 | 7,862 | 56% |
3619
+ // | 1 | 3,196 | 11,058 | 79% |
3620
+ // | 2 | 778 | 12,719 | 85% |
3621
+ // | 3 | 883 | 11,941 | 91% |
3622
+ // | 4 | 478 | 13,197 | 95% |
3623
+ // | 5 | 316 | 13,513 | 97% |
3624
+ // | 6 | 288 | 13,801 | 99% |
3625
+ // | 7 | 144 | 13,945 | 100% |
3626
+ const size_t initial_pool_capacity = 2;
3627
+ rbs_constant_pool_init(&parser->constant_pool, initial_pool_capacity);
3628
+
3629
+ rbs_parser_advance(parser);
3630
+ rbs_parser_advance(parser);
3631
+ rbs_parser_advance(parser);
3632
+
3633
+ return parser;
3634
+ }
3635
+
3636
+ void rbs_parser_free(rbs_parser_t *parser) {
3637
+ rbs_constant_pool_free(&parser->constant_pool);
3638
+ rbs_allocator_free(ALLOCATOR());
3639
+ }
3640
+
3641
+ void rbs_parser_set_error(rbs_parser_t *parser, rbs_token_t tok, bool syntax_error, const char *fmt, ...) {
3642
+ if (parser->error) {
3643
+ return;
3644
+ }
3645
+
3646
+ va_list args;
3647
+
3648
+ va_start(args, fmt);
3649
+ int length = vsnprintf(NULL, 0, fmt, args);
3650
+ va_end(args);
3651
+
3652
+ char *message = rbs_allocator_alloc_many(ALLOCATOR(), length + 1, char);
3653
+
3654
+ va_start(args, fmt);
3655
+ vsnprintf(message, length + 1, fmt, args);
3656
+ va_end(args);
3657
+
3658
+ parser->error = rbs_allocator_alloc(ALLOCATOR(), rbs_error_t);
3659
+ parser->error->token = tok;
3660
+ parser->error->message = message;
3661
+ parser->error->syntax_error = syntax_error;
3662
+ }
3663
+
3664
+ /*
3665
+ parse_method_overload ::= {} annotations <method_type>
3666
+ */
3667
+ RBS_NODISCARD
3668
+ static bool parse_method_overload(rbs_parser_t *parser, rbs_node_list_t *annotations, rbs_method_type_t **method_type) {
3669
+ rbs_position_t pos = NullPosition;
3670
+
3671
+ if (!parse_annotations(parser, annotations, &pos)) {
3672
+ return false;
3673
+ }
3674
+
3675
+ return rbs_parse_method_type(parser, method_type, false, true);
3676
+ }
3677
+
3678
+ /*
3679
+ inline_method_overloads ::= {} <overload> -- returns true
3680
+ | {} overload `|` ... `|` overload -- returns true
3681
+ | {} overload `|` ... `|` `...` -- returns true (dot3_location is set)
3682
+ | {<>} -- returns false
3683
+ */
3684
+ RBS_NODISCARD
3685
+ static bool parse_inline_method_overloads(rbs_parser_t *parser, rbs_node_list_t *overloads, rbs_location_range_list_t *bar_locations, rbs_location_range *dot3_location) {
3686
+ while (true) {
3687
+ rbs_node_list_t *annotations = rbs_node_list_new(ALLOCATOR());
3688
+ rbs_method_type_t *method_type = NULL;
3689
+
3690
+ if (!parse_method_overload(parser, annotations, &method_type)) {
3691
+ return false;
3692
+ }
3693
+
3694
+ rbs_location_range location = rbs_location_range_current_token(parser);
3695
+ rbs_ast_members_method_definition_overload_t *overload = rbs_ast_members_method_definition_overload_new(
3696
+ ALLOCATOR(),
3697
+ location,
3698
+ annotations,
3699
+ (rbs_node_t *) method_type
3700
+ );
3701
+ rbs_node_list_append(overloads, (rbs_node_t *) overload);
3702
+
3703
+ if (parser->next_token.type == pBAR) {
3704
+ rbs_location_range bar_range = RBS_RANGE_LEX2AST(parser->next_token.range);
3705
+
3706
+ rbs_parser_advance(parser);
3707
+
3708
+ rbs_location_range_list_append(bar_locations, bar_range);
3709
+
3710
+ if (parser->next_token.type == pDOT3) {
3711
+ *dot3_location = RBS_RANGE_LEX2AST(parser->next_token.range);
3712
+ rbs_parser_advance(parser);
3713
+ return true;
3714
+ }
3715
+
3716
+ continue;
3717
+ }
3718
+
3719
+ return true;
3720
+ }
3721
+ }
3722
+
3723
+ RBS_NODISCARD
3724
+ static bool parse_inline_comment(rbs_parser_t *parser, rbs_location_range *comment_range) {
3725
+ if (parser->next_token.type != tINLINECOMMENT) {
3726
+ *comment_range = RBS_LOCATION_NULL_RANGE;
3727
+ return true;
3728
+ }
3729
+
3730
+ *comment_range = RBS_RANGE_LEX2AST(parser->next_token.range);
3731
+ rbs_parser_advance(parser);
3732
+
3733
+ return true;
3734
+ }
3735
+
3736
+ RBS_NODISCARD
3737
+ static bool parse_inline_param_type_annotation(rbs_parser_t *parser, rbs_ast_ruby_annotations_t **annotation, rbs_range_t rbs_range) {
3738
+ rbs_parser_advance(parser);
3739
+
3740
+ rbs_location_range name_loc = rbs_location_range_current_token(parser);
3741
+
3742
+ ADVANCE_ASSERT(parser, pCOLON);
3743
+
3744
+ rbs_location_range colon_loc = rbs_location_range_current_token(parser);
3745
+
3746
+ rbs_node_t *param_type = NULL;
3747
+ if (!rbs_parse_type(parser, &param_type, false, true, true)) {
3748
+ return false;
3749
+ }
3750
+
3751
+ rbs_location_range comment_loc = RBS_LOCATION_NULL_RANGE;
3752
+ if (!parse_inline_comment(parser, &comment_loc)) {
3753
+ return false;
3754
+ }
3755
+
3756
+ rbs_location_range full_loc = {
3757
+ .start_char = rbs_range.start.char_pos,
3758
+ .start_byte = rbs_range.start.byte_pos,
3759
+ .end_char = parser->current_token.range.end.char_pos,
3760
+ .end_byte = parser->current_token.range.end.byte_pos,
3761
+ };
3762
+
3763
+ *annotation = (rbs_ast_ruby_annotations_t *) rbs_ast_ruby_annotations_param_type_annotation_new(
3764
+ ALLOCATOR(),
3765
+ full_loc,
3766
+ RBS_RANGE_LEX2AST(rbs_range),
3767
+ name_loc,
3768
+ colon_loc,
3769
+ param_type,
3770
+ comment_loc
3771
+ );
3772
+
3773
+ return true;
3774
+ }
3775
+
3776
+ RBS_NODISCARD
3777
+ static bool parse_inline_leading_annotation(rbs_parser_t *parser, rbs_ast_ruby_annotations_t **annotation) {
3778
+ switch (parser->next_token.type) {
3779
+ case pCOLON: {
3780
+ rbs_range_t colon_range = parser->next_token.range;
3781
+ rbs_parser_advance(parser);
3782
+
3783
+ rbs_node_list_t *annotations = rbs_node_list_new(ALLOCATOR());
3784
+ rbs_method_type_t *method_type = NULL;
3785
+
3786
+ if (!parse_method_overload(parser, annotations, &method_type)) {
3787
+ return false;
3788
+ }
3789
+
3790
+ rbs_range_t full_range = {
3791
+ .start = colon_range.start,
3792
+ .end = parser->current_token.range.end
3793
+ };
3794
+
3795
+ rbs_location_range full_loc = RBS_RANGE_LEX2AST(full_range);
3796
+
3797
+ *annotation = (rbs_ast_ruby_annotations_t *) rbs_ast_ruby_annotations_colon_method_type_annotation_new(
3798
+ ALLOCATOR(),
3799
+ full_loc,
3800
+ RBS_RANGE_LEX2AST(colon_range),
3801
+ annotations,
3802
+ (rbs_node_t *) method_type
3803
+ );
3804
+ return true;
3805
+ }
3806
+ case kATRBS: {
3807
+ rbs_range_t rbs_range = parser->next_token.range;
3808
+ rbs_parser_advance(parser);
3809
+
3810
+ switch (parser->next_token.type) {
3811
+ case pDOT3: {
3812
+ rbs_location_range dot3_range = RBS_RANGE_LEX2AST(parser->next_token.range);
3813
+ rbs_parser_advance(parser);
3814
+
3815
+ rbs_node_list_t *overloads = rbs_node_list_new(ALLOCATOR());
3816
+ rbs_location_range_list_t *bar_locations = rbs_location_range_list_new(ALLOCATOR());
3817
+
3818
+ rbs_range_t full_range = {
3819
+ .start = rbs_range.start,
3820
+ .end = parser->current_token.range.end
3821
+ };
3822
+
3823
+ rbs_location_range full_loc = RBS_RANGE_LEX2AST(full_range);
3824
+
3825
+ *annotation = (rbs_ast_ruby_annotations_t *) rbs_ast_ruby_annotations_method_types_annotation_new(
3826
+ ALLOCATOR(),
3827
+ full_loc,
3828
+ RBS_RANGE_LEX2AST(rbs_range),
3829
+ overloads,
3830
+ bar_locations,
3831
+ dot3_range
3832
+ );
3833
+ return true;
3834
+ }
3835
+ case pLPAREN:
3836
+ case pLBRACKET:
3837
+ case pLBRACE:
3838
+ case tANNOTATION: {
3839
+ rbs_node_list_t *overloads = rbs_node_list_new(ALLOCATOR());
3840
+ rbs_location_range_list_t *bar_locations = rbs_location_range_list_new(ALLOCATOR());
3841
+ rbs_location_range dot3_location = RBS_LOCATION_NULL_RANGE;
3842
+
3843
+ if (!parse_inline_method_overloads(parser, overloads, bar_locations, &dot3_location)) {
3844
+ return false;
3845
+ }
3846
+
3847
+ rbs_range_t full_range = {
3848
+ .start = rbs_range.start,
3849
+ .end = parser->current_token.range.end
3850
+ };
3851
+
3852
+ rbs_location_range full_loc = RBS_RANGE_LEX2AST(full_range);
3853
+
3854
+ *annotation = (rbs_ast_ruby_annotations_t *) rbs_ast_ruby_annotations_method_types_annotation_new(
3855
+ ALLOCATOR(),
3856
+ full_loc,
3857
+ RBS_RANGE_LEX2AST(rbs_range),
3858
+ overloads,
3859
+ bar_locations,
3860
+ dot3_location
3861
+ );
3862
+ return true;
3863
+ }
3864
+ case kSKIP: {
3865
+ if (parser->next_token2.type == pCOLON) {
3866
+ return parse_inline_param_type_annotation(parser, annotation, rbs_range);
3867
+ } else {
3868
+ rbs_parser_advance(parser);
3869
+
3870
+ rbs_range_t skip_range = parser->current_token.range;
3871
+
3872
+ rbs_location_range comment_loc = RBS_LOCATION_NULL_RANGE;
3873
+ if (!parse_inline_comment(parser, &comment_loc)) {
3874
+ return false;
3875
+ }
3876
+
3877
+ rbs_range_t full_range = {
3878
+ .start = rbs_range.start,
3879
+ .end = parser->current_token.range.end
3880
+ };
3881
+
3882
+ rbs_location_range full_loc = RBS_RANGE_LEX2AST(full_range);
3883
+
3884
+ *annotation = (rbs_ast_ruby_annotations_t *) rbs_ast_ruby_annotations_skip_annotation_new(
3885
+ ALLOCATOR(),
3886
+ full_loc,
3887
+ RBS_RANGE_LEX2AST(rbs_range),
3888
+ RBS_RANGE_LEX2AST(skip_range),
3889
+ comment_loc
3890
+ );
3891
+ return true;
3892
+ }
3893
+ }
3894
+ case kRETURN: {
3895
+ rbs_parser_advance(parser);
3896
+
3897
+ rbs_range_t return_range = parser->current_token.range;
3898
+
3899
+ ADVANCE_ASSERT(parser, pCOLON);
3900
+
3901
+ rbs_range_t colon_range = parser->current_token.range;
3902
+
3903
+ rbs_node_t *return_type = NULL;
3904
+ if (!rbs_parse_type(parser, &return_type, true, true, true)) {
3905
+ return false;
3906
+ }
3907
+
3908
+ rbs_location_range comment_loc = RBS_LOCATION_NULL_RANGE;
3909
+ if (!parse_inline_comment(parser, &comment_loc)) {
3910
+ return false;
3911
+ }
3912
+
3913
+ rbs_range_t full_range = {
3914
+ .start = rbs_range.start,
3915
+ .end = parser->current_token.range.end
3916
+ };
3917
+
3918
+ rbs_location_range full_loc = RBS_RANGE_LEX2AST(full_range);
3919
+
3920
+ *annotation = (rbs_ast_ruby_annotations_t *) rbs_ast_ruby_annotations_return_type_annotation_new(
3921
+ ALLOCATOR(),
3922
+ full_loc,
3923
+ RBS_RANGE_LEX2AST(rbs_range),
3924
+ RBS_RANGE_LEX2AST(return_range),
3925
+ RBS_RANGE_LEX2AST(colon_range),
3926
+ return_type,
3927
+ comment_loc
3928
+ );
3929
+ return true;
3930
+ }
3931
+ case tAIDENT: {
3932
+ // Parse instance variable annotation: @rbs @name: Type
3933
+ rbs_parser_advance(parser);
3934
+
3935
+ rbs_range_t ivar_name_range = parser->current_token.range;
3936
+ rbs_location_range ivar_name_loc = RBS_RANGE_LEX2AST(ivar_name_range);
3937
+
3938
+ // Extract the instance variable name as a symbol
3939
+ rbs_string_t ivar_string = rbs_parser_peek_current_token(parser);
3940
+ rbs_constant_id_t ivar_id = rbs_constant_pool_insert_string(&parser->constant_pool, ivar_string);
3941
+ rbs_ast_symbol_t *ivar_name = rbs_ast_symbol_new(ALLOCATOR(), ivar_name_loc, &parser->constant_pool, ivar_id);
3942
+
3943
+ ADVANCE_ASSERT(parser, pCOLON);
3944
+
3945
+ rbs_range_t colon_range = parser->current_token.range;
3946
+
3947
+ rbs_node_t *type = NULL;
3948
+ if (!rbs_parse_type(parser, &type, false, true, true)) {
3949
+ return false;
3950
+ }
3951
+
3952
+ rbs_location_range comment_loc = RBS_LOCATION_NULL_RANGE;
3953
+ if (!parse_inline_comment(parser, &comment_loc)) {
3954
+ return false;
3955
+ }
3956
+
3957
+ rbs_range_t full_range = {
3958
+ .start = rbs_range.start,
3959
+ .end = parser->current_token.range.end
3960
+ };
3961
+
3962
+ rbs_location_range full_loc = RBS_RANGE_LEX2AST(full_range);
3963
+
3964
+ *annotation = (rbs_ast_ruby_annotations_t *) rbs_ast_ruby_annotations_instance_variable_annotation_new(
3965
+ ALLOCATOR(),
3966
+ full_loc,
3967
+ RBS_RANGE_LEX2AST(rbs_range),
3968
+ ivar_name,
3969
+ RBS_RANGE_LEX2AST(ivar_name_range),
3970
+ RBS_RANGE_LEX2AST(colon_range),
3971
+ type,
3972
+ comment_loc
3973
+ );
3974
+ return true;
3975
+ }
3976
+ case tLIDENT:
3977
+ case tNONASCIIIDENT:
3978
+ PARAM_NAME_CASES {
3979
+ return parse_inline_param_type_annotation(parser, annotation, rbs_range);
3980
+ }
3981
+ case pSTAR: {
3982
+ rbs_parser_advance(parser);
3983
+ rbs_location_range star_loc = rbs_location_range_current_token(parser);
3984
+
3985
+ rbs_location_range name_loc = RBS_LOCATION_NULL_RANGE;
3986
+ if (is_param_name_token(parser->next_token.type)) {
3987
+ rbs_parser_advance(parser);
3988
+ name_loc = rbs_location_range_current_token(parser);
3989
+ }
3990
+
3991
+ ADVANCE_ASSERT(parser, pCOLON);
3992
+ rbs_location_range colon_loc = rbs_location_range_current_token(parser);
3993
+
3994
+ rbs_node_t *param_type = NULL;
3995
+ if (!rbs_parse_type(parser, &param_type, false, true, true)) {
3996
+ return false;
3997
+ }
3998
+
3999
+ rbs_location_range comment_loc = RBS_LOCATION_NULL_RANGE;
4000
+ if (!parse_inline_comment(parser, &comment_loc)) {
4001
+ return false;
4002
+ }
4003
+
4004
+ rbs_range_t full_range = {
4005
+ .start = rbs_range.start,
4006
+ .end = parser->current_token.range.end
4007
+ };
4008
+
4009
+ *annotation = (rbs_ast_ruby_annotations_t *) rbs_ast_ruby_annotations_splat_param_type_annotation_new(
4010
+ ALLOCATOR(),
4011
+ RBS_RANGE_LEX2AST(full_range),
4012
+ RBS_RANGE_LEX2AST(rbs_range),
4013
+ star_loc,
4014
+ name_loc,
4015
+ colon_loc,
4016
+ param_type,
4017
+ comment_loc
4018
+ );
4019
+ return true;
4020
+ }
4021
+ case pSTAR2: {
4022
+ rbs_parser_advance(parser);
4023
+ rbs_location_range star2_loc = rbs_location_range_current_token(parser);
4024
+
4025
+ rbs_location_range name_loc = RBS_LOCATION_NULL_RANGE;
4026
+ if (is_param_name_token(parser->next_token.type)) {
4027
+ rbs_parser_advance(parser);
4028
+ name_loc = rbs_location_range_current_token(parser);
4029
+ }
4030
+
4031
+ ADVANCE_ASSERT(parser, pCOLON);
4032
+ rbs_location_range colon_loc = rbs_location_range_current_token(parser);
4033
+
4034
+ rbs_node_t *param_type = NULL;
4035
+ if (!rbs_parse_type(parser, &param_type, false, true, true)) {
4036
+ return false;
4037
+ }
4038
+
4039
+ rbs_location_range comment_loc = RBS_LOCATION_NULL_RANGE;
4040
+ if (!parse_inline_comment(parser, &comment_loc)) {
4041
+ return false;
4042
+ }
4043
+
4044
+ rbs_range_t full_range = {
4045
+ .start = rbs_range.start,
4046
+ .end = parser->current_token.range.end
4047
+ };
4048
+
4049
+ *annotation = (rbs_ast_ruby_annotations_t *) rbs_ast_ruby_annotations_double_splat_param_type_annotation_new(
4050
+ ALLOCATOR(),
4051
+ RBS_RANGE_LEX2AST(full_range),
4052
+ RBS_RANGE_LEX2AST(rbs_range),
4053
+ star2_loc,
4054
+ name_loc,
4055
+ colon_loc,
4056
+ param_type,
4057
+ comment_loc
4058
+ );
4059
+ return true;
4060
+ }
4061
+ case pAMP: {
4062
+ rbs_parser_advance(parser);
4063
+ rbs_location_range ampersand_loc = rbs_location_range_current_token(parser);
4064
+
4065
+ rbs_location_range name_loc = RBS_LOCATION_NULL_RANGE;
4066
+ if (is_param_name_token(parser->next_token.type)) {
4067
+ rbs_parser_advance(parser);
4068
+ name_loc = rbs_location_range_current_token(parser);
4069
+ }
4070
+
4071
+ ADVANCE_ASSERT(parser, pCOLON);
4072
+ rbs_location_range colon_loc = rbs_location_range_current_token(parser);
4073
+
4074
+ rbs_location_range question_loc = RBS_LOCATION_NULL_RANGE;
4075
+ if (parser->next_token.type == pQUESTION) {
4076
+ rbs_parser_advance(parser);
4077
+ question_loc = rbs_location_range_current_token(parser);
4078
+ }
4079
+
4080
+ rbs_range_t type_range;
4081
+ type_range.start = parser->next_token.range.start;
4082
+
4083
+ parse_function_result *result = rbs_allocator_alloc(ALLOCATOR(), parse_function_result);
4084
+ if (!parse_function(parser, true, false, false, &result, true, true)) {
4085
+ return false;
4086
+ }
4087
+
4088
+ type_range.end = parser->current_token.range.end;
4089
+
4090
+ rbs_location_range comment_loc = RBS_LOCATION_NULL_RANGE;
4091
+ if (!parse_inline_comment(parser, &comment_loc)) {
4092
+ return false;
4093
+ }
4094
+
4095
+ rbs_range_t full_range = {
4096
+ .start = rbs_range.start,
4097
+ .end = parser->current_token.range.end
4098
+ };
4099
+
4100
+ *annotation = (rbs_ast_ruby_annotations_t *) rbs_ast_ruby_annotations_block_param_type_annotation_new(
4101
+ ALLOCATOR(),
4102
+ RBS_RANGE_LEX2AST(full_range),
4103
+ RBS_RANGE_LEX2AST(rbs_range),
4104
+ ampersand_loc,
4105
+ name_loc,
4106
+ colon_loc,
4107
+ question_loc,
4108
+ RBS_RANGE_LEX2AST(type_range),
4109
+ result->function,
4110
+ comment_loc
4111
+ );
4112
+ return true;
4113
+ }
4114
+ case kMODULESELF: {
4115
+ rbs_parser_advance(parser);
4116
+ rbs_range_t keyword_range = parser->current_token.range;
4117
+
4118
+ ADVANCE_ASSERT(parser, pCOLON);
4119
+ rbs_range_t colon_range = parser->current_token.range;
4120
+
4121
+ rbs_parser_advance(parser);
4122
+
4123
+ rbs_range_t name_range;
4124
+ rbs_type_name_t *type_name = NULL;
4125
+ if (!parse_type_name(parser, (TypeNameKind) (CLASS_NAME | INTERFACE_NAME), &name_range, &type_name)) {
4126
+ return false;
4127
+ }
4128
+
4129
+ rbs_node_list_t *args = rbs_node_list_new(ALLOCATOR());
4130
+ rbs_location_range open_bracket_loc = RBS_LOCATION_NULL_RANGE;
4131
+ rbs_location_range close_bracket_loc = RBS_LOCATION_NULL_RANGE;
4132
+ rbs_location_range_list_t *args_comma_locations = rbs_location_range_list_new(ALLOCATOR());
4133
+ if (parser->next_token.type == pLBRACKET) {
4134
+ rbs_parser_advance(parser);
4135
+ open_bracket_loc = RBS_RANGE_LEX2AST(parser->current_token.range);
4136
+ CHECK_PARSE(parse_type_list_with_commas(parser, pRBRACKET, args, args_comma_locations, true, false, false));
4137
+ rbs_parser_advance(parser);
4138
+ close_bracket_loc = RBS_RANGE_LEX2AST(parser->current_token.range);
4139
+ }
4140
+
4141
+ rbs_location_range comment_loc = RBS_LOCATION_NULL_RANGE;
4142
+ if (!parse_inline_comment(parser, &comment_loc)) {
4143
+ return false;
4144
+ }
4145
+
4146
+ rbs_range_t full_range = {
4147
+ .start = rbs_range.start,
4148
+ .end = parser->current_token.range.end
4149
+ };
4150
+
4151
+ rbs_location_range full_loc = RBS_RANGE_LEX2AST(full_range);
4152
+
4153
+ *annotation = (rbs_ast_ruby_annotations_t *) rbs_ast_ruby_annotations_module_self_annotation_new(
4154
+ ALLOCATOR(),
4155
+ full_loc,
4156
+ RBS_RANGE_LEX2AST(rbs_range),
4157
+ RBS_RANGE_LEX2AST(keyword_range),
4158
+ RBS_RANGE_LEX2AST(colon_range),
4159
+ type_name,
4160
+ args,
4161
+ open_bracket_loc,
4162
+ close_bracket_loc,
4163
+ args_comma_locations,
4164
+ comment_loc
4165
+ );
4166
+ return true;
4167
+ }
4168
+ default: {
4169
+ rbs_parser_set_error(parser, parser->next_token, true, "unexpected token for @rbs annotation");
4170
+ return false;
4171
+ }
4172
+ }
4173
+ }
4174
+ default: {
4175
+ rbs_parser_set_error(parser, parser->next_token, true, "unexpected token for inline leading annotation");
4176
+ return false;
4177
+ }
4178
+ }
4179
+ }
4180
+
4181
+ RBS_NODISCARD
4182
+ static bool parse_inline_trailing_annotation(rbs_parser_t *parser, rbs_ast_ruby_annotations_t **annotation) {
4183
+ rbs_range_t prefix_range = parser->next_token.range;
4184
+
4185
+ switch (parser->next_token.type) {
4186
+ case pCOLON: {
4187
+ rbs_parser_advance(parser);
4188
+
4189
+ // Check for class-alias or module-alias keywords
4190
+ if (parser->next_token.type == kCLASSALIAS || parser->next_token.type == kMODULEALIAS) {
4191
+ bool is_class_alias = (parser->next_token.type == kCLASSALIAS);
4192
+ rbs_range_t keyword_range = parser->next_token.range;
4193
+ rbs_parser_advance(parser);
4194
+
4195
+ rbs_type_name_t *type_name = NULL;
4196
+ rbs_location_range type_name_loc = RBS_LOCATION_NULL_RANGE;
4197
+ rbs_range_t full_range;
4198
+
4199
+ // Check if a type name is provided
4200
+ if (parser->next_token.type == tUIDENT || parser->next_token.type == pCOLON2) {
4201
+ rbs_parser_advance(parser);
4202
+
4203
+ rbs_range_t type_name_range;
4204
+ if (!parse_type_name(parser, CLASS_NAME, &type_name_range, &type_name)) {
4205
+ return false;
4206
+ }
4207
+ // parse_type_name leaves current_token at the last identifier, don't advance
4208
+ type_name_loc = RBS_RANGE_LEX2AST(type_name_range);
4209
+ full_range.start = prefix_range.start;
4210
+ full_range.end = type_name_range.end;
4211
+ } else {
4212
+ // No type name provided - will be inferred
4213
+ full_range.start = prefix_range.start;
4214
+ full_range.end = keyword_range.end;
4215
+ }
4216
+
4217
+ rbs_location_range full_loc = RBS_RANGE_LEX2AST(full_range);
4218
+
4219
+ if (is_class_alias) {
4220
+ *annotation = (rbs_ast_ruby_annotations_t *) rbs_ast_ruby_annotations_class_alias_annotation_new(
4221
+ ALLOCATOR(),
4222
+ full_loc,
4223
+ RBS_RANGE_LEX2AST(prefix_range),
4224
+ RBS_RANGE_LEX2AST(keyword_range),
4225
+ type_name,
4226
+ type_name_loc
4227
+ );
4228
+ } else {
4229
+ *annotation = (rbs_ast_ruby_annotations_t *) rbs_ast_ruby_annotations_module_alias_annotation_new(
4230
+ ALLOCATOR(),
4231
+ full_loc,
4232
+ RBS_RANGE_LEX2AST(prefix_range),
4233
+ RBS_RANGE_LEX2AST(keyword_range),
4234
+ type_name,
4235
+ type_name_loc
4236
+ );
4237
+ }
4238
+ return true;
4239
+ }
4240
+
4241
+ // Otherwise, parse as regular type assertion
4242
+ rbs_node_t *type = NULL;
4243
+ if (!rbs_parse_type(parser, &type, true, true, true)) {
4244
+ return false;
4245
+ }
4246
+
4247
+ rbs_range_t full_range = {
4248
+ .start = prefix_range.start,
4249
+ .end = parser->current_token.range.end
4250
+ };
4251
+
4252
+ rbs_location_range full_loc = RBS_RANGE_LEX2AST(full_range);
4253
+
4254
+ *annotation = (rbs_ast_ruby_annotations_t *) rbs_ast_ruby_annotations_node_type_assertion_new(
4255
+ ALLOCATOR(),
4256
+ full_loc,
4257
+ RBS_RANGE_LEX2AST(prefix_range),
4258
+ type
4259
+ );
4260
+ return true;
4261
+ }
4262
+ case pLBRACKET: {
4263
+ rbs_parser_advance(parser);
4264
+
4265
+ rbs_node_list_t *type_args = rbs_node_list_new(ALLOCATOR());
4266
+ rbs_location_range_list_t *comma_locations = rbs_location_range_list_new(ALLOCATOR());
4267
+
4268
+ // Check for empty type args
4269
+ if (parser->next_token.type == pRBRACKET) {
4270
+ rbs_parser_set_error(parser, parser->next_token, true, "type application cannot be empty");
4271
+ return false;
4272
+ }
4273
+
4274
+ // Parse type list with comma tracking
4275
+ CHECK_PARSE(parse_type_list_with_commas(parser, pRBRACKET, type_args, comma_locations, true, true, false));
4276
+
4277
+ rbs_range_t close_bracket_range = parser->next_token.range;
4278
+ rbs_parser_advance(parser); // consume ]
4279
+
4280
+ rbs_range_t full_range = {
4281
+ .start = prefix_range.start,
4282
+ .end = close_bracket_range.end
4283
+ };
4284
+
4285
+ rbs_location_range full_loc = RBS_RANGE_LEX2AST(full_range);
4286
+
4287
+ *annotation = (rbs_ast_ruby_annotations_t *) rbs_ast_ruby_annotations_type_application_annotation_new(
4288
+ ALLOCATOR(),
4289
+ full_loc,
4290
+ RBS_RANGE_LEX2AST(prefix_range),
4291
+ type_args,
4292
+ RBS_RANGE_LEX2AST(close_bracket_range),
4293
+ comma_locations
4294
+ );
4295
+ return true;
4296
+ }
4297
+ default: {
4298
+ rbs_parser_set_error(parser, parser->next_token, true, "unexpected token for inline trailing annotation");
4299
+ return false;
4300
+ }
4301
+ }
4302
+ }
4303
+
4304
+ bool rbs_parse_inline_leading_annotation(rbs_parser_t *parser, rbs_ast_ruby_annotations_t **annotation) {
4305
+ bool success = parse_inline_leading_annotation(parser, annotation);
4306
+
4307
+ ADVANCE_ASSERT(parser, pEOF);
4308
+
4309
+ return success;
4310
+ }
4311
+
4312
+ bool rbs_parse_inline_trailing_annotation(rbs_parser_t *parser, rbs_ast_ruby_annotations_t **annotation) {
4313
+ bool success = parse_inline_trailing_annotation(parser, annotation);
4314
+
4315
+ ADVANCE_ASSERT(parser, pEOF);
4316
+
4317
+ return success;
4318
+ }