rbs 3.6.1 → 3.9.5

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 (268) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/comments.yml +2 -2
  3. data/.github/workflows/dependabot.yml +1 -1
  4. data/.github/workflows/ruby.yml +34 -10
  5. data/.github/workflows/windows.yml +20 -3
  6. data/.gitignore +1 -0
  7. data/.rubocop.yml +26 -1
  8. data/CHANGELOG.md +241 -0
  9. data/Rakefile +54 -4
  10. data/config.yml +317 -0
  11. data/core/array.rbs +1756 -1591
  12. data/core/basic_object.rbs +38 -35
  13. data/core/comparable.rbs +1 -1
  14. data/core/complex.rbs +166 -94
  15. data/core/data.rbs +2 -2
  16. data/core/dir.rbs +2 -18
  17. data/core/encoding.rbs +12 -32
  18. data/core/enumerable.rbs +270 -266
  19. data/core/enumerator.rbs +14 -4
  20. data/core/env.rbs +1 -1
  21. data/core/errno.rbs +33 -16
  22. data/core/errors.rbs +6 -2
  23. data/core/exception.rbs +342 -167
  24. data/core/fiber.rbs +3 -2
  25. data/core/file.rbs +26 -75
  26. data/core/float.rbs +125 -72
  27. data/core/gc.rbs +158 -42
  28. data/core/hash.rbs +122 -143
  29. data/core/integer.rbs +79 -50
  30. data/core/io/buffer.rbs +49 -43
  31. data/core/io.rbs +108 -151
  32. data/core/kernel.rbs +341 -209
  33. data/core/match_data.rbs +76 -2
  34. data/core/math.rbs +0 -36
  35. data/core/method.rbs +2 -2
  36. data/core/module.rbs +32 -27
  37. data/core/nil_class.rbs +2 -2
  38. data/core/numeric.rbs +101 -104
  39. data/core/object.rbs +1 -5
  40. data/core/object_space/weak_key_map.rbs +3 -4
  41. data/core/object_space.rbs +3 -3
  42. data/core/proc.rbs +82 -14
  43. data/core/process.rbs +110 -58
  44. data/core/ractor.rbs +57 -4
  45. data/core/range.rbs +114 -87
  46. data/core/rational.rbs +0 -2
  47. data/core/rbs/unnamed/argf.rbs +237 -36
  48. data/core/rbs/unnamed/env_class.rbs +35 -53
  49. data/core/rbs/unnamed/random.rbs +1 -2
  50. data/core/regexp.rbs +10 -56
  51. data/core/ruby_vm.rbs +88 -9
  52. data/core/rubygems/config_file.rbs +3 -0
  53. data/core/rubygems/errors.rbs +3 -6
  54. data/core/rubygems/platform.rbs +0 -9
  55. data/core/rubygems/rubygems.rbs +3 -6
  56. data/core/rubygems/version.rbs +8 -8
  57. data/core/set.rbs +4 -16
  58. data/core/string.rbs +271 -264
  59. data/core/struct.rbs +6 -18
  60. data/core/symbol.rbs +14 -21
  61. data/core/thread.rbs +32 -35
  62. data/core/time.rbs +131 -50
  63. data/core/trace_point.rbs +124 -113
  64. data/core/true_class.rbs +0 -1
  65. data/core/unbound_method.rbs +1 -1
  66. data/core/warning.rbs +9 -2
  67. data/docs/architecture.md +1 -1
  68. data/docs/syntax.md +17 -10
  69. data/ext/rbs_extension/extconf.rb +11 -0
  70. data/ext/rbs_extension/location.c +61 -29
  71. data/ext/rbs_extension/location.h +4 -3
  72. data/ext/rbs_extension/main.c +23 -1
  73. data/ext/rbs_extension/parser.c +506 -517
  74. data/ext/rbs_extension/parserstate.c +109 -30
  75. data/ext/rbs_extension/parserstate.h +6 -4
  76. data/ext/rbs_extension/rbs_extension.h +1 -10
  77. data/{ext/rbs_extension → include/rbs}/constants.h +21 -19
  78. data/include/rbs/ruby_objs.h +72 -0
  79. data/include/rbs/util/rbs_constant_pool.h +219 -0
  80. data/include/rbs.h +7 -0
  81. data/lib/rbs/annotate/annotations.rb +3 -3
  82. data/lib/rbs/annotate/rdoc_source.rb +2 -2
  83. data/lib/rbs/ast/declarations.rb +9 -4
  84. data/lib/rbs/ast/directives.rb +10 -0
  85. data/lib/rbs/ast/members.rb +2 -0
  86. data/lib/rbs/ast/type_param.rb +2 -12
  87. data/lib/rbs/cli/diff.rb +3 -3
  88. data/lib/rbs/cli/validate.rb +2 -1
  89. data/lib/rbs/cli.rb +16 -16
  90. data/lib/rbs/collection/config/lockfile_generator.rb +58 -8
  91. data/lib/rbs/collection/config.rb +5 -3
  92. data/lib/rbs/collection/sources/rubygems.rb +1 -1
  93. data/lib/rbs/collection.rb +1 -0
  94. data/lib/rbs/definition.rb +51 -34
  95. data/lib/rbs/definition_builder/ancestor_builder.rb +5 -3
  96. data/lib/rbs/definition_builder.rb +83 -24
  97. data/lib/rbs/environment.rb +33 -18
  98. data/lib/rbs/environment_loader.rb +6 -1
  99. data/lib/rbs/errors.rb +24 -0
  100. data/lib/rbs/locator.rb +2 -0
  101. data/lib/rbs/method_type.rb +2 -0
  102. data/lib/rbs/namespace.rb +1 -0
  103. data/lib/rbs/parser_aux.rb +40 -3
  104. data/lib/rbs/prototype/rb.rb +20 -12
  105. data/lib/rbs/prototype/rbi.rb +11 -6
  106. data/lib/rbs/prototype/runtime/value_object_generator.rb +7 -5
  107. data/lib/rbs/prototype/runtime.rb +7 -5
  108. data/lib/rbs/subtractor.rb +3 -3
  109. data/lib/rbs/test/hook.rb +47 -42
  110. data/lib/rbs/test/type_check.rb +7 -5
  111. data/lib/rbs/type_name.rb +14 -9
  112. data/lib/rbs/types.rb +63 -14
  113. data/lib/rbs/unit_test/spy.rb +4 -2
  114. data/lib/rbs/unit_test/type_assertions.rb +19 -13
  115. data/lib/rbs/unit_test/with_aliases.rb +3 -1
  116. data/lib/rbs/validator.rb +7 -1
  117. data/lib/rbs/version.rb +1 -1
  118. data/lib/rbs/writer.rb +10 -5
  119. data/lib/rbs.rb +1 -0
  120. data/lib/rdoc_plugin/parser.rb +2 -2
  121. data/rbs.gemspec +6 -2
  122. data/sig/ancestor_graph.rbs +5 -5
  123. data/sig/annotate/rdoc_source.rbs +2 -0
  124. data/sig/cli.rbs +2 -0
  125. data/sig/collection/config/lockfile_generator.rbs +9 -1
  126. data/sig/declarations.rbs +10 -3
  127. data/sig/definition.rbs +80 -12
  128. data/sig/definition_builder.rbs +18 -4
  129. data/sig/directives.rbs +17 -1
  130. data/sig/environment.rbs +3 -1
  131. data/sig/errors.rbs +19 -0
  132. data/sig/namespace.rbs +2 -3
  133. data/sig/parser.rbs +5 -1
  134. data/sig/prototype/rb.rbs +1 -1
  135. data/sig/resolver/constant_resolver.rbs +2 -2
  136. data/sig/resolver/context.rbs +1 -1
  137. data/sig/subtractor.rbs +1 -1
  138. data/sig/test/type_check.rbs +2 -2
  139. data/sig/type_alias_dependency.rbs +2 -2
  140. data/sig/type_alias_regularity.rbs +6 -6
  141. data/sig/type_param.rbs +4 -4
  142. data/sig/typename.rbs +8 -5
  143. data/sig/types.rbs +1 -1
  144. data/sig/unit_test/spy.rbs +2 -0
  145. data/sig/unit_test/type_assertions.rbs +2 -0
  146. data/sig/use_map.rbs +1 -1
  147. data/sig/validator.rbs +6 -2
  148. data/sig/vendorer.rbs +1 -1
  149. data/sig/writer.rbs +1 -1
  150. data/{ext/rbs_extension → src}/constants.c +35 -36
  151. data/src/ruby_objs.c +799 -0
  152. data/src/util/rbs_constant_pool.c +342 -0
  153. data/stdlib/base64/0/base64.rbs +0 -9
  154. data/stdlib/benchmark/0/benchmark.rbs +11 -2
  155. data/stdlib/bigdecimal/0/big_decimal.rbs +26 -182
  156. data/stdlib/cgi/0/core.rbs +60 -3
  157. data/stdlib/cgi/0/manifest.yaml +1 -0
  158. data/stdlib/coverage/0/coverage.rbs +0 -3
  159. data/stdlib/csv/0/csv.rbs +18 -58
  160. data/stdlib/csv/0/manifest.yaml +1 -0
  161. data/stdlib/date/0/date.rbs +27 -42
  162. data/stdlib/did_you_mean/0/did_you_mean.rbs +1 -6
  163. data/stdlib/digest/0/digest.rbs +25 -2
  164. data/stdlib/erb/0/erb.rbs +0 -1
  165. data/stdlib/etc/0/etc.rbs +51 -34
  166. data/stdlib/fileutils/0/fileutils.rbs +3 -44
  167. data/stdlib/io-console/0/io-console.rbs +69 -15
  168. data/stdlib/ipaddr/0/ipaddr.rbs +16 -4
  169. data/stdlib/json/0/json.rbs +107 -120
  170. data/stdlib/logger/0/log_device.rbs +1 -1
  171. data/stdlib/logger/0/logger.rbs +3 -18
  172. data/stdlib/minitest/0/kernel.rbs +2 -2
  173. data/stdlib/minitest/0/minitest/abstract_reporter.rbs +4 -1
  174. data/stdlib/minitest/0/minitest/assertion.rbs +1 -0
  175. data/stdlib/minitest/0/minitest/assertions.rbs +58 -13
  176. data/stdlib/minitest/0/minitest/backtrace_filter.rbs +7 -0
  177. data/stdlib/minitest/0/minitest/bench_spec.rbs +8 -8
  178. data/stdlib/minitest/0/minitest/benchmark.rbs +17 -16
  179. data/stdlib/minitest/0/minitest/compress.rbs +13 -0
  180. data/stdlib/minitest/0/minitest/error_on_warning.rbs +3 -0
  181. data/stdlib/minitest/0/minitest/mock.rbs +9 -5
  182. data/stdlib/minitest/0/minitest/parallel/executor.rbs +4 -0
  183. data/stdlib/minitest/0/minitest/parallel/test/class_methods.rbs +0 -1
  184. data/stdlib/minitest/0/minitest/pride_io.rbs +8 -0
  185. data/stdlib/minitest/0/minitest/pride_lol.rbs +2 -0
  186. data/stdlib/minitest/0/minitest/progress_reporter.rbs +1 -1
  187. data/stdlib/minitest/0/minitest/reportable.rbs +2 -0
  188. data/stdlib/minitest/0/minitest/runnable.rbs +33 -1
  189. data/stdlib/minitest/0/minitest/spec/dsl/instance_methods.rbs +1 -1
  190. data/stdlib/minitest/0/minitest/spec/dsl.rbs +10 -6
  191. data/stdlib/minitest/0/minitest/spec.rbs +1 -1
  192. data/stdlib/minitest/0/minitest/statistics_reporter.rbs +5 -0
  193. data/stdlib/minitest/0/minitest/summary_reporter.rbs +0 -7
  194. data/stdlib/minitest/0/minitest/test/lifecycle_hooks.rbs +1 -1
  195. data/stdlib/minitest/0/minitest/test.rbs +7 -14
  196. data/stdlib/minitest/0/minitest/unexpected_error.rbs +2 -0
  197. data/stdlib/minitest/0/minitest/unexpected_warning.rbs +6 -0
  198. data/stdlib/minitest/0/minitest/unit.rbs +1 -2
  199. data/stdlib/minitest/0/minitest.rbs +41 -892
  200. data/stdlib/monitor/0/monitor.rbs +13 -4
  201. data/stdlib/net-http/0/net-http.rbs +42 -109
  202. data/stdlib/nkf/0/nkf.rbs +30 -0
  203. data/stdlib/objspace/0/objspace.rbs +1 -2
  204. data/stdlib/observable/0/observable.rbs +1 -1
  205. data/stdlib/open-uri/0/manifest.yaml +1 -0
  206. data/stdlib/open-uri/0/open-uri.rbs +52 -0
  207. data/stdlib/open3/0/open3.rbs +0 -8
  208. data/stdlib/openssl/0/manifest.yaml +1 -0
  209. data/stdlib/openssl/0/openssl.rbs +235 -143
  210. data/stdlib/optparse/0/optparse.rbs +58 -18
  211. data/stdlib/pathname/0/pathname.rbs +2 -8
  212. data/stdlib/pp/0/pp.rbs +3 -1
  213. data/stdlib/prettyprint/0/prettyprint.rbs +0 -4
  214. data/stdlib/pstore/0/pstore.rbs +0 -6
  215. data/stdlib/psych/0/core_ext.rbs +12 -0
  216. data/stdlib/psych/0/psych.rbs +15 -4
  217. data/stdlib/pty/0/pty.rbs +46 -4
  218. data/stdlib/rdoc/0/code_object.rbs +0 -4
  219. data/stdlib/rdoc/0/markup.rbs +10 -12
  220. data/stdlib/rdoc/0/rdoc.rbs +13 -8
  221. data/stdlib/resolv/0/resolv.rbs +21 -12
  222. data/stdlib/ripper/0/ripper.rbs +0 -2
  223. data/stdlib/securerandom/0/securerandom.rbs +7 -2
  224. data/stdlib/shellwords/0/shellwords.rbs +11 -12
  225. data/stdlib/singleton/0/singleton.rbs +0 -1
  226. data/stdlib/socket/0/addrinfo.rbs +1 -2
  227. data/stdlib/socket/0/basic_socket.rbs +0 -5
  228. data/stdlib/socket/0/socket.rbs +32 -27
  229. data/stdlib/socket/0/tcp_server.rbs +0 -3
  230. data/stdlib/socket/0/tcp_socket.rbs +36 -3
  231. data/stdlib/socket/0/udp_socket.rbs +0 -1
  232. data/stdlib/socket/0/unix_server.rbs +0 -3
  233. data/stdlib/socket/0/unix_socket.rbs +4 -2
  234. data/{core/string_io.rbs → stdlib/stringio/0/stringio.rbs} +1 -1
  235. data/stdlib/strscan/0/string_scanner.rbs +1265 -422
  236. data/stdlib/tempfile/0/tempfile.rbs +135 -28
  237. data/stdlib/time/0/time.rbs +48 -35
  238. data/stdlib/timeout/0/timeout.rbs +11 -8
  239. data/stdlib/tmpdir/0/tmpdir.rbs +10 -3
  240. data/stdlib/tsort/0/tsort.rbs +0 -4
  241. data/stdlib/uri/0/common.rbs +28 -30
  242. data/stdlib/uri/0/ftp.rbs +1 -1
  243. data/stdlib/uri/0/generic.rbs +22 -18
  244. data/stdlib/uri/0/http.rbs +2 -2
  245. data/stdlib/uri/0/rfc2396_parser.rbs +3 -0
  246. data/stdlib/zlib/0/buf_error.rbs +1 -70
  247. data/stdlib/zlib/0/data_error.rbs +1 -70
  248. data/stdlib/zlib/0/deflate.rbs +8 -72
  249. data/stdlib/zlib/0/error.rbs +1 -70
  250. data/stdlib/zlib/0/gzip_file/crc_error.rbs +2 -105
  251. data/stdlib/zlib/0/gzip_file/error.rbs +2 -105
  252. data/stdlib/zlib/0/gzip_file/length_error.rbs +2 -105
  253. data/stdlib/zlib/0/gzip_file/no_footer.rbs +2 -105
  254. data/stdlib/zlib/0/gzip_file.rbs +1 -71
  255. data/stdlib/zlib/0/gzip_reader.rbs +3 -74
  256. data/stdlib/zlib/0/gzip_writer.rbs +1 -70
  257. data/stdlib/zlib/0/inflate.rbs +4 -71
  258. data/stdlib/zlib/0/mem_error.rbs +1 -70
  259. data/stdlib/zlib/0/need_dict.rbs +1 -70
  260. data/stdlib/zlib/0/stream_end.rbs +1 -70
  261. data/stdlib/zlib/0/stream_error.rbs +1 -70
  262. data/stdlib/zlib/0/version_error.rbs +1 -70
  263. data/stdlib/zlib/0/zlib.rbs +0 -2
  264. data/stdlib/zlib/0/zstream.rbs +4 -72
  265. metadata +17 -13
  266. data/ext/rbs_extension/ruby_objs.c +0 -602
  267. data/ext/rbs_extension/ruby_objs.h +0 -51
  268. data/stdlib/minitest/0/manifest.yaml +0 -2
@@ -1,4 +1,12 @@
1
1
  #include "rbs_extension.h"
2
+ #include "rbs/util/rbs_constant_pool.h"
3
+
4
+ #define INTERN(str) \
5
+ rbs_constant_pool_insert_constant( \
6
+ RBS_GLOBAL_CONSTANT_POOL, \
7
+ (const uint8_t *) str, \
8
+ strlen(str) \
9
+ )
2
10
 
3
11
  #define INTERN_TOKEN(parserstate, tok) \
4
12
  rb_intern3(\
@@ -53,13 +61,20 @@ typedef struct {
53
61
  } method_params;
54
62
 
55
63
  static VALUE EMPTY_ARRAY;
64
+ static VALUE EMPTY_HASH;
56
65
 
57
- static void inline melt_array(VALUE *array) {
66
+ static inline void melt_array(VALUE *array) {
58
67
  if (*array == EMPTY_ARRAY) {
59
68
  *array = rb_ary_new();
60
69
  }
61
70
  }
62
71
 
72
+ static inline void melt_hash(VALUE *hash) {
73
+ if (*hash == EMPTY_HASH) {
74
+ *hash = rb_hash_new();
75
+ }
76
+ }
77
+
63
78
  static bool rbs_is_untyped_params(method_params *params) {
64
79
  return NIL_P(params->required_positionals);
65
80
  }
@@ -143,10 +158,9 @@ void parser_advance_no_gap(parserstate *state) {
143
158
  | {(tUIDENT `::`)*} <tXIDENT>
144
159
  | {<tXIDENT>}
145
160
  */
146
- VALUE parse_type_name(parserstate *state, TypeNameKind kind, range *rg) {
161
+ static VALUE parse_type_name(parserstate *state, TypeNameKind kind, range *rg) {
147
162
  VALUE absolute = Qfalse;
148
163
  VALUE path = EMPTY_ARRAY;
149
- VALUE namespace;
150
164
 
151
165
  if (rg) {
152
166
  rg->start = state->current_token.range.start;
@@ -169,7 +183,8 @@ VALUE parse_type_name(parserstate *state, TypeNameKind kind, range *rg) {
169
183
  parser_advance(state);
170
184
  parser_advance(state);
171
185
  }
172
- namespace = rbs_namespace(path, absolute);
186
+
187
+ VALUE namespace = rbs_namespace(path, absolute);
173
188
 
174
189
  switch (state->current_token.type) {
175
190
  case tLIDENT:
@@ -267,7 +282,6 @@ static bool is_keyword_token(enum TokenType type) {
267
282
  */
268
283
  static VALUE parse_function_param(parserstate *state) {
269
284
  range type_range;
270
-
271
285
  type_range.start = state->next_token.range.start;
272
286
  VALUE type = parse_type(state);
273
287
  type_range.end = state->current_token.range.end;
@@ -278,16 +292,18 @@ static VALUE parse_function_param(parserstate *state) {
278
292
  VALUE location = rbs_new_location(state->buffer, param_range);
279
293
  rbs_loc *loc = rbs_check_location(location);
280
294
  rbs_loc_alloc_children(loc, 1);
281
- rbs_loc_add_optional_child(loc, rb_intern("name"), NULL_RANGE);
295
+ rbs_loc_add_optional_child(loc, INTERN("name"), NULL_RANGE);
282
296
 
283
297
  return rbs_function_param(type, Qnil, location);
284
298
  } else {
285
299
  range name_range = state->next_token.range;
286
- range param_range;
287
300
 
288
301
  parser_advance(state);
289
- param_range.start = type_range.start;
290
- param_range.end = name_range.end;
302
+
303
+ range param_range = {
304
+ .start = type_range.start,
305
+ .end = name_range.end,
306
+ };
291
307
 
292
308
  if (!is_keyword_token(state->current_token.type)) {
293
309
  raise_syntax_error(
@@ -301,7 +317,7 @@ static VALUE parse_function_param(parserstate *state) {
301
317
  VALUE location = rbs_new_location(state->buffer, param_range);
302
318
  rbs_loc *loc = rbs_check_location(location);
303
319
  rbs_loc_alloc_children(loc, 1);
304
- rbs_loc_add_optional_child(loc, rb_intern("name"), name_range);
320
+ rbs_loc_add_optional_child(loc, INTERN("name"), name_range);
305
321
 
306
322
  return rbs_function_param(type, name, location);
307
323
  }
@@ -320,28 +336,22 @@ static ID intern_token_start_end(parserstate *state, token start_token, token en
320
336
  | {} keyword <`?`> `:`
321
337
  */
322
338
  static VALUE parse_keyword_key(parserstate *state) {
323
- VALUE key;
324
-
325
339
  parser_advance(state);
326
340
 
327
341
  if (state->next_token.type == pQUESTION) {
328
- key = ID2SYM(intern_token_start_end(state, state->current_token, state->next_token));
342
+ VALUE key = ID2SYM(intern_token_start_end(state, state->current_token, state->next_token));
329
343
  parser_advance(state);
344
+ return key;
330
345
  } else {
331
- key = ID2SYM(INTERN_TOKEN(state, state->current_token));
346
+ return ID2SYM(INTERN_TOKEN(state, state->current_token));
332
347
  }
333
-
334
- return key;
335
348
  }
336
349
 
337
350
  /*
338
351
  keyword ::= {} keyword `:` <function_param>
339
352
  */
340
- static void parse_keyword(parserstate *state, VALUE keywords, VALUE memo) {
341
- VALUE key;
342
- VALUE param;
343
-
344
- key = parse_keyword_key(state);
353
+ static void parse_keyword(parserstate *state, VALUE *keywords, VALUE memo) {
354
+ VALUE key = parse_keyword_key(state);
345
355
 
346
356
  if (!NIL_P(rb_hash_aref(memo, key))) {
347
357
  raise_syntax_error(
@@ -354,9 +364,10 @@ static void parse_keyword(parserstate *state, VALUE keywords, VALUE memo) {
354
364
  }
355
365
 
356
366
  parser_advance_assert(state, pCOLON);
357
- param = parse_function_param(state);
367
+ VALUE param = parse_function_param(state);
358
368
 
359
- rb_hash_aset(keywords, key, param);
369
+ melt_hash(keywords);
370
+ rb_hash_aset(*keywords, key, param);
360
371
 
361
372
  return;
362
373
  }
@@ -463,7 +474,7 @@ PARSE_OPTIONAL_PARAMS:
463
474
  parser_advance(state);
464
475
 
465
476
  if (is_keyword(state)) {
466
- parse_keyword(state, params->optional_keywords, memo);
477
+ parse_keyword(state, &params->optional_keywords, memo);
467
478
  parser_advance_if(state, pCOMMA);
468
479
  goto PARSE_KEYWORDS;
469
480
  }
@@ -530,7 +541,7 @@ PARSE_KEYWORDS:
530
541
  case pQUESTION:
531
542
  parser_advance(state);
532
543
  if (is_keyword(state)) {
533
- parse_keyword(state, params->optional_keywords, memo);
544
+ parse_keyword(state, &params->optional_keywords, memo);
534
545
  } else {
535
546
  raise_syntax_error(
536
547
  state,
@@ -553,7 +564,7 @@ PARSE_KEYWORDS:
553
564
  case tBANGIDENT:
554
565
  KEYWORD_CASES
555
566
  if (is_keyword(state)) {
556
- parse_keyword(state, params->required_keywords, memo);
567
+ parse_keyword(state, &params->required_keywords, memo);
557
568
  } else {
558
569
  raise_syntax_error(
559
570
  state,
@@ -591,6 +602,7 @@ EOP:
591
602
  static VALUE parse_optional(parserstate *state) {
592
603
  range rg;
593
604
  rg.start = state->next_token.range.start;
605
+
594
606
  VALUE type = parse_simple(state);
595
607
 
596
608
  if (state->next_token.type == pQUESTION) {
@@ -604,13 +616,15 @@ static VALUE parse_optional(parserstate *state) {
604
616
  }
605
617
 
606
618
  static void initialize_method_params(method_params *params){
607
- params->required_positionals = EMPTY_ARRAY;
608
- params->optional_positionals = EMPTY_ARRAY;
609
- params->rest_positionals = Qnil;
610
- params->trailing_positionals = EMPTY_ARRAY;
611
- params->required_keywords = rb_hash_new();
612
- params->optional_keywords = rb_hash_new();
613
- params->rest_keywords = Qnil;
619
+ *params = (method_params) {
620
+ .required_positionals = EMPTY_ARRAY,
621
+ .optional_positionals = EMPTY_ARRAY,
622
+ .rest_positionals = Qnil,
623
+ .trailing_positionals = EMPTY_ARRAY,
624
+ .required_keywords = EMPTY_HASH,
625
+ .optional_keywords = EMPTY_HASH,
626
+ .rest_keywords = Qnil,
627
+ };
614
628
  }
615
629
 
616
630
  /*
@@ -647,16 +661,16 @@ static void parse_function(parserstate *state, VALUE *function, VALUE *block, VA
647
661
  parser_advance_assert(state, pRPAREN);
648
662
  }
649
663
 
650
- // Untyped method parameter means it cannot have block
651
- if (rbs_is_untyped_params(&params)) {
652
- if (state->next_token.type != pARROW) {
653
- raise_syntax_error(state, state->next_token2, "A method type with untyped method parameter cannot have block");
654
- }
655
- }
656
-
657
664
  // Passing NULL to function_self_type means the function itself doesn't accept self type binding. (== method type)
658
665
  if (function_self_type) {
659
666
  *function_self_type = parse_self_type_binding(state);
667
+ } else {
668
+ // Parsing method type. untyped_params means it cannot have a block
669
+ if (rbs_is_untyped_params(&params)) {
670
+ if (state->next_token.type != pARROW) {
671
+ raise_syntax_error(state, state->next_token2, "A method type with untyped method parameter cannot have block");
672
+ }
673
+ }
660
674
  }
661
675
 
662
676
  VALUE required = Qtrue;
@@ -757,7 +771,7 @@ static void check_key_duplication(parserstate *state, VALUE fields, VALUE key) {
757
771
  record_attribute ::= {} keyword_token `:` <type>
758
772
  | {} literal_type `=>` <type>
759
773
  */
760
- VALUE parse_record_attributes(parserstate *state) {
774
+ static VALUE parse_record_attributes(parserstate *state) {
761
775
  VALUE fields = rb_hash_new();
762
776
 
763
777
  if (state->next_token.type == pRBRACE) {
@@ -790,9 +804,10 @@ VALUE parse_record_attributes(parserstate *state) {
790
804
  case tDQSTRING:
791
805
  case tINTEGER:
792
806
  case kTRUE:
793
- case kFALSE:
807
+ case kFALSE: {
794
808
  key = rb_funcall(parse_simple(state), rb_intern("literal"), 0);
795
809
  break;
810
+ }
796
811
  default:
797
812
  raise_syntax_error(
798
813
  state,
@@ -864,15 +879,12 @@ static VALUE parse_symbol(parserstate *state) {
864
879
  | {} `[` type_list <`]`>
865
880
  */
866
881
  static VALUE parse_instance_type(parserstate *state, bool parse_alias) {
867
- range name_range;
868
- range args_range;
869
- range type_range;
870
-
871
882
  TypeNameKind expected_kind = INTERFACE_NAME | CLASS_NAME;
872
883
  if (parse_alias) {
873
884
  expected_kind |= ALIAS_NAME;
874
885
  }
875
886
 
887
+ range name_range;
876
888
  VALUE typename = parse_type_name(state, expected_kind, &name_range);
877
889
  VALUE types = EMPTY_ARRAY;
878
890
 
@@ -887,6 +899,7 @@ static VALUE parse_instance_type(parserstate *state, bool parse_alias) {
887
899
  rbs_abort();
888
900
  }
889
901
 
902
+ range args_range;
890
903
  if (state->next_token.type == pLBRACKET) {
891
904
  parser_advance(state);
892
905
  args_range.start = state->current_token.range.start;
@@ -897,14 +910,16 @@ static VALUE parse_instance_type(parserstate *state, bool parse_alias) {
897
910
  args_range = NULL_RANGE;
898
911
  }
899
912
 
900
- type_range.start = name_range.start;
901
- type_range.end = nonnull_pos_or(args_range.end, name_range.end);
913
+ range type_range = {
914
+ .start = name_range.start,
915
+ .end = nonnull_pos_or(args_range.end, name_range.end),
916
+ };
902
917
 
903
918
  VALUE location = rbs_new_location(state->buffer, type_range);
904
919
  rbs_loc *loc = rbs_check_location(location);
905
920
  rbs_loc_alloc_children(loc, 2);
906
- rbs_loc_add_required_child(loc, rb_intern("name"), name_range);
907
- rbs_loc_add_optional_child(loc, rb_intern("args"), args_range);
921
+ rbs_loc_add_required_child(loc, INTERN("name"), name_range);
922
+ rbs_loc_add_optional_child(loc, INTERN("args"), args_range);
908
923
 
909
924
  if (kind == CLASS_NAME) {
910
925
  return rbs_class_instance(typename, types, location);
@@ -921,15 +936,14 @@ static VALUE parse_instance_type(parserstate *state, bool parse_alias) {
921
936
  singleton_type ::= {`singleton`} `(` type_name <`)`>
922
937
  */
923
938
  static VALUE parse_singleton_type(parserstate *state) {
924
- range name_range;
925
- range type_range;
926
-
927
939
  parser_assert(state, kSINGLETON);
928
940
 
941
+ range type_range;
929
942
  type_range.start = state->current_token.range.start;
930
943
  parser_advance_assert(state, pLPAREN);
931
944
  parser_advance(state);
932
945
 
946
+ range name_range;
933
947
  VALUE typename = parse_type_name(state, CLASS_NAME, &name_range);
934
948
 
935
949
  parser_advance_assert(state, pRPAREN);
@@ -938,7 +952,7 @@ static VALUE parse_singleton_type(parserstate *state) {
938
952
  VALUE location = rbs_new_location(state->buffer, type_range);
939
953
  rbs_loc *loc = rbs_check_location(location);
940
954
  rbs_loc_alloc_children(loc, 1);
941
- rbs_loc_add_required_child(loc, rb_intern("name"), name_range);
955
+ rbs_loc_add_required_child(loc, INTERN("name"), name_range);
942
956
 
943
957
  return rbs_class_singleton(typename, location);
944
958
  }
@@ -962,28 +976,35 @@ static VALUE parse_simple(parserstate *state) {
962
976
  parser_advance_assert(state, pRPAREN);
963
977
  return type;
964
978
  }
965
- case kBOOL:
966
- return rbs_base_type(RBS_Types_Bases_Bool, rbs_location_current_token(state));
967
- case kBOT:
968
- return rbs_base_type(RBS_Types_Bases_Bottom, rbs_location_current_token(state));
969
- case kCLASS:
970
- return rbs_base_type(RBS_Types_Bases_Class, rbs_location_current_token(state));
971
- case kINSTANCE:
972
- return rbs_base_type(RBS_Types_Bases_Instance, rbs_location_current_token(state));
973
- case kNIL:
974
- return rbs_base_type(RBS_Types_Bases_Nil, rbs_location_current_token(state));
975
- case kSELF:
976
- return rbs_base_type(RBS_Types_Bases_Self, rbs_location_current_token(state));
977
- case kTOP:
978
- return rbs_base_type(RBS_Types_Bases_Top, rbs_location_current_token(state));
979
- case kVOID:
980
- return rbs_base_type(RBS_Types_Bases_Void, rbs_location_current_token(state));
981
- case kUNTYPED:
982
- return rbs_base_type(RBS_Types_Bases_Any, rbs_location_current_token(state));
979
+ case kBOOL: {
980
+ return rbs_bases_bool(rbs_location_current_token(state));
981
+ }
982
+ case kBOT: {
983
+ return rbs_bases_bottom(rbs_location_current_token(state));
984
+ }
985
+ case kCLASS: {
986
+ return rbs_bases_class(rbs_location_current_token(state));
987
+ }
988
+ case kINSTANCE: {
989
+ return rbs_bases_instance(rbs_location_current_token(state));
990
+ }
991
+ case kNIL: {
992
+ return rbs_bases_nil(rbs_location_current_token(state));
993
+ }
994
+ case kSELF: {
995
+ return rbs_bases_self(rbs_location_current_token(state));
996
+ }
997
+ case kTOP: {
998
+ return rbs_bases_top(rbs_location_current_token(state));
999
+ }
1000
+ case kVOID: {
1001
+ return rbs_bases_void(rbs_location_current_token(state));
1002
+ }
1003
+ case kUNTYPED: {
1004
+ return rbs_bases_any(false, rbs_location_current_token(state));
1005
+ }
983
1006
  case k__TODO__: {
984
- VALUE type = rbs_base_type(RBS_Types_Bases_Any, rbs_location_current_token(state));
985
- rb_funcall(type, rb_intern("todo!"), 0);
986
- return type;
1007
+ return rbs_bases_any(true, rbs_location_current_token(state));
987
1008
  }
988
1009
  case tINTEGER: {
989
1010
  VALUE literal = rb_funcall(
@@ -996,10 +1017,12 @@ static VALUE parse_simple(parserstate *state) {
996
1017
  rbs_location_current_token(state)
997
1018
  );
998
1019
  }
999
- case kTRUE:
1020
+ case kTRUE: {
1000
1021
  return rbs_literal(Qtrue, rbs_location_current_token(state));
1001
- case kFALSE:
1022
+ }
1023
+ case kFALSE: {
1002
1024
  return rbs_literal(Qfalse, rbs_location_current_token(state));
1025
+ }
1003
1026
  case tSQSTRING:
1004
1027
  case tDQSTRING: {
1005
1028
  VALUE literal = rbs_unquote_string(state, state->current_token.range, 0);
@@ -1014,18 +1037,25 @@ static VALUE parse_simple(parserstate *state) {
1014
1037
  return parse_symbol(state);
1015
1038
  }
1016
1039
  case tUIDENT: {
1017
- ID name = INTERN_TOKEN(state, state->current_token);
1040
+ const char *name_str = peek_token(state->lexstate, state->current_token);
1041
+ size_t name_len = token_bytes(state->current_token);
1042
+
1043
+ rbs_constant_id_t name = rbs_constant_pool_find(&state->constant_pool, (const uint8_t *) name_str, name_len);
1044
+
1018
1045
  if (parser_typevar_member(state, name)) {
1046
+ ID name = rb_intern3(name_str, name_len, rb_enc_get(state->lexstate->string));
1019
1047
  return rbs_variable(ID2SYM(name), rbs_location_current_token(state));
1020
1048
  }
1021
1049
  // fallthrough for type name
1022
1050
  }
1023
1051
  case tULIDENT: // fallthrough
1024
1052
  case tLIDENT: // fallthrough
1025
- case pCOLON2:
1053
+ case pCOLON2: {
1026
1054
  return parse_instance_type(state, true);
1027
- case kSINGLETON:
1055
+ }
1056
+ case kSINGLETON: {
1028
1057
  return parse_singleton_type(state);
1058
+ }
1029
1059
  case pLBRACKET: {
1030
1060
  range rg;
1031
1061
  rg.start = state->current_token.range.start;
@@ -1066,26 +1096,24 @@ static VALUE parse_simple(parserstate *state) {
1066
1096
  | {} <optional>
1067
1097
  */
1068
1098
  static VALUE parse_intersection(parserstate *state) {
1069
- range rg;
1070
-
1071
- rg.start = state->next_token.range.start;
1099
+ position start = state->next_token.range.start;
1072
1100
  VALUE type = parse_optional(state);
1073
- VALUE intersection_types = rb_ary_new();
1101
+ if (state->next_token.type != pAMP) {
1102
+ return type;
1103
+ }
1074
1104
 
1105
+ VALUE intersection_types = rb_ary_new();
1075
1106
  rb_ary_push(intersection_types, type);
1076
1107
  while (state->next_token.type == pAMP) {
1077
1108
  parser_advance(state);
1078
1109
  rb_ary_push(intersection_types, parse_optional(state));
1079
1110
  }
1080
-
1081
- rg.end = state->current_token.range.end;
1082
-
1083
- if (rb_array_len(intersection_types) > 1) {
1084
- VALUE location = rbs_new_location(state->buffer, rg);
1085
- type = rbs_intersection(intersection_types, location);
1086
- }
1087
-
1088
- return type;
1111
+ range rg = (range) {
1112
+ .start = start,
1113
+ .end = state->current_token.range.end,
1114
+ };
1115
+ VALUE location = rbs_new_location(state->buffer, rg);
1116
+ return rbs_intersection(intersection_types, location);
1089
1117
  }
1090
1118
 
1091
1119
  /*
@@ -1093,26 +1121,24 @@ static VALUE parse_intersection(parserstate *state) {
1093
1121
  | {} <intersection>
1094
1122
  */
1095
1123
  VALUE parse_type(parserstate *state) {
1096
- range rg;
1097
-
1098
- rg.start = state->next_token.range.start;
1124
+ position start = state->next_token.range.start;
1099
1125
  VALUE type = parse_intersection(state);
1100
- VALUE union_types = rb_ary_new();
1126
+ if (state->next_token.type != pBAR) {
1127
+ return type;
1128
+ }
1101
1129
 
1130
+ VALUE union_types = rb_ary_new();
1102
1131
  rb_ary_push(union_types, type);
1103
1132
  while (state->next_token.type == pBAR) {
1104
1133
  parser_advance(state);
1105
1134
  rb_ary_push(union_types, parse_intersection(state));
1106
1135
  }
1107
-
1108
- rg.end = state->current_token.range.end;
1109
-
1110
- if (rb_array_len(union_types) > 1) {
1111
- VALUE location = rbs_new_location(state->buffer, rg);
1112
- type = rbs_union(union_types, location);
1113
- }
1114
-
1115
- return type;
1136
+ range rg = (range) {
1137
+ .start = start,
1138
+ .end = state->current_token.range.end,
1139
+ };
1140
+ VALUE location = rbs_new_location(state->buffer, rg);
1141
+ return rbs_union(union_types, location);
1116
1142
  }
1117
1143
 
1118
1144
  /*
@@ -1123,7 +1149,7 @@ VALUE parse_type(parserstate *state) {
1123
1149
 
1124
1150
  type_param ::= tUIDENT upper_bound? default_type? (module_type_params == false)
1125
1151
  */
1126
- VALUE parse_type_params(parserstate *state, range *rg, bool module_type_params) {
1152
+ static VALUE parse_type_params(parserstate *state, range *rg, bool module_type_params) {
1127
1153
  VALUE params = EMPTY_ARRAY;
1128
1154
 
1129
1155
  bool required_param_allowed = true;
@@ -1134,24 +1160,19 @@ VALUE parse_type_params(parserstate *state, range *rg, bool module_type_params)
1134
1160
  rg->start = state->current_token.range.start;
1135
1161
 
1136
1162
  while (true) {
1137
- VALUE name;
1138
- bool unchecked = false;
1163
+ VALUE unchecked = Qfalse;
1139
1164
  VALUE variance = ID2SYM(rb_intern("invariant"));
1140
1165
  VALUE upper_bound = Qnil;
1141
1166
  VALUE default_type = Qnil;
1142
1167
 
1143
- range param_range = NULL_RANGE;
1144
- range name_range;
1145
- range variance_range = NULL_RANGE;
1146
- range unchecked_range = NULL_RANGE;
1147
- range upper_bound_range = NULL_RANGE;
1148
- range default_type_range = NULL_RANGE;
1149
-
1168
+ range param_range;
1150
1169
  param_range.start = state->next_token.range.start;
1151
1170
 
1171
+ range variance_range = NULL_RANGE;
1172
+ range unchecked_range = NULL_RANGE;
1152
1173
  if (module_type_params) {
1153
1174
  if (state->next_token.type == kUNCHECKED) {
1154
- unchecked = true;
1175
+ unchecked = Qtrue;
1155
1176
  parser_advance(state);
1156
1177
  unchecked_range = state->current_token.range;
1157
1178
  }
@@ -1174,13 +1195,19 @@ VALUE parse_type_params(parserstate *state, range *rg, bool module_type_params)
1174
1195
  }
1175
1196
 
1176
1197
  parser_advance_assert(state, tUIDENT);
1177
- name_range = state->current_token.range;
1198
+ range name_range = state->current_token.range;
1199
+
1200
+ rbs_constant_id_t id = rbs_constant_pool_insert_shared(
1201
+ &state->constant_pool,
1202
+ (const uint8_t *) peek_token(state->lexstate, state->current_token),
1203
+ token_bytes(state->current_token)
1204
+ );
1178
1205
 
1179
- ID id = INTERN_TOKEN(state, state->current_token);
1180
- name = ID2SYM(id);
1206
+ VALUE name = ID2SYM(INTERN_TOKEN(state, state->current_token));
1181
1207
 
1182
1208
  parser_insert_typevar(state, id);
1183
1209
 
1210
+ range upper_bound_range = NULL_RANGE;
1184
1211
  if (state->next_token.type == pLT) {
1185
1212
  parser_advance(state);
1186
1213
  upper_bound_range.start = state->current_token.range.start;
@@ -1188,6 +1215,7 @@ VALUE parse_type_params(parserstate *state, range *rg, bool module_type_params)
1188
1215
  upper_bound_range.end = state->current_token.range.end;
1189
1216
  }
1190
1217
 
1218
+ range default_type_range = NULL_RANGE;
1191
1219
  if (module_type_params) {
1192
1220
  if (state->next_token.type == pEQ) {
1193
1221
  parser_advance(state);
@@ -1212,14 +1240,15 @@ VALUE parse_type_params(parserstate *state, range *rg, bool module_type_params)
1212
1240
 
1213
1241
  VALUE location = rbs_new_location(state->buffer, param_range);
1214
1242
  rbs_loc *loc = rbs_check_location(location);
1215
- rbs_loc_alloc_children(loc, 4);
1216
- rbs_loc_add_required_child(loc, rb_intern("name"), name_range);
1217
- rbs_loc_add_optional_child(loc, rb_intern("variance"), variance_range);
1218
- rbs_loc_add_optional_child(loc, rb_intern("unchecked"), unchecked_range);
1219
- rbs_loc_add_optional_child(loc, rb_intern("upper_bound"), upper_bound_range);
1220
- rbs_loc_add_optional_child(loc, rb_intern("default"), default_type_range);
1221
-
1222
- VALUE param = rbs_ast_type_param(name, variance, unchecked, upper_bound, default_type, location);
1243
+ rbs_loc_alloc_children(loc, 5);
1244
+ rbs_loc_add_required_child(loc, INTERN("name"), name_range);
1245
+ rbs_loc_add_optional_child(loc, INTERN("variance"), variance_range);
1246
+ rbs_loc_add_optional_child(loc, INTERN("unchecked"), unchecked_range);
1247
+ rbs_loc_add_optional_child(loc, INTERN("upper_bound"), upper_bound_range);
1248
+ rbs_loc_add_optional_child(loc, INTERN("default"), default_type_range);
1249
+
1250
+ VALUE param = rbs_ast_type_param(name, variance, upper_bound, default_type, unchecked, location);
1251
+
1223
1252
  melt_array(&params);
1224
1253
  rb_ary_push(params, param);
1225
1254
 
@@ -1252,20 +1281,19 @@ VALUE parse_type_params(parserstate *state, range *rg, bool module_type_params)
1252
1281
  method_type ::= {} type_params <function>
1253
1282
  */
1254
1283
  VALUE parse_method_type(parserstate *state) {
1255
- range rg;
1256
- range params_range = NULL_RANGE;
1257
- range type_range;
1258
-
1259
- VALUE function = Qnil;
1260
- VALUE block = Qnil;
1261
1284
  parser_push_typevar_table(state, false);
1262
1285
 
1286
+ range rg;
1263
1287
  rg.start = state->next_token.range.start;
1264
1288
 
1289
+ range params_range = NULL_RANGE;
1265
1290
  VALUE type_params = parse_type_params(state, &params_range, false);
1266
1291
 
1292
+ range type_range;
1267
1293
  type_range.start = state->next_token.range.start;
1268
1294
 
1295
+ VALUE function = Qnil;
1296
+ VALUE block = Qnil;
1269
1297
  parse_function(state, &function, &block, NULL);
1270
1298
 
1271
1299
  rg.end = state->current_token.range.end;
@@ -1276,8 +1304,8 @@ VALUE parse_method_type(parserstate *state) {
1276
1304
  VALUE location = rbs_new_location(state->buffer, rg);
1277
1305
  rbs_loc *loc = rbs_check_location(location);
1278
1306
  rbs_loc_alloc_children(loc, 2);
1279
- rbs_loc_add_required_child(loc, rb_intern("type"), type_range);
1280
- rbs_loc_add_optional_child(loc, rb_intern("type_params"), params_range);
1307
+ rbs_loc_add_required_child(loc, INTERN("type"), type_range);
1308
+ rbs_loc_add_optional_child(loc, INTERN("type_params"), params_range);
1281
1309
 
1282
1310
  return rbs_method_type(
1283
1311
  type_params,
@@ -1290,93 +1318,78 @@ VALUE parse_method_type(parserstate *state) {
1290
1318
  /*
1291
1319
  global_decl ::= {tGIDENT} `:` <type>
1292
1320
  */
1293
- VALUE parse_global_decl(parserstate *state) {
1321
+ static VALUE parse_global_decl(parserstate *state, VALUE annotations) {
1294
1322
  range decl_range;
1295
- range name_range, colon_range;
1296
-
1297
- VALUE typename;
1298
- VALUE type;
1299
- VALUE location;
1300
- VALUE comment;
1301
-
1302
- rbs_loc *loc;
1303
-
1304
1323
  decl_range.start = state->current_token.range.start;
1305
- comment = get_comment(state, decl_range.start.line);
1306
1324
 
1307
- name_range = state->current_token.range;
1308
- typename = ID2SYM(INTERN_TOKEN(state, state->current_token));
1325
+ VALUE comment = get_comment(state, decl_range.start.line);
1326
+ range name_range = state->current_token.range;
1327
+ VALUE typename = ID2SYM(INTERN_TOKEN(state, state->current_token));
1309
1328
 
1310
1329
  parser_advance_assert(state, pCOLON);
1311
- colon_range = state->current_token.range;
1330
+ range colon_range = state->current_token.range;
1312
1331
 
1313
- type = parse_type(state);
1332
+ VALUE type = parse_type(state);
1314
1333
  decl_range.end = state->current_token.range.end;
1315
1334
 
1316
- location = rbs_new_location(state->buffer, decl_range);
1317
- loc = rbs_check_location(location);
1335
+ VALUE location = rbs_new_location(state->buffer, decl_range);
1336
+ rbs_loc *loc = rbs_check_location(location);
1318
1337
  rbs_loc_alloc_children(loc, 2);
1319
- rbs_loc_add_required_child(loc, rb_intern("name"), name_range);
1320
- rbs_loc_add_required_child(loc, rb_intern("colon"), colon_range);
1338
+ rbs_loc_add_required_child(loc, INTERN("name"), name_range);
1339
+ rbs_loc_add_required_child(loc, INTERN("colon"), colon_range);
1321
1340
 
1322
- return rbs_ast_decl_global(typename, type, location, comment);
1341
+ return rbs_ast_decl_global(typename, type, location, comment, annotations);
1323
1342
  }
1324
1343
 
1325
1344
  /*
1326
1345
  const_decl ::= {const_name} `:` <type>
1327
1346
  */
1328
- VALUE parse_const_decl(parserstate *state) {
1347
+ static VALUE parse_const_decl(parserstate *state, VALUE annotations) {
1329
1348
  range decl_range;
1330
- range name_range, colon_range;
1331
-
1332
- VALUE typename;
1333
- VALUE type;
1334
- VALUE location;
1335
- VALUE comment;
1336
-
1337
- rbs_loc *loc;
1338
1349
 
1339
1350
  decl_range.start = state->current_token.range.start;
1340
- comment = get_comment(state, decl_range.start.line);
1351
+ VALUE comment = get_comment(state, decl_range.start.line);
1341
1352
 
1342
- typename = parse_type_name(state, CLASS_NAME, &name_range);
1353
+ range name_range;
1354
+ VALUE typename = parse_type_name(state, CLASS_NAME, &name_range);
1343
1355
 
1344
1356
  parser_advance_assert(state, pCOLON);
1345
- colon_range = state->current_token.range;
1357
+ range colon_range = state->current_token.range;
1346
1358
 
1347
- type = parse_type(state);
1359
+ VALUE type = parse_type(state);
1348
1360
  decl_range.end = state->current_token.range.end;
1349
1361
 
1350
- location = rbs_new_location(state->buffer, decl_range);
1351
- loc = rbs_check_location(location);
1362
+ VALUE location = rbs_new_location(state->buffer, decl_range);
1363
+ rbs_loc *loc = rbs_check_location(location);
1352
1364
  rbs_loc_alloc_children(loc, 2);
1353
- rbs_loc_add_required_child(loc, rb_intern("name"), name_range);
1354
- rbs_loc_add_required_child(loc, rb_intern("colon"), colon_range);
1365
+ rbs_loc_add_required_child(loc, INTERN("name"), name_range);
1366
+ rbs_loc_add_required_child(loc, INTERN("colon"), colon_range);
1355
1367
 
1356
- return rbs_ast_decl_constant(typename, type, location, comment);
1368
+ return rbs_ast_decl_constant(typename, type, location, comment, annotations);
1357
1369
  }
1358
1370
 
1359
1371
  /*
1360
1372
  type_decl ::= {kTYPE} alias_name `=` <type>
1361
1373
  */
1362
- VALUE parse_type_decl(parserstate *state, position comment_pos, VALUE annotations) {
1363
- range decl_range;
1364
- range keyword_range, name_range, params_range, eq_range;
1365
-
1374
+ static VALUE parse_type_decl(parserstate *state, position comment_pos, VALUE annotations) {
1366
1375
  parser_push_typevar_table(state, true);
1367
1376
 
1377
+ range decl_range;
1368
1378
  decl_range.start = state->current_token.range.start;
1369
1379
  comment_pos = nonnull_pos_or(comment_pos, decl_range.start);
1370
1380
 
1371
- keyword_range = state->current_token.range;
1381
+ range keyword_range = state->current_token.range;
1372
1382
 
1373
1383
  parser_advance(state);
1384
+
1385
+ range name_range;
1374
1386
  VALUE typename = parse_type_name(state, ALIAS_NAME, &name_range);
1375
1387
 
1388
+ range params_range;
1376
1389
  VALUE type_params = parse_type_params(state, &params_range, true);
1377
1390
 
1378
1391
  parser_advance_assert(state, pEQ);
1379
- eq_range = state->current_token.range;
1392
+ range eq_range = state->current_token.range;
1380
1393
 
1381
1394
  VALUE type = parse_type(state);
1382
1395
  decl_range.end = state->current_token.range.end;
@@ -1384,10 +1397,10 @@ VALUE parse_type_decl(parserstate *state, position comment_pos, VALUE annotation
1384
1397
  VALUE location = rbs_new_location(state->buffer, decl_range);
1385
1398
  rbs_loc *loc = rbs_check_location(location);
1386
1399
  rbs_loc_alloc_children(loc, 4);
1387
- rbs_loc_add_required_child(loc, rb_intern("keyword"), keyword_range);
1388
- rbs_loc_add_required_child(loc, rb_intern("name"), name_range);
1389
- rbs_loc_add_optional_child(loc, rb_intern("type_params"), params_range);
1390
- rbs_loc_add_required_child(loc, rb_intern("eq"), eq_range);
1400
+ rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range);
1401
+ rbs_loc_add_required_child(loc, INTERN("name"), name_range);
1402
+ rbs_loc_add_optional_child(loc, INTERN("type_params"), params_range);
1403
+ rbs_loc_add_required_child(loc, INTERN("eq"), eq_range);
1391
1404
 
1392
1405
  parser_pop_typevar_table(state);
1393
1406
 
@@ -1404,7 +1417,7 @@ VALUE parse_type_decl(parserstate *state, position comment_pos, VALUE annotation
1404
1417
  /*
1405
1418
  annotation ::= {<tANNOTATION>}
1406
1419
  */
1407
- VALUE parse_annotation(parserstate *state) {
1420
+ static VALUE parse_annotation(parserstate *state) {
1408
1421
  VALUE content = rb_funcall(state->buffer, rb_intern("content"), 0);
1409
1422
  rb_encoding *enc = rb_enc_get(content);
1410
1423
 
@@ -1460,7 +1473,7 @@ VALUE parse_annotation(parserstate *state) {
1460
1473
  annotations ::= {} annotation ... <annotation>
1461
1474
  | {<>}
1462
1475
  */
1463
- void parse_annotations(parserstate *state, VALUE *annotations, position *annot_pos) {
1476
+ static void parse_annotations(parserstate *state, VALUE *annotations, position *annot_pos) {
1464
1477
  *annot_pos = NullPosition;
1465
1478
 
1466
1479
  while (true) {
@@ -1483,7 +1496,7 @@ void parse_annotations(parserstate *state, VALUE *annotations, position *annot_p
1483
1496
  method_name ::= {} <IDENT | keyword>
1484
1497
  | {} (IDENT | keyword)~<`?`>
1485
1498
  */
1486
- VALUE parse_method_name(parserstate *state, range *range) {
1499
+ static VALUE parse_method_name(parserstate *state, range *range) {
1487
1500
  parser_advance(state);
1488
1501
 
1489
1502
  switch (state->current_token.type)
@@ -1515,8 +1528,9 @@ VALUE parse_method_name(parserstate *state, range *range) {
1515
1528
  *range = state->current_token.range;
1516
1529
  return ID2SYM(INTERN_TOKEN(state, state->current_token));
1517
1530
 
1518
- case tQIDENT:
1531
+ case tQIDENT: {
1519
1532
  return rb_to_symbol(rbs_unquote_string(state, state->current_token.range, 0));
1533
+ }
1520
1534
 
1521
1535
  case pBAR:
1522
1536
  case pHAT:
@@ -1551,7 +1565,7 @@ typedef enum {
1551
1565
 
1552
1566
  @param allow_selfq `true` to accept `self?` kind.
1553
1567
  */
1554
- InstanceSingletonKind parse_instance_singleton_kind(parserstate *state, bool allow_selfq, range *rg) {
1568
+ static InstanceSingletonKind parse_instance_singleton_kind(parserstate *state, bool allow_selfq, range *rg) {
1555
1569
  InstanceSingletonKind kind = INSTANCE_KIND;
1556
1570
 
1557
1571
  if (state->next_token.type == kSELF) {
@@ -1561,8 +1575,6 @@ InstanceSingletonKind parse_instance_singleton_kind(parserstate *state, bool all
1561
1575
  parser_advance(state);
1562
1576
  parser_advance(state);
1563
1577
  kind = SINGLETON_KIND;
1564
- rg->start = self_range.start;
1565
- rg->end = state->current_token.range.end;
1566
1578
  } else if (
1567
1579
  state->next_token2.type == pQUESTION
1568
1580
  && state->next_token.range.end.char_pos == state->next_token2.range.start.char_pos
@@ -1572,9 +1584,12 @@ InstanceSingletonKind parse_instance_singleton_kind(parserstate *state, bool all
1572
1584
  parser_advance(state);
1573
1585
  parser_advance(state);
1574
1586
  kind = INSTANCE_SINGLETON_KIND;
1575
- rg->start = self_range.start;
1576
- rg->end = state->current_token.range.end;
1577
1587
  }
1588
+
1589
+ *rg = (range) {
1590
+ .start = self_range.start,
1591
+ .end = state->current_token.range.end,
1592
+ };
1578
1593
  } else {
1579
1594
  *rg = NULL_RANGE;
1580
1595
  }
@@ -1594,42 +1609,40 @@ InstanceSingletonKind parse_instance_singleton_kind(parserstate *state, bool all
1594
1609
  * @param instance_only `true` to reject singleton method definition.
1595
1610
  * @param accept_overload `true` to accept overloading (...) definition.
1596
1611
  * */
1597
- VALUE parse_member_def(parserstate *state, bool instance_only, bool accept_overload, position comment_pos, VALUE annotations) {
1612
+ static VALUE parse_member_def(parserstate *state, bool instance_only, bool accept_overload, position comment_pos, VALUE annotations) {
1598
1613
  range member_range;
1599
- range visibility_range;
1600
- range keyword_range;
1601
- range name_range;
1602
- range kind_range;
1603
- range overloading_range = NULL_RANGE;
1604
-
1605
- VALUE visibility;
1606
-
1607
1614
  member_range.start = state->current_token.range.start;
1608
1615
  comment_pos = nonnull_pos_or(comment_pos, member_range.start);
1616
+
1609
1617
  VALUE comment = get_comment(state, comment_pos.line);
1610
1618
 
1619
+ range visibility_range;
1620
+ VALUE visibility;
1611
1621
  switch (state->current_token.type)
1612
1622
  {
1613
- case kPRIVATE:
1623
+ case kPRIVATE: {
1614
1624
  visibility_range = state->current_token.range;
1615
1625
  visibility = ID2SYM(rb_intern("private"));
1616
1626
  member_range.start = visibility_range.start;
1617
1627
  parser_advance(state);
1618
1628
  break;
1619
- case kPUBLIC:
1629
+ }
1630
+ case kPUBLIC: {
1620
1631
  visibility_range = state->current_token.range;
1621
1632
  visibility = ID2SYM(rb_intern("public"));
1622
1633
  member_range.start = visibility_range.start;
1623
1634
  parser_advance(state);
1624
1635
  break;
1636
+ }
1625
1637
  default:
1626
1638
  visibility_range = NULL_RANGE;
1627
1639
  visibility = Qnil;
1628
1640
  break;
1629
1641
  }
1630
1642
 
1631
- keyword_range = state->current_token.range;
1643
+ range keyword_range = state->current_token.range;
1632
1644
 
1645
+ range kind_range;
1633
1646
  InstanceSingletonKind kind;
1634
1647
  if (instance_only) {
1635
1648
  kind_range = NULL_RANGE;
@@ -1638,6 +1651,7 @@ VALUE parse_member_def(parserstate *state, bool instance_only, bool accept_overl
1638
1651
  kind = parse_instance_singleton_kind(state, NIL_P(visibility), &kind_range);
1639
1652
  }
1640
1653
 
1654
+ range name_range;
1641
1655
  VALUE name = parse_method_name(state, &name_range);
1642
1656
  VALUE overloads = rb_ary_new();
1643
1657
  VALUE overloading = Qfalse;
@@ -1654,6 +1668,7 @@ VALUE parse_member_def(parserstate *state, bool instance_only, bool accept_overl
1654
1668
 
1655
1669
  parser_push_typevar_table(state, kind != INSTANCE_KIND);
1656
1670
 
1671
+ range overloading_range = NULL_RANGE;
1657
1672
  bool loop = true;
1658
1673
  while (loop) {
1659
1674
  VALUE annotations = EMPTY_ARRAY;
@@ -1727,11 +1742,11 @@ VALUE parse_member_def(parserstate *state, bool instance_only, bool accept_overl
1727
1742
  VALUE location = rbs_new_location(state->buffer, member_range);
1728
1743
  rbs_loc *loc = rbs_check_location(location);
1729
1744
  rbs_loc_alloc_children(loc, 5);
1730
- rbs_loc_add_required_child(loc, rb_intern("keyword"), keyword_range);
1731
- rbs_loc_add_required_child(loc, rb_intern("name"), name_range);
1732
- rbs_loc_add_optional_child(loc, rb_intern("kind"), kind_range);
1733
- rbs_loc_add_optional_child(loc, rb_intern("overloading"), overloading_range);
1734
- rbs_loc_add_optional_child(loc, rb_intern("visibility"), visibility_range);
1745
+ rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range);
1746
+ rbs_loc_add_required_child(loc, INTERN("name"), name_range);
1747
+ rbs_loc_add_optional_child(loc, INTERN("kind"), kind_range);
1748
+ rbs_loc_add_optional_child(loc, INTERN("overloading"), overloading_range);
1749
+ rbs_loc_add_optional_child(loc, INTERN("visibility"), visibility_range);
1735
1750
 
1736
1751
  return rbs_ast_members_method_definition(
1737
1752
  name,
@@ -1774,31 +1789,24 @@ void class_instance_name(parserstate *state, TypeNameKind kind, VALUE *name, VAL
1774
1789
  *
1775
1790
  * @param from_interface `true` when the member is in an interface.
1776
1791
  * */
1777
- VALUE parse_mixin_member(parserstate *state, bool from_interface, position comment_pos, VALUE annotations) {
1792
+ static VALUE parse_mixin_member(parserstate *state, bool from_interface, position comment_pos, VALUE annotations) {
1778
1793
  range member_range;
1779
- range name_range;
1780
- range keyword_range;
1781
- range args_range = NULL_RANGE;
1782
- bool reset_typevar_scope;
1783
-
1784
1794
  member_range.start = state->current_token.range.start;
1785
1795
  comment_pos = nonnull_pos_or(comment_pos, member_range.start);
1786
1796
 
1787
- keyword_range = state->current_token.range;
1797
+ enum TokenType type = state->current_token.type;
1798
+ range keyword_range = state->current_token.range;
1788
1799
 
1789
- VALUE klass = Qnil;
1790
- switch (state->current_token.type)
1800
+ bool reset_typevar_scope;
1801
+ switch (type)
1791
1802
  {
1792
1803
  case kINCLUDE:
1793
- klass = RBS_AST_Members_Include;
1794
1804
  reset_typevar_scope = false;
1795
1805
  break;
1796
1806
  case kEXTEND:
1797
- klass = RBS_AST_Members_Extend;
1798
1807
  reset_typevar_scope = true;
1799
1808
  break;
1800
1809
  case kPREPEND:
1801
- klass = RBS_AST_Members_Prepend;
1802
1810
  reset_typevar_scope = false;
1803
1811
  break;
1804
1812
  default:
@@ -1819,6 +1827,8 @@ VALUE parse_mixin_member(parserstate *state, bool from_interface, position comme
1819
1827
 
1820
1828
  VALUE name;
1821
1829
  VALUE args = EMPTY_ARRAY;
1830
+ range name_range;
1831
+ range args_range = NULL_RANGE;
1822
1832
  class_instance_name(
1823
1833
  state,
1824
1834
  from_interface ? INTERFACE_NAME : (INTERFACE_NAME | CLASS_NAME),
@@ -1832,18 +1842,22 @@ VALUE parse_mixin_member(parserstate *state, bool from_interface, position comme
1832
1842
  VALUE location = rbs_new_location(state->buffer, member_range);
1833
1843
  rbs_loc *loc = rbs_check_location(location);
1834
1844
  rbs_loc_alloc_children(loc, 3);
1835
- rbs_loc_add_required_child(loc, rb_intern("name"), name_range);
1836
- rbs_loc_add_required_child(loc, rb_intern("keyword"), keyword_range);
1837
- rbs_loc_add_optional_child(loc, rb_intern("args"), args_range);
1845
+ rbs_loc_add_required_child(loc, INTERN("name"), name_range);
1846
+ rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range);
1847
+ rbs_loc_add_optional_child(loc, INTERN("args"), args_range);
1838
1848
 
1839
- return rbs_ast_members_mixin(
1840
- klass,
1841
- name,
1842
- args,
1843
- annotations,
1844
- location,
1845
- get_comment(state, comment_pos.line)
1846
- );
1849
+ VALUE comment = get_comment(state, comment_pos.line);
1850
+ switch (type)
1851
+ {
1852
+ case kINCLUDE:
1853
+ return rbs_ast_members_include(name, args, annotations, location, comment);
1854
+ case kEXTEND:
1855
+ return rbs_ast_members_extend(name, args, annotations, location, comment);
1856
+ case kPREPEND:
1857
+ return rbs_ast_members_prepend(name, args, annotations, location, comment);
1858
+ default:
1859
+ rbs_abort();
1860
+ }
1847
1861
  }
1848
1862
 
1849
1863
  /**
@@ -1854,21 +1868,16 @@ VALUE parse_mixin_member(parserstate *state, bool from_interface, position comme
1854
1868
  *
1855
1869
  * @param[in] instance_only `true` to reject `self.` alias.
1856
1870
  * */
1857
- VALUE parse_alias_member(parserstate *state, bool instance_only, position comment_pos, VALUE annotations) {
1871
+ static VALUE parse_alias_member(parserstate *state, bool instance_only, position comment_pos, VALUE annotations) {
1858
1872
  range member_range;
1859
- range keyword_range, new_name_range, old_name_range;
1860
- range new_kind_range, old_kind_range;
1861
-
1862
1873
  member_range.start = state->current_token.range.start;
1863
- keyword_range = state->current_token.range;
1874
+ range keyword_range = state->current_token.range;
1864
1875
 
1865
1876
  comment_pos = nonnull_pos_or(comment_pos, member_range.start);
1866
1877
  VALUE comment = get_comment(state, comment_pos.line);
1867
1878
 
1868
- VALUE new_name;
1869
- VALUE old_name;
1870
- VALUE kind;
1871
-
1879
+ VALUE kind, new_name, old_name;
1880
+ range new_kind_range, old_kind_range, new_name_range, old_name_range;
1872
1881
  if (!instance_only && state->next_token.type == kSELF) {
1873
1882
  kind = ID2SYM(rb_intern("singleton"));
1874
1883
 
@@ -1896,11 +1905,11 @@ VALUE parse_alias_member(parserstate *state, bool instance_only, position commen
1896
1905
  VALUE location = rbs_new_location(state->buffer, member_range);
1897
1906
  rbs_loc *loc = rbs_check_location(location);
1898
1907
  rbs_loc_alloc_children(loc, 5);
1899
- rbs_loc_add_required_child(loc, rb_intern("keyword"), keyword_range);
1900
- rbs_loc_add_required_child(loc, rb_intern("new_name"), new_name_range);
1901
- rbs_loc_add_required_child(loc, rb_intern("old_name"), old_name_range);
1902
- rbs_loc_add_optional_child(loc, rb_intern("new_kind"), new_kind_range);
1903
- rbs_loc_add_optional_child(loc, rb_intern("old_kind"), old_kind_range);
1908
+ rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range);
1909
+ rbs_loc_add_required_child(loc, INTERN("new_name"), new_name_range);
1910
+ rbs_loc_add_required_child(loc, INTERN("old_name"), old_name_range);
1911
+ rbs_loc_add_optional_child(loc, INTERN("new_kind"), new_kind_range);
1912
+ rbs_loc_add_optional_child(loc, INTERN("old_kind"), old_kind_range);
1904
1913
 
1905
1914
  return rbs_ast_members_alias(
1906
1915
  new_name,
@@ -1917,11 +1926,7 @@ VALUE parse_alias_member(parserstate *state, bool instance_only, position commen
1917
1926
  | {kSELF} `.` tAIDENT `:` <type>
1918
1927
  | {tA2IDENT} `:` <type>
1919
1928
  */
1920
- VALUE parse_variable_member(parserstate *state, position comment_pos, VALUE annotations) {
1921
- range member_range;
1922
- range name_range, colon_range;
1923
- range kind_range = NULL_RANGE;
1924
-
1929
+ static VALUE parse_variable_member(parserstate *state, position comment_pos, VALUE annotations) {
1925
1930
  if (rb_array_len(annotations) > 0) {
1926
1931
  raise_syntax_error(
1927
1932
  state,
@@ -1930,88 +1935,92 @@ VALUE parse_variable_member(parserstate *state, position comment_pos, VALUE anno
1930
1935
  );
1931
1936
  }
1932
1937
 
1938
+ range member_range;
1933
1939
  member_range.start = state->current_token.range.start;
1934
1940
  comment_pos = nonnull_pos_or(comment_pos, member_range.start);
1935
1941
  VALUE comment = get_comment(state, comment_pos.line);
1936
1942
 
1937
- VALUE klass;
1938
- VALUE location;
1939
- VALUE name;
1940
- VALUE type;
1941
-
1942
1943
  switch (state->current_token.type)
1943
1944
  {
1944
- case tAIDENT:
1945
- klass = RBS_AST_Members_InstanceVariable;
1946
-
1947
- name_range = state->current_token.range;
1948
- name = ID2SYM(INTERN_TOKEN(state, state->current_token));
1945
+ case tAIDENT: {
1946
+ range name_range = state->current_token.range;
1947
+ VALUE name = ID2SYM(INTERN_TOKEN(state, state->current_token));
1949
1948
 
1950
1949
  parser_advance_assert(state, pCOLON);
1951
- colon_range = state->current_token.range;
1950
+ range colon_range = state->current_token.range;
1952
1951
 
1953
- type = parse_type(state);
1952
+ VALUE type = parse_type(state);
1954
1953
  member_range.end = state->current_token.range.end;
1955
1954
 
1956
- break;
1957
-
1958
- case tA2IDENT:
1959
- klass = RBS_AST_Members_ClassVariable;
1955
+ VALUE location = rbs_new_location(state->buffer, member_range);
1956
+ rbs_loc *loc = rbs_check_location(location);
1957
+ rbs_loc_alloc_children(loc, 3);
1958
+ rbs_loc_add_required_child(loc, INTERN("name"), name_range);
1959
+ rbs_loc_add_required_child(loc, INTERN("colon"), colon_range);
1960
+ rbs_loc_add_optional_child(loc, INTERN("kind"), NULL_RANGE);
1960
1961
 
1961
- name_range = state->current_token.range;
1962
- name = ID2SYM(INTERN_TOKEN(state, state->current_token));
1962
+ return rbs_ast_members_instance_variable(name, type, location, comment);
1963
+ }
1964
+ case tA2IDENT: {
1965
+ range name_range = state->current_token.range;
1966
+ VALUE name = ID2SYM(INTERN_TOKEN(state, state->current_token));
1963
1967
 
1964
1968
  parser_advance_assert(state, pCOLON);
1965
- colon_range = state->current_token.range;
1969
+ range colon_range = state->current_token.range;
1966
1970
 
1967
1971
  parser_push_typevar_table(state, true);
1968
- type = parse_type(state);
1972
+ VALUE type = parse_type(state);
1969
1973
  parser_pop_typevar_table(state);
1970
1974
  member_range.end = state->current_token.range.end;
1971
1975
 
1972
- break;
1973
-
1974
- case kSELF:
1975
- klass = RBS_AST_Members_ClassInstanceVariable;
1976
+ VALUE location = rbs_new_location(state->buffer, member_range);
1977
+ rbs_loc *loc = rbs_check_location(location);
1978
+ rbs_loc_alloc_children(loc, 3);
1979
+ rbs_loc_add_required_child(loc, INTERN("name"), name_range);
1980
+ rbs_loc_add_required_child(loc, INTERN("colon"), colon_range);
1981
+ rbs_loc_add_optional_child(loc, INTERN("kind"), NULL_RANGE);
1976
1982
 
1977
- kind_range.start = state->current_token.range.start;
1978
- kind_range.end = state->next_token.range.end;
1983
+ return rbs_ast_members_class_variable(name, type, location, comment);
1984
+ }
1985
+ case kSELF: {
1986
+ range kind_range = {
1987
+ .start = state->current_token.range.start,
1988
+ .end = state->next_token.range.end
1989
+ };
1979
1990
 
1980
1991
  parser_advance_assert(state, pDOT);
1981
1992
  parser_advance_assert(state, tAIDENT);
1982
1993
 
1983
- name_range = state->current_token.range;
1984
- name = ID2SYM(INTERN_TOKEN(state, state->current_token));
1994
+ range name_range = state->current_token.range;
1995
+ VALUE name = ID2SYM(INTERN_TOKEN(state, state->current_token));
1985
1996
 
1986
1997
  parser_advance_assert(state, pCOLON);
1987
- colon_range = state->current_token.range;
1998
+ range colon_range = state->current_token.range;
1988
1999
 
1989
2000
  parser_push_typevar_table(state, true);
1990
- type = parse_type(state);
2001
+ VALUE type = parse_type(state);
1991
2002
  parser_pop_typevar_table(state);
1992
2003
  member_range.end = state->current_token.range.end;
1993
2004
 
1994
- break;
2005
+ VALUE location = rbs_new_location(state->buffer, member_range);
2006
+ rbs_loc *loc = rbs_check_location(location);
2007
+ rbs_loc_alloc_children(loc, 3);
2008
+ rbs_loc_add_required_child(loc, INTERN("name"), name_range);
2009
+ rbs_loc_add_required_child(loc, INTERN("colon"), colon_range);
2010
+ rbs_loc_add_optional_child(loc, INTERN("kind"), kind_range);
1995
2011
 
2012
+ return rbs_ast_members_class_instance_variable(name, type, location, comment);
2013
+ }
1996
2014
  default:
1997
2015
  rbs_abort();
1998
2016
  }
1999
-
2000
- location = rbs_new_location(state->buffer, member_range);
2001
- rbs_loc *loc = rbs_check_location(location);
2002
- rbs_loc_alloc_children(loc, 3);
2003
- rbs_loc_add_required_child(loc, rb_intern("name"), name_range);
2004
- rbs_loc_add_required_child(loc, rb_intern("colon"), colon_range);
2005
- rbs_loc_add_optional_child(loc, rb_intern("kind"), kind_range);
2006
-
2007
- return rbs_ast_members_variable(klass, name, type, location, comment);
2008
2017
  }
2009
2018
 
2010
2019
  /*
2011
2020
  visibility_member ::= {<`public`>}
2012
2021
  | {<`private`>}
2013
2022
  */
2014
- VALUE parse_visibility_member(parserstate *state, VALUE annotations) {
2023
+ static VALUE parse_visibility_member(parserstate *state, VALUE annotations) {
2015
2024
  if (rb_array_len(annotations) > 0) {
2016
2025
  raise_syntax_error(
2017
2026
  state,
@@ -2020,24 +2029,17 @@ VALUE parse_visibility_member(parserstate *state, VALUE annotations) {
2020
2029
  );
2021
2030
  }
2022
2031
 
2023
- VALUE klass;
2032
+ VALUE location = rbs_new_location(state->buffer, state->current_token.range);
2024
2033
 
2025
2034
  switch (state->current_token.type)
2026
2035
  {
2027
2036
  case kPUBLIC:
2028
- klass = RBS_AST_Members_Public;
2029
- break;
2037
+ return rbs_ast_members_public(location);
2030
2038
  case kPRIVATE:
2031
- klass = RBS_AST_Members_Private;
2032
- break;
2039
+ return rbs_ast_members_private(location);
2033
2040
  default:
2034
2041
  rbs_abort();
2035
2042
  }
2036
-
2037
- return rbs_ast_members_visibility(
2038
- klass,
2039
- rbs_new_location(state->buffer, state->current_token.range)
2040
- );
2041
2043
  }
2042
2044
 
2043
2045
  /*
@@ -2054,26 +2056,14 @@ VALUE parse_visibility_member(parserstate *state, VALUE annotations) {
2054
2056
  | `(` tAIDENT `)` # Ivar name
2055
2057
  | `(` `)` # No variable
2056
2058
  */
2057
- VALUE parse_attribute_member(parserstate *state, position comment_pos, VALUE annotations) {
2059
+ static VALUE parse_attribute_member(parserstate *state, position comment_pos, VALUE annotations) {
2058
2060
  range member_range;
2059
- range keyword_range, name_range, colon_range;
2060
- range kind_range = NULL_RANGE, ivar_range = NULL_RANGE, ivar_name_range = NULL_RANGE, visibility_range = NULL_RANGE;
2061
-
2062
- InstanceSingletonKind is_kind;
2063
- VALUE klass;
2064
- VALUE kind;
2065
- VALUE attr_name;
2066
- VALUE ivar_name;
2067
- VALUE type;
2068
- VALUE comment;
2069
- VALUE location;
2070
- VALUE visibility;
2071
- rbs_loc *loc;
2072
-
2073
2061
  member_range.start = state->current_token.range.start;
2074
2062
  comment_pos = nonnull_pos_or(comment_pos, member_range.start);
2075
- comment = get_comment(state, comment_pos.line);
2063
+ VALUE comment = get_comment(state, comment_pos.line);
2076
2064
 
2065
+ VALUE visibility;
2066
+ range visibility_range;
2077
2067
  switch (state->current_token.type)
2078
2068
  {
2079
2069
  case kPRIVATE:
@@ -2092,31 +2082,18 @@ VALUE parse_attribute_member(parserstate *state, position comment_pos, VALUE ann
2092
2082
  break;
2093
2083
  }
2094
2084
 
2095
- keyword_range = state->current_token.range;
2096
- switch (state->current_token.type)
2097
- {
2098
- case kATTRREADER:
2099
- klass = RBS_AST_Members_AttrReader;
2100
- break;
2101
- case kATTRWRITER:
2102
- klass = RBS_AST_Members_AttrWriter;
2103
- break;
2104
- case kATTRACCESSOR:
2105
- klass = RBS_AST_Members_AttrAccessor;
2106
- break;
2107
- default:
2108
- rbs_abort();
2109
- }
2085
+ enum TokenType attr_type = state->current_token.type;
2086
+ range keyword_range = state->current_token.range;
2110
2087
 
2111
- is_kind = parse_instance_singleton_kind(state, false, &kind_range);
2112
- if (is_kind == INSTANCE_KIND) {
2113
- kind = ID2SYM(rb_intern("instance"));
2114
- } else {
2115
- kind = ID2SYM(rb_intern("singleton"));
2116
- }
2088
+ range kind_range;
2089
+ InstanceSingletonKind is_kind = parse_instance_singleton_kind(state, false, &kind_range);
2090
+ VALUE kind = ID2SYM(rb_intern((is_kind == INSTANCE_KIND) ? "instance" : "singleton"));
2117
2091
 
2118
- attr_name = parse_method_name(state, &name_range);
2092
+ range name_range;
2093
+ VALUE attr_name = parse_method_name(state, &name_range);
2119
2094
 
2095
+ VALUE ivar_name;
2096
+ range ivar_range, ivar_name_range;
2120
2097
  if (state->next_token.type == pLPAREN) {
2121
2098
  parser_advance_assert(state, pLPAREN);
2122
2099
  ivar_range.start = state->current_token.range.start;
@@ -2126,44 +2103,47 @@ VALUE parse_attribute_member(parserstate *state, position comment_pos, VALUE ann
2126
2103
  ivar_name_range = state->current_token.range;
2127
2104
  } else {
2128
2105
  ivar_name = Qfalse;
2106
+ ivar_name_range = NULL_RANGE;
2129
2107
  }
2130
2108
 
2131
2109
  parser_advance_assert(state, pRPAREN);
2132
2110
  ivar_range.end = state->current_token.range.end;
2133
2111
  } else {
2112
+ ivar_range = NULL_RANGE;
2134
2113
  ivar_name = Qnil;
2114
+ ivar_name_range = NULL_RANGE;
2135
2115
  }
2136
2116
 
2137
2117
  parser_advance_assert(state, pCOLON);
2138
- colon_range = state->current_token.range;
2118
+ range colon_range = state->current_token.range;
2139
2119
 
2140
2120
  parser_push_typevar_table(state, is_kind == SINGLETON_KIND);
2141
- type = parse_type(state);
2121
+ VALUE type = parse_type(state);
2142
2122
  parser_pop_typevar_table(state);
2143
2123
  member_range.end = state->current_token.range.end;
2144
2124
 
2145
- location = rbs_new_location(state->buffer, member_range);
2146
- loc = rbs_check_location(location);
2125
+ VALUE location = rbs_new_location(state->buffer, member_range);
2126
+ rbs_loc *loc = rbs_check_location(location);
2147
2127
  rbs_loc_alloc_children(loc, 7);
2148
- rbs_loc_add_required_child(loc, rb_intern("keyword"), keyword_range);
2149
- rbs_loc_add_required_child(loc, rb_intern("name"), name_range);
2150
- rbs_loc_add_required_child(loc, rb_intern("colon"), colon_range);
2151
- rbs_loc_add_optional_child(loc, rb_intern("kind"), kind_range);
2152
- rbs_loc_add_optional_child(loc, rb_intern("ivar"), ivar_range);
2153
- rbs_loc_add_optional_child(loc, rb_intern("ivar_name"), ivar_name_range);
2154
- rbs_loc_add_optional_child(loc, rb_intern("visibility"), visibility_range);
2155
-
2156
- return rbs_ast_members_attribute(
2157
- klass,
2158
- attr_name,
2159
- type,
2160
- ivar_name,
2161
- kind,
2162
- annotations,
2163
- location,
2164
- comment,
2165
- visibility
2166
- );
2128
+ rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range);
2129
+ rbs_loc_add_required_child(loc, INTERN("name"), name_range);
2130
+ rbs_loc_add_required_child(loc, INTERN("colon"), colon_range);
2131
+ rbs_loc_add_optional_child(loc, INTERN("kind"), kind_range);
2132
+ rbs_loc_add_optional_child(loc, INTERN("ivar"), ivar_range);
2133
+ rbs_loc_add_optional_child(loc, INTERN("ivar_name"), ivar_name_range);
2134
+ rbs_loc_add_optional_child(loc, INTERN("visibility"), visibility_range);
2135
+
2136
+ switch (attr_type)
2137
+ {
2138
+ case kATTRREADER:
2139
+ return rbs_ast_members_attr_reader(attr_name, type, ivar_name, kind, annotations, location, comment, visibility);
2140
+ case kATTRWRITER:
2141
+ return rbs_ast_members_attr_writer(attr_name, type, ivar_name, kind, annotations, location, comment, visibility);
2142
+ case kATTRACCESSOR:
2143
+ return rbs_ast_members_attr_accessor(attr_name, type, ivar_name, kind, annotations, location, comment, visibility);
2144
+ default:
2145
+ rbs_abort();
2146
+ }
2167
2147
  }
2168
2148
 
2169
2149
  /*
@@ -2173,7 +2153,7 @@ VALUE parse_attribute_member(parserstate *state, position comment_pos, VALUE ann
2173
2153
  | mixin_member (interface only)
2174
2154
  | alias_member (instance only)
2175
2155
  */
2176
- VALUE parse_interface_members(parserstate *state) {
2156
+ static VALUE parse_interface_members(parserstate *state) {
2177
2157
  VALUE members = EMPTY_ARRAY;
2178
2158
 
2179
2159
  while (state->next_token.type != kEND) {
@@ -2186,19 +2166,22 @@ VALUE parse_interface_members(parserstate *state) {
2186
2166
 
2187
2167
  VALUE member;
2188
2168
  switch (state->current_token.type) {
2189
- case kDEF:
2169
+ case kDEF: {
2190
2170
  member = parse_member_def(state, true, true, annot_pos, annotations);
2191
2171
  break;
2172
+ }
2192
2173
 
2193
2174
  case kINCLUDE:
2194
2175
  case kEXTEND:
2195
- case kPREPEND:
2176
+ case kPREPEND: {
2196
2177
  member = parse_mixin_member(state, true, annot_pos, annotations);
2197
2178
  break;
2179
+ }
2198
2180
 
2199
- case kALIAS:
2181
+ case kALIAS: {
2200
2182
  member = parse_alias_member(state, true, annot_pos, annotations);
2201
2183
  break;
2184
+ }
2202
2185
 
2203
2186
  default:
2204
2187
  raise_syntax_error(
@@ -2218,25 +2201,25 @@ VALUE parse_interface_members(parserstate *state) {
2218
2201
  /*
2219
2202
  interface_decl ::= {`interface`} interface_name module_type_params interface_members <kEND>
2220
2203
  */
2221
- VALUE parse_interface_decl(parserstate *state, position comment_pos, VALUE annotations) {
2222
- range member_range;
2223
- range name_range, keyword_range, end_range;
2224
- range type_params_range = NULL_RANGE;
2204
+ static VALUE parse_interface_decl(parserstate *state, position comment_pos, VALUE annotations) {
2205
+ parser_push_typevar_table(state, true);
2225
2206
 
2207
+ range member_range;
2226
2208
  member_range.start = state->current_token.range.start;
2227
2209
  comment_pos = nonnull_pos_or(comment_pos, member_range.start);
2228
2210
 
2229
- parser_push_typevar_table(state, true);
2230
- keyword_range = state->current_token.range;
2211
+ range keyword_range = state->current_token.range;
2231
2212
 
2232
2213
  parser_advance(state);
2233
2214
 
2215
+ range name_range;
2234
2216
  VALUE name = parse_type_name(state, INTERFACE_NAME, &name_range);
2217
+ range type_params_range;
2235
2218
  VALUE params = parse_type_params(state, &type_params_range, true);
2236
2219
  VALUE members = parse_interface_members(state);
2237
2220
 
2238
2221
  parser_advance_assert(state, kEND);
2239
- end_range = state->current_token.range;
2222
+ range end_range = state->current_token.range;
2240
2223
  member_range.end = end_range.end;
2241
2224
 
2242
2225
  parser_pop_typevar_table(state);
@@ -2244,10 +2227,10 @@ VALUE parse_interface_decl(parserstate *state, position comment_pos, VALUE annot
2244
2227
  VALUE location = rbs_new_location(state->buffer, member_range);
2245
2228
  rbs_loc *loc = rbs_check_location(location);
2246
2229
  rbs_loc_alloc_children(loc, 4);
2247
- rbs_loc_add_required_child(loc, rb_intern("keyword"), keyword_range);
2248
- rbs_loc_add_required_child(loc, rb_intern("name"), name_range);
2249
- rbs_loc_add_required_child(loc, rb_intern("end"), end_range);
2250
- rbs_loc_add_optional_child(loc, rb_intern("type_params"), type_params_range);
2230
+ rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range);
2231
+ rbs_loc_add_required_child(loc, INTERN("name"), name_range);
2232
+ rbs_loc_add_required_child(loc, INTERN("end"), end_range);
2233
+ rbs_loc_add_optional_child(loc, INTERN("type_params"), type_params_range);
2251
2234
 
2252
2235
  return rbs_ast_decl_interface(
2253
2236
  name,
@@ -2265,20 +2248,18 @@ VALUE parse_interface_decl(parserstate *state, position comment_pos, VALUE annot
2265
2248
  module_self_type ::= <module_name>
2266
2249
  | module_name `[` type_list <`]`>
2267
2250
  */
2268
- void parse_module_self_types(parserstate *state, VALUE *array) {
2251
+ static void parse_module_self_types(parserstate *state, VALUE *array) {
2269
2252
  while (true) {
2270
- range self_range;
2271
- range name_range;
2272
- range args_range = NULL_RANGE;
2273
-
2274
2253
  parser_advance(state);
2275
2254
 
2255
+ range self_range;
2276
2256
  self_range.start = state->current_token.range.start;
2277
-
2257
+ range name_range;
2278
2258
  VALUE module_name = parse_type_name(state, CLASS_NAME | INTERFACE_NAME, &name_range);
2279
2259
  self_range.end = name_range.end;
2280
2260
 
2281
2261
  VALUE args = EMPTY_ARRAY;
2262
+ range args_range = NULL_RANGE;
2282
2263
  if (state->next_token.type == pLBRACKET) {
2283
2264
  parser_advance(state);
2284
2265
  args_range.start = state->current_token.range.start;
@@ -2290,8 +2271,8 @@ void parse_module_self_types(parserstate *state, VALUE *array) {
2290
2271
  VALUE location = rbs_new_location(state->buffer, self_range);
2291
2272
  rbs_loc *loc = rbs_check_location(location);
2292
2273
  rbs_loc_alloc_children(loc, 2);
2293
- rbs_loc_add_required_child(loc, rb_intern("name"), name_range);
2294
- rbs_loc_add_optional_child(loc, rb_intern("args"), args_range);
2274
+ rbs_loc_add_required_child(loc, INTERN("name"), name_range);
2275
+ rbs_loc_add_optional_child(loc, INTERN("args"), args_range);
2295
2276
 
2296
2277
  VALUE self_type = rbs_ast_decl_module_self(module_name, args, location);
2297
2278
  melt_array(array);
@@ -2305,7 +2286,7 @@ void parse_module_self_types(parserstate *state, VALUE *array) {
2305
2286
  }
2306
2287
  }
2307
2288
 
2308
- VALUE parse_nested_decl(parserstate *state, const char *nested_in, position annot_pos, VALUE annotations);
2289
+ static VALUE parse_nested_decl(parserstate *state, const char *nested_in, position annot_pos, VALUE annotations);
2309
2290
 
2310
2291
  /*
2311
2292
  module_members ::= {} ...<module_member> kEND
@@ -2318,59 +2299,65 @@ VALUE parse_nested_decl(parserstate *state, const char *nested_in, position anno
2318
2299
  | `public`
2319
2300
  | `private`
2320
2301
  */
2321
- VALUE parse_module_members(parserstate *state) {
2302
+ static VALUE parse_module_members(parserstate *state) {
2322
2303
  VALUE members = EMPTY_ARRAY;
2323
2304
 
2324
2305
  while (state->next_token.type != kEND) {
2325
- VALUE member;
2326
2306
  VALUE annotations = EMPTY_ARRAY;
2327
2307
  position annot_pos = NullPosition;
2328
-
2329
2308
  parse_annotations(state, &annotations, &annot_pos);
2330
2309
 
2331
2310
  parser_advance(state);
2332
2311
 
2312
+ VALUE member;
2333
2313
  switch (state->current_token.type)
2334
2314
  {
2335
- case kDEF:
2315
+ case kDEF: {
2336
2316
  member = parse_member_def(state, false, true, annot_pos, annotations);
2337
2317
  break;
2318
+ }
2338
2319
 
2339
2320
  case kINCLUDE:
2340
2321
  case kEXTEND:
2341
- case kPREPEND:
2322
+ case kPREPEND: {
2342
2323
  member = parse_mixin_member(state, false, annot_pos, annotations);
2343
2324
  break;
2325
+ }
2344
2326
 
2345
- case kALIAS:
2327
+ case kALIAS: {
2346
2328
  member = parse_alias_member(state, false, annot_pos, annotations);
2347
2329
  break;
2330
+ }
2348
2331
 
2349
2332
  case tAIDENT:
2350
2333
  case tA2IDENT:
2351
- case kSELF:
2334
+ case kSELF: {
2352
2335
  member = parse_variable_member(state, annot_pos, annotations);
2353
2336
  break;
2337
+ }
2354
2338
 
2355
2339
  case kATTRREADER:
2356
2340
  case kATTRWRITER:
2357
- case kATTRACCESSOR:
2341
+ case kATTRACCESSOR: {
2358
2342
  member = parse_attribute_member(state, annot_pos, annotations);
2359
2343
  break;
2344
+ }
2360
2345
 
2361
2346
  case kPUBLIC:
2362
2347
  case kPRIVATE:
2363
2348
  if (state->next_token.range.start.line == state->current_token.range.start.line) {
2364
2349
  switch (state->next_token.type)
2365
2350
  {
2366
- case kDEF:
2351
+ case kDEF: {
2367
2352
  member = parse_member_def(state, false, true, annot_pos, annotations);
2368
2353
  break;
2354
+ }
2369
2355
  case kATTRREADER:
2370
2356
  case kATTRWRITER:
2371
- case kATTRACCESSOR:
2357
+ case kATTRACCESSOR: {
2372
2358
  member = parse_attribute_member(state, annot_pos, annotations);
2373
2359
  break;
2360
+ }
2374
2361
  default:
2375
2362
  raise_syntax_error(state, state->next_token, "method or attribute definition is expected after visibility modifier");
2376
2363
  }
@@ -2395,20 +2382,17 @@ VALUE parse_module_members(parserstate *state) {
2395
2382
  module_decl ::= {module_name} module_type_params module_members <kEND>
2396
2383
  | {module_name} module_name module_type_params `:` module_self_types module_members <kEND>
2397
2384
  */
2398
- VALUE parse_module_decl0(parserstate *state, range keyword_range, VALUE module_name, range name_range, VALUE comment, VALUE annotations) {
2399
- range decl_range;
2400
- range end_range;
2401
- range type_params_range;
2402
- range colon_range;
2403
- range self_types_range;
2404
-
2385
+ static VALUE parse_module_decl0(parserstate *state, range keyword_range, VALUE module_name, range name_range, VALUE comment, VALUE annotations) {
2405
2386
  parser_push_typevar_table(state, true);
2406
2387
 
2388
+ range decl_range;
2407
2389
  decl_range.start = keyword_range.start;
2408
-
2390
+ range type_params_range;
2409
2391
  VALUE type_params = parse_type_params(state, &type_params_range, true);
2410
- VALUE self_types = EMPTY_ARRAY;
2411
2392
 
2393
+ VALUE self_types = EMPTY_ARRAY;
2394
+ range colon_range;
2395
+ range self_types_range;
2412
2396
  if (state->next_token.type == pCOLON) {
2413
2397
  parser_advance(state);
2414
2398
  colon_range = state->current_token.range;
@@ -2423,18 +2407,18 @@ VALUE parse_module_decl0(parserstate *state, range keyword_range, VALUE module_n
2423
2407
  VALUE members = parse_module_members(state);
2424
2408
 
2425
2409
  parser_advance_assert(state, kEND);
2426
- end_range = state->current_token.range;
2410
+ range end_range = state->current_token.range;
2427
2411
  decl_range.end = state->current_token.range.end;
2428
2412
 
2429
2413
  VALUE location = rbs_new_location(state->buffer, decl_range);
2430
2414
  rbs_loc *loc = rbs_check_location(location);
2431
2415
  rbs_loc_alloc_children(loc, 6);
2432
- rbs_loc_add_required_child(loc, rb_intern("keyword"), keyword_range);
2433
- rbs_loc_add_required_child(loc, rb_intern("name"), name_range);
2434
- rbs_loc_add_required_child(loc, rb_intern("end"), end_range);
2435
- rbs_loc_add_optional_child(loc, rb_intern("type_params"), type_params_range);
2436
- rbs_loc_add_optional_child(loc, rb_intern("colon"), colon_range);
2437
- rbs_loc_add_optional_child(loc, rb_intern("self_types"), self_types_range);
2416
+ rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range);
2417
+ rbs_loc_add_required_child(loc, INTERN("name"), name_range);
2418
+ rbs_loc_add_required_child(loc, INTERN("end"), end_range);
2419
+ rbs_loc_add_optional_child(loc, INTERN("type_params"), type_params_range);
2420
+ rbs_loc_add_optional_child(loc, INTERN("colon"), colon_range);
2421
+ rbs_loc_add_optional_child(loc, INTERN("self_types"), self_types_range);
2438
2422
 
2439
2423
  parser_pop_typevar_table(state);
2440
2424
 
@@ -2454,14 +2438,14 @@ VALUE parse_module_decl0(parserstate *state, range keyword_range, VALUE module_n
2454
2438
  | {`module`} module_name module_decl0 <kEND>
2455
2439
 
2456
2440
  */
2457
- VALUE parse_module_decl(parserstate *state, position comment_pos, VALUE annotations) {
2441
+ static VALUE parse_module_decl(parserstate *state, position comment_pos, VALUE annotations) {
2458
2442
  range keyword_range = state->current_token.range;
2459
- range module_name_range;
2460
2443
 
2461
2444
  comment_pos = nonnull_pos_or(comment_pos, state->current_token.range.start);
2462
2445
  VALUE comment = get_comment(state, comment_pos.line);
2463
2446
 
2464
2447
  parser_advance(state);
2448
+ range module_name_range;
2465
2449
  VALUE module_name = parse_type_name(state, CLASS_NAME, &module_name_range);
2466
2450
 
2467
2451
  if (state->next_token.type == pEQ) {
@@ -2472,19 +2456,20 @@ VALUE parse_module_decl(parserstate *state, position comment_pos, VALUE annotati
2472
2456
  range old_name_range;
2473
2457
  VALUE old_name = parse_type_name(state, CLASS_NAME, &old_name_range);
2474
2458
 
2475
- range decl_range;
2476
- decl_range.start = keyword_range.start;
2477
- decl_range.end = old_name_range.end;
2459
+ range decl_range = {
2460
+ .start = keyword_range.start,
2461
+ .end = old_name_range.end
2462
+ };
2478
2463
 
2479
2464
  VALUE location = rbs_new_location(state->buffer, decl_range);
2480
2465
  rbs_loc *loc = rbs_check_location(location);
2481
2466
  rbs_loc_alloc_children(loc, 4);
2482
- rbs_loc_add_required_child(loc, rb_intern("keyword"), keyword_range);
2483
- rbs_loc_add_required_child(loc, rb_intern("new_name"), module_name_range);
2484
- rbs_loc_add_required_child(loc, rb_intern("eq"), eq_range);
2485
- rbs_loc_add_optional_child(loc, rb_intern("old_name"), old_name_range);
2467
+ rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range);
2468
+ rbs_loc_add_required_child(loc, INTERN("new_name"), module_name_range);
2469
+ rbs_loc_add_required_child(loc, INTERN("eq"), eq_range);
2470
+ rbs_loc_add_optional_child(loc, INTERN("old_name"), old_name_range);
2486
2471
 
2487
- return rbs_ast_decl_module_alias(module_name, old_name, location, comment);
2472
+ return rbs_ast_decl_module_alias(module_name, old_name, location, comment, annotations);
2488
2473
  } else {
2489
2474
  return parse_module_decl0(state, keyword_range, module_name, module_name_range, comment, annotations);
2490
2475
  }
@@ -2494,30 +2479,25 @@ VALUE parse_module_decl(parserstate *state, position comment_pos, VALUE annotati
2494
2479
  class_decl_super ::= {} `<` <class_instance_name>
2495
2480
  | {<>}
2496
2481
  */
2497
- VALUE parse_class_decl_super(parserstate *state, range *lt_range) {
2482
+ static VALUE parse_class_decl_super(parserstate *state, range *lt_range) {
2498
2483
  if (parser_advance_if(state, pLT)) {
2499
- range super_range;
2500
- range name_range;
2501
- range args_range = NULL_RANGE;
2502
-
2503
- VALUE name;
2504
- VALUE args;
2505
- VALUE location;
2506
- rbs_loc *loc;
2507
-
2508
2484
  *lt_range = state->current_token.range;
2485
+
2486
+ range super_range;
2509
2487
  super_range.start = state->next_token.range.start;
2510
2488
 
2511
- args = EMPTY_ARRAY;
2489
+ VALUE name;
2490
+ VALUE args = EMPTY_ARRAY;
2491
+ range name_range, args_range;
2512
2492
  class_instance_name(state, CLASS_NAME, &name, &args, &name_range, &args_range);
2513
2493
 
2514
2494
  super_range.end = state->current_token.range.end;
2515
2495
 
2516
- location = rbs_new_location(state->buffer, super_range);
2517
- loc = rbs_check_location(location);
2496
+ VALUE location = rbs_new_location(state->buffer, super_range);
2497
+ rbs_loc *loc = rbs_check_location(location);
2518
2498
  rbs_loc_alloc_children(loc, 2);
2519
- rbs_loc_add_required_child(loc, rb_intern("name"), name_range);
2520
- rbs_loc_add_optional_child(loc, rb_intern("args"), args_range);
2499
+ rbs_loc_add_required_child(loc, INTERN("name"), name_range);
2500
+ rbs_loc_add_optional_child(loc, INTERN("args"), args_range);
2521
2501
 
2522
2502
  return rbs_ast_decl_class_super(name, args, location);
2523
2503
  } else {
@@ -2529,41 +2509,36 @@ VALUE parse_class_decl_super(parserstate *state, range *lt_range) {
2529
2509
  /*
2530
2510
  class_decl ::= {class_name} type_params class_decl_super class_members <`end`>
2531
2511
  */
2532
- VALUE parse_class_decl0(parserstate *state, range keyword_range, VALUE name, range name_range, VALUE comment, VALUE annotations) {
2533
- range decl_range;
2534
- range end_range;
2535
- range type_params_range;
2536
- range lt_range;
2512
+ static VALUE parse_class_decl0(parserstate *state, range keyword_range, VALUE name, range name_range, VALUE comment, VALUE annotations) {
2513
+ parser_push_typevar_table(state, true);
2537
2514
 
2538
- VALUE type_params;
2539
- VALUE super;
2540
- VALUE members;
2541
- VALUE location;
2515
+ range decl_range;
2516
+ decl_range.start = keyword_range.start;
2542
2517
 
2543
- rbs_loc *loc;
2518
+ range type_params_range;
2519
+ VALUE type_params = parse_type_params(state, &type_params_range, true);
2544
2520
 
2545
- parser_push_typevar_table(state, true);
2521
+ range lt_range;
2522
+ VALUE super = parse_class_decl_super(state, &lt_range);
2546
2523
 
2547
- decl_range.start = keyword_range.start;
2524
+ VALUE members = parse_module_members(state);
2548
2525
 
2549
- type_params = parse_type_params(state, &type_params_range, true);
2550
- super = parse_class_decl_super(state, &lt_range);
2551
- members = parse_module_members(state);
2552
2526
  parser_advance_assert(state, kEND);
2553
- end_range = state->current_token.range;
2527
+
2528
+ range end_range = state->current_token.range;
2554
2529
 
2555
2530
  decl_range.end = end_range.end;
2556
2531
 
2557
2532
  parser_pop_typevar_table(state);
2558
2533
 
2559
- location = rbs_new_location(state->buffer, decl_range);
2560
- loc = rbs_check_location(location);
2534
+ VALUE location = rbs_new_location(state->buffer, decl_range);
2535
+ rbs_loc *loc = rbs_check_location(location);
2561
2536
  rbs_loc_alloc_children(loc, 5);
2562
- rbs_loc_add_required_child(loc, rb_intern("keyword"), keyword_range);
2563
- rbs_loc_add_required_child(loc, rb_intern("name"), name_range);
2564
- rbs_loc_add_required_child(loc, rb_intern("end"), end_range);
2565
- rbs_loc_add_optional_child(loc, rb_intern("type_params"), type_params_range);
2566
- rbs_loc_add_optional_child(loc, rb_intern("lt"), lt_range);
2537
+ rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range);
2538
+ rbs_loc_add_required_child(loc, INTERN("name"), name_range);
2539
+ rbs_loc_add_required_child(loc, INTERN("end"), end_range);
2540
+ rbs_loc_add_optional_child(loc, INTERN("type_params"), type_params_range);
2541
+ rbs_loc_add_optional_child(loc, INTERN("lt"), lt_range);
2567
2542
 
2568
2543
  return rbs_ast_decl_class(
2569
2544
  name,
@@ -2580,14 +2555,14 @@ VALUE parse_class_decl0(parserstate *state, range keyword_range, VALUE name, ran
2580
2555
  class_decl ::= {`class`} class_name `=` <class_name>
2581
2556
  | {`class`} class_name <class_decl0>
2582
2557
  */
2583
- VALUE parse_class_decl(parserstate *state, position comment_pos, VALUE annotations) {
2558
+ static VALUE parse_class_decl(parserstate *state, position comment_pos, VALUE annotations) {
2584
2559
  range keyword_range = state->current_token.range;
2585
- range class_name_range;
2586
2560
 
2587
2561
  comment_pos = nonnull_pos_or(comment_pos, state->current_token.range.start);
2588
2562
  VALUE comment = get_comment(state, comment_pos.line);
2589
2563
 
2590
2564
  parser_advance(state);
2565
+ range class_name_range;
2591
2566
  VALUE class_name = parse_type_name(state, CLASS_NAME, &class_name_range);
2592
2567
 
2593
2568
  if (state->next_token.type == pEQ) {
@@ -2598,19 +2573,20 @@ VALUE parse_class_decl(parserstate *state, position comment_pos, VALUE annotatio
2598
2573
  range old_name_range;
2599
2574
  VALUE old_name = parse_type_name(state, CLASS_NAME, &old_name_range);
2600
2575
 
2601
- range decl_range;
2602
- decl_range.start = keyword_range.start;
2603
- decl_range.end = old_name_range.end;
2576
+ range decl_range = {
2577
+ .start = keyword_range.start,
2578
+ .end = old_name_range.end,
2579
+ };
2604
2580
 
2605
2581
  VALUE location = rbs_new_location(state->buffer, decl_range);
2606
2582
  rbs_loc *loc = rbs_check_location(location);
2607
2583
  rbs_loc_alloc_children(loc, 4);
2608
- rbs_loc_add_required_child(loc, rb_intern("keyword"), keyword_range);
2609
- rbs_loc_add_required_child(loc, rb_intern("new_name"), class_name_range);
2610
- rbs_loc_add_required_child(loc, rb_intern("eq"), eq_range);
2611
- rbs_loc_add_optional_child(loc, rb_intern("old_name"), old_name_range);
2584
+ rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range);
2585
+ rbs_loc_add_required_child(loc, INTERN("new_name"), class_name_range);
2586
+ rbs_loc_add_required_child(loc, INTERN("eq"), eq_range);
2587
+ rbs_loc_add_optional_child(loc, INTERN("old_name"), old_name_range);
2612
2588
 
2613
- return rbs_ast_decl_class_alias(class_name, old_name, location, comment);
2589
+ return rbs_ast_decl_class_alias(class_name, old_name, location, comment, annotations);
2614
2590
  } else {
2615
2591
  return parse_class_decl0(state, keyword_range, class_name, class_name_range, comment, annotations);
2616
2592
  }
@@ -2623,31 +2599,36 @@ VALUE parse_class_decl(parserstate *state, position comment_pos, VALUE annotatio
2623
2599
  | {<module_decl>}
2624
2600
  | {<class_decl>}
2625
2601
  */
2626
- VALUE parse_nested_decl(parserstate *state, const char *nested_in, position annot_pos, VALUE annotations) {
2627
- VALUE decl;
2628
-
2602
+ static VALUE parse_nested_decl(parserstate *state, const char *nested_in, position annot_pos, VALUE annotations) {
2629
2603
  parser_push_typevar_table(state, true);
2630
2604
 
2605
+ VALUE decl;
2631
2606
  switch (state->current_token.type) {
2632
2607
  case tUIDENT:
2633
- case pCOLON2:
2634
- decl = parse_const_decl(state);
2608
+ case pCOLON2: {
2609
+ decl = parse_const_decl(state, annotations);
2635
2610
  break;
2636
- case tGIDENT:
2637
- decl = parse_global_decl(state);
2611
+ }
2612
+ case tGIDENT: {
2613
+ decl = parse_global_decl(state, annotations);
2638
2614
  break;
2639
- case kTYPE:
2615
+ }
2616
+ case kTYPE: {
2640
2617
  decl = parse_type_decl(state, annot_pos, annotations);
2641
2618
  break;
2642
- case kINTERFACE:
2619
+ }
2620
+ case kINTERFACE: {
2643
2621
  decl = parse_interface_decl(state, annot_pos, annotations);
2644
2622
  break;
2645
- case kMODULE:
2623
+ }
2624
+ case kMODULE: {
2646
2625
  decl = parse_module_decl(state, annot_pos, annotations);
2647
2626
  break;
2648
- case kCLASS:
2627
+ }
2628
+ case kCLASS: {
2649
2629
  decl = parse_class_decl(state, annot_pos, annotations);
2650
2630
  break;
2631
+ }
2651
2632
  default:
2652
2633
  raise_syntax_error(
2653
2634
  state,
@@ -2661,7 +2642,7 @@ VALUE parse_nested_decl(parserstate *state, const char *nested_in, position anno
2661
2642
  return decl;
2662
2643
  }
2663
2644
 
2664
- VALUE parse_decl(parserstate *state) {
2645
+ static VALUE parse_decl(parserstate *state) {
2665
2646
  VALUE annotations = EMPTY_ARRAY;
2666
2647
  position annot_pos = NullPosition;
2667
2648
 
@@ -2670,18 +2651,24 @@ VALUE parse_decl(parserstate *state) {
2670
2651
  parser_advance(state);
2671
2652
  switch (state->current_token.type) {
2672
2653
  case tUIDENT:
2673
- case pCOLON2:
2674
- return parse_const_decl(state);
2675
- case tGIDENT:
2676
- return parse_global_decl(state);
2677
- case kTYPE:
2654
+ case pCOLON2: {
2655
+ return parse_const_decl(state, annotations);
2656
+ }
2657
+ case tGIDENT: {
2658
+ return parse_global_decl(state, annotations);
2659
+ }
2660
+ case kTYPE: {
2678
2661
  return parse_type_decl(state, annot_pos, annotations);
2679
- case kINTERFACE:
2662
+ }
2663
+ case kINTERFACE: {
2680
2664
  return parse_interface_decl(state, annot_pos, annotations);
2681
- case kMODULE:
2665
+ }
2666
+ case kMODULE: {
2682
2667
  return parse_module_decl(state, annot_pos, annotations);
2683
- case kCLASS:
2668
+ }
2669
+ case kCLASS: {
2684
2670
  return parse_class_decl(state, annot_pos, annotations);
2671
+ }
2685
2672
  default:
2686
2673
  raise_syntax_error(
2687
2674
  state,
@@ -2695,12 +2682,14 @@ VALUE parse_decl(parserstate *state) {
2695
2682
  namespace ::= {} (`::`)? (`tUIDENT` `::`)* `tUIDENT` <`::`>
2696
2683
  | {} <> (empty -- returns empty namespace)
2697
2684
  */
2698
- VALUE parse_namespace(parserstate *state, range *rg) {
2685
+ static VALUE parse_namespace(parserstate *state, range *rg) {
2699
2686
  bool is_absolute = false;
2700
2687
 
2701
2688
  if (state->next_token.type == pCOLON2) {
2702
- rg->start = state->next_token.range.start;
2703
- rg->end = state->next_token.range.end;
2689
+ *rg = (range) {
2690
+ .start = state->next_token.range.start,
2691
+ .end = state->next_token.range.end,
2692
+ };
2704
2693
  is_absolute = true;
2705
2694
 
2706
2695
  parser_advance(state);
@@ -2733,13 +2722,11 @@ VALUE parse_namespace(parserstate *state, range *rg) {
2733
2722
  | {} namespace tUIDENT `as` <tUIDENT>
2734
2723
  | {} namespace <tSTAR>
2735
2724
  */
2736
- void parse_use_clauses(parserstate *state, VALUE clauses) {
2725
+ static void parse_use_clauses(parserstate *state, VALUE clauses) {
2737
2726
  while (true) {
2738
2727
  range namespace_range = NULL_RANGE;
2739
2728
  VALUE namespace = parse_namespace(state, &namespace_range);
2740
2729
 
2741
- range clause_range = namespace_range;
2742
-
2743
2730
  switch (state->next_token.type)
2744
2731
  {
2745
2732
  case tLIDENT:
@@ -2749,14 +2736,9 @@ void parse_use_clauses(parserstate *state, VALUE clauses) {
2749
2736
 
2750
2737
  enum TokenType ident_type = state->current_token.type;
2751
2738
 
2752
- range type_name_range;
2753
- if (null_range_p(namespace_range)) {
2754
- type_name_range = state->current_token.range;
2755
- } else {
2756
- type_name_range.start = namespace_range.start;
2757
- type_name_range.end = state->current_token.range.end;
2758
- }
2759
- clause_range = type_name_range;
2739
+ range type_name_range = null_range_p(namespace_range)
2740
+ ? state->current_token.range
2741
+ : (range) { .start = namespace_range.start, .end = state->current_token.range.end };
2760
2742
 
2761
2743
  VALUE type_name = rbs_type_name(namespace, ID2SYM(INTERN_TOKEN(state, state->current_token)));
2762
2744
 
@@ -2764,6 +2746,7 @@ void parse_use_clauses(parserstate *state, VALUE clauses) {
2764
2746
  range new_name_range = NULL_RANGE;
2765
2747
 
2766
2748
  VALUE new_name = Qnil;
2749
+ range clause_range = type_name_range;
2767
2750
  if (state->next_token.type == kAS) {
2768
2751
  parser_advance(state);
2769
2752
  keyword_range = state->current_token.range;
@@ -2780,9 +2763,9 @@ void parse_use_clauses(parserstate *state, VALUE clauses) {
2780
2763
  VALUE location = rbs_new_location(state->buffer, clause_range);
2781
2764
  rbs_loc *loc = rbs_check_location(location);
2782
2765
  rbs_loc_alloc_children(loc, 3);
2783
- rbs_loc_add_required_child(loc, rb_intern("type_name"), type_name_range);
2784
- rbs_loc_add_optional_child(loc, rb_intern("keyword"), keyword_range);
2785
- rbs_loc_add_optional_child(loc, rb_intern("new_name"), new_name_range);
2766
+ rbs_loc_add_required_child(loc, INTERN("type_name"), type_name_range);
2767
+ rbs_loc_add_optional_child(loc, INTERN("keyword"), keyword_range);
2768
+ rbs_loc_add_optional_child(loc, INTERN("new_name"), new_name_range);
2786
2769
 
2787
2770
  rb_ary_push(clauses, rbs_ast_directives_use_single_clause(type_name, new_name, location));
2788
2771
 
@@ -2790,6 +2773,7 @@ void parse_use_clauses(parserstate *state, VALUE clauses) {
2790
2773
  }
2791
2774
  case pSTAR:
2792
2775
  {
2776
+ range clause_range = namespace_range;
2793
2777
  parser_advance(state);
2794
2778
 
2795
2779
  range star_range = state->current_token.range;
@@ -2798,8 +2782,8 @@ void parse_use_clauses(parserstate *state, VALUE clauses) {
2798
2782
  VALUE location = rbs_new_location(state->buffer, clause_range);
2799
2783
  rbs_loc *loc = rbs_check_location(location);
2800
2784
  rbs_loc_alloc_children(loc, 2);
2801
- rbs_loc_add_required_child(loc, rb_intern("namespace"), namespace_range);
2802
- rbs_loc_add_required_child(loc, rb_intern("star"), star_range);
2785
+ rbs_loc_add_required_child(loc, INTERN("namespace"), namespace_range);
2786
+ rbs_loc_add_required_child(loc, INTERN("star"), star_range);
2803
2787
 
2804
2788
  rb_ary_push(clauses, rbs_ast_directives_use_wildcard_clause(namespace, location));
2805
2789
 
@@ -2826,7 +2810,7 @@ void parse_use_clauses(parserstate *state, VALUE clauses) {
2826
2810
  /*
2827
2811
  use_directive ::= {} `use` <clauses>
2828
2812
  */
2829
- VALUE parse_use_directive(parserstate *state) {
2813
+ static VALUE parse_use_directive(parserstate *state) {
2830
2814
  if (state->next_token.type == kUSE) {
2831
2815
  parser_advance(state);
2832
2816
 
@@ -2841,7 +2825,7 @@ VALUE parse_use_directive(parserstate *state) {
2841
2825
  VALUE location = rbs_new_location(state->buffer, directive_range);
2842
2826
  rbs_loc *loc = rbs_check_location(location);
2843
2827
  rbs_loc_alloc_children(loc, 1);
2844
- rbs_loc_add_required_child(loc, rb_intern("keyword"), keyword_range);
2828
+ rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range);
2845
2829
 
2846
2830
  return rbs_ast_directives_use(clauses, location);
2847
2831
  } else {
@@ -2949,12 +2933,12 @@ parse_signature_try(VALUE a) {
2949
2933
  }
2950
2934
 
2951
2935
  static VALUE
2952
- rbsparser_parse_signature(VALUE self, VALUE buffer, VALUE end_pos)
2936
+ rbsparser_parse_signature(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos)
2953
2937
  {
2954
2938
  VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
2955
2939
  StringValue(string);
2956
- lexstate *lexer = alloc_lexer(string, 0, FIX2INT(end_pos));
2957
- parserstate *parser = alloc_parser(buffer, lexer, 0, FIX2INT(end_pos), Qnil);
2940
+ lexstate *lexer = alloc_lexer(string, FIX2INT(start_pos), FIX2INT(end_pos));
2941
+ parserstate *parser = alloc_parser(buffer, lexer, FIX2INT(start_pos), FIX2INT(end_pos), Qnil);
2958
2942
  return rb_ensure(parse_signature_try, (VALUE)parser, ensure_free_parser, (VALUE)parser);
2959
2943
  }
2960
2944
 
@@ -2982,12 +2966,17 @@ rbsparser_lex(VALUE self, VALUE buffer, VALUE end_pos) {
2982
2966
  void rbs__init_parser(void) {
2983
2967
  RBS_Parser = rb_define_class_under(RBS, "Parser", rb_cObject);
2984
2968
  rb_gc_register_mark_object(RBS_Parser);
2969
+
2985
2970
  VALUE empty_array = rb_obj_freeze(rb_ary_new());
2986
2971
  rb_gc_register_mark_object(empty_array);
2987
2972
  EMPTY_ARRAY = empty_array;
2988
2973
 
2974
+ VALUE empty_hash = rb_obj_freeze(rb_hash_new());
2975
+ rb_gc_register_mark_object(empty_hash);
2976
+ EMPTY_HASH = empty_hash;
2977
+
2989
2978
  rb_define_singleton_method(RBS_Parser, "_parse_type", rbsparser_parse_type, 5);
2990
2979
  rb_define_singleton_method(RBS_Parser, "_parse_method_type", rbsparser_parse_method_type, 5);
2991
- rb_define_singleton_method(RBS_Parser, "_parse_signature", rbsparser_parse_signature, 2);
2980
+ rb_define_singleton_method(RBS_Parser, "_parse_signature", rbsparser_parse_signature, 3);
2992
2981
  rb_define_singleton_method(RBS_Parser, "_lex", rbsparser_lex, 2);
2993
2982
  }