Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve parsing error on EOF #463

Merged
merged 1 commit into from
Nov 8, 2020

Conversation

pocke
Copy link
Member

@pocke pocke commented Nov 8, 2020

This pull request improves parsing error on EOF.

Problem

Currently rbs parse command raises an error when the target file has a syntax error that is caused by an EOF.
For example:

# test.rbs
class Foo
$ rbs parse test.rbs
/path/to/lib/rbs/cli.rb:741:in `rescue in block (2 levels) in run_parse': undefined method `location' for "$":String (NoMethodError)
	from /path/to/lib/rbs/cli.rb:739:in `block (2 levels) in run_parse'
	from /path/to/lib/rbs/environment_loader.rb:93:in `each_file'
	from /path/to/lib/rbs/cli.rb:738:in `block in run_parse'
	from /path/to/lib/rbs/cli.rb:736:in `each'
	from /path/to/lib/rbs/cli.rb:736:in `run_parse'
	from /path/to/lib/rbs/cli.rb:113:in `run'
	from /path/to/exe/rbs:7:in `<top (required)>'
	from /path2/to/bin/rbs:23:in `load'
	from /path2/to/bin/rbs:23:in `<main>'
parser.y:1380:in `on_error': parse error on value: "$" ($end) (RBS::Parser::SyntaxError)
	from (eval):3:in `_racc_do_parse_c'
	from (eval):3:in `do_parse'
	from parser.y:1110:in `parse_signature'
	from /path/to/lib/rbs/cli.rb:739:in `block (2 levels) in run_parse'
	from /path/to/lib/rbs/environment_loader.rb:93:in `each_file'
	from /path/to/lib/rbs/cli.rb:738:in `block in run_parse'
	from /path/to/lib/rbs/cli.rb:736:in `each'
	from /path/to/lib/rbs/cli.rb:736:in `run_parse'
	from /path/to/lib/rbs/cli.rb:113:in `run'
	from /path/to/exe/rbs:7:in `<top (required)>'
	from /path2/to/bin/rbs:23:in `load'
	from /path2/to/bin/rbs:23:in `<main>'

Because RBS::Parser::SyntaxError expects a LocatedValue as error_value, but actually, it receives a String, which is "$", on this case.

Solution

Handle EOF properly in next_token method.

Currently it just returns a nil on EOF, so Racc can't be aware of the LocatedValue for EOF.

next_token can returns [false, any_value] on EOF.

The final symbol (End of file) must be false.

https://github.com/ruby/racc/blob/aff6613fb00e350864dc80cbe513cf4a70d3744b/lib/racc/parser.rb#L276

So I changed the returned value of next_token to [false, LocatedValue].

rbs parse command will display the following.

$ rbs parse test.rbs
test.rbs:2:9: parse error on value: ($end)

Copy link
Member

@soutaro soutaro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great improvement! 👏

@soutaro soutaro merged commit 4409819 into ruby:master Nov 8, 2020
@pocke pocke deleted the Improve-parsing-error-on-EOF branch November 8, 2020 14:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants