Skip to content

Commit 6bd99d9

Browse files
committed
Merge pull request #2678 from ruby/fix-eol-type
Move `require_eof` handling to C API
1 parent 79586f2 commit 6bd99d9

File tree

4 files changed

+34
-13
lines changed

4 files changed

+34
-13
lines changed

ext/rbs_extension/main.c

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

189189
rbs_method_type_t *method_type = NULL;
190-
rbs_parse_method_type(parser, &method_type);
190+
rbs_parse_method_type(parser, &method_type, RB_TEST(arg->require_eof));
191191

192192
raise_error_if_any(parser, arg->buffer);
193193

194-
if (RB_TEST(arg->require_eof)) {
195-
rbs_parser_advance(parser);
196-
if (parser->current_token.type != pEOF) {
197-
rbs_parser_set_error(parser, parser->current_token, true, "expected a token `%s`", rbs_token_type_str(pEOF));
198-
raise_error(parser->error, arg->buffer);
199-
}
200-
}
201-
202194
rbs_translation_context_t ctx = rbs_translation_context_create(
203195
&parser->constant_pool,
204196
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);
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: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1452,7 +1452,7 @@ static bool parser_pop_typevar_table(rbs_parser_t *parser) {
14521452
method_type ::= {} type_params <function>
14531453
*/
14541454
// TODO: Should this be NODISCARD?
1455-
bool rbs_parse_method_type(rbs_parser_t *parser, rbs_method_type_t **method_type) {
1455+
bool rbs_parse_method_type(rbs_parser_t *parser, rbs_method_type_t **method_type, bool require_eof) {
14561456
rbs_parser_push_typevar_table(parser, false);
14571457

14581458
rbs_range_t rg;
@@ -1468,10 +1468,18 @@ bool rbs_parse_method_type(rbs_parser_t *parser, rbs_method_type_t **method_type
14681468
parse_function_result *result = rbs_allocator_alloc(ALLOCATOR(), parse_function_result);
14691469
CHECK_PARSE(parse_function(parser, false, &result));
14701470

1471+
CHECK_PARSE(parser_pop_typevar_table(parser));
1472+
14711473
rg.end = parser->current_token.range.end;
14721474
type_range.end = rg.end;
14731475

1474-
CHECK_PARSE(parser_pop_typevar_table(parser));
1476+
if (require_eof) {
1477+
rbs_parser_advance(parser);
1478+
if (parser->current_token.type != pEOF) {
1479+
rbs_parser_set_error(parser, parser->current_token, true, "expected a token `%s`", rbs_token_type_str(pEOF));
1480+
return false;
1481+
}
1482+
}
14751483

14761484
rbs_location_t *loc = rbs_location_new(ALLOCATOR(), rg);
14771485
rbs_loc_alloc_children(ALLOCATOR(), loc, 2);
@@ -1881,7 +1889,7 @@ static bool parse_member_def(rbs_parser_t *parser, bool instance_only, bool acce
18811889
case pLBRACKET:
18821890
case pQUESTION: {
18831891
rbs_method_type_t *method_type = NULL;
1884-
CHECK_PARSE(rbs_parse_method_type(parser, &method_type));
1892+
CHECK_PARSE(rbs_parse_method_type(parser, &method_type, false));
18851893

18861894
overload_range.end = parser->current_token.range.end;
18871895
rbs_location_t *loc = rbs_location_new(ALLOCATOR(), overload_range);

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)