rbs 3.4.4 → 3.5.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (90) hide show
  1. checksums.yaml +4 -4
  2. data/.github/dependabot.yml +12 -4
  3. data/.github/workflows/dependabot.yml +26 -0
  4. data/.github/workflows/ruby.yml +14 -1
  5. data/CHANGELOG.md +87 -1
  6. data/README.md +5 -3
  7. data/Rakefile +20 -3
  8. data/Steepfile +1 -1
  9. data/core/enumerator.rbs +1 -1
  10. data/core/float.rbs +2 -1
  11. data/core/gc.rbs +272 -150
  12. data/core/integer.rbs +6 -4
  13. data/core/io/wait.rbs +4 -4
  14. data/core/io.rbs +10 -3
  15. data/core/kernel.rbs +8 -7
  16. data/core/module.rbs +17 -4
  17. data/core/proc.rbs +1 -1
  18. data/core/range.rbs +2 -2
  19. data/core/rational.rbs +2 -1
  20. data/core/regexp.rbs +101 -90
  21. data/core/ruby_vm.rbs +107 -103
  22. data/core/rubygems/rubygems.rbs +1 -1
  23. data/core/set.rbs +13 -7
  24. data/core/string.rbs +3 -3
  25. data/core/symbol.rbs +2 -1
  26. data/core/thread.rbs +1 -1
  27. data/core/time.rbs +24 -4
  28. data/docs/architecture.md +110 -0
  29. data/docs/gem.md +0 -1
  30. data/docs/syntax.md +5 -1
  31. data/ext/rbs_extension/constants.c +2 -0
  32. data/ext/rbs_extension/constants.h +1 -0
  33. data/ext/rbs_extension/lexer.c +1338 -1341
  34. data/ext/rbs_extension/lexer.h +2 -0
  35. data/ext/rbs_extension/lexer.re +2 -3
  36. data/ext/rbs_extension/lexstate.c +5 -1
  37. data/ext/rbs_extension/location.c +80 -70
  38. data/ext/rbs_extension/location.h +25 -5
  39. data/ext/rbs_extension/parser.c +149 -43
  40. data/ext/rbs_extension/parserstate.c +12 -1
  41. data/ext/rbs_extension/parserstate.h +9 -0
  42. data/ext/rbs_extension/ruby_objs.c +13 -3
  43. data/ext/rbs_extension/ruby_objs.h +1 -0
  44. data/lib/rbs/cli/validate.rb +2 -2
  45. data/lib/rbs/cli.rb +4 -6
  46. data/lib/rbs/collection/config.rb +1 -1
  47. data/lib/rbs/collection/sources/git.rb +1 -6
  48. data/lib/rbs/definition_builder/method_builder.rb +1 -1
  49. data/lib/rbs/definition_builder.rb +8 -8
  50. data/lib/rbs/diff.rb +1 -1
  51. data/lib/rbs/environment_loader.rb +2 -1
  52. data/lib/rbs/errors.rb +0 -14
  53. data/lib/rbs/location_aux.rb +6 -1
  54. data/lib/rbs/parser/lex_result.rb +15 -0
  55. data/lib/rbs/parser/token.rb +23 -0
  56. data/lib/rbs/parser_aux.rb +12 -5
  57. data/lib/rbs/prototype/helpers.rb +22 -12
  58. data/lib/rbs/prototype/rb.rb +38 -4
  59. data/lib/rbs/prototype/rbi.rb +30 -20
  60. data/lib/rbs/test/errors.rb +19 -14
  61. data/lib/rbs/test/tester.rb +1 -1
  62. data/lib/rbs/test/type_check.rb +95 -16
  63. data/lib/rbs/types.rb +112 -13
  64. data/lib/rbs/unit_test/spy.rb +1 -1
  65. data/lib/rbs/version.rb +1 -1
  66. data/rbs.gemspec +7 -2
  67. data/sig/environment_loader.rbs +1 -1
  68. data/sig/errors.rbs +1 -1
  69. data/sig/manifest.yaml +0 -1
  70. data/sig/method_types.rbs +3 -3
  71. data/sig/parser.rbs +28 -0
  72. data/sig/prototype/helpers.rbs +4 -0
  73. data/sig/prototype/rbi.rbs +2 -0
  74. data/sig/types.rbs +54 -4
  75. data/sig/variance_calculator.rbs +2 -2
  76. data/stdlib/csv/0/csv.rbs +4 -1
  77. data/stdlib/fileutils/0/fileutils.rbs +1 -1
  78. data/stdlib/net-http/0/net-http.rbs +29 -27
  79. data/stdlib/socket/0/socket.rbs +2 -2
  80. data/stdlib/timeout/0/timeout.rbs +6 -0
  81. data/stdlib/uri/0/generic.rbs +2 -2
  82. data/stdlib/uri/0/http.rbs +2 -2
  83. data/stdlib/uri/0/mailto.rbs +84 -0
  84. metadata +7 -9
  85. data/Gemfile +0 -30
  86. data/Gemfile.lock +0 -117
  87. data/lib/rbs/parser_compat/lexer_error.rb +0 -6
  88. data/lib/rbs/parser_compat/located_value.rb +0 -7
  89. data/lib/rbs/parser_compat/semantics_error.rb +0 -6
  90. data/lib/rbs/parser_compat/syntax_error.rb +0 -6
@@ -52,6 +52,10 @@ typedef struct {
52
52
  VALUE rest_keywords;
53
53
  } method_params;
54
54
 
55
+ static bool rbs_is_untyped_params(method_params *params) {
56
+ return NIL_P(params->required_positionals);
57
+ }
58
+
55
59
  // /**
56
60
  // * Returns RBS::Location object of `current_token` of a parser state.
57
61
  // *
@@ -78,7 +82,7 @@ static VALUE string_of_loc(parserstate *state, position start, position end) {
78
82
  }
79
83
 
80
84
  /**
81
- * Raises RuntimeError with "Unexpected error " messsage.
85
+ * Raises RuntimeError with "Unexpected error " message.
82
86
  * */
83
87
  static NORETURN(void) rbs_abort(void) {
84
88
  rb_raise(
@@ -265,6 +269,7 @@ static VALUE parse_function_param(parserstate *state) {
265
269
 
266
270
  VALUE location = rbs_new_location(state->buffer, param_range);
267
271
  rbs_loc *loc = rbs_check_location(location);
272
+ rbs_loc_alloc_children(loc, 1);
268
273
  rbs_loc_add_optional_child(loc, rb_intern("name"), NULL_RANGE);
269
274
 
270
275
  return rbs_function_param(type, Qnil, location);
@@ -287,6 +292,7 @@ static VALUE parse_function_param(parserstate *state) {
287
292
  VALUE name = rb_to_symbol(rbs_unquote_string(state, state->current_token.range, 0));
288
293
  VALUE location = rbs_new_location(state->buffer, param_range);
289
294
  rbs_loc *loc = rbs_check_location(location);
295
+ rbs_loc_alloc_children(loc, 1);
290
296
  rbs_loc_add_optional_child(loc, rb_intern("name"), name_range);
291
297
 
292
298
  return rbs_function_param(type, name, location);
@@ -360,6 +366,7 @@ static bool is_keyword(parserstate *state) {
360
366
 
361
367
  /*
362
368
  params ::= {} `)`
369
+ | {} `?` `)` -- Untyped function params (assign params.required = nil)
363
370
  | <required_params> `)`
364
371
  | <required_params> `,` `)`
365
372
 
@@ -387,6 +394,11 @@ static bool is_keyword(parserstate *state) {
387
394
  | {} `**` <function_param>
388
395
  */
389
396
  static void parse_params(parserstate *state, method_params *params) {
397
+ if (state->next_token.type == pQUESTION && state->next_token2.type == pRPAREN) {
398
+ params->required_positionals = Qnil;
399
+ parser_advance(state);
400
+ return;
401
+ }
390
402
  if (state->next_token.type == pRPAREN) {
391
403
  return;
392
404
  }
@@ -611,6 +623,13 @@ static void parse_function(parserstate *state, VALUE *function, VALUE *block, VA
611
623
  parser_advance_assert(state, pRPAREN);
612
624
  }
613
625
 
626
+ // Untyped method parameter means it cannot have block
627
+ if (rbs_is_untyped_params(&params)) {
628
+ if (state->next_token.type != pARROW) {
629
+ raise_syntax_error(state, state->next_token2, "A method type with untyped method parameter cannot have block");
630
+ }
631
+ }
632
+
614
633
  // Passing NULL to function_self_type means the function itself doesn't accept self type binding. (== method type)
615
634
  if (function_self_type) {
616
635
  *function_self_type = parse_self_type_binding(state);
@@ -639,8 +658,11 @@ static void parse_function(parserstate *state, VALUE *function, VALUE *block, VA
639
658
  parser_advance_assert(state, pARROW);
640
659
  VALUE block_return_type = parse_optional(state);
641
660
 
642
- *block = rbs_block(
643
- rbs_function(
661
+ VALUE block_function = Qnil;
662
+ if (rbs_is_untyped_params(&block_params)) {
663
+ block_function = rbs_untyped_function(block_return_type);
664
+ } else {
665
+ block_function = rbs_function(
644
666
  block_params.required_positionals,
645
667
  block_params.optional_positionals,
646
668
  block_params.rest_positionals,
@@ -649,10 +671,10 @@ static void parse_function(parserstate *state, VALUE *function, VALUE *block, VA
649
671
  block_params.optional_keywords,
650
672
  block_params.rest_keywords,
651
673
  block_return_type
652
- ),
653
- required,
654
- block_self_type
655
- );
674
+ );
675
+ }
676
+
677
+ *block = rbs_block(block_function, required, block_self_type);
656
678
 
657
679
  parser_advance_assert(state, pRBRACE);
658
680
  }
@@ -660,16 +682,20 @@ static void parse_function(parserstate *state, VALUE *function, VALUE *block, VA
660
682
  parser_advance_assert(state, pARROW);
661
683
  VALUE type = parse_optional(state);
662
684
 
663
- *function = rbs_function(
664
- params.required_positionals,
665
- params.optional_positionals,
666
- params.rest_positionals,
667
- params.trailing_positionals,
668
- params.required_keywords,
669
- params.optional_keywords,
670
- params.rest_keywords,
671
- type
672
- );
685
+ if (rbs_is_untyped_params(&params)) {
686
+ *function = rbs_untyped_function(type);
687
+ } else {
688
+ *function = rbs_function(
689
+ params.required_positionals,
690
+ params.optional_positionals,
691
+ params.rest_positionals,
692
+ params.trailing_positionals,
693
+ params.required_keywords,
694
+ params.optional_keywords,
695
+ params.rest_keywords,
696
+ type
697
+ );
698
+ }
673
699
  }
674
700
 
675
701
  /*
@@ -698,15 +724,22 @@ static VALUE parse_proc_type(parserstate *state) {
698
724
  | {} literal_type `=>` <type>
699
725
  */
700
726
  VALUE parse_record_attributes(parserstate *state) {
701
- VALUE hash = rb_hash_new();
727
+ VALUE fields = rb_hash_new();
702
728
 
703
729
  if (state->next_token.type == pRBRACE) {
704
- return hash;
730
+ return fields;
705
731
  }
706
732
 
707
733
  while (true) {
708
- VALUE key;
709
- VALUE type;
734
+ VALUE key, type,
735
+ value = rb_ary_new(),
736
+ required = Qtrue;
737
+
738
+ if (state->next_token.type == pQUESTION) {
739
+ // { ?foo: type } syntax
740
+ required = Qfalse;
741
+ parser_advance(state);
742
+ }
710
743
 
711
744
  if (is_keyword(state)) {
712
745
  // { foo: type } syntax
@@ -735,7 +768,9 @@ VALUE parse_record_attributes(parserstate *state) {
735
768
  parser_advance_assert(state, pFATARROW);
736
769
  }
737
770
  type = parse_type(state);
738
- rb_hash_aset(hash, key, type);
771
+ rb_ary_push(value, type);
772
+ rb_ary_push(value, required);
773
+ rb_hash_aset(fields, key, value);
739
774
 
740
775
  if (parser_advance_if(state, pCOMMA)) {
741
776
  if (state->next_token.type == pRBRACE) {
@@ -745,8 +780,7 @@ VALUE parse_record_attributes(parserstate *state) {
745
780
  break;
746
781
  }
747
782
  }
748
-
749
- return hash;
783
+ return fields;
750
784
  }
751
785
 
752
786
  /*
@@ -832,6 +866,7 @@ static VALUE parse_instance_type(parserstate *state, bool parse_alias) {
832
866
 
833
867
  VALUE location = rbs_new_location(state->buffer, type_range);
834
868
  rbs_loc *loc = rbs_check_location(location);
869
+ rbs_loc_alloc_children(loc, 2);
835
870
  rbs_loc_add_required_child(loc, rb_intern("name"), name_range);
836
871
  rbs_loc_add_optional_child(loc, rb_intern("args"), args_range);
837
872
 
@@ -866,6 +901,7 @@ static VALUE parse_singleton_type(parserstate *state) {
866
901
 
867
902
  VALUE location = rbs_new_location(state->buffer, type_range);
868
903
  rbs_loc *loc = rbs_check_location(location);
904
+ rbs_loc_alloc_children(loc, 1);
869
905
  rbs_loc_add_required_child(loc, rb_intern("name"), name_range);
870
906
 
871
907
  return rbs_class_singleton(typename, location);
@@ -1121,6 +1157,7 @@ VALUE parse_type_params(parserstate *state, range *rg, bool module_type_params)
1121
1157
 
1122
1158
  VALUE location = rbs_new_location(state->buffer, param_range);
1123
1159
  rbs_loc *loc = rbs_check_location(location);
1160
+ rbs_loc_alloc_children(loc, 4);
1124
1161
  rbs_loc_add_required_child(loc, rb_intern("name"), name_range);
1125
1162
  rbs_loc_add_optional_child(loc, rb_intern("variance"), variance_range);
1126
1163
  rbs_loc_add_optional_child(loc, rb_intern("unchecked"), unchecked_range);
@@ -1181,6 +1218,7 @@ VALUE parse_method_type(parserstate *state) {
1181
1218
 
1182
1219
  VALUE location = rbs_new_location(state->buffer, rg);
1183
1220
  rbs_loc *loc = rbs_check_location(location);
1221
+ rbs_loc_alloc_children(loc, 2);
1184
1222
  rbs_loc_add_required_child(loc, rb_intern("type"), type_range);
1185
1223
  rbs_loc_add_optional_child(loc, rb_intern("type_params"), params_range);
1186
1224
 
@@ -1220,6 +1258,7 @@ VALUE parse_global_decl(parserstate *state) {
1220
1258
 
1221
1259
  location = rbs_new_location(state->buffer, decl_range);
1222
1260
  loc = rbs_check_location(location);
1261
+ rbs_loc_alloc_children(loc, 2);
1223
1262
  rbs_loc_add_required_child(loc, rb_intern("name"), name_range);
1224
1263
  rbs_loc_add_required_child(loc, rb_intern("colon"), colon_range);
1225
1264
 
@@ -1253,6 +1292,7 @@ VALUE parse_const_decl(parserstate *state) {
1253
1292
 
1254
1293
  location = rbs_new_location(state->buffer, decl_range);
1255
1294
  loc = rbs_check_location(location);
1295
+ rbs_loc_alloc_children(loc, 2);
1256
1296
  rbs_loc_add_required_child(loc, rb_intern("name"), name_range);
1257
1297
  rbs_loc_add_required_child(loc, rb_intern("colon"), colon_range);
1258
1298
 
@@ -1286,6 +1326,7 @@ VALUE parse_type_decl(parserstate *state, position comment_pos, VALUE annotation
1286
1326
 
1287
1327
  VALUE location = rbs_new_location(state->buffer, decl_range);
1288
1328
  rbs_loc *loc = rbs_check_location(location);
1329
+ rbs_loc_alloc_children(loc, 4);
1289
1330
  rbs_loc_add_required_child(loc, rb_intern("keyword"), keyword_range);
1290
1331
  rbs_loc_add_required_child(loc, rb_intern("name"), name_range);
1291
1332
  rbs_loc_add_optional_child(loc, rb_intern("type_params"), params_range);
@@ -1627,6 +1668,7 @@ VALUE parse_member_def(parserstate *state, bool instance_only, bool accept_overl
1627
1668
 
1628
1669
  VALUE location = rbs_new_location(state->buffer, member_range);
1629
1670
  rbs_loc *loc = rbs_check_location(location);
1671
+ rbs_loc_alloc_children(loc, 5);
1630
1672
  rbs_loc_add_required_child(loc, rb_intern("keyword"), keyword_range);
1631
1673
  rbs_loc_add_required_child(loc, rb_intern("name"), name_range);
1632
1674
  rbs_loc_add_optional_child(loc, rb_intern("kind"), kind_range);
@@ -1731,6 +1773,7 @@ VALUE parse_mixin_member(parserstate *state, bool from_interface, position comme
1731
1773
 
1732
1774
  VALUE location = rbs_new_location(state->buffer, member_range);
1733
1775
  rbs_loc *loc = rbs_check_location(location);
1776
+ rbs_loc_alloc_children(loc, 3);
1734
1777
  rbs_loc_add_required_child(loc, rb_intern("name"), name_range);
1735
1778
  rbs_loc_add_required_child(loc, rb_intern("keyword"), keyword_range);
1736
1779
  rbs_loc_add_optional_child(loc, rb_intern("args"), args_range);
@@ -1794,6 +1837,7 @@ VALUE parse_alias_member(parserstate *state, bool instance_only, position commen
1794
1837
  member_range.end = state->current_token.range.end;
1795
1838
  VALUE location = rbs_new_location(state->buffer, member_range);
1796
1839
  rbs_loc *loc = rbs_check_location(location);
1840
+ rbs_loc_alloc_children(loc, 5);
1797
1841
  rbs_loc_add_required_child(loc, rb_intern("keyword"), keyword_range);
1798
1842
  rbs_loc_add_required_child(loc, rb_intern("new_name"), new_name_range);
1799
1843
  rbs_loc_add_required_child(loc, rb_intern("old_name"), old_name_range);
@@ -1897,6 +1941,7 @@ VALUE parse_variable_member(parserstate *state, position comment_pos, VALUE anno
1897
1941
 
1898
1942
  location = rbs_new_location(state->buffer, member_range);
1899
1943
  rbs_loc *loc = rbs_check_location(location);
1944
+ rbs_loc_alloc_children(loc, 3);
1900
1945
  rbs_loc_add_required_child(loc, rb_intern("name"), name_range);
1901
1946
  rbs_loc_add_required_child(loc, rb_intern("colon"), colon_range);
1902
1947
  rbs_loc_add_optional_child(loc, rb_intern("kind"), kind_range);
@@ -2041,6 +2086,7 @@ VALUE parse_attribute_member(parserstate *state, position comment_pos, VALUE ann
2041
2086
 
2042
2087
  location = rbs_new_location(state->buffer, member_range);
2043
2088
  loc = rbs_check_location(location);
2089
+ rbs_loc_alloc_children(loc, 7);
2044
2090
  rbs_loc_add_required_child(loc, rb_intern("keyword"), keyword_range);
2045
2091
  rbs_loc_add_required_child(loc, rb_intern("name"), name_range);
2046
2092
  rbs_loc_add_required_child(loc, rb_intern("colon"), colon_range);
@@ -2138,6 +2184,7 @@ VALUE parse_interface_decl(parserstate *state, position comment_pos, VALUE annot
2138
2184
 
2139
2185
  VALUE location = rbs_new_location(state->buffer, member_range);
2140
2186
  rbs_loc *loc = rbs_check_location(location);
2187
+ rbs_loc_alloc_children(loc, 4);
2141
2188
  rbs_loc_add_required_child(loc, rb_intern("keyword"), keyword_range);
2142
2189
  rbs_loc_add_required_child(loc, rb_intern("name"), name_range);
2143
2190
  rbs_loc_add_required_child(loc, rb_intern("end"), end_range);
@@ -2183,6 +2230,7 @@ void parse_module_self_types(parserstate *state, VALUE array) {
2183
2230
 
2184
2231
  VALUE location = rbs_new_location(state->buffer, self_range);
2185
2232
  rbs_loc *loc = rbs_check_location(location);
2233
+ rbs_loc_alloc_children(loc, 2);
2186
2234
  rbs_loc_add_required_child(loc, rb_intern("name"), name_range);
2187
2235
  rbs_loc_add_optional_child(loc, rb_intern("args"), args_range);
2188
2236
 
@@ -2319,6 +2367,7 @@ VALUE parse_module_decl0(parserstate *state, range keyword_range, VALUE module_n
2319
2367
 
2320
2368
  VALUE location = rbs_new_location(state->buffer, decl_range);
2321
2369
  rbs_loc *loc = rbs_check_location(location);
2370
+ rbs_loc_alloc_children(loc, 6);
2322
2371
  rbs_loc_add_required_child(loc, rb_intern("keyword"), keyword_range);
2323
2372
  rbs_loc_add_required_child(loc, rb_intern("name"), name_range);
2324
2373
  rbs_loc_add_required_child(loc, rb_intern("end"), end_range);
@@ -2368,6 +2417,7 @@ VALUE parse_module_decl(parserstate *state, position comment_pos, VALUE annotati
2368
2417
 
2369
2418
  VALUE location = rbs_new_location(state->buffer, decl_range);
2370
2419
  rbs_loc *loc = rbs_check_location(location);
2420
+ rbs_loc_alloc_children(loc, 4);
2371
2421
  rbs_loc_add_required_child(loc, rb_intern("keyword"), keyword_range);
2372
2422
  rbs_loc_add_required_child(loc, rb_intern("new_name"), module_name_range);
2373
2423
  rbs_loc_add_required_child(loc, rb_intern("eq"), eq_range);
@@ -2404,6 +2454,7 @@ VALUE parse_class_decl_super(parserstate *state, range *lt_range) {
2404
2454
 
2405
2455
  location = rbs_new_location(state->buffer, super_range);
2406
2456
  loc = rbs_check_location(location);
2457
+ rbs_loc_alloc_children(loc, 2);
2407
2458
  rbs_loc_add_required_child(loc, rb_intern("name"), name_range);
2408
2459
  rbs_loc_add_optional_child(loc, rb_intern("args"), args_range);
2409
2460
 
@@ -2446,6 +2497,7 @@ VALUE parse_class_decl0(parserstate *state, range keyword_range, VALUE name, ran
2446
2497
 
2447
2498
  location = rbs_new_location(state->buffer, decl_range);
2448
2499
  loc = rbs_check_location(location);
2500
+ rbs_loc_alloc_children(loc, 5);
2449
2501
  rbs_loc_add_required_child(loc, rb_intern("keyword"), keyword_range);
2450
2502
  rbs_loc_add_required_child(loc, rb_intern("name"), name_range);
2451
2503
  rbs_loc_add_required_child(loc, rb_intern("end"), end_range);
@@ -2491,6 +2543,7 @@ VALUE parse_class_decl(parserstate *state, position comment_pos, VALUE annotatio
2491
2543
 
2492
2544
  VALUE location = rbs_new_location(state->buffer, decl_range);
2493
2545
  rbs_loc *loc = rbs_check_location(location);
2546
+ rbs_loc_alloc_children(loc, 4);
2494
2547
  rbs_loc_add_required_child(loc, rb_intern("keyword"), keyword_range);
2495
2548
  rbs_loc_add_required_child(loc, rb_intern("new_name"), class_name_range);
2496
2549
  rbs_loc_add_required_child(loc, rb_intern("eq"), eq_range);
@@ -2664,6 +2717,7 @@ void parse_use_clauses(parserstate *state, VALUE clauses) {
2664
2717
 
2665
2718
  VALUE location = rbs_new_location(state->buffer, clause_range);
2666
2719
  rbs_loc *loc = rbs_check_location(location);
2720
+ rbs_loc_alloc_children(loc, 3);
2667
2721
  rbs_loc_add_required_child(loc, rb_intern("type_name"), type_name_range);
2668
2722
  rbs_loc_add_optional_child(loc, rb_intern("keyword"), keyword_range);
2669
2723
  rbs_loc_add_optional_child(loc, rb_intern("new_name"), new_name_range);
@@ -2681,6 +2735,7 @@ void parse_use_clauses(parserstate *state, VALUE clauses) {
2681
2735
 
2682
2736
  VALUE location = rbs_new_location(state->buffer, clause_range);
2683
2737
  rbs_loc *loc = rbs_check_location(location);
2738
+ rbs_loc_alloc_children(loc, 2);
2684
2739
  rbs_loc_add_required_child(loc, rb_intern("namespace"), namespace_range);
2685
2740
  rbs_loc_add_required_child(loc, rb_intern("star"), star_range);
2686
2741
 
@@ -2723,6 +2778,7 @@ VALUE parse_use_directive(parserstate *state) {
2723
2778
 
2724
2779
  VALUE location = rbs_new_location(state->buffer, directive_range);
2725
2780
  rbs_loc *loc = rbs_check_location(location);
2781
+ rbs_loc_alloc_children(loc, 1);
2726
2782
  rbs_loc_add_required_child(loc, rb_intern("keyword"), keyword_range);
2727
2783
 
2728
2784
  return rbs_ast_directives_use(clauses, location);
@@ -2749,54 +2805,103 @@ VALUE parse_signature(parserstate *state) {
2749
2805
  return ret;
2750
2806
  }
2751
2807
 
2808
+ struct parse_type_arg {
2809
+ parserstate *parser;
2810
+ VALUE require_eof;
2811
+ };
2812
+
2752
2813
  static VALUE
2753
- rbsparser_parse_type(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos, VALUE variables, VALUE require_eof)
2754
- {
2755
- parserstate *parser = alloc_parser(buffer, FIX2INT(start_pos), FIX2INT(end_pos), variables);
2814
+ ensure_free_parser(VALUE parser) {
2815
+ free_parser((parserstate *)parser);
2816
+ return Qnil;
2817
+ }
2818
+
2819
+ static VALUE
2820
+ parse_type_try(VALUE a) {
2821
+ struct parse_type_arg *arg = (struct parse_type_arg *)a;
2756
2822
 
2757
- if (parser->next_token.type == pEOF) {
2823
+ if (arg->parser->next_token.type == pEOF) {
2758
2824
  return Qnil;
2759
2825
  }
2760
2826
 
2761
- VALUE type = parse_type(parser);
2827
+ VALUE type = parse_type(arg->parser);
2762
2828
 
2763
- if (RB_TEST(require_eof)) {
2764
- parser_advance_assert(parser, pEOF);
2829
+ if (RB_TEST(arg->require_eof)) {
2830
+ parser_advance_assert(arg->parser, pEOF);
2765
2831
  }
2766
2832
 
2767
- free_parser(parser);
2768
-
2769
2833
  return type;
2770
2834
  }
2771
2835
 
2772
2836
  static VALUE
2773
- rbsparser_parse_method_type(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos, VALUE variables, VALUE require_eof)
2837
+ rbsparser_parse_type(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos, VALUE variables, VALUE require_eof)
2774
2838
  {
2775
2839
  parserstate *parser = alloc_parser(buffer, FIX2INT(start_pos), FIX2INT(end_pos), variables);
2840
+ struct parse_type_arg arg = {
2841
+ parser,
2842
+ require_eof
2843
+ };
2844
+ return rb_ensure(parse_type_try, (VALUE)&arg, ensure_free_parser, (VALUE)parser);
2845
+ }
2776
2846
 
2777
- if (parser->next_token.type == pEOF) {
2847
+ static VALUE
2848
+ parse_method_type_try(VALUE a) {
2849
+ struct parse_type_arg *arg = (struct parse_type_arg *)a;
2850
+
2851
+ if (arg->parser->next_token.type == pEOF) {
2778
2852
  return Qnil;
2779
2853
  }
2780
2854
 
2781
- VALUE method_type = parse_method_type(parser);
2855
+ VALUE method_type = parse_method_type(arg->parser);
2782
2856
 
2783
- if (RB_TEST(require_eof)) {
2784
- parser_advance_assert(parser, pEOF);
2857
+ if (RB_TEST(arg->require_eof)) {
2858
+ parser_advance_assert(arg->parser, pEOF);
2785
2859
  }
2786
2860
 
2787
- free_parser(parser);
2788
-
2789
2861
  return method_type;
2790
2862
  }
2791
2863
 
2864
+ static VALUE
2865
+ rbsparser_parse_method_type(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos, VALUE variables, VALUE require_eof)
2866
+ {
2867
+ parserstate *parser = alloc_parser(buffer, FIX2INT(start_pos), FIX2INT(end_pos), variables);
2868
+ struct parse_type_arg arg = {
2869
+ parser,
2870
+ require_eof
2871
+ };
2872
+ return rb_ensure(parse_method_type_try, (VALUE)&arg, ensure_free_parser, (VALUE)parser);
2873
+ }
2874
+
2875
+ static VALUE
2876
+ parse_signature_try(VALUE a) {
2877
+ parserstate *parser = (parserstate *)a;
2878
+ return parse_signature(parser);
2879
+ }
2880
+
2792
2881
  static VALUE
2793
2882
  rbsparser_parse_signature(VALUE self, VALUE buffer, VALUE end_pos)
2794
2883
  {
2795
2884
  parserstate *parser = alloc_parser(buffer, 0, FIX2INT(end_pos), Qnil);
2796
- VALUE pair = parse_signature(parser);
2797
- free_parser(parser);
2885
+ return rb_ensure(parse_signature_try, (VALUE)parser, ensure_free_parser, (VALUE)parser);
2886
+ }
2887
+
2888
+ static VALUE
2889
+ rbsparser_lex(VALUE self, VALUE buffer, VALUE end_pos) {
2890
+ lexstate *lexer = alloc_lexer(buffer, 0, FIX2INT(end_pos));
2891
+ VALUE results = rb_ary_new();
2892
+
2893
+ token token = NullToken;
2894
+ while (token.type != pEOF) {
2895
+ token = rbsparser_next_token(lexer);
2896
+ VALUE type = ID2SYM(rb_intern(token_type_str(token.type)));
2897
+ VALUE location = rbs_new_location(buffer, token.range);
2898
+ VALUE pair = rb_ary_new3(2, type, location);
2899
+ rb_ary_push(results, pair);
2900
+ }
2901
+
2902
+ free(lexer);
2798
2903
 
2799
- return pair;
2904
+ return results;
2800
2905
  }
2801
2906
 
2802
2907
  void rbs__init_parser(void) {
@@ -2804,4 +2909,5 @@ void rbs__init_parser(void) {
2804
2909
  rb_define_singleton_method(RBS_Parser, "_parse_type", rbsparser_parse_type, 5);
2805
2910
  rb_define_singleton_method(RBS_Parser, "_parse_method_type", rbsparser_parse_method_type, 5);
2806
2911
  rb_define_singleton_method(RBS_Parser, "_parse_signature", rbsparser_parse_signature, 2);
2912
+ rb_define_singleton_method(RBS_Parser, "_lex", rbsparser_lex, 2);
2807
2913
  }
@@ -109,6 +109,8 @@ void parser_advance(parserstate *state) {
109
109
  // skip
110
110
  } else if (state->next_token3.type == tLINECOMMENT) {
111
111
  insert_comment_line(state, state->next_token3);
112
+ } else if (state->next_token3.type == tTRIVIA) {
113
+ //skip
112
114
  } else {
113
115
  break;
114
116
  }
@@ -272,11 +274,15 @@ VALUE comment_to_ruby(comment *com, VALUE buffer) {
272
274
  );
273
275
  }
274
276
 
275
- parserstate *alloc_parser(VALUE buffer, int start_pos, int end_pos, VALUE variables) {
277
+ lexstate *alloc_lexer(VALUE buffer, int start_pos, int end_pos) {
276
278
  VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
277
279
 
278
280
  StringValue(string);
279
281
 
282
+ if (start_pos < 0 || end_pos < 0) {
283
+ rb_raise(rb_eArgError, "negative position range: %d...%d", start_pos, end_pos);
284
+ }
285
+
280
286
  lexstate *lexer = calloc(1, sizeof(lexstate));
281
287
  lexer->string = string;
282
288
  lexer->current.line = 1;
@@ -286,6 +292,11 @@ parserstate *alloc_parser(VALUE buffer, int start_pos, int end_pos, VALUE variab
286
292
  lexer->start = lexer->current;
287
293
  lexer->first_token_of_line = lexer->current.column == 0;
288
294
 
295
+ return lexer;
296
+ }
297
+
298
+ parserstate *alloc_parser(VALUE buffer, int start_pos, int end_pos, VALUE variables) {
299
+ lexstate *lexer = alloc_lexer(buffer, start_pos, end_pos);
289
300
  parserstate *parser = calloc(1, sizeof(parserstate));
290
301
  parser->lexstate = lexer;
291
302
  parser->buffer = buffer;
@@ -93,6 +93,15 @@ void parser_insert_typevar(parserstate *state, ID id);
93
93
  * */
94
94
  bool parser_typevar_member(parserstate *state, ID id);
95
95
 
96
+ /**
97
+ * Allocate new lexstate object.
98
+ *
99
+ * ```
100
+ * alloc_lexer(buffer, 0, 31) // New lexstate with buffer
101
+ * ```
102
+ * */
103
+ lexstate *alloc_lexer(VALUE buffer, int start_pos, int end_pos);
104
+
96
105
  /**
97
106
  * Allocate new parserstate object.
98
107
  *
@@ -170,6 +170,17 @@ VALUE rbs_function_param(VALUE type, VALUE name, VALUE location) {
170
170
  );
171
171
  }
172
172
 
173
+ VALUE rbs_untyped_function(VALUE return_type) {
174
+ VALUE args = rb_hash_new();
175
+ rb_hash_aset(args, ID2SYM(rb_intern("return_type")), return_type);
176
+
177
+ return CLASS_NEW_INSTANCE(
178
+ RBS_Types_UntypedFunction,
179
+ 1,
180
+ &args
181
+ );
182
+ }
183
+
173
184
  VALUE rbs_function(
174
185
  VALUE required_positional_params,
175
186
  VALUE optional_positional_params,
@@ -234,10 +245,10 @@ VALUE rbs_literal(VALUE literal, VALUE location) {
234
245
  );
235
246
  }
236
247
 
237
- VALUE rbs_record(VALUE fields, VALUE location) {
248
+ VALUE rbs_record(VALUE fields,VALUE location) {
238
249
  VALUE args = rb_hash_new();
239
250
  rb_hash_aset(args, ID2SYM(rb_intern("location")), location);
240
- rb_hash_aset(args, ID2SYM(rb_intern("fields")), fields);
251
+ rb_hash_aset(args, ID2SYM(rb_intern("all_fields")), fields);
241
252
 
242
253
  return CLASS_NEW_INSTANCE(
243
254
  RBS_Types_Record,
@@ -588,4 +599,3 @@ VALUE rbs_ast_directives_use_wildcard_clause(VALUE namespace, VALUE location) {
588
599
 
589
600
  return CLASS_NEW_INSTANCE(RBS_AST_Directives_Use_WildcardClause, 1, &kwargs);
590
601
  }
591
-
@@ -30,6 +30,7 @@ VALUE rbs_class_instance(VALUE typename, VALUE type_args, VALUE location);
30
30
  VALUE rbs_class_singleton(VALUE typename, VALUE location);
31
31
  VALUE rbs_function_param(VALUE type, VALUE name, VALUE location);
32
32
  VALUE rbs_function(VALUE required_positional_params, VALUE optional_positional_params, VALUE rest_positional_params, VALUE trailing_positional_params, VALUE required_keywords, VALUE optional_keywords, VALUE rest_keywords, VALUE return_type);
33
+ VALUE rbs_untyped_function(VALUE return_type);
33
34
  VALUE rbs_interface(VALUE typename, VALUE type_args, VALUE location);
34
35
  VALUE rbs_intersection(VALUE types, VALUE location);
35
36
  VALUE rbs_literal(VALUE literal, VALUE location);
@@ -66,8 +66,8 @@ Examples:
66
66
  $ rbs validate
67
67
  EOU
68
68
 
69
- opts.on("--silent") do
70
- RBS.print_warning { "`--silent` option is deprecated." }
69
+ opts.on("--silent", "This option has been deprecated and does nothing.") do
70
+ RBS.print_warning { "`--silent` option is deprecated because it's silent by default. You can use --log-level option of rbs command to display more information." }
71
71
  end
72
72
  opts.on("--[no-]exit-error-on-syntax-error", "exit(1) if syntax error is detected") {|bool|
73
73
  exit_error = bool
data/lib/rbs/cli.rb CHANGED
@@ -3,7 +3,6 @@
3
3
  require "open3"
4
4
  require "optparse"
5
5
  require "shellwords"
6
- require "abbrev"
7
6
  require "stringio"
8
7
 
9
8
  module RBS
@@ -1062,14 +1061,13 @@ EOB
1062
1061
  config_path = options.config_path or raise
1063
1062
  lock_path = Collection::Config.to_lockfile_path(config_path)
1064
1063
 
1065
- subcommand = Abbrev.abbrev(['install', 'update', 'help'])[args[0]] || args[0]
1066
- case subcommand
1067
- when 'install'
1064
+ case args[0]
1065
+ when 'install', 'instal', 'insta', 'inst', 'ins', 'in', 'i'
1068
1066
  unless params[:frozen]
1069
1067
  Collection::Config.generate_lockfile(config_path: config_path, definition: Bundler.definition)
1070
1068
  end
1071
1069
  Collection::Installer.new(lockfile_path: lock_path, stdout: stdout).install_from_lockfile
1072
- when 'update'
1070
+ when 'update', 'updat', 'upda', 'upd', 'up', 'u'
1073
1071
  # TODO: Be aware of argv to update only specified gem
1074
1072
  Collection::Config.generate_lockfile(config_path: config_path, definition: Bundler.definition, with_lockfile: false)
1075
1073
  Collection::Installer.new(lockfile_path: lock_path, stdout: stdout).install_from_lockfile
@@ -1107,7 +1105,7 @@ EOB
1107
1105
  exit 1
1108
1106
  end
1109
1107
  Collection::Cleaner.new(lockfile_path: lock_path)
1110
- when 'help'
1108
+ when 'help', 'hel', 'he', 'h'
1111
1109
  puts opts.help
1112
1110
  else
1113
1111
  puts opts.help
@@ -65,8 +65,8 @@ module RBS
65
65
 
66
66
  def sources
67
67
  @sources ||= [
68
- Sources::Stdlib.instance,
69
68
  Sources::Rubygems.instance,
69
+ Sources::Stdlib.instance,
70
70
  *@data['sources'].map { |c| Sources.from_config_entry(c, base_directory: @config_path.dirname) }
71
71
  ]
72
72
  end
@@ -152,12 +152,7 @@ module RBS
152
152
  private def need_to_fetch?(revision)
153
153
  return true unless commit_hash?
154
154
 
155
- begin
156
- git('cat-file', '-e', revision)
157
- false
158
- rescue CommandError
159
- true
160
- end
155
+ !git?('cat-file', '-e', revision)
161
156
  end
162
157
 
163
158
  private def git_dir
@@ -4,7 +4,7 @@ module RBS
4
4
  class DefinitionBuilder
5
5
  class MethodBuilder
6
6
  class Methods
7
- Definition = _ = Struct.new(:name, :type, :originals, :overloads, :accessibilities, keyword_init: true) do
7
+ class Definition < Struct.new(:name, :type, :originals, :overloads, :accessibilities, keyword_init: true)
8
8
  # @implements Definition
9
9
 
10
10
  def original