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

Include error location to parse error messages #148

Merged
merged 1 commit into from
Oct 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/lrama/parser.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -409,3 +409,8 @@ end
def next_token
@lexer.next_token
end

def on_error(error_token_id, error_value, value_stack)
raise ParseError, sprintf("\n%d:%d: parse error on value %s (%s)",
@lexer.line, @lexer.column, error_value.inspect, token_to_str(error_token_id) || '?')
end
19 changes: 19 additions & 0 deletions spec/lrama/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1316,6 +1316,25 @@ class : keyword_class tSTRING keyword_end { code 1 }
end
end
end

describe "error messages" do
it "contains line number and column" do
y = <<~INPUT
%{
// Prologue
%}

%expect invalid

%%

program: /* empty */
;
INPUT

expect { Lrama::Parser.new(y).parse }.to raise_error(/5:14: parse error/)
end
end
end

describe "#fill_symbol_number" do
Expand Down