Skip to content

Commit

Permalink
Merge pull request #148 from yui-knk/error_locations
Browse files Browse the repository at this point in the history
Include error location to parse error messages
  • Loading branch information
yui-knk authored Oct 21, 2023
2 parents 374bdcf + c6f4d0b commit 1de81f0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
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

0 comments on commit 1de81f0

Please sign in to comment.