Skip to content

Commit

Permalink
Merge pull request #1652 from ksss/syntax-error-function-param
Browse files Browse the repository at this point in the history
Should raise ParsingError instead of ArgumentError
  • Loading branch information
soutaro authored Nov 27, 2023
2 parents b9930cb + c94370e commit c5c2aee
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
8 changes: 8 additions & 0 deletions ext/rbs_extension/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,14 @@ static VALUE parse_function_param(parserstate *state) {
param_range.start = type_range.start;
param_range.end = name_range.end;

if (!is_keyword_token(state->current_token.type)) {
raise_syntax_error(
state,
state->current_token,
"unexpected token for function parameter name"
);
}

VALUE name = rb_to_symbol(rbs_unquote_string(state, state->current_token.range, 0));
VALUE location = rbs_new_location(state->buffer, param_range);
rbs_loc *loc = rbs_check_location(location);
Expand Down
14 changes: 13 additions & 1 deletion test/rbs/parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,7 @@ def test_parse_method_type
assert_raises RBS::ParsingError do
RBS::Parser.parse_method_type(buffer("(foo + 1) -> void"))
end.tap do |exn|
assert_equal "test.rbs:1:7...1:8: Syntax error: unexpected token for method type parameters, token=`1` (tINTEGER)", exn.message
assert_equal "test.rbs:1:5...1:6: Syntax error: unexpected token for function parameter name, token=`+` (tOPERATOR)", exn.message
end

assert_raises RBS::ParsingError do
Expand All @@ -650,6 +650,18 @@ def test_parse_method_type
assert_equal "test.rbs:1:15...1:18: Syntax error: required keyword argument type is expected, token=`Bar` (tUIDENT)", exn.message
end

assert_raises RBS::ParsingError do
RBS::Parser.parse_method_type(buffer("(foo`: untyped) -> void"))
end.tap do |exn|
assert_equal "test.rbs:1:4...1:5: Syntax error: unexpected token for function parameter name, token=``` (tOPERATOR)", exn.message
end

assert_raises RBS::ParsingError do
RBS::Parser.parse_method_type(buffer("(?foo\": untyped) -> void"))
end.tap do |exn|
assert_equal "test.rbs:1:5...1:6: Syntax error: unexpected token for function parameter name, token=`\"` (ErrorToken)", exn.message
end

assert_raises RBS::ParsingError do
RBS::Parser.parse_method_type(buffer("(**untyped, ?Bar) -> void"))
end.tap do |exn|
Expand Down

0 comments on commit c5c2aee

Please sign in to comment.