Skip to content

Commit

Permalink
Merge pull request #187 from yui-knk/error_message_for_objects
Browse files Browse the repository at this point in the history
Error message for objects
  • Loading branch information
yui-knk authored Nov 5, 2023
2 parents 865718c + 7c17215 commit a7a5283
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 18 deletions.
1 change: 1 addition & 0 deletions lib/lrama/lexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

module Lrama
class Lexer
attr_reader :head_line, :head_column
attr_accessor :status
attr_accessor :end_symbol

Expand Down
18 changes: 13 additions & 5 deletions lib/lrama/parser.rb

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

18 changes: 13 additions & 5 deletions parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -409,10 +409,18 @@ def next_token
end

def on_error(error_token_id, error_value, value_stack)
if error_value.respond_to?(:line) && error_value.respond_to?(:column)
line = error_value.line
first_column = error_value.column
else
line = @lexer.line
first_column = @lexer.head_column
end

raise ParseError, <<~ERROR
#{@path}:#{@lexer.line}:#{error_value.column}: parse error on value #{error_value.inspect} (#{token_to_str(error_token_id) || '?'})
#{@text.split("\n")[error_value.line - 1]}
#{carrets(error_value)}
#{@path}:#{line}:#{first_column}: parse error on value #{error_value.inspect} (#{token_to_str(error_token_id) || '?'})
#{@text.split("\n")[line - 1]}
#{carrets(first_column)}
ERROR
end

Expand All @@ -433,6 +441,6 @@ def end_c_declaration
@lexer.end_symbol = nil
end

def carrets(error_value)
' ' * (error_value.column + 1) + '^' * (@lexer.column - error_value.column)
def carrets(first_column)
' ' * (first_column + 1) + '^' * (@lexer.column - first_column)
end
40 changes: 32 additions & 8 deletions spec/lrama/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1497,8 +1497,9 @@ class : keyword_class tSTRING keyword_end { code 1 }
end

describe "error messages" do
it "contains line number and column" do
y = <<~INPUT
context "error_value has line number and column" do
it "contains line number and column" do
y = <<~INPUT
%{
// Prologue
%}
Expand All @@ -1509,12 +1510,35 @@ class : keyword_class tSTRING keyword_end { code 1 }
program: /* empty */
;
INPUT
expect { Lrama::Parser.new(y, "error_messages/parse.y").parse }.to raise_error(<<~ERROR)
error_messages/parse.y:5:7: parse error on value #<struct Lrama::Lexer::Token::Ident s_value="invalid", alias_name=nil> (IDENTIFIER)
%expect invalid
^^^^^^^
ERROR
INPUT
expect { Lrama::Parser.new(y, "error_messages/parse.y").parse }.to raise_error(ParseError, <<~ERROR)
error_messages/parse.y:5:7: parse error on value #<struct Lrama::Lexer::Token::Ident s_value="invalid", alias_name=nil> (IDENTIFIER)
%expect invalid
^^^^^^^
ERROR
end
end

context "error_value doesn't have line number and column" do
it "contains line number and column" do
y = <<~INPUT
%{
// Prologue
%}
%expect 0 10
%%
program: /* empty */
;
INPUT
expect { Lrama::Parser.new(y, "error_messages/parse.y").parse }.to raise_error(ParseError, <<~ERROR)
error_messages/parse.y:5:9: parse error on value 10 (INTEGER)
%expect 0 10
^^
ERROR
end
end
end
end
Expand Down

0 comments on commit a7a5283

Please sign in to comment.