Skip to content

Commit 3e50e9f

Browse files
committed
Implement parser
1 parent e853fd0 commit 3e50e9f

2 files changed

Lines changed: 103 additions & 0 deletions

File tree

src/parser.c

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,41 @@
6464
case kRETURN: \
6565
/* nop */
6666

67+
#define PARAM_NAME_CASES \
68+
case kBOOL:\
69+
case kBOT: \
70+
case kCLASS: \
71+
case kFALSE: \
72+
case kINSTANCE: \
73+
case kINTERFACE: \
74+
case kNIL: \
75+
case kSELF: \
76+
case kSINGLETON: \
77+
case kTOP: \
78+
case kTRUE: \
79+
case kVOID: \
80+
case kTYPE: \
81+
case kUNCHECKED: \
82+
case kIN: \
83+
case kOUT: \
84+
case kEND: \
85+
case kDEF: \
86+
case kINCLUDE: \
87+
case kEXTEND: \
88+
case kPREPEND: \
89+
case kALIAS: \
90+
case kMODULE: \
91+
case kATTRREADER: \
92+
case kATTRWRITER: \
93+
case kATTRACCESSOR: \
94+
case kPUBLIC: \
95+
case kPRIVATE: \
96+
case kUNTYPED: \
97+
case kUSE: \
98+
case kAS: \
99+
case k__TODO__: \
100+
/* nop */
101+
67102
#define CHECK_PARSE(call) \
68103
if (!call) { \
69104
return false; \
@@ -3541,6 +3576,49 @@ static bool parse_inline_comment(rbs_parser_t *parser, rbs_location_t **comment)
35413576
return true;
35423577
}
35433578

3579+
NODISCARD
3580+
static bool parse_inline_param_type_annotation(rbs_parser_t *parser, rbs_ast_ruby_annotations_t **annotation, rbs_range_t rbs_range) {
3581+
rbs_parser_advance(parser);
3582+
3583+
rbs_range_t name_range = parser->current_token.range;
3584+
rbs_location_t *name_loc = rbs_location_new(ALLOCATOR(), name_range);
3585+
3586+
ADVANCE_ASSERT(parser, pCOLON);
3587+
3588+
rbs_range_t colon_range = parser->current_token.range;
3589+
rbs_location_t *colon_loc = rbs_location_new(ALLOCATOR(), colon_range);
3590+
3591+
rbs_node_t *param_type = NULL;
3592+
if (!rbs_parse_type(parser, &param_type)) {
3593+
return false;
3594+
}
3595+
3596+
rbs_location_t *comment_loc = NULL;
3597+
if (!parse_inline_comment(parser, &comment_loc)) {
3598+
return false;
3599+
}
3600+
3601+
rbs_range_t full_range = {
3602+
.start = rbs_range.start,
3603+
.end = parser->current_token.range.end
3604+
};
3605+
3606+
rbs_location_t *full_loc = rbs_location_new(ALLOCATOR(), full_range);
3607+
rbs_location_t *rbs_loc = rbs_location_new(ALLOCATOR(), rbs_range);
3608+
3609+
*annotation = (rbs_ast_ruby_annotations_t *) rbs_ast_ruby_annotations_param_type_annotation_new(
3610+
ALLOCATOR(),
3611+
full_loc,
3612+
rbs_loc,
3613+
name_loc,
3614+
colon_loc,
3615+
param_type,
3616+
comment_loc
3617+
);
3618+
3619+
return true;
3620+
}
3621+
35443622
NODISCARD
35453623
static bool parse_inline_leading_annotation(rbs_parser_t *parser, rbs_ast_ruby_annotations_t **annotation) {
35463624
switch (parser->next_token.type) {
@@ -3673,6 +3751,11 @@ static bool parse_inline_leading_annotation(rbs_parser_t *parser, rbs_ast_ruby_a
36733751
);
36743752
return true;
36753753
}
3754+
case tLIDENT:
3755+
PARAM_NAME_CASES
3756+
{
3757+
return parse_inline_param_type_annotation(parser, annotation, rbs_range);
3758+
}
36763759
default: {
36773760
rbs_parser_set_error(parser, parser->next_token, true, "unexpected token for @rbs annotation");
36783761
return false;

test/rbs/inline_annotation_parsing_test.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,24 @@ def test_parse__return
115115
assert_equal "-- some comment here", annot.comment_location.source
116116
end
117117
end
118+
119+
def test_parse__param_type
120+
Parser.parse_inline_leading_annotation("@rbs x: untyped", 0...).tap do |annot|
121+
assert_instance_of AST::Ruby::Annotations::ParamTypeAnnotation, annot
122+
assert_equal "@rbs x: untyped", annot.location.source
123+
assert_equal "x", annot.name_location.source
124+
assert_equal ":", annot.colon_location.source
125+
assert_equal "untyped", annot.param_type.location.source
126+
assert_nil annot.comment_location
127+
end
128+
129+
Parser.parse_inline_leading_annotation("@rbs abc: untyped -- some comment here", 0...).tap do |annot|
130+
assert_instance_of AST::Ruby::Annotations::ParamTypeAnnotation, annot
131+
assert_equal "@rbs abc: untyped -- some comment here", annot.location.source
132+
assert_equal "abc", annot.name_location.source
133+
assert_equal ":", annot.colon_location.source
134+
assert_equal "untyped", annot.param_type.location.source
135+
assert_equal "-- some comment here", annot.comment_location.source
136+
end
137+
end
118138
end

0 commit comments

Comments
 (0)