rbs 1.8.1 → 2.0.0.pre1
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +51 -4
- data/docs/collection.md +23 -1
- data/docs/syntax.md +94 -41
- data/ext/rbs_extension/constants.c +2 -6
- data/ext/rbs_extension/constants.h +1 -2
- data/ext/rbs_extension/parser.c +212 -178
- data/ext/rbs_extension/parserstate.c +6 -2
- data/ext/rbs_extension/parserstate.h +10 -0
- data/ext/rbs_extension/ruby_objs.c +9 -11
- data/ext/rbs_extension/ruby_objs.h +1 -2
- data/lib/rbs/ast/declarations.rb +0 -97
- data/lib/rbs/ast/type_param.rb +134 -0
- data/lib/rbs/cli.rb +32 -4
- data/lib/rbs/collection/config/lockfile_generator.rb +26 -18
- data/lib/rbs/collection/sources/git.rb +9 -0
- data/lib/rbs/collection/sources/rubygems.rb +7 -0
- data/lib/rbs/collection/sources/stdlib.rb +6 -0
- data/lib/rbs/definition.rb +9 -0
- data/lib/rbs/definition_builder.rb +20 -14
- data/lib/rbs/environment.rb +32 -9
- data/lib/rbs/environment_loader.rb +0 -2
- data/lib/rbs/errors.rb +20 -7
- data/lib/rbs/location_aux.rb +2 -0
- data/lib/rbs/method_type.rb +29 -6
- data/lib/rbs/prototype/rb.rb +3 -3
- data/lib/rbs/prototype/rbi.rb +8 -6
- data/lib/rbs/prototype/runtime.rb +4 -4
- data/lib/rbs/types.rb +89 -0
- data/lib/rbs/validator.rb +56 -1
- data/lib/rbs/variance_calculator.rb +9 -8
- data/lib/rbs/version.rb +1 -1
- data/lib/rbs/writer.rb +1 -13
- data/lib/rbs.rb +1 -0
- data/schema/decls.json +16 -55
- data/schema/methodType.json +1 -1
- data/schema/typeParam.json +36 -0
- data/sig/collection/collections.rbs +9 -0
- data/sig/collection/config.rbs +2 -2
- data/sig/declarations.rbs +8 -58
- data/sig/definition.rbs +11 -1
- data/sig/definition_builder.rbs +1 -1
- data/sig/environment.rbs +7 -1
- data/sig/errors.rbs +19 -4
- data/sig/location.rbs +3 -1
- data/sig/locator.rbs +1 -1
- data/sig/method_types.rbs +25 -4
- data/sig/type_param.rbs +74 -0
- data/sig/types.rbs +27 -1
- data/sig/validator.rbs +31 -2
- data/sig/variance_calculator.rbs +1 -1
- data/sig/writer.rbs +1 -1
- data/stdlib/bigdecimal-math/0/manifest.yaml +2 -0
- data/stdlib/csv/0/manifest.yaml +2 -0
- data/stdlib/logger/0/manifest.yaml +2 -0
- data/stdlib/net-http/0/manifest.yaml +2 -0
- data/stdlib/openssl/0/manifest.yaml +2 -0
- data/stdlib/prime/0/manifest.yaml +2 -0
- data/stdlib/resolv/0/manifest.yaml +3 -0
- data/stdlib/uri/0/common.rbs +10 -5
- data/stdlib/uri/0/ftp.rbs +10 -0
- data/stdlib/uri/0/mailto.rbs +5 -0
- data/stdlib/uri/0/ws.rbs +10 -0
- data/stdlib/uri/0/wss.rbs +7 -0
- data/stdlib/yaml/0/manifest.yaml +3 -0
- metadata +19 -4
data/ext/rbs_extension/parser.c
CHANGED
@@ -741,6 +741,90 @@ static VALUE parse_symbol(parserstate *state) {
|
|
741
741
|
);
|
742
742
|
}
|
743
743
|
|
744
|
+
/*
|
745
|
+
instance_type ::= {type_name} <type_args>
|
746
|
+
|
747
|
+
type_args ::= {} <> /empty/
|
748
|
+
| {} `[` type_list <`]`>
|
749
|
+
*/
|
750
|
+
static VALUE parse_instance_type(parserstate *state, bool parse_alias) {
|
751
|
+
range name_range;
|
752
|
+
range args_range;
|
753
|
+
range type_range;
|
754
|
+
|
755
|
+
TypeNameKind expected_kind = INTERFACE_NAME | CLASS_NAME;
|
756
|
+
if (parse_alias) {
|
757
|
+
expected_kind |= ALIAS_NAME;
|
758
|
+
}
|
759
|
+
|
760
|
+
VALUE typename = parse_type_name(state, expected_kind, &name_range);
|
761
|
+
VALUE types = rb_ary_new();
|
762
|
+
|
763
|
+
TypeNameKind kind;
|
764
|
+
if (state->current_token.type == tUIDENT) {
|
765
|
+
kind = CLASS_NAME;
|
766
|
+
} else if (state->current_token.type == tULIDENT) {
|
767
|
+
kind = INTERFACE_NAME;
|
768
|
+
} else if (state->current_token.type == tLIDENT) {
|
769
|
+
kind = ALIAS_NAME;
|
770
|
+
} else {
|
771
|
+
rbs_abort();
|
772
|
+
}
|
773
|
+
|
774
|
+
if (state->next_token.type == pLBRACKET) {
|
775
|
+
parser_advance(state);
|
776
|
+
args_range.start = state->current_token.range.start;
|
777
|
+
parse_type_list(state, pRBRACKET, types);
|
778
|
+
parser_advance_assert(state, pRBRACKET);
|
779
|
+
args_range.end = state->current_token.range.end;
|
780
|
+
} else {
|
781
|
+
args_range = NULL_RANGE;
|
782
|
+
}
|
783
|
+
|
784
|
+
type_range.start = name_range.start;
|
785
|
+
type_range.end = nonnull_pos_or(args_range.end, name_range.end);
|
786
|
+
|
787
|
+
VALUE location = rbs_new_location(state->buffer, type_range);
|
788
|
+
rbs_loc *loc = rbs_check_location(location);
|
789
|
+
rbs_loc_add_required_child(loc, rb_intern("name"), name_range);
|
790
|
+
rbs_loc_add_optional_child(loc, rb_intern("args"), args_range);
|
791
|
+
|
792
|
+
if (kind == CLASS_NAME) {
|
793
|
+
return rbs_class_instance(typename, types, location);
|
794
|
+
} else if (kind == INTERFACE_NAME) {
|
795
|
+
return rbs_interface(typename, types, location);
|
796
|
+
} else if (kind == ALIAS_NAME) {
|
797
|
+
return rbs_alias(typename, types, location);
|
798
|
+
} else {
|
799
|
+
return Qnil;
|
800
|
+
}
|
801
|
+
}
|
802
|
+
|
803
|
+
/*
|
804
|
+
singleton_type ::= {`singleton`} `(` type_name <`)`>
|
805
|
+
*/
|
806
|
+
static VALUE parse_singleton_type(parserstate *state) {
|
807
|
+
range name_range;
|
808
|
+
range type_range;
|
809
|
+
|
810
|
+
parser_assert(state, kSINGLETON);
|
811
|
+
|
812
|
+
type_range.start = state->current_token.range.start;
|
813
|
+
parser_advance_assert(state, pLPAREN);
|
814
|
+
parser_advance(state);
|
815
|
+
|
816
|
+
VALUE typename = parse_type_name(state, CLASS_NAME, &name_range);
|
817
|
+
|
818
|
+
parser_advance_assert(state, pRPAREN);
|
819
|
+
type_range.end = state->current_token.range.end;
|
820
|
+
|
821
|
+
VALUE location = rbs_new_location(state->buffer, type_range);
|
822
|
+
rbs_loc *loc = rbs_check_location(location);
|
823
|
+
rbs_loc_add_required_child(loc, rb_intern("name"), name_range);
|
824
|
+
|
825
|
+
return rbs_class_singleton(typename, location);
|
826
|
+
}
|
827
|
+
|
744
828
|
/*
|
745
829
|
simple ::= {} `(` type <`)`>
|
746
830
|
| {} <base type>
|
@@ -813,76 +897,12 @@ static VALUE parse_simple(parserstate *state) {
|
|
813
897
|
}
|
814
898
|
// fallthrough for type name
|
815
899
|
}
|
816
|
-
case tULIDENT:
|
817
|
-
|
818
|
-
case
|
819
|
-
|
820
|
-
case
|
821
|
-
|
822
|
-
range args_range;
|
823
|
-
range type_range;
|
824
|
-
|
825
|
-
VALUE typename = parse_type_name(state, INTERFACE_NAME | CLASS_NAME | ALIAS_NAME, &name_range);
|
826
|
-
VALUE types = rb_ary_new();
|
827
|
-
|
828
|
-
TypeNameKind kind;
|
829
|
-
if (state->current_token.type == tUIDENT) {
|
830
|
-
kind = CLASS_NAME;
|
831
|
-
} else if (state->current_token.type == tULIDENT) {
|
832
|
-
kind = INTERFACE_NAME;
|
833
|
-
} else if (state->current_token.type == tLIDENT) {
|
834
|
-
kind = ALIAS_NAME;
|
835
|
-
} else {
|
836
|
-
rbs_abort();
|
837
|
-
}
|
838
|
-
|
839
|
-
if (state->next_token.type == pLBRACKET) {
|
840
|
-
parser_advance(state);
|
841
|
-
args_range.start = state->current_token.range.start;
|
842
|
-
parse_type_list(state, pRBRACKET, types);
|
843
|
-
parser_advance_assert(state, pRBRACKET);
|
844
|
-
args_range.end = state->current_token.range.end;
|
845
|
-
} else {
|
846
|
-
args_range = NULL_RANGE;
|
847
|
-
}
|
848
|
-
|
849
|
-
type_range.start = name_range.start;
|
850
|
-
type_range.end = nonnull_pos_or(args_range.end, name_range.end);
|
851
|
-
|
852
|
-
VALUE location = rbs_new_location(state->buffer, type_range);
|
853
|
-
rbs_loc *loc = rbs_check_location(location);
|
854
|
-
rbs_loc_add_required_child(loc, rb_intern("name"), name_range);
|
855
|
-
rbs_loc_add_optional_child(loc, rb_intern("args"), args_range);
|
856
|
-
|
857
|
-
if (kind == CLASS_NAME) {
|
858
|
-
return rbs_class_instance(typename, types, location);
|
859
|
-
} else if (kind == INTERFACE_NAME) {
|
860
|
-
return rbs_interface(typename, types, location);
|
861
|
-
} else if (kind == ALIAS_NAME) {
|
862
|
-
return rbs_alias(typename, types, location);
|
863
|
-
} else {
|
864
|
-
return Qnil;
|
865
|
-
}
|
866
|
-
}
|
867
|
-
case kSINGLETON: {
|
868
|
-
range name_range;
|
869
|
-
range type_range;
|
870
|
-
|
871
|
-
type_range.start = state->current_token.range.start;
|
872
|
-
parser_advance_assert(state, pLPAREN);
|
873
|
-
parser_advance(state);
|
874
|
-
|
875
|
-
VALUE typename = parse_type_name(state, CLASS_NAME, &name_range);
|
876
|
-
|
877
|
-
parser_advance_assert(state, pRPAREN);
|
878
|
-
type_range.end = state->current_token.range.end;
|
879
|
-
|
880
|
-
VALUE location = rbs_new_location(state->buffer, type_range);
|
881
|
-
rbs_loc *loc = rbs_check_location(location);
|
882
|
-
rbs_loc_add_required_child(loc, rb_intern("name"), name_range);
|
883
|
-
|
884
|
-
return rbs_class_singleton(typename, location);
|
885
|
-
}
|
900
|
+
case tULIDENT: // fallthrough
|
901
|
+
case tLIDENT: // fallthrough
|
902
|
+
case pCOLON2:
|
903
|
+
return parse_instance_type(state, true);
|
904
|
+
case kSINGLETON:
|
905
|
+
return parse_singleton_type(state);
|
886
906
|
case pLBRACKET: {
|
887
907
|
range rg;
|
888
908
|
rg.start = state->current_token.range.start;
|
@@ -970,53 +990,151 @@ VALUE parse_type(parserstate *state) {
|
|
970
990
|
}
|
971
991
|
|
972
992
|
/*
|
973
|
-
|
974
|
-
| {}
|
993
|
+
type_params ::= {} `[` type_param `,` ... <`]`>
|
994
|
+
| {<>}
|
995
|
+
|
996
|
+
type_param ::= kUNCHECKED? (kIN|kOUT|) tUIDENT (module_type_params == true)
|
997
|
+
|
998
|
+
type_param ::= tUIDENT (module_type_params == false)
|
975
999
|
*/
|
976
|
-
VALUE parse_method_type(parserstate *state) {
|
977
|
-
VALUE function = Qnil;
|
978
|
-
VALUE block = Qnil;
|
979
|
-
id_table *table = parser_push_typevar_table(state, false);
|
980
1000
|
|
981
|
-
|
1001
|
+
VALUE parse_type_params(parserstate *state, range *rg, bool module_type_params) {
|
1002
|
+
VALUE params = rb_ary_new();
|
982
1003
|
|
983
1004
|
if (state->next_token.type == pLBRACKET) {
|
984
1005
|
parser_advance(state);
|
985
1006
|
|
1007
|
+
rg->start = state->current_token.range.start;
|
1008
|
+
|
986
1009
|
while (true) {
|
1010
|
+
VALUE name;
|
1011
|
+
bool unchecked = false;
|
1012
|
+
VALUE variance = ID2SYM(rb_intern("invariant"));
|
1013
|
+
VALUE upper_bound = Qnil;
|
1014
|
+
|
1015
|
+
range param_range = NULL_RANGE;
|
1016
|
+
range name_range;
|
1017
|
+
range variance_range = NULL_RANGE;
|
1018
|
+
range unchecked_range = NULL_RANGE;
|
1019
|
+
range upper_bound_range = NULL_RANGE;
|
1020
|
+
|
1021
|
+
param_range.start = state->next_token.range.start;
|
1022
|
+
|
1023
|
+
if (module_type_params) {
|
1024
|
+
if (state->next_token.type == kUNCHECKED) {
|
1025
|
+
unchecked = true;
|
1026
|
+
parser_advance(state);
|
1027
|
+
unchecked_range = state->current_token.range;
|
1028
|
+
}
|
1029
|
+
|
1030
|
+
if (state->next_token.type == kIN || state->next_token.type == kOUT) {
|
1031
|
+
switch (state->next_token.type) {
|
1032
|
+
case kIN:
|
1033
|
+
variance = ID2SYM(rb_intern("contravariant"));
|
1034
|
+
break;
|
1035
|
+
case kOUT:
|
1036
|
+
variance = ID2SYM(rb_intern("covariant"));
|
1037
|
+
break;
|
1038
|
+
default:
|
1039
|
+
rbs_abort();
|
1040
|
+
}
|
1041
|
+
|
1042
|
+
parser_advance(state);
|
1043
|
+
variance_range = state->current_token.range;
|
1044
|
+
}
|
1045
|
+
}
|
1046
|
+
|
987
1047
|
parser_advance_assert(state, tUIDENT);
|
988
|
-
|
989
|
-
|
1048
|
+
name_range = state->current_token.range;
|
1049
|
+
param_range.end = state->current_token.range.end;
|
990
1050
|
|
991
|
-
|
1051
|
+
ID id = INTERN_TOKEN(state, state->current_token);
|
1052
|
+
name = ID2SYM(id);
|
1053
|
+
|
1054
|
+
parser_insert_typevar(state, id);
|
1055
|
+
|
1056
|
+
if (state->next_token.type == pLT) {
|
992
1057
|
parser_advance(state);
|
993
|
-
|
994
|
-
|
1058
|
+
|
1059
|
+
if (state->next_token.type == kSINGLETON) {
|
1060
|
+
parser_advance(state);
|
1061
|
+
upper_bound = parse_singleton_type(state);
|
1062
|
+
} else {
|
1063
|
+
parser_advance(state);
|
1064
|
+
upper_bound = parse_instance_type(state, false);
|
995
1065
|
}
|
996
|
-
}
|
1066
|
+
}
|
1067
|
+
|
1068
|
+
VALUE location = rbs_new_location(state->buffer, param_range);
|
1069
|
+
rbs_loc *loc = rbs_check_location(location);
|
1070
|
+
rbs_loc_add_required_child(loc, rb_intern("name"), name_range);
|
1071
|
+
rbs_loc_add_optional_child(loc, rb_intern("variance"), variance_range);
|
1072
|
+
rbs_loc_add_optional_child(loc, rb_intern("unchecked"), unchecked_range);
|
1073
|
+
rbs_loc_add_optional_child(loc, rb_intern("upper_bound"), upper_bound_range);
|
1074
|
+
|
1075
|
+
VALUE param = rbs_ast_type_param(name, variance, unchecked, upper_bound, location);
|
1076
|
+
rb_ary_push(params, param);
|
1077
|
+
|
1078
|
+
if (state->next_token.type == pCOMMA) {
|
1079
|
+
parser_advance(state);
|
1080
|
+
}
|
1081
|
+
|
1082
|
+
if (state->next_token.type == pRBRACKET) {
|
997
1083
|
break;
|
998
1084
|
}
|
999
1085
|
}
|
1000
1086
|
|
1001
1087
|
parser_advance_assert(state, pRBRACKET);
|
1088
|
+
rg->end = state->current_token.range.end;
|
1089
|
+
} else {
|
1090
|
+
*rg = NULL_RANGE;
|
1002
1091
|
}
|
1003
1092
|
|
1004
|
-
|
1093
|
+
rb_funcall(
|
1094
|
+
RBS_AST_TypeParam,
|
1095
|
+
rb_intern("resolve_variables"),
|
1096
|
+
1,
|
1097
|
+
params
|
1098
|
+
);
|
1005
1099
|
|
1006
|
-
|
1100
|
+
return params;
|
1101
|
+
}
|
1007
1102
|
|
1008
|
-
|
1009
|
-
|
1010
|
-
|
1011
|
-
|
1103
|
+
/*
|
1104
|
+
method_type ::= {} type_params <function>
|
1105
|
+
*/
|
1106
|
+
VALUE parse_method_type(parserstate *state) {
|
1107
|
+
range rg;
|
1108
|
+
range params_range = NULL_RANGE;
|
1109
|
+
range type_range;
|
1110
|
+
|
1111
|
+
VALUE function = Qnil;
|
1112
|
+
VALUE block = Qnil;
|
1113
|
+
parser_push_typevar_table(state, false);
|
1114
|
+
|
1115
|
+
rg.start = state->next_token.range.start;
|
1116
|
+
|
1117
|
+
VALUE type_params = parse_type_params(state, ¶ms_range, false);
|
1118
|
+
|
1119
|
+
type_range.start = state->next_token.range.start;
|
1120
|
+
|
1121
|
+
parse_function(state, &function, &block);
|
1122
|
+
|
1123
|
+
rg.end = state->current_token.range.end;
|
1124
|
+
type_range.end = rg.end;
|
1012
1125
|
|
1013
1126
|
parser_pop_typevar_table(state);
|
1014
1127
|
|
1128
|
+
VALUE location = rbs_new_location(state->buffer, rg);
|
1129
|
+
rbs_loc *loc = rbs_check_location(location);
|
1130
|
+
rbs_loc_add_required_child(loc, rb_intern("type"), type_range);
|
1131
|
+
rbs_loc_add_optional_child(loc, rb_intern("type_params"), params_range);
|
1132
|
+
|
1015
1133
|
return rbs_method_type(
|
1016
1134
|
type_params,
|
1017
1135
|
function,
|
1018
1136
|
block,
|
1019
|
-
|
1137
|
+
location
|
1020
1138
|
);
|
1021
1139
|
}
|
1022
1140
|
|
@@ -1087,90 +1205,6 @@ VALUE parse_const_decl(parserstate *state) {
|
|
1087
1205
|
return rbs_ast_decl_constant(typename, type, location, comment);
|
1088
1206
|
}
|
1089
1207
|
|
1090
|
-
/*
|
1091
|
-
module_type_params ::= {} `[` module_type_param `,` ... <`]`>
|
1092
|
-
| {<>}
|
1093
|
-
|
1094
|
-
module_type_param ::= kUNCHECKED? (kIN|kOUT|) tUIDENT
|
1095
|
-
*/
|
1096
|
-
VALUE parse_module_type_params(parserstate *state, range *rg) {
|
1097
|
-
VALUE params = rbs_ast_decl_module_type_params();
|
1098
|
-
|
1099
|
-
if (state->next_token.type == pLBRACKET) {
|
1100
|
-
parser_advance(state);
|
1101
|
-
|
1102
|
-
rg->start = state->current_token.range.start;
|
1103
|
-
|
1104
|
-
while (true) {
|
1105
|
-
VALUE name;
|
1106
|
-
VALUE unchecked = Qfalse;
|
1107
|
-
VALUE variance = ID2SYM(rb_intern("invariant"));
|
1108
|
-
|
1109
|
-
range param_range = NULL_RANGE;
|
1110
|
-
range name_range;
|
1111
|
-
range variance_range = NULL_RANGE;
|
1112
|
-
range unchecked_range = NULL_RANGE;
|
1113
|
-
|
1114
|
-
param_range.start = state->next_token.range.start;
|
1115
|
-
|
1116
|
-
if (state->next_token.type == kUNCHECKED) {
|
1117
|
-
unchecked = Qtrue;
|
1118
|
-
parser_advance(state);
|
1119
|
-
unchecked_range = state->current_token.range;
|
1120
|
-
}
|
1121
|
-
|
1122
|
-
if (state->next_token.type == kIN || state->next_token.type == kOUT) {
|
1123
|
-
switch (state->next_token.type) {
|
1124
|
-
case kIN:
|
1125
|
-
variance = ID2SYM(rb_intern("contravariant"));
|
1126
|
-
break;
|
1127
|
-
case kOUT:
|
1128
|
-
variance = ID2SYM(rb_intern("covariant"));
|
1129
|
-
break;
|
1130
|
-
default:
|
1131
|
-
rbs_abort();
|
1132
|
-
}
|
1133
|
-
|
1134
|
-
parser_advance(state);
|
1135
|
-
variance_range = state->current_token.range;
|
1136
|
-
}
|
1137
|
-
|
1138
|
-
parser_advance_assert(state, tUIDENT);
|
1139
|
-
name_range = state->current_token.range;
|
1140
|
-
param_range.end = state->current_token.range.end;
|
1141
|
-
|
1142
|
-
ID id = INTERN_TOKEN(state, state->current_token);
|
1143
|
-
name = ID2SYM(id);
|
1144
|
-
|
1145
|
-
parser_insert_typevar(state, id);
|
1146
|
-
|
1147
|
-
VALUE location = rbs_new_location(state->buffer, param_range);
|
1148
|
-
rbs_loc *loc = rbs_check_location(location);
|
1149
|
-
rbs_loc_add_required_child(loc, rb_intern("name"), name_range);
|
1150
|
-
rbs_loc_add_optional_child(loc, rb_intern("variance"), variance_range);
|
1151
|
-
rbs_loc_add_optional_child(loc, rb_intern("unchecked"), unchecked_range);
|
1152
|
-
|
1153
|
-
VALUE param = rbs_ast_decl_module_type_params_param(name, variance, unchecked, location);
|
1154
|
-
rb_funcall(params, rb_intern("add"), 1, param);
|
1155
|
-
|
1156
|
-
if (state->next_token.type == pCOMMA) {
|
1157
|
-
parser_advance(state);
|
1158
|
-
}
|
1159
|
-
|
1160
|
-
if (state->next_token.type == pRBRACKET) {
|
1161
|
-
break;
|
1162
|
-
}
|
1163
|
-
}
|
1164
|
-
|
1165
|
-
parser_advance_assert(state, pRBRACKET);
|
1166
|
-
rg->end = state->current_token.range.end;
|
1167
|
-
} else {
|
1168
|
-
*rg = NULL_RANGE;
|
1169
|
-
}
|
1170
|
-
|
1171
|
-
return params;
|
1172
|
-
}
|
1173
|
-
|
1174
1208
|
/*
|
1175
1209
|
type_decl ::= {kTYPE} alias_name `=` <type>
|
1176
1210
|
*/
|
@@ -1188,7 +1222,7 @@ VALUE parse_type_decl(parserstate *state, position comment_pos, VALUE annotation
|
|
1188
1222
|
parser_advance(state);
|
1189
1223
|
VALUE typename = parse_type_name(state, ALIAS_NAME, &name_range);
|
1190
1224
|
|
1191
|
-
VALUE type_params =
|
1225
|
+
VALUE type_params = parse_type_params(state, ¶ms_range, true);
|
1192
1226
|
|
1193
1227
|
parser_advance_assert(state, pEQ);
|
1194
1228
|
eq_range = state->current_token.range;
|
@@ -1968,7 +2002,7 @@ VALUE parse_interface_decl(parserstate *state, position comment_pos, VALUE annot
|
|
1968
2002
|
parser_advance(state);
|
1969
2003
|
|
1970
2004
|
VALUE name = parse_type_name(state, INTERFACE_NAME, &name_range);
|
1971
|
-
VALUE params =
|
2005
|
+
VALUE params = parse_type_params(state, &type_params_range, true);
|
1972
2006
|
VALUE members = parse_interface_members(state);
|
1973
2007
|
|
1974
2008
|
parser_advance_assert(state, kEND);
|
@@ -2132,7 +2166,7 @@ VALUE parse_module_decl(parserstate *state, position comment_pos, VALUE annotati
|
|
2132
2166
|
|
2133
2167
|
parser_advance(state);
|
2134
2168
|
VALUE module_name = parse_type_name(state, CLASS_NAME, &name_range);
|
2135
|
-
VALUE type_params =
|
2169
|
+
VALUE type_params = parse_type_params(state, &type_params_range, true);
|
2136
2170
|
VALUE self_types = rb_ary_new();
|
2137
2171
|
|
2138
2172
|
if (state->next_token.type == pCOLON) {
|
@@ -2239,7 +2273,7 @@ VALUE parse_class_decl(parserstate *state, position comment_pos, VALUE annotatio
|
|
2239
2273
|
|
2240
2274
|
parser_advance(state);
|
2241
2275
|
name = parse_type_name(state, CLASS_NAME, &name_range);
|
2242
|
-
type_params =
|
2276
|
+
type_params = parse_type_params(state, &type_params_range, true);
|
2243
2277
|
super = parse_class_decl_super(state, <_range);
|
2244
2278
|
members = parse_module_members(state);
|
2245
2279
|
parser_advance_assert(state, kEND);
|
@@ -130,8 +130,7 @@ bool parser_advance_if(parserstate *state, enum TokenType type) {
|
|
130
130
|
}
|
131
131
|
}
|
132
132
|
|
133
|
-
void
|
134
|
-
parser_advance(state);
|
133
|
+
void parser_assert(parserstate *state, enum TokenType type) {
|
135
134
|
if (state->current_token.type != type) {
|
136
135
|
raise_syntax_error(
|
137
136
|
state,
|
@@ -142,6 +141,11 @@ void parser_advance_assert(parserstate *state, enum TokenType type) {
|
|
142
141
|
}
|
143
142
|
}
|
144
143
|
|
144
|
+
void parser_advance_assert(parserstate *state, enum TokenType type) {
|
145
|
+
parser_advance(state);
|
146
|
+
parser_assert(state, type);
|
147
|
+
}
|
148
|
+
|
145
149
|
void print_token(token tok) {
|
146
150
|
printf(
|
147
151
|
"%s char=%d...%d\n",
|
@@ -107,11 +107,21 @@ void free_parser(parserstate *parser);
|
|
107
107
|
* Advance one token.
|
108
108
|
* */
|
109
109
|
void parser_advance(parserstate *state);
|
110
|
+
|
111
|
+
/**
|
112
|
+
* @brief Raises an exception if `current_token->type != type`.
|
113
|
+
*
|
114
|
+
* @param state
|
115
|
+
* @param type
|
116
|
+
*/
|
117
|
+
void parser_assert(parserstate *state, enum TokenType type);
|
118
|
+
|
110
119
|
/**
|
111
120
|
* Advance one token, and assert the current token type.
|
112
121
|
* Raises an exception if `current_token->type != type`.
|
113
122
|
* */
|
114
123
|
void parser_advance_assert(parserstate *state, enum TokenType type);
|
124
|
+
|
115
125
|
/**
|
116
126
|
* Advance one token if the next_token is a token of the type.
|
117
127
|
* */
|
@@ -294,22 +294,20 @@ VALUE rbs_ast_annotation(VALUE string, VALUE location) {
|
|
294
294
|
);
|
295
295
|
}
|
296
296
|
|
297
|
-
VALUE
|
298
|
-
return rb_funcall(RBS_AST_Declarations_ModuleTypeParams, rb_intern("new"), 0);
|
299
|
-
}
|
300
|
-
|
301
|
-
VALUE rbs_ast_decl_module_type_params_param(VALUE name, VALUE variance, VALUE skip_validation, VALUE location) {
|
297
|
+
VALUE rbs_ast_type_param(VALUE name, VALUE variance, bool unchecked, VALUE upper_bound, VALUE location) {
|
302
298
|
VALUE args = rb_hash_new();
|
303
299
|
rb_hash_aset(args, ID2SYM(rb_intern("name")), name);
|
304
300
|
rb_hash_aset(args, ID2SYM(rb_intern("variance")), variance);
|
305
|
-
rb_hash_aset(args, ID2SYM(rb_intern("
|
301
|
+
rb_hash_aset(args, ID2SYM(rb_intern("upper_bound")), upper_bound);
|
306
302
|
rb_hash_aset(args, ID2SYM(rb_intern("location")), location);
|
307
303
|
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
304
|
+
VALUE type_param = CLASS_NEW_INSTANCE(RBS_AST_TypeParam, 1, &args);
|
305
|
+
|
306
|
+
if (unchecked) {
|
307
|
+
rb_funcall(type_param, rb_intern("unchecked!"), 0);
|
308
|
+
}
|
309
|
+
|
310
|
+
return type_param;
|
313
311
|
}
|
314
312
|
|
315
313
|
VALUE rbs_ast_decl_constant(VALUE name, VALUE type, VALUE location, VALUE comment) {
|
@@ -6,6 +6,7 @@
|
|
6
6
|
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
|
+
VALUE rbs_ast_type_param(VALUE name, VALUE variance, bool unchecked, VALUE upper_bound, VALUE location);
|
9
10
|
VALUE rbs_ast_decl_alias(VALUE name, VALUE type_params, VALUE type, VALUE annotations, VALUE location, VALUE comment);
|
10
11
|
VALUE rbs_ast_decl_class_super(VALUE name, VALUE args, VALUE location);
|
11
12
|
VALUE rbs_ast_decl_class(VALUE name, VALUE type_params, VALUE super_class, VALUE members, VALUE annotations, VALUE location, VALUE comment);
|
@@ -13,8 +14,6 @@ VALUE rbs_ast_decl_constant(VALUE name, VALUE type, VALUE location, VALUE commen
|
|
13
14
|
VALUE rbs_ast_decl_global(VALUE name, VALUE type, VALUE location, VALUE comment);
|
14
15
|
VALUE rbs_ast_decl_interface(VALUE name, VALUE type_params, VALUE members, VALUE annotations, VALUE location, VALUE comment);
|
15
16
|
VALUE rbs_ast_decl_module_self(VALUE name, VALUE args, VALUE location);
|
16
|
-
VALUE rbs_ast_decl_module_type_params_param(VALUE name, VALUE variance, VALUE skip_validation, VALUE location);
|
17
|
-
VALUE rbs_ast_decl_module_type_params();
|
18
17
|
VALUE rbs_ast_decl_module(VALUE name, VALUE type_params, VALUE self_types, VALUE members, VALUE annotations, VALUE location, VALUE comment);
|
19
18
|
VALUE rbs_ast_members_alias(VALUE new_name, VALUE old_name, VALUE kind, VALUE annotations, VALUE location, VALUE comment);
|
20
19
|
VALUE rbs_ast_members_attribute(VALUE klass, VALUE name, VALUE type, VALUE ivar_name, VALUE kind, VALUE annotations, VALUE location, VALUE comment);
|
data/lib/rbs/ast/declarations.rb
CHANGED
@@ -4,103 +4,6 @@ module RBS
|
|
4
4
|
class Base
|
5
5
|
end
|
6
6
|
|
7
|
-
class ModuleTypeParams
|
8
|
-
attr_reader :params
|
9
|
-
|
10
|
-
TypeParam = _ = Struct.new(:name, :variance, :skip_validation, :location, keyword_init: true) do
|
11
|
-
# @implements TypeParam
|
12
|
-
|
13
|
-
def to_json(state = _ = nil)
|
14
|
-
{
|
15
|
-
name: name,
|
16
|
-
variance: variance,
|
17
|
-
skip_validation: skip_validation,
|
18
|
-
}.to_json(state)
|
19
|
-
end
|
20
|
-
|
21
|
-
def ==(other)
|
22
|
-
other.is_a?(TypeParam) &&
|
23
|
-
other.name == name &&
|
24
|
-
other.variance == variance &&
|
25
|
-
other.skip_validation == skip_validation
|
26
|
-
end
|
27
|
-
|
28
|
-
alias eql? ==
|
29
|
-
|
30
|
-
def hash
|
31
|
-
self.class.hash ^ name.hash ^ variance.hash ^ skip_validation.hash
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
def initialize()
|
36
|
-
@params = []
|
37
|
-
end
|
38
|
-
|
39
|
-
def add(param)
|
40
|
-
params << param
|
41
|
-
self
|
42
|
-
end
|
43
|
-
|
44
|
-
def ==(other)
|
45
|
-
other.is_a?(ModuleTypeParams) && other.params == params
|
46
|
-
end
|
47
|
-
|
48
|
-
alias eql? ==
|
49
|
-
|
50
|
-
def hash
|
51
|
-
params.hash
|
52
|
-
end
|
53
|
-
|
54
|
-
def [](name)
|
55
|
-
params.find {|p| p.name == name }
|
56
|
-
end
|
57
|
-
|
58
|
-
def to_json(state = _ = nil)
|
59
|
-
{
|
60
|
-
params: params
|
61
|
-
}.to_json(state)
|
62
|
-
end
|
63
|
-
|
64
|
-
def each(&block)
|
65
|
-
if block
|
66
|
-
params.each(&block)
|
67
|
-
else
|
68
|
-
params.each
|
69
|
-
end
|
70
|
-
end
|
71
|
-
|
72
|
-
def self.empty
|
73
|
-
new
|
74
|
-
end
|
75
|
-
|
76
|
-
def variance(name)
|
77
|
-
var = self[name] or raise
|
78
|
-
var.variance
|
79
|
-
end
|
80
|
-
|
81
|
-
def skip_validation?(name)
|
82
|
-
var = self[name] or raise
|
83
|
-
var.skip_validation
|
84
|
-
end
|
85
|
-
|
86
|
-
def empty?
|
87
|
-
params.empty?
|
88
|
-
end
|
89
|
-
|
90
|
-
def size
|
91
|
-
params.size
|
92
|
-
end
|
93
|
-
|
94
|
-
def rename_to(names)
|
95
|
-
ModuleTypeParams.new().tap do |params|
|
96
|
-
names.each.with_index do |new_name, index|
|
97
|
-
param = self.params[index]
|
98
|
-
params.add(TypeParam.new(name: new_name, variance: param.variance, skip_validation: param.skip_validation, location: param.location))
|
99
|
-
end
|
100
|
-
end
|
101
|
-
end
|
102
|
-
end
|
103
|
-
|
104
7
|
module NestedDeclarationHelper
|
105
8
|
def each_member
|
106
9
|
if block_given?
|