rbs 4.1.3-java → 4.2.0.pre.1-java

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 (59) hide show
  1. checksums.yaml +4 -4
  2. data/.gitattributes +1 -0
  3. data/.github/workflows/bundle-update.yml +2 -2
  4. data/.github/workflows/c-check.yml +2 -2
  5. data/.github/workflows/changelog.yml +121 -0
  6. data/.github/workflows/comments.yml +2 -2
  7. data/.github/workflows/dependabot.yml +1 -1
  8. data/.github/workflows/jruby.yml +3 -3
  9. data/.github/workflows/release-gems.yml +6 -7
  10. data/.github/workflows/ruby.yml +6 -6
  11. data/.github/workflows/rust.yml +12 -12
  12. data/.github/workflows/truffleruby.yml +2 -2
  13. data/.github/workflows/typecheck.yml +2 -2
  14. data/.github/workflows/wasm.yml +3 -3
  15. data/.github/workflows/windows.yml +2 -2
  16. data/CHANGELOG.md +27 -0
  17. data/Rakefile +50 -21
  18. data/config.yml +5 -0
  19. data/docs/CONTRIBUTING.md +1 -1
  20. data/docs/release.md +57 -1
  21. data/docs/stdlib.md +8 -0
  22. data/docs/syntax.md +12 -0
  23. data/ext/rbs_extension/ast_translation.c +15 -0
  24. data/ext/rbs_extension/class_constants.c +2 -0
  25. data/ext/rbs_extension/class_constants.h +1 -0
  26. data/ext/rbs_extension/main.c +55 -19
  27. data/include/rbs/ast.h +21 -13
  28. data/include/rbs/defines.h +2 -2
  29. data/include/rbs/lexer.h +13 -12
  30. data/include/rbs/parser.h +37 -3
  31. data/lib/rbs/ast/declarations.rb +1 -1
  32. data/lib/rbs/ast/type_param.rb +1 -1
  33. data/lib/rbs/definition_builder/ancestor_builder.rb +8 -5
  34. data/lib/rbs/definition_builder/method_builder.rb +4 -4
  35. data/lib/rbs/definition_builder.rb +5 -2
  36. data/lib/rbs/environment/class_entry.rb +12 -0
  37. data/lib/rbs/environment/module_entry.rb +26 -1
  38. data/lib/rbs/parser_aux.rb +2 -2
  39. data/lib/rbs/types.rb +44 -2
  40. data/lib/rbs/version.rb +1 -1
  41. data/lib/rbs/wasm/parser.rb +62 -25
  42. data/lib/rbs/wasm/rbs_parser.wasm +0 -0
  43. data/lib/rbs/wasm/runtime.rb +19 -4
  44. data/lib/rbs/wasm/serialization_schema.rb +3 -2
  45. data/schema/function.json +12 -1
  46. data/sig/environment/class_entry.rbs +6 -0
  47. data/sig/environment/module_entry.rbs +15 -0
  48. data/sig/parser.rbs +4 -4
  49. data/sig/types.rbs +11 -1
  50. data/src/ast.c +17 -1
  51. data/src/lexer.c +1562 -1560
  52. data/src/lexer.re +50 -18
  53. data/src/lexstate.c +2 -1
  54. data/src/parser.c +130 -70
  55. data/src/serialize.c +20 -13
  56. data/src/util/rbs_encoding.c +195 -49
  57. data/wasm/README.md +20 -4
  58. data/wasm/rbs_wasm.c +93 -37
  59. metadata +3 -1
data/src/parser.c CHANGED
@@ -124,6 +124,7 @@ typedef struct {
124
124
  rbs_hash_t *required_keywords;
125
125
  rbs_hash_t *optional_keywords;
126
126
  rbs_node_t *rest_keywords;
127
+ rbs_node_t *forwarding;
127
128
  } method_params;
128
129
 
129
130
  /**
@@ -183,7 +184,7 @@ static void parser_advance_no_gap(rbs_parser_t *parser) {
183
184
  | {(tUIDENT `::`)*} <tXIDENT>
184
185
  | {<tXIDENT>}
185
186
  */
186
- NODISCARD
187
+ RBS_NODISCARD
187
188
  static bool parse_type_name(rbs_parser_t *parser, TypeNameKind kind, rbs_range_t *rg, rbs_type_name_t **type_name) {
188
189
  bool absolute = false;
189
190
 
@@ -267,7 +268,7 @@ error_handling: {
267
268
  type_list ::= {} type `,` ... <`,`> eol
268
269
  | {} type `,` ... `,` <type> eol
269
270
  */
270
- NODISCARD
271
+ RBS_NODISCARD
271
272
  static bool parse_type_list(rbs_parser_t *parser, enum RBSTokenType eol, rbs_node_list_t *types, bool void_allowed, bool self_allowed, bool classish_allowed) {
272
273
  while (true) {
273
274
  rbs_node_t *type;
@@ -297,7 +298,7 @@ static bool parse_type_list(rbs_parser_t *parser, enum RBSTokenType eol, rbs_nod
297
298
  type_list_with_commas ::= {} type `,` ... <`,`> eol
298
299
  | {} type `,` ... `,` <type> eol
299
300
  */
300
- NODISCARD
301
+ RBS_NODISCARD
301
302
  static bool parse_type_list_with_commas(rbs_parser_t *parser, enum RBSTokenType eol, rbs_node_list_t *types, rbs_location_range_list_t *comma_locations, bool void_allowed, bool self_allowed, bool classish_allowed) {
302
303
  while (true) {
303
304
  rbs_node_t *type;
@@ -336,6 +337,7 @@ static bool is_keyword_token(enum RBSTokenType type) {
336
337
  case tUIDENT:
337
338
  case tULIDENT:
338
339
  case tULLIDENT:
340
+ case tNONASCIIIDENT:
339
341
  case tQIDENT:
340
342
  case tBANGIDENT:
341
343
  KEYWORD_CASES
@@ -345,11 +347,20 @@ static bool is_keyword_token(enum RBSTokenType type) {
345
347
  }
346
348
  }
347
349
 
350
+ /**
351
+ * Ruby lets a local variable name open with a character outside ASCII, and a
352
+ * parameter name is a local variable name. Nothing here reads the case of that
353
+ * character, so both spellings are a name.
354
+ * */
355
+ static bool is_param_name_token(enum RBSTokenType type) {
356
+ return type == tLIDENT || type == tNONASCIIIDENT;
357
+ }
358
+
348
359
  /*
349
360
  function_param ::= {} <type>
350
361
  | {} type <param>
351
362
  */
352
- NODISCARD
363
+ RBS_NODISCARD
353
364
  static bool parse_function_param(rbs_parser_t *parser, rbs_types_function_param_t **function_param, bool self_allowed, bool classish_allowed) {
354
365
  rbs_range_t type_range;
355
366
  type_range.start = parser->next_token.range.start;
@@ -401,7 +412,7 @@ static rbs_constant_id_t intern_token_start_end(rbs_parser_t *parser, rbs_token_
401
412
  keyword_key ::= {} <keyword> `:`
402
413
  | {} keyword <`?`> `:`
403
414
  */
404
- NODISCARD
415
+ RBS_NODISCARD
405
416
  static bool parse_keyword_key(rbs_parser_t *parser, rbs_ast_symbol_t **key) {
406
417
  rbs_parser_advance(parser);
407
418
 
@@ -428,7 +439,7 @@ static bool parse_keyword_key(rbs_parser_t *parser, rbs_ast_symbol_t **key) {
428
439
  /*
429
440
  keyword ::= {} keyword `:` <function_param>
430
441
  */
431
- NODISCARD
442
+ RBS_NODISCARD
432
443
  static bool parse_keyword(rbs_parser_t *parser, rbs_hash_t *keywords, rbs_hash_t *memo, bool self_allowed, bool classish_allowed) {
433
444
  rbs_ast_symbol_t *key = NULL;
434
445
  CHECK_PARSE(parse_keyword_key(parser, &key));
@@ -487,10 +498,12 @@ static bool parser_advance_if(rbs_parser_t *parser, enum RBSTokenType type) {
487
498
  /*
488
499
  params ::= {} `)`
489
500
  | {} `?` `)` -- Untyped function params (assign params.required = nil)
501
+ | {} `...` `)` -- Forwarding params
490
502
  | <required_params> `)`
491
503
  | <required_params> `,` `)`
492
504
 
493
505
  required_params ::= {} function_param `,` <required_params>
506
+ | {} function_param `,` `...`
494
507
  | {} <function_param>
495
508
  | {} <optional_params>
496
509
 
@@ -513,8 +526,8 @@ static bool parser_advance_if(rbs_parser_t *parser, enum RBSTokenType type) {
513
526
  | {} `?` <optional_keyword>
514
527
  | {} `**` <function_param>
515
528
  */
516
- NODISCARD
517
- static bool parse_params(rbs_parser_t *parser, method_params *params, bool self_allowed, bool classish_allowed) {
529
+ RBS_NODISCARD
530
+ static bool parse_params(rbs_parser_t *parser, method_params *params, bool forwarding_allowed, bool self_allowed, bool classish_allowed) {
518
531
  if (parser->next_token.type == pQUESTION && parser->next_token2.type == pRPAREN) {
519
532
  params->required_positionals = NULL;
520
533
  rbs_parser_advance(parser);
@@ -537,6 +550,24 @@ static bool parse_params(rbs_parser_t *parser, method_params *params, bool self_
537
550
  case pRPAREN:
538
551
  goto EOP;
539
552
 
553
+ case pDOT3: {
554
+ if (!forwarding_allowed) {
555
+ rbs_parser_set_error(parser, parser->next_token, true, "forwarding parameter is not allowed in this context");
556
+ return false;
557
+ }
558
+
559
+ if (!parser->options.enable_forwarding_params) {
560
+ rbs_parser_set_error(parser, parser->next_token, true, "forwarding parameter syntax is not enabled");
561
+ return false;
562
+ }
563
+
564
+ rbs_parser_advance(parser);
565
+ params->forwarding = (rbs_node_t *) rbs_types_function_forwarding_param_new(
566
+ ALLOCATOR(),
567
+ rbs_location_range_current_token(parser)
568
+ );
569
+ goto EOP;
570
+ }
540
571
  default:
541
572
  if (is_keyword(parser)) {
542
573
  goto PARSE_KEYWORDS;
@@ -648,6 +679,7 @@ PARSE_KEYWORDS:
648
679
  case tQIDENT:
649
680
  case tULIDENT:
650
681
  case tULLIDENT:
682
+ case tNONASCIIIDENT:
651
683
  case tBANGIDENT:
652
684
  KEYWORD_CASES
653
685
  if (is_keyword(parser)) {
@@ -680,7 +712,7 @@ EOP:
680
712
  optional ::= {} <simple_type>
681
713
  | {} simple_type <`?`>
682
714
  */
683
- NODISCARD
715
+ RBS_NODISCARD
684
716
  static bool parse_optional(rbs_parser_t *parser, rbs_node_t **optional, bool void_allowed, bool self_allowed, bool classish_allowed) {
685
717
  rbs_range_t rg;
686
718
  rg.start = parser->next_token.range.start;
@@ -713,6 +745,7 @@ static void initialize_method_params(method_params *params, rbs_allocator_t *all
713
745
  .required_keywords = rbs_hash_new(allocator),
714
746
  .optional_keywords = rbs_hash_new(allocator),
715
747
  .rest_keywords = NULL,
748
+ .forwarding = NULL,
716
749
  };
717
750
  }
718
751
 
@@ -720,7 +753,7 @@ static void initialize_method_params(method_params *params, rbs_allocator_t *all
720
753
  self_type_binding ::= {} <>
721
754
  | {} `[` `self` `:` type <`]`>
722
755
  */
723
- NODISCARD
756
+ RBS_NODISCARD
724
757
  static bool parse_self_type_binding(rbs_parser_t *parser, rbs_node_t **self_type, bool self_allowed, bool classish_allowed) {
725
758
  if (parser->next_token.type == pLBRACKET) {
726
759
  rbs_parser_advance(parser);
@@ -748,8 +781,8 @@ typedef struct {
748
781
  | {} self_type_binding? `{` self_type_binding `->` optional `}` `->` <optional>
749
782
  | {} self_type_binding? `->` <optional>
750
783
  */
751
- NODISCARD
752
- static bool parse_function(rbs_parser_t *parser, bool accept_type_binding, bool block_allowed, parse_function_result **result, bool self_allowed, bool classish_allowed) {
784
+ RBS_NODISCARD
785
+ static bool parse_function(rbs_parser_t *parser, bool accept_type_binding, bool block_allowed, bool forwarding_allowed, parse_function_result **result, bool self_allowed, bool classish_allowed) {
753
786
  rbs_node_t *function = NULL;
754
787
  rbs_types_block_t *block = NULL;
755
788
  rbs_node_t *function_self_type = NULL;
@@ -761,7 +794,7 @@ static bool parse_function(rbs_parser_t *parser, bool accept_type_binding, bool
761
794
 
762
795
  if (parser->next_token.type == pLPAREN) {
763
796
  rbs_parser_advance(parser);
764
- CHECK_PARSE(parse_params(parser, &params, self_allowed, classish_allowed));
797
+ CHECK_PARSE(parse_params(parser, &params, forwarding_allowed, self_allowed, classish_allowed));
765
798
  ADVANCE_ASSERT(parser, pRPAREN);
766
799
  }
767
800
 
@@ -779,8 +812,14 @@ static bool parse_function(rbs_parser_t *parser, bool accept_type_binding, bool
779
812
 
780
813
  bool required = true;
781
814
  rbs_range_t block_range;
815
+ bool has_block = parser->next_token.type == pLBRACE || (parser->next_token.type == pQUESTION && parser->next_token2.type == pLBRACE);
782
816
 
783
- if (!block_allowed && (parser->next_token.type == pLBRACE || (parser->next_token.type == pQUESTION && parser->next_token2.type == pLBRACE))) {
817
+ if (params.forwarding && has_block) {
818
+ rbs_parser_set_error(parser, parser->next_token, true, "method type with forwarding parameter cannot have block");
819
+ return false;
820
+ }
821
+
822
+ if (!block_allowed && has_block) {
784
823
  rbs_parser_set_error(parser, parser->next_token, true, "block is not allowed in this context");
785
824
  return false;
786
825
  }
@@ -802,7 +841,7 @@ static bool parse_function(rbs_parser_t *parser, bool accept_type_binding, bool
802
841
 
803
842
  if (parser->next_token.type == pLPAREN) {
804
843
  rbs_parser_advance(parser);
805
- CHECK_PARSE(parse_params(parser, &block_params, self_allowed, classish_allowed));
844
+ CHECK_PARSE(parse_params(parser, &block_params, false, self_allowed, classish_allowed));
806
845
  ADVANCE_ASSERT(parser, pRPAREN);
807
846
  }
808
847
 
@@ -831,6 +870,7 @@ static bool parse_function(rbs_parser_t *parser, bool accept_type_binding, bool
831
870
  block_params.required_keywords,
832
871
  block_params.optional_keywords,
833
872
  block_params.rest_keywords,
873
+ block_params.forwarding,
834
874
  block_return_type
835
875
  );
836
876
  }
@@ -856,6 +896,7 @@ static bool parse_function(rbs_parser_t *parser, bool accept_type_binding, bool
856
896
  params.required_keywords,
857
897
  params.optional_keywords,
858
898
  params.rest_keywords,
899
+ params.forwarding,
859
900
  type
860
901
  );
861
902
  }
@@ -869,11 +910,11 @@ static bool parse_function(rbs_parser_t *parser, bool accept_type_binding, bool
869
910
  /*
870
911
  proc_type ::= {`^`} <function>
871
912
  */
872
- NODISCARD
913
+ RBS_NODISCARD
873
914
  static bool parse_proc_type(rbs_parser_t *parser, rbs_types_proc_t **proc, bool self_allowed, bool classish_allowed) {
874
915
  rbs_position_t start = parser->current_token.range.start;
875
916
  parse_function_result *result = rbs_allocator_alloc(ALLOCATOR(), parse_function_result);
876
- CHECK_PARSE(parse_function(parser, true, true, &result, self_allowed, classish_allowed));
917
+ CHECK_PARSE(parse_function(parser, true, true, false, &result, self_allowed, classish_allowed));
877
918
 
878
919
  rbs_position_t end = parser->current_token.range.end;
879
920
  rbs_location_range range = { .start_char = start.char_pos, .start_byte = start.byte_pos, .end_char = end.char_pos, .end_byte = end.byte_pos };
@@ -897,7 +938,7 @@ static void check_key_duplication(rbs_parser_t *parser, rbs_hash_t *fields, rbs_
897
938
  record_attribute ::= {} keyword_token `:` <type>
898
939
  | {} literal_type `=>` <type>
899
940
  */
900
- NODISCARD
941
+ RBS_NODISCARD
901
942
  static bool parse_record_attributes(rbs_parser_t *parser, rbs_hash_t **fields, bool self_allowed, bool classish_allowed) {
902
943
  *fields = rbs_hash_new(ALLOCATOR());
903
944
 
@@ -967,7 +1008,7 @@ static bool parse_record_attributes(rbs_parser_t *parser, rbs_hash_t **fields, b
967
1008
  /*
968
1009
  symbol ::= {<tSYMBOL>}
969
1010
  */
970
- NODISCARD
1011
+ RBS_NODISCARD
971
1012
  static bool parse_symbol(rbs_parser_t *parser, rbs_location_range location, rbs_types_literal_t **symbol) {
972
1013
  size_t offset_bytes = parser->lexer->encoding->char_width((const uint8_t *) ":", (size_t) 1);
973
1014
  size_t bytes = rbs_token_bytes(parser->current_token) - offset_bytes;
@@ -1016,7 +1057,7 @@ static bool parse_symbol(rbs_parser_t *parser, rbs_location_range location, rbs_
1016
1057
  type_args ::= {} <> /empty/
1017
1058
  | {} `[` type_list <`]`>
1018
1059
  */
1019
- NODISCARD
1060
+ RBS_NODISCARD
1020
1061
  static bool parse_instance_type(rbs_parser_t *parser, bool parse_alias, rbs_node_t **type) {
1021
1062
  TypeNameKind expected_kind = (TypeNameKind) (INTERFACE_NAME | CLASS_NAME);
1022
1063
  if (parse_alias) {
@@ -1088,7 +1129,7 @@ static bool parse_instance_type(rbs_parser_t *parser, bool parse_alias, rbs_node
1088
1129
  /*
1089
1130
  singleton_type ::= {`singleton`} `(` type_name <`)`> type_args?
1090
1131
  */
1091
- NODISCARD
1132
+ RBS_NODISCARD
1092
1133
  static bool parse_singleton_type(rbs_parser_t *parser, rbs_types_class_singleton_t **singleton, bool self_allowed, bool classish_allowed) {
1093
1134
  ASSERT_TOKEN(parser, kSINGLETON);
1094
1135
 
@@ -1156,7 +1197,7 @@ static bool parser_typevar_member(rbs_parser_t *parser, rbs_constant_id_t id) {
1156
1197
  | {} `{` record_attributes <`}`>
1157
1198
  | {} `^` <function>
1158
1199
  */
1159
- NODISCARD
1200
+ RBS_NODISCARD
1160
1201
  static bool parse_simple(rbs_parser_t *parser, rbs_node_t **type, bool void_allowed, bool self_allowed, bool classish_allowed) {
1161
1202
  rbs_parser_advance(parser);
1162
1203
 
@@ -1352,7 +1393,7 @@ static bool parse_simple(rbs_parser_t *parser, rbs_node_t **type, bool void_allo
1352
1393
  intersection ::= {} optional `&` ... '&' <optional>
1353
1394
  | {} <optional>
1354
1395
  */
1355
- NODISCARD
1396
+ RBS_NODISCARD
1356
1397
  static bool parse_intersection(rbs_parser_t *parser, rbs_node_t **type, bool void_allowed, bool self_allowed, bool classish_allowed) {
1357
1398
  rbs_range_t rg;
1358
1399
  rg.start = parser->next_token.range.start;
@@ -1427,7 +1468,7 @@ bool rbs_parse_type(rbs_parser_t *parser, rbs_node_t **type, bool void_allowed,
1427
1468
 
1428
1469
  type_param ::= tUIDENT upper_bound? lower_bound? default_type? (module_type_params == false)
1429
1470
  */
1430
- NODISCARD
1471
+ RBS_NODISCARD
1431
1472
  static bool parse_type_params(rbs_parser_t *parser, rbs_range_t *rg, bool module_type_params, rbs_node_list_t **params) {
1432
1473
  *params = rbs_node_list_new(ALLOCATOR());
1433
1474
 
@@ -1567,7 +1608,7 @@ static bool parse_type_params(rbs_parser_t *parser, rbs_range_t *rg, bool module
1567
1608
  return true;
1568
1609
  }
1569
1610
 
1570
- NODISCARD
1611
+ RBS_NODISCARD
1571
1612
  static bool parser_pop_typevar_table(rbs_parser_t *parser) {
1572
1613
  id_table *table;
1573
1614
 
@@ -1590,7 +1631,7 @@ static bool parser_pop_typevar_table(rbs_parser_t *parser) {
1590
1631
  /*
1591
1632
  method_type ::= {} type_params <function>
1592
1633
  */
1593
- // TODO: Should this be NODISCARD?
1634
+ // TODO: Should this be RBS_NODISCARD?
1594
1635
  bool rbs_parse_method_type(rbs_parser_t *parser, rbs_method_type_t **method_type, bool require_eof, bool classish_allowed) {
1595
1636
  rbs_parser_push_typevar_table(parser, false);
1596
1637
 
@@ -1605,7 +1646,7 @@ bool rbs_parse_method_type(rbs_parser_t *parser, rbs_method_type_t **method_type
1605
1646
  type_range.start = parser->next_token.range.start;
1606
1647
 
1607
1648
  parse_function_result *result = rbs_allocator_alloc(ALLOCATOR(), parse_function_result);
1608
- CHECK_PARSE(parse_function(parser, false, true, &result, true, classish_allowed));
1649
+ CHECK_PARSE(parse_function(parser, false, true, true, &result, true, classish_allowed));
1609
1650
 
1610
1651
  CHECK_PARSE(parser_pop_typevar_table(parser));
1611
1652
 
@@ -1629,7 +1670,7 @@ bool rbs_parse_method_type(rbs_parser_t *parser, rbs_method_type_t **method_type
1629
1670
  /*
1630
1671
  global_decl ::= {tGIDENT} `:` <type>
1631
1672
  */
1632
- NODISCARD
1673
+ RBS_NODISCARD
1633
1674
  static bool parse_global_decl(rbs_parser_t *parser, rbs_node_list_t *annotations, rbs_ast_declarations_global_t **global) {
1634
1675
  rbs_range_t decl_range;
1635
1676
  decl_range.start = parser->current_token.range.start;
@@ -1654,7 +1695,7 @@ static bool parse_global_decl(rbs_parser_t *parser, rbs_node_list_t *annotations
1654
1695
  /*
1655
1696
  const_decl ::= {const_name} `:` <type>
1656
1697
  */
1657
- NODISCARD
1698
+ RBS_NODISCARD
1658
1699
  static bool parse_const_decl(rbs_parser_t *parser, rbs_node_list_t *annotations, rbs_ast_declarations_constant_t **constant) {
1659
1700
  rbs_range_t decl_range;
1660
1701
 
@@ -1680,7 +1721,7 @@ static bool parse_const_decl(rbs_parser_t *parser, rbs_node_list_t *annotations,
1680
1721
  /*
1681
1722
  type_decl ::= {kTYPE} alias_name `=` <type>
1682
1723
  */
1683
- NODISCARD
1724
+ RBS_NODISCARD
1684
1725
  static bool parse_type_decl(rbs_parser_t *parser, rbs_position_t comment_pos, rbs_node_list_t *annotations, rbs_ast_declarations_type_alias_t **typealias) {
1685
1726
  rbs_parser_push_typevar_table(parser, true);
1686
1727
 
@@ -1720,7 +1761,7 @@ static bool parse_type_decl(rbs_parser_t *parser, rbs_position_t comment_pos, rb
1720
1761
  /*
1721
1762
  annotation ::= {<tANNOTATION>}
1722
1763
  */
1723
- NODISCARD
1764
+ RBS_NODISCARD
1724
1765
  static bool parse_annotation(rbs_parser_t *parser, rbs_ast_annotation_t **annotation) {
1725
1766
  rbs_range_t rg = parser->current_token.range;
1726
1767
 
@@ -1780,7 +1821,7 @@ static bool parse_annotation(rbs_parser_t *parser, rbs_ast_annotation_t **annota
1780
1821
  annotations ::= {} annotation ... <annotation>
1781
1822
  | {<>}
1782
1823
  */
1783
- NODISCARD
1824
+ RBS_NODISCARD
1784
1825
  static bool parse_annotations(rbs_parser_t *parser, rbs_node_list_t *annotations, rbs_position_t *annot_pos) {
1785
1826
  *annot_pos = NullPosition;
1786
1827
 
@@ -1807,7 +1848,7 @@ static bool parse_annotations(rbs_parser_t *parser, rbs_node_list_t *annotations
1807
1848
  method_name ::= {} <IDENT | keyword>
1808
1849
  | {} (IDENT | keyword)~<`?`>
1809
1850
  */
1810
- NODISCARD
1851
+ RBS_NODISCARD
1811
1852
  static bool parse_method_name(rbs_parser_t *parser, rbs_range_t *range, rbs_ast_symbol_t **symbol) {
1812
1853
  rbs_parser_advance(parser);
1813
1854
 
@@ -1816,6 +1857,7 @@ static bool parse_method_name(rbs_parser_t *parser, rbs_range_t *range, rbs_ast_
1816
1857
  case tLIDENT:
1817
1858
  case tULIDENT:
1818
1859
  case tULLIDENT:
1860
+ case tNONASCIIIDENT:
1819
1861
  KEYWORD_CASES
1820
1862
  if (parser->next_token.type == pQUESTION && parser->current_token.range.end.byte_pos == parser->next_token.range.start.byte_pos) {
1821
1863
  range->start = parser->current_token.range.start;
@@ -1925,7 +1967,7 @@ static InstanceSingletonKind parse_instance_singleton_kind(rbs_parser_t *parser,
1925
1967
  * @param instance_only `true` to reject singleton method definition.
1926
1968
  * @param accept_overload `true` to accept overloading (...) definition.
1927
1969
  * */
1928
- NODISCARD
1970
+ RBS_NODISCARD
1929
1971
  static bool parse_member_def(rbs_parser_t *parser, bool instance_only, bool accept_overload, rbs_position_t comment_pos, rbs_node_list_t *annotations, rbs_ast_members_method_definition_t **method_definition) {
1930
1972
  rbs_range_t member_range;
1931
1973
  member_range.start = parser->current_token.range.start;
@@ -2073,7 +2115,7 @@ static bool parse_member_def(rbs_parser_t *parser, bool instance_only, bool acce
2073
2115
  *
2074
2116
  * @param kind
2075
2117
  * */
2076
- NODISCARD
2118
+ RBS_NODISCARD
2077
2119
  static bool class_instance_name(rbs_parser_t *parser, TypeNameKind kind, rbs_node_list_t *args, rbs_range_t *name_range, rbs_range_t *args_range, rbs_type_name_t **name, bool classish_allowed) {
2078
2120
  rbs_parser_advance(parser);
2079
2121
 
@@ -2101,7 +2143,7 @@ static bool class_instance_name(rbs_parser_t *parser, TypeNameKind kind, rbs_nod
2101
2143
  *
2102
2144
  * @param from_interface `true` when the member is in an interface.
2103
2145
  * */
2104
- NODISCARD
2146
+ RBS_NODISCARD
2105
2147
  static bool parse_mixin_member(rbs_parser_t *parser, bool from_interface, rbs_position_t comment_pos, rbs_node_list_t *annotations, rbs_node_t **mixin_member) {
2106
2148
  rbs_range_t member_range;
2107
2149
  member_range.start = parser->current_token.range.start;
@@ -2192,7 +2234,7 @@ static bool parse_mixin_member(rbs_parser_t *parser, bool from_interface, rbs_po
2192
2234
  *
2193
2235
  * @param[in] instance_only `true` to reject `self.` alias.
2194
2236
  * */
2195
- NODISCARD
2237
+ RBS_NODISCARD
2196
2238
  static bool parse_alias_member(rbs_parser_t *parser, bool instance_only, rbs_position_t comment_pos, rbs_node_list_t *annotations, rbs_ast_members_alias_t **alias_member) {
2197
2239
  rbs_range_t member_range;
2198
2240
  member_range.start = parser->current_token.range.start;
@@ -2243,7 +2285,7 @@ static bool parse_alias_member(rbs_parser_t *parser, bool instance_only, rbs_pos
2243
2285
  | {kSELF} `.` tAIDENT `:` <type>
2244
2286
  | {tA2IDENT} `:` <type>
2245
2287
  */
2246
- NODISCARD
2288
+ RBS_NODISCARD
2247
2289
  static bool parse_variable_member(rbs_parser_t *parser, rbs_position_t comment_pos, rbs_node_list_t *annotations, rbs_node_t **variable_member) {
2248
2290
  if (annotations->length > 0) {
2249
2291
  rbs_parser_set_error(parser, parser->current_token, true, "annotation cannot be given to variable members");
@@ -2347,7 +2389,7 @@ static bool parse_variable_member(rbs_parser_t *parser, rbs_position_t comment_p
2347
2389
  visibility_member ::= {<`public`>}
2348
2390
  | {<`private`>}
2349
2391
  */
2350
- NODISCARD
2392
+ RBS_NODISCARD
2351
2393
  static bool parse_visibility_member(rbs_parser_t *parser, rbs_node_list_t *annotations, rbs_node_t **visibility_member) {
2352
2394
  if (annotations->length > 0) {
2353
2395
  rbs_parser_set_error(parser, parser->current_token, true, "annotation cannot be given to visibility members");
@@ -2385,7 +2427,7 @@ static bool parse_visibility_member(rbs_parser_t *parser, rbs_node_list_t *annot
2385
2427
  | `(` tAIDENT `)` # Ivar name
2386
2428
  | `(` `)` # No variable
2387
2429
  */
2388
- NODISCARD
2430
+ RBS_NODISCARD
2389
2431
  static bool parse_attribute_member(rbs_parser_t *parser, rbs_position_t comment_pos, rbs_node_list_t *annotations, rbs_node_t **attribute_member) {
2390
2432
  rbs_range_t member_range;
2391
2433
 
@@ -2514,7 +2556,7 @@ static bool parse_attribute_member(rbs_parser_t *parser, rbs_position_t comment_
2514
2556
  | mixin_member (interface only)
2515
2557
  | alias_member (instance only)
2516
2558
  */
2517
- NODISCARD
2559
+ RBS_NODISCARD
2518
2560
  static bool parse_interface_members(rbs_parser_t *parser, rbs_node_list_t **members) {
2519
2561
  *members = rbs_node_list_new(ALLOCATOR());
2520
2562
 
@@ -2562,7 +2604,7 @@ static bool parse_interface_members(rbs_parser_t *parser, rbs_node_list_t **memb
2562
2604
  /*
2563
2605
  interface_decl ::= {`interface`} interface_name module_type_params interface_members <kEND>
2564
2606
  */
2565
- NODISCARD
2607
+ RBS_NODISCARD
2566
2608
  static bool parse_interface_decl(rbs_parser_t *parser, rbs_position_t comment_pos, rbs_node_list_t *annotations, rbs_ast_declarations_interface_t **interface_decl) {
2567
2609
  rbs_parser_push_typevar_table(parser, true);
2568
2610
 
@@ -2606,7 +2648,7 @@ static bool parse_interface_decl(rbs_parser_t *parser, rbs_position_t comment_po
2606
2648
  module_self_type ::= <module_name>
2607
2649
  | module_name `[` type_list <`]`>
2608
2650
  */
2609
- NODISCARD
2651
+ RBS_NODISCARD
2610
2652
  static bool parse_module_self_types(rbs_parser_t *parser, rbs_node_list_t *array) {
2611
2653
  while (true) {
2612
2654
  rbs_parser_advance(parser);
@@ -2645,7 +2687,7 @@ static bool parse_module_self_types(rbs_parser_t *parser, rbs_node_list_t *array
2645
2687
  return true;
2646
2688
  }
2647
2689
 
2648
- NODISCARD
2690
+ RBS_NODISCARD
2649
2691
  static bool parse_nested_decl(rbs_parser_t *parser, const char *nested_in, rbs_position_t annot_pos, rbs_node_list_t *annotations, rbs_node_t **decl);
2650
2692
 
2651
2693
  /*
@@ -2659,7 +2701,7 @@ static bool parse_nested_decl(rbs_parser_t *parser, const char *nested_in, rbs_p
2659
2701
  | `public`
2660
2702
  | `private`
2661
2703
  */
2662
- NODISCARD
2704
+ RBS_NODISCARD
2663
2705
  static bool parse_module_members(rbs_parser_t *parser, rbs_node_list_t **members) {
2664
2706
  *members = rbs_node_list_new(ALLOCATOR());
2665
2707
 
@@ -2746,7 +2788,7 @@ static bool parse_module_members(rbs_parser_t *parser, rbs_node_list_t **members
2746
2788
  module_decl ::= {module_name} module_type_params module_members <kEND>
2747
2789
  | {module_name} module_name module_type_params `:` module_self_types module_members <kEND>
2748
2790
  */
2749
- NODISCARD
2791
+ RBS_NODISCARD
2750
2792
  static bool parse_module_decl0(rbs_parser_t *parser, rbs_range_t keyword_range, rbs_type_name_t *module_name, rbs_range_t name_range, rbs_ast_comment_t *comment, rbs_node_list_t *annotations, rbs_ast_declarations_module_t **module_decl) {
2751
2793
  rbs_parser_push_typevar_table(parser, true);
2752
2794
 
@@ -2794,7 +2836,7 @@ static bool parse_module_decl0(rbs_parser_t *parser, rbs_range_t keyword_range,
2794
2836
  | {`module`} module_name module_decl0 <kEND>
2795
2837
 
2796
2838
  */
2797
- NODISCARD
2839
+ RBS_NODISCARD
2798
2840
  static bool parse_module_decl(rbs_parser_t *parser, rbs_position_t comment_pos, rbs_node_list_t *annotations, rbs_node_t **module_decl) {
2799
2841
  rbs_range_t keyword_range = parser->current_token.range;
2800
2842
 
@@ -2837,7 +2879,7 @@ static bool parse_module_decl(rbs_parser_t *parser, rbs_position_t comment_pos,
2837
2879
  class_decl_super ::= {} `<` <class_instance_name>
2838
2880
  | {<>}
2839
2881
  */
2840
- NODISCARD
2882
+ RBS_NODISCARD
2841
2883
  static bool parse_class_decl_super(rbs_parser_t *parser, rbs_range_t *lt_range, rbs_ast_declarations_class_super_t **super) {
2842
2884
  if (parser_advance_if(parser, pLT)) {
2843
2885
  *lt_range = parser->current_token.range;
@@ -2865,7 +2907,7 @@ static bool parse_class_decl_super(rbs_parser_t *parser, rbs_range_t *lt_range,
2865
2907
  /*
2866
2908
  class_decl ::= {class_name} type_params class_decl_super class_members <`end`>
2867
2909
  */
2868
- NODISCARD
2910
+ RBS_NODISCARD
2869
2911
  static bool parse_class_decl0(rbs_parser_t *parser, rbs_range_t keyword_range, rbs_type_name_t *name, rbs_range_t name_range, rbs_ast_comment_t *comment, rbs_node_list_t *annotations, rbs_ast_declarations_class_t **class_decl) {
2870
2912
  rbs_parser_push_typevar_table(parser, true);
2871
2913
 
@@ -2904,7 +2946,7 @@ static bool parse_class_decl0(rbs_parser_t *parser, rbs_range_t keyword_range, r
2904
2946
  class_decl ::= {`class`} class_name `=` <class_name>
2905
2947
  | {`class`} class_name <class_decl0>
2906
2948
  */
2907
- NODISCARD
2949
+ RBS_NODISCARD
2908
2950
  static bool parse_class_decl(rbs_parser_t *parser, rbs_position_t comment_pos, rbs_node_list_t *annotations, rbs_node_t **class_decl) {
2909
2951
  rbs_range_t keyword_range = parser->current_token.range;
2910
2952
 
@@ -2949,7 +2991,7 @@ static bool parse_class_decl(rbs_parser_t *parser, rbs_position_t comment_pos, r
2949
2991
  | {<module_decl>}
2950
2992
  | {<class_decl>}
2951
2993
  */
2952
- NODISCARD
2994
+ RBS_NODISCARD
2953
2995
  static bool parse_nested_decl(rbs_parser_t *parser, const char *nested_in, rbs_position_t annot_pos, rbs_node_list_t *annotations, rbs_node_t **decl) {
2954
2996
  rbs_parser_push_typevar_table(parser, true);
2955
2997
 
@@ -3001,7 +3043,7 @@ static bool parse_nested_decl(rbs_parser_t *parser, const char *nested_in, rbs_p
3001
3043
  return true;
3002
3044
  }
3003
3045
 
3004
- NODISCARD
3046
+ RBS_NODISCARD
3005
3047
  static bool parse_decl(rbs_parser_t *parser, rbs_node_t **decl) {
3006
3048
  rbs_node_list_t *annotations = rbs_node_list_new(ALLOCATOR());
3007
3049
  rbs_position_t annot_pos = NullPosition;
@@ -3057,7 +3099,7 @@ static bool parse_decl(rbs_parser_t *parser, rbs_node_t **decl) {
3057
3099
  namespace ::= {} (`::`)? (`tUIDENT` `::`)* `tUIDENT` <`::`>
3058
3100
  | {} <> (empty -- returns empty namespace)
3059
3101
  */
3060
- NODISCARD
3102
+ RBS_NODISCARD
3061
3103
  static bool parse_namespace(rbs_parser_t *parser, rbs_range_t *rg, rbs_namespace_t **out_ns) {
3062
3104
  bool is_absolute = false;
3063
3105
 
@@ -3100,7 +3142,7 @@ static bool parse_namespace(rbs_parser_t *parser, rbs_range_t *rg, rbs_namespace
3100
3142
  | {} namespace tUIDENT `as` <tUIDENT>
3101
3143
  | {} namespace <tSTAR>
3102
3144
  */
3103
- NODISCARD
3145
+ RBS_NODISCARD
3104
3146
  static bool parse_use_clauses(rbs_parser_t *parser, rbs_node_list_t *clauses) {
3105
3147
  while (true) {
3106
3148
  rbs_range_t namespace_range = NULL_RANGE;
@@ -3181,7 +3223,7 @@ static bool parse_use_clauses(rbs_parser_t *parser, rbs_node_list_t *clauses) {
3181
3223
  /*
3182
3224
  use_directive ::= {} `use` <clauses>
3183
3225
  */
3184
- NODISCARD
3226
+ RBS_NODISCARD
3185
3227
  static bool parse_use_directive(rbs_parser_t *parser, rbs_ast_directives_use_t **use_directive) {
3186
3228
  if (parser->next_token.type == kUSE) {
3187
3229
  rbs_parser_advance(parser);
@@ -3399,7 +3441,7 @@ void rbs_parser_push_typevar_table(rbs_parser_t *parser, bool reset) {
3399
3441
  parser->vars = table;
3400
3442
  }
3401
3443
 
3402
- NODISCARD
3444
+ RBS_NODISCARD
3403
3445
  bool rbs_parser_insert_typevar(rbs_parser_t *parser, rbs_constant_id_t id) {
3404
3446
  id_table *table = parser->vars;
3405
3447
 
@@ -3514,21 +3556,36 @@ rbs_lexer_t *rbs_lexer_new(rbs_allocator_t *allocator, rbs_string_t string, cons
3514
3556
  lexer->current_character_bytes = 1;
3515
3557
  }
3516
3558
 
3517
- if (start_pos > 0) {
3518
- while (lexer->current.byte_pos < start_pos) {
3519
- rbs_skip(lexer);
3520
- }
3559
+ // `rbs_skip` moves a whole character at a time, and moves nothing at all
3560
+ // once the input is spent, so this walk can only ever stand on the first
3561
+ // byte of a character.
3562
+ while (lexer->current.byte_pos < start_pos && lexer->current_code_point != '\0') {
3563
+ rbs_skip(lexer);
3521
3564
  }
3522
3565
 
3566
+ // Stopping anywhere else means `start_pos` is a position the lexer cannot
3567
+ // start from: over it, and the walk stepped across a character that
3568
+ // straddles it; short of it, and the input ran out first.
3569
+ if (lexer->current.byte_pos != start_pos) return NULL;
3570
+
3523
3571
  lexer->start = lexer->current;
3524
3572
 
3525
3573
  return lexer;
3526
3574
  }
3527
3575
 
3528
3576
  rbs_parser_t *rbs_parser_new(rbs_string_t string, const rbs_encoding_t *encoding, int start_pos, int end_pos) {
3577
+ return rbs_parser_new_with_options(string, encoding, start_pos, end_pos, (rbs_parser_options_t) { 0 });
3578
+ }
3579
+
3580
+ rbs_parser_t *rbs_parser_new_with_options(rbs_string_t string, const rbs_encoding_t *encoding, int start_pos, int end_pos, rbs_parser_options_t options) {
3529
3581
  rbs_allocator_t *allocator = rbs_allocator_init();
3530
3582
 
3531
3583
  rbs_lexer_t *lexer = rbs_lexer_new(allocator, string, encoding, start_pos, end_pos);
3584
+ if (lexer == NULL) {
3585
+ rbs_allocator_free(allocator);
3586
+ return NULL;
3587
+ }
3588
+
3532
3589
  rbs_parser_t *parser = rbs_allocator_alloc(allocator, rbs_parser_t);
3533
3590
 
3534
3591
  *parser = (rbs_parser_t) {
@@ -3545,6 +3602,8 @@ rbs_parser_t *rbs_parser_new(rbs_string_t string, const rbs_encoding_t *encoding
3545
3602
  .constant_pool = { 0 },
3546
3603
  .allocator = allocator,
3547
3604
  .error = NULL,
3605
+
3606
+ .options = options,
3548
3607
  };
3549
3608
 
3550
3609
  // The parser's constant pool is mainly used for storing the names of type variables, which usually aren't many.
@@ -3605,7 +3664,7 @@ void rbs_parser_set_error(rbs_parser_t *parser, rbs_token_t tok, bool syntax_err
3605
3664
  /*
3606
3665
  parse_method_overload ::= {} annotations <method_type>
3607
3666
  */
3608
- NODISCARD
3667
+ RBS_NODISCARD
3609
3668
  static bool parse_method_overload(rbs_parser_t *parser, rbs_node_list_t *annotations, rbs_method_type_t **method_type) {
3610
3669
  rbs_position_t pos = NullPosition;
3611
3670
 
@@ -3622,7 +3681,7 @@ static bool parse_method_overload(rbs_parser_t *parser, rbs_node_list_t *annotat
3622
3681
  | {} overload `|` ... `|` `...` -- returns true (dot3_location is set)
3623
3682
  | {<>} -- returns false
3624
3683
  */
3625
- NODISCARD
3684
+ RBS_NODISCARD
3626
3685
  static bool parse_inline_method_overloads(rbs_parser_t *parser, rbs_node_list_t *overloads, rbs_location_range_list_t *bar_locations, rbs_location_range *dot3_location) {
3627
3686
  while (true) {
3628
3687
  rbs_node_list_t *annotations = rbs_node_list_new(ALLOCATOR());
@@ -3661,7 +3720,7 @@ static bool parse_inline_method_overloads(rbs_parser_t *parser, rbs_node_list_t
3661
3720
  }
3662
3721
  }
3663
3722
 
3664
- NODISCARD
3723
+ RBS_NODISCARD
3665
3724
  static bool parse_inline_comment(rbs_parser_t *parser, rbs_location_range *comment_range) {
3666
3725
  if (parser->next_token.type != tINLINECOMMENT) {
3667
3726
  *comment_range = RBS_LOCATION_NULL_RANGE;
@@ -3674,7 +3733,7 @@ static bool parse_inline_comment(rbs_parser_t *parser, rbs_location_range *comme
3674
3733
  return true;
3675
3734
  }
3676
3735
 
3677
- NODISCARD
3736
+ RBS_NODISCARD
3678
3737
  static bool parse_inline_param_type_annotation(rbs_parser_t *parser, rbs_ast_ruby_annotations_t **annotation, rbs_range_t rbs_range) {
3679
3738
  rbs_parser_advance(parser);
3680
3739
 
@@ -3714,7 +3773,7 @@ static bool parse_inline_param_type_annotation(rbs_parser_t *parser, rbs_ast_rub
3714
3773
  return true;
3715
3774
  }
3716
3775
 
3717
- NODISCARD
3776
+ RBS_NODISCARD
3718
3777
  static bool parse_inline_leading_annotation(rbs_parser_t *parser, rbs_ast_ruby_annotations_t **annotation) {
3719
3778
  switch (parser->next_token.type) {
3720
3779
  case pCOLON: {
@@ -3915,6 +3974,7 @@ static bool parse_inline_leading_annotation(rbs_parser_t *parser, rbs_ast_ruby_a
3915
3974
  return true;
3916
3975
  }
3917
3976
  case tLIDENT:
3977
+ case tNONASCIIIDENT:
3918
3978
  PARAM_NAME_CASES {
3919
3979
  return parse_inline_param_type_annotation(parser, annotation, rbs_range);
3920
3980
  }
@@ -3923,7 +3983,7 @@ static bool parse_inline_leading_annotation(rbs_parser_t *parser, rbs_ast_ruby_a
3923
3983
  rbs_location_range star_loc = rbs_location_range_current_token(parser);
3924
3984
 
3925
3985
  rbs_location_range name_loc = RBS_LOCATION_NULL_RANGE;
3926
- if (parser->next_token.type == tLIDENT) {
3986
+ if (is_param_name_token(parser->next_token.type)) {
3927
3987
  rbs_parser_advance(parser);
3928
3988
  name_loc = rbs_location_range_current_token(parser);
3929
3989
  }
@@ -3963,7 +4023,7 @@ static bool parse_inline_leading_annotation(rbs_parser_t *parser, rbs_ast_ruby_a
3963
4023
  rbs_location_range star2_loc = rbs_location_range_current_token(parser);
3964
4024
 
3965
4025
  rbs_location_range name_loc = RBS_LOCATION_NULL_RANGE;
3966
- if (parser->next_token.type == tLIDENT) {
4026
+ if (is_param_name_token(parser->next_token.type)) {
3967
4027
  rbs_parser_advance(parser);
3968
4028
  name_loc = rbs_location_range_current_token(parser);
3969
4029
  }
@@ -4003,7 +4063,7 @@ static bool parse_inline_leading_annotation(rbs_parser_t *parser, rbs_ast_ruby_a
4003
4063
  rbs_location_range ampersand_loc = rbs_location_range_current_token(parser);
4004
4064
 
4005
4065
  rbs_location_range name_loc = RBS_LOCATION_NULL_RANGE;
4006
- if (parser->next_token.type == tLIDENT) {
4066
+ if (is_param_name_token(parser->next_token.type)) {
4007
4067
  rbs_parser_advance(parser);
4008
4068
  name_loc = rbs_location_range_current_token(parser);
4009
4069
  }
@@ -4021,7 +4081,7 @@ static bool parse_inline_leading_annotation(rbs_parser_t *parser, rbs_ast_ruby_a
4021
4081
  type_range.start = parser->next_token.range.start;
4022
4082
 
4023
4083
  parse_function_result *result = rbs_allocator_alloc(ALLOCATOR(), parse_function_result);
4024
- if (!parse_function(parser, true, false, &result, true, true)) {
4084
+ if (!parse_function(parser, true, false, false, &result, true, true)) {
4025
4085
  return false;
4026
4086
  }
4027
4087
 
@@ -4118,7 +4178,7 @@ static bool parse_inline_leading_annotation(rbs_parser_t *parser, rbs_ast_ruby_a
4118
4178
  }
4119
4179
  }
4120
4180
 
4121
- NODISCARD
4181
+ RBS_NODISCARD
4122
4182
  static bool parse_inline_trailing_annotation(rbs_parser_t *parser, rbs_ast_ruby_annotations_t **annotation) {
4123
4183
  rbs_range_t prefix_range = parser->next_token.range;
4124
4184