diff --git a/lib/lrama/parser.rb b/lib/lrama/parser.rb index 343bd8ac..0f30ea56 100644 --- a/lib/lrama/parser.rb +++ b/lib/lrama/parser.rb @@ -694,6 +694,11 @@ def parse 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 ...end parser.y/module_eval... ##### State transition tables begin ### diff --git a/parser.y b/parser.y index fa89c7e5..68763085 100644 --- a/parser.y +++ b/parser.y @@ -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 diff --git a/spec/lrama/parser_spec.rb b/spec/lrama/parser_spec.rb index 6bae8ace..634e1402 100644 --- a/spec/lrama/parser_spec.rb +++ b/spec/lrama/parser_spec.rb @@ -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