Skip to content

Commit d86ab02

Browse files
authored
Merge pull request #2678 from ruby/fix-eol-type
Move `require_eof` handling to C API
2 parents 2f9832a + d64f6e9 commit d86ab02

File tree

4 files changed

+35
-14
lines changed

4 files changed

+35
-14
lines changed

ext/rbs_extension/main.c

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -208,18 +208,10 @@ static VALUE parse_method_type_try(VALUE a) {
208208
}
209209

210210
rbs_method_type_t *method_type = NULL;
211-
rbs_parse_method_type(parser, &method_type);
211+
rbs_parse_method_type(parser, &method_type, RB_TEST(arg->require_eof));
212212

213213
raise_error_if_any(parser, arg->buffer);
214214

215-
if (RB_TEST(arg->require_eof)) {
216-
rbs_parser_advance(parser);
217-
if (parser->current_token.type != pEOF) {
218-
rbs_parser_set_error(parser, parser->current_token, true, "expected a token `%s`", rbs_token_type_str(pEOF));
219-
raise_error(parser->error, arg->buffer);
220-
}
221-
}
222-
223215
rbs_translation_context_t ctx = rbs_translation_context_create(
224216
&parser->constant_pool,
225217
arg->buffer,

include/rbs/parser.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ rbs_ast_comment_t *rbs_parser_get_comment(rbs_parser_t *parser, int subject_line
127127
void rbs_parser_set_error(rbs_parser_t *parser, rbs_token_t tok, bool syntax_error, const char *fmt, ...) RBS_ATTRIBUTE_FORMAT(4, 5);
128128

129129
bool rbs_parse_type(rbs_parser_t *parser, rbs_node_t **type, bool void_allowed, bool self_allowed);
130-
bool rbs_parse_method_type(rbs_parser_t *parser, rbs_method_type_t **method_type);
130+
bool rbs_parse_method_type(rbs_parser_t *parser, rbs_method_type_t **method_type, bool require_eof);
131131
bool rbs_parse_signature(rbs_parser_t *parser, rbs_signature_t **signature);
132132

133133
bool rbs_parse_type_params(rbs_parser_t *parser, bool module_type_params, rbs_node_list_t **params);

src/parser.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1551,7 +1551,7 @@ static bool parser_pop_typevar_table(rbs_parser_t *parser) {
15511551
method_type ::= {} type_params <function>
15521552
*/
15531553
// TODO: Should this be NODISCARD?
1554-
bool rbs_parse_method_type(rbs_parser_t *parser, rbs_method_type_t **method_type) {
1554+
bool rbs_parse_method_type(rbs_parser_t *parser, rbs_method_type_t **method_type, bool require_eof) {
15551555
rbs_parser_push_typevar_table(parser, false);
15561556

15571557
rbs_range_t rg;
@@ -1567,10 +1567,18 @@ bool rbs_parse_method_type(rbs_parser_t *parser, rbs_method_type_t **method_type
15671567
parse_function_result *result = rbs_allocator_alloc(ALLOCATOR(), parse_function_result);
15681568
CHECK_PARSE(parse_function(parser, false, &result, true));
15691569

1570+
CHECK_PARSE(parser_pop_typevar_table(parser));
1571+
15701572
rg.end = parser->current_token.range.end;
15711573
type_range.end = rg.end;
15721574

1573-
CHECK_PARSE(parser_pop_typevar_table(parser));
1575+
if (require_eof) {
1576+
rbs_parser_advance(parser);
1577+
if (parser->current_token.type != pEOF) {
1578+
rbs_parser_set_error(parser, parser->current_token, true, "expected a token `%s`", rbs_token_type_str(pEOF));
1579+
return false;
1580+
}
1581+
}
15741582

15751583
rbs_location_t *loc = rbs_location_new(ALLOCATOR(), rg);
15761584
rbs_loc_alloc_children(ALLOCATOR(), loc, 2);
@@ -1981,7 +1989,7 @@ static bool parse_member_def(rbs_parser_t *parser, bool instance_only, bool acce
19811989
case pLBRACKET:
19821990
case pQUESTION: {
19831991
rbs_method_type_t *method_type = NULL;
1984-
CHECK_PARSE(rbs_parse_method_type(parser, &method_type));
1992+
CHECK_PARSE(rbs_parse_method_type(parser, &method_type, false));
19851993

19861994
overload_range.end = parser->current_token.range.end;
19871995
rbs_location_t *loc = rbs_location_new(ALLOCATOR(), overload_range);
@@ -3611,7 +3619,7 @@ static bool parse_method_overload(rbs_parser_t *parser, rbs_node_list_t *annotat
36113619
return false;
36123620
}
36133621

3614-
return rbs_parse_method_type(parser, method_type);
3622+
return rbs_parse_method_type(parser, method_type, false);
36153623
}
36163624

36173625
/*

test/rbs/parser_test.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,27 @@ def test_parse_method_type
689689
end
690690
end
691691

692+
def test_parse_method_type__require_eof
693+
RBS::Parser.parse_method_type(buffer("-> Foo extra input")).tap do |method_type|
694+
assert_instance_of RBS::MethodType, method_type
695+
assert_equal "-> Foo", method_type.location.source
696+
end
697+
698+
RBS::Parser.parse_method_type(buffer("-> Foo extra input")).tap do |method_type|
699+
assert_instance_of RBS::MethodType, method_type
700+
assert_equal "-> Foo", method_type.location.source
701+
end
702+
703+
assert_raises RBS::ParsingError do
704+
RBS::Parser.parse_method_type(buffer("-> Foo extra input"), require_eof: true)
705+
end.tap do |exn|
706+
assert_equal(
707+
"test.rbs:1:7...1:12: Syntax error: expected a token `pEOF`, token=`extra` (tLIDENT)",
708+
exn.message
709+
)
710+
end
711+
end
712+
692713
def test_duplicate_keyword
693714
RBS::Parser.parse_method_type(buffer("(top foo, foo: top) -> void")).tap do |method_type|
694715
assert_equal "top foo, foo: top", method_type.type.param_to_s

0 commit comments

Comments
 (0)