rbs 3.0.0.dev.1 → 3.0.0.dev.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (165) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/comments.yml +2 -1
  3. data/.github/workflows/ruby.yml +4 -3
  4. data/CHANGELOG.md +28 -0
  5. data/Gemfile.lock +12 -12
  6. data/README.md +1 -0
  7. data/Rakefile +77 -3
  8. data/Steepfile +1 -1
  9. data/core/array.rbs +574 -424
  10. data/core/basic_object.rbs +11 -39
  11. data/core/binding.rbs +1 -1
  12. data/core/builtin.rbs +9 -1
  13. data/core/class.rbs +37 -0
  14. data/core/comparable.rbs +7 -18
  15. data/core/complex.rbs +2 -2
  16. data/core/data.rbs +419 -0
  17. data/core/dir.rbs +52 -104
  18. data/core/encoding.rbs +22 -181
  19. data/core/enumerable.rbs +212 -175
  20. data/core/enumerator/product.rbs +96 -0
  21. data/core/enumerator.rbs +57 -8
  22. data/core/errors.rbs +8 -2
  23. data/core/exception.rbs +41 -0
  24. data/core/fiber.rbs +95 -12
  25. data/core/file.rbs +840 -275
  26. data/core/file_test.rbs +34 -19
  27. data/core/float.rbs +40 -96
  28. data/core/gc.rbs +15 -3
  29. data/core/hash.rbs +114 -176
  30. data/core/integer.rbs +85 -145
  31. data/core/io/buffer.rbs +187 -60
  32. data/core/io/wait.rbs +28 -16
  33. data/core/io.rbs +1859 -1389
  34. data/core/kernel.rbs +525 -961
  35. data/core/match_data.rbs +306 -142
  36. data/core/math.rbs +506 -234
  37. data/core/method.rbs +0 -24
  38. data/core/module.rbs +111 -18
  39. data/core/nil_class.rbs +2 -0
  40. data/core/numeric.rbs +76 -144
  41. data/core/object.rbs +88 -212
  42. data/core/proc.rbs +17 -5
  43. data/core/process.rbs +22 -5
  44. data/core/ractor.rbs +1 -1
  45. data/core/random.rbs +20 -3
  46. data/core/range.rbs +91 -89
  47. data/core/rational.rbs +2 -3
  48. data/core/rbs/unnamed/argf.rbs +177 -120
  49. data/core/rbs/unnamed/env_class.rbs +89 -163
  50. data/core/rbs/unnamed/random.rbs +36 -12
  51. data/core/refinement.rbs +8 -0
  52. data/core/regexp.rbs +462 -272
  53. data/core/ruby_vm.rbs +210 -0
  54. data/{stdlib/set/0 → core}/set.rbs +43 -47
  55. data/core/string.rbs +1403 -1332
  56. data/core/string_io.rbs +191 -107
  57. data/core/struct.rbs +67 -63
  58. data/core/symbol.rbs +187 -201
  59. data/core/thread.rbs +40 -35
  60. data/core/time.rbs +902 -826
  61. data/core/trace_point.rbs +55 -6
  62. data/core/unbound_method.rbs +48 -24
  63. data/docs/collection.md +4 -0
  64. data/docs/syntax.md +55 -0
  65. data/ext/rbs_extension/constants.c +16 -2
  66. data/ext/rbs_extension/constants.h +8 -1
  67. data/ext/rbs_extension/extconf.rb +1 -1
  68. data/ext/rbs_extension/lexer.c +834 -777
  69. data/ext/rbs_extension/lexer.h +3 -1
  70. data/ext/rbs_extension/lexer.re +3 -1
  71. data/ext/rbs_extension/lexstate.c +4 -2
  72. data/ext/rbs_extension/parser.c +262 -43
  73. data/ext/rbs_extension/ruby_objs.c +56 -2
  74. data/ext/rbs_extension/ruby_objs.h +7 -1
  75. data/lib/rbs/annotate/rdoc_annotator.rb +1 -1
  76. data/lib/rbs/ast/declarations.rb +49 -2
  77. data/lib/rbs/ast/directives.rb +39 -0
  78. data/lib/rbs/cli.rb +38 -19
  79. data/lib/rbs/collection/cleaner.rb +8 -1
  80. data/lib/rbs/collection/config/lockfile.rb +3 -1
  81. data/lib/rbs/collection/config/lockfile_generator.rb +37 -30
  82. data/lib/rbs/collection/config.rb +3 -3
  83. data/lib/rbs/collection/sources/git.rb +10 -3
  84. data/lib/rbs/collection/sources/local.rb +79 -0
  85. data/lib/rbs/collection/sources.rb +8 -1
  86. data/lib/rbs/definition_builder/ancestor_builder.rb +24 -8
  87. data/lib/rbs/definition_builder.rb +8 -8
  88. data/lib/rbs/environment/use_map.rb +77 -0
  89. data/lib/rbs/environment.rb +358 -88
  90. data/lib/rbs/environment_loader.rb +12 -9
  91. data/lib/rbs/environment_walker.rb +1 -1
  92. data/lib/rbs/errors.rb +52 -37
  93. data/lib/rbs/locator.rb +27 -8
  94. data/lib/rbs/parser_aux.rb +8 -6
  95. data/lib/rbs/resolver/constant_resolver.rb +23 -7
  96. data/lib/rbs/resolver/type_name_resolver.rb +2 -1
  97. data/lib/rbs/sorter.rb +5 -5
  98. data/lib/rbs/test/setup.rb +1 -1
  99. data/lib/rbs/type_alias_dependency.rb +1 -1
  100. data/lib/rbs/type_alias_regularity.rb +3 -3
  101. data/lib/rbs/validator.rb +23 -2
  102. data/lib/rbs/variance_calculator.rb +2 -2
  103. data/lib/rbs/version.rb +1 -1
  104. data/lib/rbs/writer.rb +28 -2
  105. data/lib/rbs.rb +2 -2
  106. data/lib/rdoc_plugin/parser.rb +2 -2
  107. data/rbs.gemspec +1 -1
  108. data/sig/ancestor_graph.rbs +22 -2
  109. data/sig/collection/config/lockfile_generator.rbs +8 -10
  110. data/sig/collection/config.rbs +1 -1
  111. data/sig/collection/sources.rbs +44 -9
  112. data/sig/constant.rbs +1 -1
  113. data/sig/declarations.rbs +36 -3
  114. data/sig/definition.rbs +1 -1
  115. data/sig/definition_builder.rbs +0 -1
  116. data/sig/directives.rbs +61 -0
  117. data/sig/environment.rbs +150 -29
  118. data/sig/environment_loader.rbs +1 -1
  119. data/sig/errors.rbs +22 -1
  120. data/sig/locator.rbs +14 -2
  121. data/sig/parser.rbs +8 -15
  122. data/sig/resolver/constant_resolver.rbs +1 -2
  123. data/sig/shims/{abstract_syntax_tree.rbs → _abstract_syntax_tree.rbs} +0 -0
  124. data/sig/shims/bundler.rbs +18 -0
  125. data/sig/shims/rubygems.rbs +6 -0
  126. data/sig/use_map.rbs +35 -0
  127. data/sig/validator.rbs +12 -5
  128. data/sig/writer.rbs +4 -2
  129. data/stdlib/bigdecimal/0/big_decimal.rbs +16 -13
  130. data/stdlib/cgi/0/core.rbs +16 -0
  131. data/stdlib/coverage/0/coverage.rbs +50 -8
  132. data/stdlib/csv/0/csv.rbs +1 -1
  133. data/stdlib/date/0/date.rbs +856 -726
  134. data/stdlib/date/0/date_time.rbs +83 -210
  135. data/stdlib/erb/0/erb.rbs +13 -36
  136. data/stdlib/etc/0/etc.rbs +127 -20
  137. data/stdlib/fileutils/0/fileutils.rbs +1290 -381
  138. data/stdlib/logger/0/logger.rbs +466 -316
  139. data/stdlib/net-http/0/net-http.rbs +2211 -534
  140. data/stdlib/nkf/0/nkf.rbs +5 -5
  141. data/stdlib/objspace/0/objspace.rbs +31 -14
  142. data/stdlib/openssl/0/openssl.rbs +11 -7
  143. data/stdlib/optparse/0/optparse.rbs +20 -17
  144. data/stdlib/pathname/0/pathname.rbs +21 -4
  145. data/stdlib/pstore/0/pstore.rbs +378 -154
  146. data/stdlib/pty/0/pty.rbs +24 -8
  147. data/stdlib/ripper/0/ripper.rbs +1650 -0
  148. data/stdlib/socket/0/addrinfo.rbs +9 -15
  149. data/stdlib/socket/0/socket.rbs +36 -3
  150. data/stdlib/strscan/0/string_scanner.rbs +7 -5
  151. data/stdlib/tempfile/0/tempfile.rbs +104 -44
  152. data/stdlib/time/0/time.rbs +2 -2
  153. data/stdlib/uri/0/file.rbs +5 -0
  154. data/stdlib/uri/0/generic.rbs +2 -2
  155. data/stdlib/yaml/0/yaml.rbs +2 -2
  156. data/stdlib/zlib/0/zlib.rbs +1 -1
  157. metadata +13 -13
  158. data/core/deprecated.rbs +0 -9
  159. data/lib/rbs/constant_table.rb +0 -167
  160. data/lib/rbs/type_name_resolver.rb +0 -67
  161. data/sig/constant_table.rbs +0 -30
  162. data/sig/shims/ripper.rbs +0 -8
  163. data/sig/type_name_resolver.rbs +0 -26
  164. data/steep/Gemfile +0 -3
  165. data/steep/Gemfile.lock +0 -61
@@ -58,6 +58,8 @@ enum TokenType {
58
58
  kUNCHECKED, /* unchecked */
59
59
  kUNTYPED, /* untyped */
60
60
  kVOID, /* void */
61
+ kUSE, /* use */
62
+ kAS, /* as */
61
63
 
62
64
  tLIDENT, /* Identifiers starting with lower case */
63
65
  tUIDENT, /* Identifiers starting with upper case */
@@ -150,7 +152,7 @@ unsigned int peek(lexstate *state);
150
152
  /**
151
153
  * Skip one character.
152
154
  * */
153
- void skip(lexstate *state);
155
+ void rbs_skip(lexstate *state);
154
156
 
155
157
  /**
156
158
  * Skip n characters.
@@ -12,7 +12,7 @@ start:
12
12
  re2c:flags:input = custom;
13
13
  re2c:define:YYCTYPE = "unsigned int";
14
14
  re2c:define:YYPEEK = "peek(state)";
15
- re2c:define:YYSKIP = "skip(state);";
15
+ re2c:define:YYSKIP = "rbs_skip(state);";
16
16
  re2c:define:YYBACKUP = "backup = *state;";
17
17
  re2c:define:YYRESTORE = "*state = backup;";
18
18
  re2c:yyfill:enable = 0;
@@ -93,6 +93,8 @@ start:
93
93
  "unchecked" { return next_token(state, kUNCHECKED); }
94
94
  "untyped" { return next_token(state, kUNTYPED); }
95
95
  "void" { return next_token(state, kVOID); }
96
+ "use" { return next_token(state, kUSE); }
97
+ "as" { return next_token(state, kAS); }
96
98
 
97
99
  dqstring = ["] ("\\"[abefnrstv"\\] | [^"\\\x00])* ["];
98
100
  sqstring = ['] ("\\"['\\] | [^'\x00])* ['];
@@ -57,6 +57,8 @@ static const char *RBS_TOKENTYPE_NAMES[] = {
57
57
  "kUNCHECKED", /* unchecked */
58
58
  "kUNTYPED", /* untyped */
59
59
  "kVOID", /* void */
60
+ "kUSE", /* use */
61
+ "kAS", /* as */
60
62
 
61
63
  "tLIDENT", /* Identifiers starting with lower case */
62
64
  "tUIDENT", /* Identifiers starting with upper case */
@@ -122,7 +124,7 @@ token next_token(lexstate *state, enum TokenType type) {
122
124
  return t;
123
125
  }
124
126
 
125
- void skip(lexstate *state) {
127
+ void rbs_skip(lexstate *state) {
126
128
  if (!state->last_char) {
127
129
  peek(state);
128
130
  }
@@ -143,7 +145,7 @@ void skip(lexstate *state) {
143
145
  void skipn(lexstate *state, size_t size) {
144
146
  for (size_t i = 0; i < size; i ++) {
145
147
  peek(state);
146
- skip(state);
148
+ rbs_skip(state);
147
149
  }
148
150
  }
149
151
 
@@ -37,6 +37,8 @@
37
37
  case kPUBLIC: \
38
38
  case kPRIVATE: \
39
39
  case kUNTYPED: \
40
+ case kUSE: \
41
+ case kAS: \
40
42
  /* nop */
41
43
 
42
44
  typedef struct {
@@ -1034,7 +1036,6 @@ VALUE parse_type(parserstate *state) {
1034
1036
 
1035
1037
  type_param ::= tUIDENT (module_type_params == false)
1036
1038
  */
1037
-
1038
1039
  VALUE parse_type_params(parserstate *state, range *rg, bool module_type_params) {
1039
1040
  VALUE params = rb_ary_new();
1040
1041
 
@@ -1277,7 +1278,7 @@ VALUE parse_type_decl(parserstate *state, position comment_pos, VALUE annotation
1277
1278
 
1278
1279
  parser_pop_typevar_table(state);
1279
1280
 
1280
- return rbs_ast_decl_alias(
1281
+ return rbs_ast_decl_type_alias(
1281
1282
  typename,
1282
1283
  type_params,
1283
1284
  type,
@@ -2267,13 +2268,11 @@ VALUE parse_module_members(parserstate *state) {
2267
2268
  }
2268
2269
 
2269
2270
  /*
2270
- module_decl ::= {`module`} module_name module_type_params module_members <kEND>
2271
- | {`module`} module_name module_type_params `:` module_self_types module_members <kEND>
2271
+ module_decl ::= {module_name} module_type_params module_members <kEND>
2272
+ | {module_name} module_name module_type_params `:` module_self_types module_members <kEND>
2272
2273
  */
2273
- VALUE parse_module_decl(parserstate *state, position comment_pos, VALUE annotations) {
2274
+ VALUE parse_module_decl0(parserstate *state, range keyword_range, VALUE module_name, range name_range, VALUE comment, VALUE annotations) {
2274
2275
  range decl_range;
2275
- range keyword_range;
2276
- range name_range;
2277
2276
  range end_range;
2278
2277
  range type_params_range;
2279
2278
  range colon_range;
@@ -2281,15 +2280,8 @@ VALUE parse_module_decl(parserstate *state, position comment_pos, VALUE annotati
2281
2280
 
2282
2281
  parser_push_typevar_table(state, true);
2283
2282
 
2284
- position start = state->current_token.range.start;
2285
- comment_pos = nonnull_pos_or(comment_pos, start);
2286
- VALUE comment = get_comment(state, comment_pos.line);
2287
-
2288
- keyword_range = state->current_token.range;
2289
- decl_range.start = state->current_token.range.start;
2283
+ decl_range.start = keyword_range.start;
2290
2284
 
2291
- parser_advance(state);
2292
- VALUE module_name = parse_type_name(state, CLASS_NAME, &name_range);
2293
2285
  VALUE type_params = parse_type_params(state, &type_params_range, true);
2294
2286
  VALUE self_types = rb_ary_new();
2295
2287
 
@@ -2332,6 +2324,46 @@ VALUE parse_module_decl(parserstate *state, position comment_pos, VALUE annotati
2332
2324
  );
2333
2325
  }
2334
2326
 
2327
+ /*
2328
+ module_decl ::= {`module`} module_name `=` old_module_name <kEND>
2329
+ | {`module`} module_name module_decl0 <kEND>
2330
+
2331
+ */
2332
+ VALUE parse_module_decl(parserstate *state, position comment_pos, VALUE annotations) {
2333
+ range keyword_range = state->current_token.range;
2334
+ range module_name_range;
2335
+
2336
+ comment_pos = nonnull_pos_or(comment_pos, state->current_token.range.start);
2337
+ VALUE comment = get_comment(state, comment_pos.line);
2338
+
2339
+ parser_advance(state);
2340
+ VALUE module_name = parse_type_name(state, CLASS_NAME, &module_name_range);
2341
+
2342
+ if (state->next_token.type == pEQ) {
2343
+ range eq_range = state->next_token.range;
2344
+ parser_advance(state);
2345
+ parser_advance(state);
2346
+
2347
+ range old_name_range;
2348
+ VALUE old_name = parse_type_name(state, CLASS_NAME, &old_name_range);
2349
+
2350
+ range decl_range;
2351
+ decl_range.start = keyword_range.start;
2352
+ decl_range.end = old_name_range.end;
2353
+
2354
+ VALUE location = rbs_new_location(state->buffer, decl_range);
2355
+ rbs_loc *loc = rbs_check_location(location);
2356
+ rbs_loc_add_required_child(loc, rb_intern("keyword"), keyword_range);
2357
+ rbs_loc_add_required_child(loc, rb_intern("new_name"), module_name_range);
2358
+ rbs_loc_add_required_child(loc, rb_intern("eq"), eq_range);
2359
+ rbs_loc_add_optional_child(loc, rb_intern("old_name"), old_name_range);
2360
+
2361
+ return rbs_ast_decl_module_alias(module_name, old_name, location, comment);
2362
+ } else {
2363
+ return parse_module_decl0(state, keyword_range, module_name, module_name_range, comment, annotations);
2364
+ }
2365
+ }
2366
+
2335
2367
  /*
2336
2368
  class_decl_super ::= {} `<` <class_instance_name>
2337
2369
  | {<>}
@@ -2368,35 +2400,25 @@ VALUE parse_class_decl_super(parserstate *state, range *lt_range) {
2368
2400
  }
2369
2401
 
2370
2402
  /*
2371
- class_decl ::= {`class`} class_name type_params class_decl_super class_members <`end`>
2403
+ class_decl ::= {class_name} type_params class_decl_super class_members <`end`>
2372
2404
  */
2373
- VALUE parse_class_decl(parserstate *state, position comment_pos, VALUE annotations) {
2405
+ VALUE parse_class_decl0(parserstate *state, range keyword_range, VALUE name, range name_range, VALUE comment, VALUE annotations) {
2374
2406
  range decl_range;
2375
- range keyword_range;
2376
- range name_range;
2377
2407
  range end_range;
2378
2408
  range type_params_range;
2379
2409
  range lt_range;
2380
2410
 
2381
- VALUE name;
2382
2411
  VALUE type_params;
2383
2412
  VALUE super;
2384
2413
  VALUE members;
2385
- VALUE comment;
2386
2414
  VALUE location;
2387
2415
 
2388
2416
  rbs_loc *loc;
2389
2417
 
2390
2418
  parser_push_typevar_table(state, true);
2391
2419
 
2392
- decl_range.start = state->current_token.range.start;
2393
- keyword_range = state->current_token.range;
2394
-
2395
- comment_pos = nonnull_pos_or(comment_pos, decl_range.start);
2396
- comment = get_comment(state, comment_pos.line);
2420
+ decl_range.start = keyword_range.start;
2397
2421
 
2398
- parser_advance(state);
2399
- name = parse_type_name(state, CLASS_NAME, &name_range);
2400
2422
  type_params = parse_type_params(state, &type_params_range, true);
2401
2423
  super = parse_class_decl_super(state, &lt_range);
2402
2424
  members = parse_module_members(state);
@@ -2426,6 +2448,45 @@ VALUE parse_class_decl(parserstate *state, position comment_pos, VALUE annotatio
2426
2448
  );
2427
2449
  }
2428
2450
 
2451
+ /*
2452
+ class_decl ::= {`class`} class_name `=` <class_name>
2453
+ | {`class`} class_name <class_decl0>
2454
+ */
2455
+ VALUE parse_class_decl(parserstate *state, position comment_pos, VALUE annotations) {
2456
+ range keyword_range = state->current_token.range;
2457
+ range class_name_range;
2458
+
2459
+ comment_pos = nonnull_pos_or(comment_pos, state->current_token.range.start);
2460
+ VALUE comment = get_comment(state, comment_pos.line);
2461
+
2462
+ parser_advance(state);
2463
+ VALUE class_name = parse_type_name(state, CLASS_NAME, &class_name_range);
2464
+
2465
+ if (state->next_token.type == pEQ) {
2466
+ range eq_range = state->next_token.range;
2467
+ parser_advance(state);
2468
+ parser_advance(state);
2469
+
2470
+ range old_name_range;
2471
+ VALUE old_name = parse_type_name(state, CLASS_NAME, &old_name_range);
2472
+
2473
+ range decl_range;
2474
+ decl_range.start = keyword_range.start;
2475
+ decl_range.end = old_name_range.end;
2476
+
2477
+ VALUE location = rbs_new_location(state->buffer, decl_range);
2478
+ rbs_loc *loc = rbs_check_location(location);
2479
+ rbs_loc_add_required_child(loc, rb_intern("keyword"), keyword_range);
2480
+ rbs_loc_add_required_child(loc, rb_intern("new_name"), class_name_range);
2481
+ rbs_loc_add_required_child(loc, rb_intern("eq"), eq_range);
2482
+ rbs_loc_add_optional_child(loc, rb_intern("old_name"), old_name_range);
2483
+
2484
+ return rbs_ast_decl_class_alias(class_name, old_name, location, comment);
2485
+ } else {
2486
+ return parse_class_decl0(state, keyword_range, class_name, class_name_range, comment, annotations);
2487
+ }
2488
+ }
2489
+
2429
2490
  /*
2430
2491
  nested_decl ::= {<const_decl>}
2431
2492
  | {<class_decl>}
@@ -2501,18 +2562,184 @@ VALUE parse_decl(parserstate *state) {
2501
2562
  }
2502
2563
  }
2503
2564
 
2565
+ /*
2566
+ namespace ::= {} (`::`)? (`tUIDENT` `::`)* `tUIDENT` <`::`>
2567
+ | {} <> (empty -- returns empty namespace)
2568
+ */
2569
+ VALUE parse_namespace(parserstate *state, range *rg) {
2570
+ bool is_absolute = false;
2571
+
2572
+ if (state->next_token.type == pCOLON2) {
2573
+ rg->start = state->next_token.range.start;
2574
+ rg->end = state->next_token.range.end;
2575
+ is_absolute = true;
2576
+
2577
+ parser_advance(state);
2578
+ }
2579
+
2580
+ VALUE path = rb_ary_new();
2581
+
2582
+ while (true) {
2583
+ if (state->next_token.type == tUIDENT && state->next_token2.type == pCOLON2) {
2584
+ rb_ary_push(path, ID2SYM(INTERN_TOKEN(state, state->next_token)));
2585
+ if (null_position_p(rg->start)) {
2586
+ rg->start = state->next_token.range.start;
2587
+ }
2588
+ rg->end = state->next_token2.range.end;
2589
+ parser_advance(state);
2590
+ parser_advance(state);
2591
+ } else {
2592
+ break;
2593
+ }
2594
+ }
2595
+
2596
+ return rbs_namespace(path, is_absolute ? Qtrue : Qfalse);
2597
+ }
2598
+
2599
+ /*
2600
+ use_clauses ::= {} use_clause `,` ... `,` <use_clause>
2601
+
2602
+ use_clause ::= {} namespace <tUIDENT>
2603
+ | {} namespace tUIDENT `as` <tUIDENT>
2604
+ | {} namespace <tSTAR>
2605
+ */
2606
+ void parse_use_clauses(parserstate *state, VALUE clauses) {
2607
+ while (true) {
2608
+ range namespace_range = NULL_RANGE;
2609
+ VALUE namespace = parse_namespace(state, &namespace_range);
2610
+
2611
+ range clause_range = namespace_range;
2612
+
2613
+ switch (state->next_token.type)
2614
+ {
2615
+ case tLIDENT:
2616
+ case tULIDENT:
2617
+ case tUIDENT: {
2618
+ parser_advance(state);
2619
+
2620
+ enum TokenType ident_type = state->current_token.type;
2621
+
2622
+ range type_name_range;
2623
+ if (null_range_p(namespace_range)) {
2624
+ type_name_range = state->current_token.range;
2625
+ } else {
2626
+ type_name_range.start = namespace_range.start;
2627
+ type_name_range.end = state->current_token.range.end;
2628
+ }
2629
+ clause_range = type_name_range;
2630
+
2631
+ VALUE type_name = rbs_type_name(namespace, ID2SYM(INTERN_TOKEN(state, state->current_token)));
2632
+
2633
+ range keyword_range = NULL_RANGE;
2634
+ range new_name_range = NULL_RANGE;
2635
+
2636
+ VALUE new_name = Qnil;
2637
+ if (state->next_token.type == kAS) {
2638
+ parser_advance(state);
2639
+ keyword_range = state->current_token.range;
2640
+
2641
+ if (ident_type == tUIDENT) parser_advance_assert(state, tUIDENT);
2642
+ if (ident_type == tLIDENT) parser_advance_assert(state, tLIDENT);
2643
+ if (ident_type == tULIDENT) parser_advance_assert(state, tULIDENT);
2644
+
2645
+ new_name = ID2SYM(INTERN_TOKEN(state, state->current_token));
2646
+ new_name_range = state->current_token.range;
2647
+ clause_range.end = new_name_range.end;
2648
+ }
2649
+
2650
+ VALUE location = rbs_new_location(state->buffer, clause_range);
2651
+ rbs_loc *loc = rbs_check_location(location);
2652
+ rbs_loc_add_required_child(loc, rb_intern("type_name"), state->current_token.range);
2653
+ if (!null_range_p(keyword_range)) {
2654
+ rbs_loc_add_optional_child(loc, rb_intern("keyword"), keyword_range);
2655
+ }
2656
+ if (!null_range_p(new_name_range)) {
2657
+ rbs_loc_add_optional_child(loc, rb_intern("new_name"), new_name_range);
2658
+ }
2659
+
2660
+ rb_ary_push(clauses, rbs_ast_directives_use_single_clause(type_name, new_name, location));
2661
+
2662
+ break;
2663
+ }
2664
+ case pSTAR:
2665
+ {
2666
+ parser_advance(state);
2667
+
2668
+ range star_range = state->current_token.range;
2669
+ clause_range.end = star_range.end;
2670
+
2671
+ VALUE location = rbs_new_location(state->buffer, clause_range);
2672
+ rbs_loc *loc = rbs_check_location(location);
2673
+ rbs_loc_add_required_child(loc, rb_intern("namespace"), namespace_range);
2674
+ rbs_loc_add_required_child(loc, rb_intern("star"), star_range);
2675
+
2676
+ rb_ary_push(clauses, rbs_ast_directives_use_wildcard_clause(namespace, location));
2677
+
2678
+ break;
2679
+ }
2680
+ default:
2681
+ raise_syntax_error(
2682
+ state,
2683
+ state->next_token,
2684
+ "use clause is expected"
2685
+ );
2686
+ }
2687
+
2688
+ if (state->next_token.type == pCOMMA) {
2689
+ parser_advance(state);
2690
+ } else {
2691
+ break;
2692
+ }
2693
+ }
2694
+
2695
+ return;
2696
+ }
2697
+
2698
+ /*
2699
+ use_directive ::= {} `use` <clauses>
2700
+ */
2701
+ VALUE parse_use_directive(parserstate *state) {
2702
+ if (state->next_token.type == kUSE) {
2703
+ parser_advance(state);
2704
+
2705
+ range keyword_range = state->current_token.range;
2706
+
2707
+ VALUE clauses = rb_ary_new();
2708
+ parse_use_clauses(state, clauses);
2709
+
2710
+ range directive_range = keyword_range;
2711
+ directive_range.end = state->current_token.range.end;
2712
+
2713
+ VALUE location = rbs_new_location(state->buffer, directive_range);
2714
+ rbs_loc *loc = rbs_check_location(location);
2715
+ rbs_loc_add_required_child(loc, rb_intern("keyword"), keyword_range);
2716
+
2717
+ return rbs_ast_directives_use(clauses, location);
2718
+ } else {
2719
+ return Qnil;
2720
+ }
2721
+ }
2722
+
2504
2723
  VALUE parse_signature(parserstate *state) {
2724
+ VALUE dirs = rb_ary_new();
2505
2725
  VALUE decls = rb_ary_new();
2506
2726
 
2727
+ while (state->next_token.type == kUSE) {
2728
+ rb_ary_push(dirs, parse_use_directive(state));
2729
+ }
2730
+
2507
2731
  while (state->next_token.type != pEOF) {
2508
2732
  rb_ary_push(decls, parse_decl(state));
2509
2733
  }
2510
2734
 
2511
- return decls;
2735
+ VALUE ret = rb_ary_new();
2736
+ rb_ary_push(ret, dirs);
2737
+ rb_ary_push(ret, decls);
2738
+ return ret;
2512
2739
  }
2513
2740
 
2514
2741
  static VALUE
2515
- rbsparser_parse_type(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos, VALUE variables, VALUE requires_eof)
2742
+ rbsparser_parse_type(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos, VALUE variables)
2516
2743
  {
2517
2744
  parserstate *parser = alloc_parser(buffer, FIX2INT(start_pos), FIX2INT(end_pos), variables);
2518
2745
 
@@ -2522,17 +2749,13 @@ rbsparser_parse_type(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos, V
2522
2749
 
2523
2750
  VALUE type = parse_type(parser);
2524
2751
 
2525
- if (RTEST(requires_eof)) {
2526
- parser_advance_assert(parser, pEOF);
2527
- }
2528
-
2529
2752
  free_parser(parser);
2530
2753
 
2531
2754
  return type;
2532
2755
  }
2533
2756
 
2534
2757
  static VALUE
2535
- rbsparser_parse_method_type(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos, VALUE variables, VALUE requires_eof)
2758
+ rbsparser_parse_method_type(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos, VALUE variables)
2536
2759
  {
2537
2760
  parserstate *parser = alloc_parser(buffer, FIX2INT(start_pos), FIX2INT(end_pos), variables);
2538
2761
 
@@ -2542,10 +2765,6 @@ rbsparser_parse_method_type(VALUE self, VALUE buffer, VALUE start_pos, VALUE end
2542
2765
 
2543
2766
  VALUE method_type = parse_method_type(parser);
2544
2767
 
2545
- if (RTEST(requires_eof)) {
2546
- parser_advance_assert(parser, pEOF);
2547
- }
2548
-
2549
2768
  free_parser(parser);
2550
2769
 
2551
2770
  return method_type;
@@ -2555,15 +2774,15 @@ static VALUE
2555
2774
  rbsparser_parse_signature(VALUE self, VALUE buffer, VALUE end_pos)
2556
2775
  {
2557
2776
  parserstate *parser = alloc_parser(buffer, 0, FIX2INT(end_pos), Qnil);
2558
- VALUE signature = parse_signature(parser);
2777
+ VALUE pair = parse_signature(parser);
2559
2778
  free_parser(parser);
2560
2779
 
2561
- return signature;
2780
+ return pair;
2562
2781
  }
2563
2782
 
2564
2783
  void rbs__init_parser(void) {
2565
2784
  RBS_Parser = rb_define_class_under(RBS, "Parser", rb_cObject);
2566
- rb_define_singleton_method(RBS_Parser, "_parse_type", rbsparser_parse_type, 5);
2567
- rb_define_singleton_method(RBS_Parser, "_parse_method_type", rbsparser_parse_method_type, 5);
2785
+ rb_define_singleton_method(RBS_Parser, "_parse_type", rbsparser_parse_type, 4);
2786
+ rb_define_singleton_method(RBS_Parser, "_parse_method_type", rbsparser_parse_method_type, 4);
2568
2787
  rb_define_singleton_method(RBS_Parser, "_parse_signature", rbsparser_parse_signature, 2);
2569
2788
  }
@@ -340,7 +340,7 @@ VALUE rbs_ast_decl_global(VALUE name, VALUE type, VALUE location, VALUE comment)
340
340
  );
341
341
  }
342
342
 
343
- VALUE rbs_ast_decl_alias(VALUE name, VALUE type_params, VALUE type, VALUE annotations, VALUE location, VALUE comment) {
343
+ VALUE rbs_ast_decl_type_alias(VALUE name, VALUE type_params, VALUE type, VALUE annotations, VALUE location, VALUE comment) {
344
344
  VALUE args = rb_hash_new();
345
345
  rb_hash_aset(args, ID2SYM(rb_intern("name")), name);
346
346
  rb_hash_aset(args, ID2SYM(rb_intern("type_params")), type_params);
@@ -350,7 +350,7 @@ VALUE rbs_ast_decl_alias(VALUE name, VALUE type_params, VALUE type, VALUE annota
350
350
  rb_hash_aset(args, ID2SYM(rb_intern("comment")), comment);
351
351
 
352
352
  return CLASS_NEW_INSTANCE(
353
- RBS_AST_Declarations_Alias,
353
+ RBS_AST_Declarations_TypeAlias,
354
354
  1,
355
355
  &args
356
356
  );
@@ -402,6 +402,34 @@ VALUE rbs_ast_decl_module(VALUE name, VALUE type_params, VALUE self_types, VALUE
402
402
  );
403
403
  }
404
404
 
405
+ VALUE rbs_ast_decl_class_alias(VALUE new_name, VALUE old_name, VALUE location, VALUE comment) {
406
+ VALUE args = rb_hash_new();
407
+ rb_hash_aset(args, ID2SYM(rb_intern("new_name")), new_name);
408
+ rb_hash_aset(args, ID2SYM(rb_intern("old_name")), old_name);
409
+ rb_hash_aset(args, ID2SYM(rb_intern("location")), location);
410
+ rb_hash_aset(args, ID2SYM(rb_intern("comment")), comment);
411
+
412
+ return CLASS_NEW_INSTANCE(
413
+ RBS_AST_Declarations_ClassAlias,
414
+ 1,
415
+ &args
416
+ );
417
+ }
418
+
419
+ VALUE rbs_ast_decl_module_alias(VALUE new_name, VALUE old_name, VALUE location, VALUE comment) {
420
+ VALUE args = rb_hash_new();
421
+ rb_hash_aset(args, ID2SYM(rb_intern("new_name")), new_name);
422
+ rb_hash_aset(args, ID2SYM(rb_intern("old_name")), old_name);
423
+ rb_hash_aset(args, ID2SYM(rb_intern("location")), location);
424
+ rb_hash_aset(args, ID2SYM(rb_intern("comment")), comment);
425
+
426
+ return CLASS_NEW_INSTANCE(
427
+ RBS_AST_Declarations_ModuleAlias,
428
+ 1,
429
+ &args
430
+ );
431
+ }
432
+
405
433
  VALUE rbs_ast_members_method_definition_overload(VALUE annotations, VALUE method_type) {
406
434
  VALUE args = rb_hash_new();
407
435
  rb_hash_aset(args, ID2SYM(rb_intern("annotations")), annotations);
@@ -535,3 +563,29 @@ VALUE rbs_ast_decl_class(VALUE name, VALUE type_params, VALUE super_class, VALUE
535
563
  &kwargs
536
564
  );
537
565
  }
566
+
567
+ VALUE rbs_ast_directives_use(VALUE clauses, VALUE location) {
568
+ VALUE kwargs = rb_hash_new();
569
+ rb_hash_aset(kwargs, ID2SYM(rb_intern("clauses")), clauses);
570
+ rb_hash_aset(kwargs, ID2SYM(rb_intern("location")), location);
571
+
572
+ return CLASS_NEW_INSTANCE(RBS_AST_Directives_Use, 1, &kwargs);
573
+ }
574
+
575
+ VALUE rbs_ast_directives_use_single_clause(VALUE type_name, VALUE new_name, VALUE location) {
576
+ VALUE kwargs = rb_hash_new();
577
+ rb_hash_aset(kwargs, ID2SYM(rb_intern("type_name")), type_name);
578
+ rb_hash_aset(kwargs, ID2SYM(rb_intern("new_name")), new_name);
579
+ rb_hash_aset(kwargs, ID2SYM(rb_intern("location")), location);
580
+
581
+ return CLASS_NEW_INSTANCE(RBS_AST_Directives_Use_SingleClause, 1, &kwargs);
582
+ }
583
+
584
+ VALUE rbs_ast_directives_use_wildcard_clause(VALUE namespace, VALUE location) {
585
+ VALUE kwargs = rb_hash_new();
586
+ rb_hash_aset(kwargs, ID2SYM(rb_intern("namespace")), namespace);
587
+ rb_hash_aset(kwargs, ID2SYM(rb_intern("location")), location);
588
+
589
+ return CLASS_NEW_INSTANCE(RBS_AST_Directives_Use_WildcardClause, 1, &kwargs);
590
+ }
591
+
@@ -7,7 +7,7 @@ VALUE rbs_alias(VALUE typename, VALUE args, VALUE location);
7
7
  VALUE rbs_ast_annotation(VALUE string, VALUE location);
8
8
  VALUE rbs_ast_comment(VALUE string, VALUE location);
9
9
  VALUE rbs_ast_type_param(VALUE name, VALUE variance, bool unchecked, VALUE upper_bound, VALUE location);
10
- VALUE rbs_ast_decl_alias(VALUE name, VALUE type_params, VALUE type, VALUE annotations, VALUE location, VALUE comment);
10
+ VALUE rbs_ast_decl_type_alias(VALUE name, VALUE type_params, VALUE type, VALUE annotations, VALUE location, VALUE comment);
11
11
  VALUE rbs_ast_decl_class_super(VALUE name, VALUE args, VALUE location);
12
12
  VALUE rbs_ast_decl_class(VALUE name, VALUE type_params, VALUE super_class, VALUE members, VALUE annotations, VALUE location, VALUE comment);
13
13
  VALUE rbs_ast_decl_constant(VALUE name, VALUE type, VALUE location, VALUE comment);
@@ -15,6 +15,8 @@ VALUE rbs_ast_decl_global(VALUE name, VALUE type, VALUE location, VALUE comment)
15
15
  VALUE rbs_ast_decl_interface(VALUE name, VALUE type_params, VALUE members, VALUE annotations, VALUE location, VALUE comment);
16
16
  VALUE rbs_ast_decl_module_self(VALUE name, VALUE args, VALUE location);
17
17
  VALUE rbs_ast_decl_module(VALUE name, VALUE type_params, VALUE self_types, VALUE members, VALUE annotations, VALUE location, VALUE comment);
18
+ VALUE rbs_ast_decl_module_alias(VALUE new_name, VALUE old_name, VALUE location, VALUE comment);
19
+ VALUE rbs_ast_decl_class_alias(VALUE new_name, VALUE old_name, VALUE location, VALUE comment);
18
20
  VALUE rbs_ast_members_alias(VALUE new_name, VALUE old_name, VALUE kind, VALUE annotations, VALUE location, VALUE comment);
19
21
  VALUE rbs_ast_members_attribute(VALUE klass, VALUE name, VALUE type, VALUE ivar_name, VALUE kind, VALUE annotations, VALUE location, VALUE comment, VALUE visibility);
20
22
  VALUE rbs_ast_members_method_definition(VALUE name, VALUE kind, VALUE overloads, VALUE annotations, VALUE location, VALUE comment, VALUE overloading, VALUE visibility);
@@ -41,4 +43,8 @@ VALUE rbs_type_name(VALUE namespace, VALUE name);
41
43
  VALUE rbs_union(VALUE types, VALUE location);
42
44
  VALUE rbs_variable(VALUE name, VALUE location);
43
45
 
46
+ VALUE rbs_ast_directives_use(VALUE clauses, VALUE location);
47
+ VALUE rbs_ast_directives_use_single_clause(VALUE type_name, VALUE new_name, VALUE location);
48
+ VALUE rbs_ast_directives_use_wildcard_clause(VALUE namespace, VALUE location);
49
+
44
50
  #endif
@@ -16,7 +16,7 @@ module RBS
16
16
  def annotate_file(path, preserve:)
17
17
  content = path.read()
18
18
 
19
- decls = Parser.parse_signature(content)
19
+ _, _, decls = Parser.parse_signature(content)
20
20
 
21
21
  annotate_decls(decls)
22
22
 
@@ -265,7 +265,7 @@ module RBS
265
265
  end
266
266
  end
267
267
 
268
- class Alias < Base
268
+ class TypeAlias < Base
269
269
  attr_reader :name
270
270
  attr_reader :type_params
271
271
  attr_reader :type
@@ -283,7 +283,7 @@ module RBS
283
283
  end
284
284
 
285
285
  def ==(other)
286
- other.is_a?(Alias) &&
286
+ other.is_a?(TypeAlias) &&
287
287
  other.name == name &&
288
288
  other.type_params == type_params &&
289
289
  other.type == type
@@ -379,6 +379,53 @@ module RBS
379
379
  }.to_json(state)
380
380
  end
381
381
  end
382
+
383
+ class AliasDecl < Base
384
+ attr_reader :new_name, :old_name, :location, :comment
385
+
386
+ def initialize(new_name:, old_name:, location:, comment:)
387
+ @new_name = new_name
388
+ @old_name = old_name
389
+ @location = location
390
+ @comment = comment
391
+ end
392
+
393
+ def ==(other)
394
+ other.is_a?(self.class) &&
395
+ other.new_name == new_name &&
396
+ other.old_name == old_name
397
+ end
398
+
399
+ alias eql? ==
400
+
401
+ def hash
402
+ self.class.hash ^ new_name.hash ^ old_name.hash
403
+ end
404
+ end
405
+
406
+ class ClassAlias < AliasDecl
407
+ def to_json(state = _ = nil)
408
+ {
409
+ declaration: :class_alias,
410
+ new_name: new_name,
411
+ old_name: old_name,
412
+ location: location,
413
+ comment: comment
414
+ }.to_json(state)
415
+ end
416
+ end
417
+
418
+ class ModuleAlias < AliasDecl
419
+ def to_json(state = _ = nil)
420
+ {
421
+ declaration: :module_alias,
422
+ new_name: new_name,
423
+ old_name: old_name,
424
+ location: location,
425
+ comment: comment
426
+ }.to_json(state)
427
+ end
428
+ end
382
429
  end
383
430
  end
384
431
  end