Skip to content

Commit 21752fb

Browse files
st0012soutarodependabot[bot]amomchilov
authored
Merge master and support upstream's parser enhancements (#41)
`ruby/rbs` recently received a few enhancements for new keywords and inline parsing: - ruby#2403 - ruby#2405 - ruby#2406 So the major change of this PR is to reimplement them under the `c-api` structure. --------- Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: Soutaro Matsumoto <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Alexander Momchilov <[email protected]>
1 parent 563e081 commit 21752fb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+4018
-1166
lines changed

Gemfile.lock

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ GEM
3434
benchmark-ips (2.14.0)
3535
bigdecimal (3.1.9)
3636
concurrent-ruby (1.3.5)
37-
connection_pool (2.5.0)
38-
csv (3.3.3)
37+
connection_pool (2.5.1)
38+
csv (3.3.4)
3939
dbm (1.1.0)
4040
diff-lcs (1.6.1)
4141
digest (3.2.0)
4242
drb (2.2.1)
43-
ffi (1.17.1)
43+
ffi (1.17.2)
4444
fileutils (1.7.3)
4545
goodcheck (3.1.0)
4646
marcel (>= 1.0, < 2.0)
@@ -69,12 +69,12 @@ GEM
6969
net-smtp (0.5.1)
7070
net-protocol
7171
nkf (0.2.0)
72-
nokogiri (1.18.7)
72+
nokogiri (1.18.8)
7373
mini_portile2 (~> 2.8.2)
7474
racc (~> 1.4)
7575
ostruct (0.6.1)
76-
parallel (1.26.3)
77-
parser (3.3.7.4)
76+
parallel (1.27.0)
77+
parser (3.3.8.0)
7878
ast (~> 2.4.1)
7979
racc
8080
pathname (0.4.0)
@@ -90,7 +90,7 @@ GEM
9090
racc (1.8.1)
9191
rainbow (3.1.1)
9292
rake (13.2.1)
93-
rake-compiler (1.2.9)
93+
rake-compiler (1.3.0)
9494
rake
9595
rb-fsevent (0.11.2)
9696
rb-inotify (0.11.1)
@@ -111,7 +111,7 @@ GEM
111111
diff-lcs (>= 1.2.0, < 2.0)
112112
rspec-support (~> 3.13.0)
113113
rspec-support (3.13.2)
114-
rubocop (1.75.2)
114+
rubocop (1.75.3)
115115
json (~> 2.3)
116116
language_server-protocol (~> 3.17.0.2)
117117
lint_roller (~> 1.1.0)
@@ -122,7 +122,7 @@ GEM
122122
rubocop-ast (>= 1.44.0, < 2.0)
123123
ruby-progressbar (~> 1.7)
124124
unicode-display_width (>= 2.4.0, < 4.0)
125-
rubocop-ast (1.44.0)
125+
rubocop-ast (1.44.1)
126126
parser (>= 3.3.7.2)
127127
prism (~> 1.4)
128128
rubocop-on-rbs (1.5.0)
@@ -155,9 +155,9 @@ GEM
155155
strscan (>= 1.0.0)
156156
terminal-table (>= 2, < 5)
157157
uri (>= 0.12.0)
158-
stringio (3.1.6)
158+
stringio (3.1.7)
159159
strong_json (2.1.2)
160-
strscan (3.1.2)
160+
strscan (3.1.3)
161161
tempfile (0.3.1)
162162
terminal-table (4.0.0)
163163
unicode-display_width (>= 1.1.1, < 4)

config.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,3 +443,45 @@ nodes:
443443
fields:
444444
- name: name
445445
c_type: rbs_ast_symbol
446+
- name: RBS::AST::Ruby::Annotations::NodeTypeAssertion
447+
fields:
448+
- name: prefix_location
449+
c_type: rbs_location
450+
- name: type
451+
c_type: rbs_node
452+
- name: RBS::AST::Ruby::Annotations::ColonMethodTypeAnnotation
453+
fields:
454+
- name: prefix_location
455+
c_type: rbs_location
456+
- name: annotations
457+
c_type: rbs_node_list
458+
- name: method_type
459+
c_type: rbs_node
460+
- name: RBS::AST::Ruby::Annotations::MethodTypesAnnotation
461+
fields:
462+
- name: prefix_location
463+
c_type: rbs_location
464+
- name: overloads
465+
c_type: rbs_node_list
466+
- name: vertical_bar_locations
467+
c_type: rbs_location_list
468+
- name: RBS::AST::Ruby::Annotations::SkipAnnotation
469+
fields:
470+
- name: prefix_location
471+
c_type: rbs_location
472+
- name: skip_location
473+
c_type: rbs_location
474+
- name: comment_location
475+
c_type: rbs_location
476+
- name: RBS::AST::Ruby::Annotations::ReturnTypeAnnotation
477+
fields:
478+
- name: prefix_location
479+
c_type: rbs_location
480+
- name: return_location
481+
c_type: rbs_location
482+
- name: colon_location
483+
c_type: rbs_location
484+
- name: return_type
485+
c_type: rbs_node
486+
- name: comment_location
487+
c_type: rbs_location

ext/rbs_extension/ast_translation.c

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ VALUE rbs_hash_to_ruby_hash(rbs_translation_context_t ctx, rbs_hash_t *rbs_hash)
4444
}
4545

4646
VALUE rbs_loc_to_ruby_location(rbs_translation_context_t ctx, rbs_location_t *source_loc) {
47+
if (source_loc == NULL) {
48+
return Qnil;
49+
}
50+
4751
VALUE new_loc = rbs_new_location(ctx.buffer, source_loc->rg);
4852
rbs_loc *new_loc_struct = rbs_check_location(new_loc);
4953

@@ -55,6 +59,20 @@ VALUE rbs_loc_to_ruby_location(rbs_translation_context_t ctx, rbs_location_t *so
5559
return new_loc;
5660
}
5761

62+
VALUE rbs_location_list_to_ruby_array(rbs_translation_context_t ctx, rbs_location_list_t *list) {
63+
VALUE ruby_array = rb_ary_new();
64+
65+
if (list == NULL) {
66+
return ruby_array;
67+
}
68+
69+
for (rbs_location_list_node_t *n = list->head; n != NULL; n = n->next) {
70+
rb_ary_push(ruby_array, rbs_loc_to_ruby_location(ctx, n->loc));
71+
}
72+
73+
return ruby_array;
74+
}
75+
5876
#ifdef RB_PASS_KEYWORDS
5977
// Ruby 2.7 or later
6078
#define CLASS_NEW_INSTANCE(klass, argc, argv)\
@@ -586,6 +604,87 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan
586604
&h
587605
);
588606
}
607+
case RBS_AST_RUBY_ANNOTATIONS_COLON_METHOD_TYPE_ANNOTATION: {
608+
rbs_ast_ruby_annotations_colon_method_type_annotation_t *node = (rbs_ast_ruby_annotations_colon_method_type_annotation_t *)instance;
609+
610+
VALUE h = rb_hash_new();
611+
rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location));
612+
rb_hash_aset(h, ID2SYM(rb_intern("prefix_location")), rbs_loc_to_ruby_location(ctx, node->prefix_location));
613+
rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations));
614+
rb_hash_aset(h, ID2SYM(rb_intern("method_type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->method_type)); // rbs_node
615+
616+
617+
return CLASS_NEW_INSTANCE(
618+
RBS_AST_Ruby_Annotations_ColonMethodTypeAnnotation,
619+
1,
620+
&h
621+
);
622+
}
623+
case RBS_AST_RUBY_ANNOTATIONS_METHOD_TYPES_ANNOTATION: {
624+
rbs_ast_ruby_annotations_method_types_annotation_t *node = (rbs_ast_ruby_annotations_method_types_annotation_t *)instance;
625+
626+
VALUE h = rb_hash_new();
627+
rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location));
628+
rb_hash_aset(h, ID2SYM(rb_intern("prefix_location")), rbs_loc_to_ruby_location(ctx, node->prefix_location));
629+
rb_hash_aset(h, ID2SYM(rb_intern("overloads")), rbs_node_list_to_ruby_array(ctx, node->overloads));
630+
rb_hash_aset(h, ID2SYM(rb_intern("vertical_bar_locations")), rbs_location_list_to_ruby_array(ctx, node->vertical_bar_locations));
631+
632+
633+
return CLASS_NEW_INSTANCE(
634+
RBS_AST_Ruby_Annotations_MethodTypesAnnotation,
635+
1,
636+
&h
637+
);
638+
}
639+
case RBS_AST_RUBY_ANNOTATIONS_NODE_TYPE_ASSERTION: {
640+
rbs_ast_ruby_annotations_node_type_assertion_t *node = (rbs_ast_ruby_annotations_node_type_assertion_t *)instance;
641+
642+
VALUE h = rb_hash_new();
643+
rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location));
644+
rb_hash_aset(h, ID2SYM(rb_intern("prefix_location")), rbs_loc_to_ruby_location(ctx, node->prefix_location));
645+
rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node
646+
647+
648+
return CLASS_NEW_INSTANCE(
649+
RBS_AST_Ruby_Annotations_NodeTypeAssertion,
650+
1,
651+
&h
652+
);
653+
}
654+
case RBS_AST_RUBY_ANNOTATIONS_RETURN_TYPE_ANNOTATION: {
655+
rbs_ast_ruby_annotations_return_type_annotation_t *node = (rbs_ast_ruby_annotations_return_type_annotation_t *)instance;
656+
657+
VALUE h = rb_hash_new();
658+
rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location));
659+
rb_hash_aset(h, ID2SYM(rb_intern("prefix_location")), rbs_loc_to_ruby_location(ctx, node->prefix_location));
660+
rb_hash_aset(h, ID2SYM(rb_intern("return_location")), rbs_loc_to_ruby_location(ctx, node->return_location));
661+
rb_hash_aset(h, ID2SYM(rb_intern("colon_location")), rbs_loc_to_ruby_location(ctx, node->colon_location));
662+
rb_hash_aset(h, ID2SYM(rb_intern("return_type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->return_type)); // rbs_node
663+
rb_hash_aset(h, ID2SYM(rb_intern("comment_location")), rbs_loc_to_ruby_location(ctx, node->comment_location));
664+
665+
666+
return CLASS_NEW_INSTANCE(
667+
RBS_AST_Ruby_Annotations_ReturnTypeAnnotation,
668+
1,
669+
&h
670+
);
671+
}
672+
case RBS_AST_RUBY_ANNOTATIONS_SKIP_ANNOTATION: {
673+
rbs_ast_ruby_annotations_skip_annotation_t *node = (rbs_ast_ruby_annotations_skip_annotation_t *)instance;
674+
675+
VALUE h = rb_hash_new();
676+
rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location));
677+
rb_hash_aset(h, ID2SYM(rb_intern("prefix_location")), rbs_loc_to_ruby_location(ctx, node->prefix_location));
678+
rb_hash_aset(h, ID2SYM(rb_intern("skip_location")), rbs_loc_to_ruby_location(ctx, node->skip_location));
679+
rb_hash_aset(h, ID2SYM(rb_intern("comment_location")), rbs_loc_to_ruby_location(ctx, node->comment_location));
680+
681+
682+
return CLASS_NEW_INSTANCE(
683+
RBS_AST_Ruby_Annotations_SkipAnnotation,
684+
1,
685+
&h
686+
);
687+
}
589688
case RBS_AST_STRING: {
590689
rbs_ast_string_t *string_node = (rbs_ast_string_t *) instance;
591690
rbs_string_t s = string_node->string;

ext/rbs_extension/class_constants.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ VALUE RBS_AST;
1414
VALUE RBS_AST_Declarations;
1515
VALUE RBS_AST_Directives;
1616
VALUE RBS_AST_Members;
17+
VALUE RBS_AST_Ruby;
18+
VALUE RBS_AST_Ruby_Annotations;
1719
VALUE RBS_Parser;
1820
VALUE RBS_Types;
1921
VALUE RBS_Types_Bases;
@@ -47,6 +49,11 @@ VALUE RBS_AST_Members_MethodDefinition_Overload;
4749
VALUE RBS_AST_Members_Prepend;
4850
VALUE RBS_AST_Members_Private;
4951
VALUE RBS_AST_Members_Public;
52+
VALUE RBS_AST_Ruby_Annotations_ColonMethodTypeAnnotation;
53+
VALUE RBS_AST_Ruby_Annotations_MethodTypesAnnotation;
54+
VALUE RBS_AST_Ruby_Annotations_NodeTypeAssertion;
55+
VALUE RBS_AST_Ruby_Annotations_ReturnTypeAnnotation;
56+
VALUE RBS_AST_Ruby_Annotations_SkipAnnotation;
5057
VALUE RBS_AST_TypeParam;
5158
VALUE RBS_MethodType;
5259
VALUE RBS_Namespace;
@@ -89,6 +96,8 @@ void rbs__init_constants(void) {
8996
IMPORT_CONSTANT(RBS_AST_Declarations, RBS_AST, "Declarations");
9097
IMPORT_CONSTANT(RBS_AST_Directives, RBS_AST, "Directives");
9198
IMPORT_CONSTANT(RBS_AST_Members, RBS_AST, "Members");
99+
IMPORT_CONSTANT(RBS_AST_Ruby, RBS_AST, "Ruby");
100+
IMPORT_CONSTANT(RBS_AST_Ruby_Annotations, RBS_AST_Ruby, "Annotations");
92101
IMPORT_CONSTANT(RBS_Types, RBS, "Types");
93102
IMPORT_CONSTANT(RBS_Types_Bases, RBS_Types, "Bases");
94103

@@ -121,6 +130,11 @@ void rbs__init_constants(void) {
121130
IMPORT_CONSTANT(RBS_AST_Members_Prepend, RBS_AST_Members, "Prepend");
122131
IMPORT_CONSTANT(RBS_AST_Members_Private, RBS_AST_Members, "Private");
123132
IMPORT_CONSTANT(RBS_AST_Members_Public, RBS_AST_Members, "Public");
133+
IMPORT_CONSTANT(RBS_AST_Ruby_Annotations_ColonMethodTypeAnnotation, RBS_AST_Ruby_Annotations, "ColonMethodTypeAnnotation");
134+
IMPORT_CONSTANT(RBS_AST_Ruby_Annotations_MethodTypesAnnotation, RBS_AST_Ruby_Annotations, "MethodTypesAnnotation");
135+
IMPORT_CONSTANT(RBS_AST_Ruby_Annotations_NodeTypeAssertion, RBS_AST_Ruby_Annotations, "NodeTypeAssertion");
136+
IMPORT_CONSTANT(RBS_AST_Ruby_Annotations_ReturnTypeAnnotation, RBS_AST_Ruby_Annotations, "ReturnTypeAnnotation");
137+
IMPORT_CONSTANT(RBS_AST_Ruby_Annotations_SkipAnnotation, RBS_AST_Ruby_Annotations, "SkipAnnotation");
124138
IMPORT_CONSTANT(RBS_AST_TypeParam, RBS_AST, "TypeParam");
125139
IMPORT_CONSTANT(RBS_MethodType, RBS, "MethodType");
126140
IMPORT_CONSTANT(RBS_Namespace, RBS, "Namespace");

ext/rbs_extension/class_constants.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ extern VALUE RBS_AST;
1616
extern VALUE RBS_AST_Declarations;
1717
extern VALUE RBS_AST_Directives;
1818
extern VALUE RBS_AST_Members;
19+
extern VALUE RBS_AST_Ruby;
20+
extern VALUE RBS_AST_Ruby_Annotations;
1921
extern VALUE RBS_Types;
2022
extern VALUE RBS_Types_Bases;
2123
extern VALUE RBS_ParsingError;
@@ -49,6 +51,11 @@ extern VALUE RBS_AST_Members_MethodDefinition_Overload;
4951
extern VALUE RBS_AST_Members_Prepend;
5052
extern VALUE RBS_AST_Members_Private;
5153
extern VALUE RBS_AST_Members_Public;
54+
extern VALUE RBS_AST_Ruby_Annotations_ColonMethodTypeAnnotation;
55+
extern VALUE RBS_AST_Ruby_Annotations_MethodTypesAnnotation;
56+
extern VALUE RBS_AST_Ruby_Annotations_NodeTypeAssertion;
57+
extern VALUE RBS_AST_Ruby_Annotations_ReturnTypeAnnotation;
58+
extern VALUE RBS_AST_Ruby_Annotations_SkipAnnotation;
5259
extern VALUE RBS_AST_TypeParam;
5360
extern VALUE RBS_MethodType;
5461
extern VALUE RBS_Namespace;

ext/rbs_extension/legacy_location.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,8 @@ void rbs__init_location(void) {
305305
rb_define_private_method(RBS_Location, "initialize", location_initialize, 3);
306306
rb_define_private_method(RBS_Location, "initialize_copy", location_initialize_copy, 1);
307307
rb_define_method(RBS_Location, "buffer", location_buffer, 0);
308-
rb_define_method(RBS_Location, "start_pos", location_start_pos, 0);
309-
rb_define_method(RBS_Location, "end_pos", location_end_pos, 0);
308+
rb_define_method(RBS_Location, "_start_pos", location_start_pos, 0);
309+
rb_define_method(RBS_Location, "_end_pos", location_end_pos, 0);
310310
rb_define_method(RBS_Location, "_add_required_child", location_add_required_child, 3);
311311
rb_define_method(RBS_Location, "_add_optional_child", location_add_optional_child, 3);
312312
rb_define_method(RBS_Location, "_add_optional_no_child", location_add_optional_no_child, 1);

0 commit comments

Comments
 (0)