Skip to content

Commit

Permalink
Extract error message construction into method
Browse files Browse the repository at this point in the history
  • Loading branch information
yui-knk committed Dec 23, 2023
1 parent 203e745 commit 1d487d6
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 22 deletions.
4 changes: 2 additions & 2 deletions lib/lrama/grammar/rule_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ def numberize_references
else
candidates = rhs.each_with_index.select {|token, i| token.referred_by?(ref_name) }

raise "Referring symbol `#{ref_name}` is duplicated.\n#{token.location.line_with_carrets}" if candidates.size >= 2
raise "Referring symbol `#{ref_name}` is not found.\n#{token.location.line_with_carrets}" unless referring_symbol = candidates.first
raise token.location.generate_error_message("Referring symbol `#{ref_name}` is duplicated.") if candidates.size >= 2
raise token.location.generate_error_message("Referring symbol `#{ref_name}` is not found.") unless referring_symbol = candidates.first

ref.index = referring_symbol[1] + 1
end
Expand Down
7 changes: 7 additions & 0 deletions lib/lrama/lexer/location.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ def to_s
"#{grammar_file_path} (#{first_line},#{first_column})-(#{last_line},#{last_column})"
end

def generate_error_message(error_message)
<<~ERROR.chomp
#{grammar_file_path}:#{first_line}:#{first_column}: #{error_message}
#{line_with_carrets}
ERROR
end

def line_with_carrets
<<~TEXT
#{text}
Expand Down
5 changes: 1 addition & 4 deletions lib/lrama/parser.rb

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

5 changes: 1 addition & 4 deletions parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -567,8 +567,5 @@ def end_c_declaration
end
def raise_parse_error(error_message, location)
raise ParseError, <<~ERROR
#{location.grammar_file_path}:#{location.first_line}:#{location.first_column}: #{error_message}
#{location.line_with_carrets}
ERROR
raise ParseError, location.generate_error_message(error_message)
end
1 change: 1 addition & 0 deletions sig/lrama/lexer/location.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module Lrama
def initialize: (grammar_file_path: String, first_line: Integer, first_column: Integer, last_line: Integer, last_column: Integer) -> void

def ==: (Location other) -> bool
def generate_error_message: (String) -> String
def line_with_carrets: () -> String

private
Expand Down
4 changes: 2 additions & 2 deletions spec/lrama/grammar/rule_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@
rule_builder.complete_input

expected = <<-TEXT
Referring symbol `classes` is not found.
parse.y:1:42: Referring symbol `classes` is not found.
class : keyword_class tSTRING keyword_end { $classes = $1; }
^^^^^^^^^^^^^^^^^^
TEXT
Expand Down Expand Up @@ -323,7 +323,7 @@ class : keyword_class tSTRING keyword_end { $classes = $1; }
rule_builder.complete_input

expected = <<-TEXT
Referring symbol `tSTRING` is duplicated.
parse.y:1:50: Referring symbol `tSTRING` is duplicated.
class : keyword_class tSTRING tSTRING keyword_end { $class = $tSTRING; }
^^^^^^^^^^^^^^^^^^^^^^
TEXT
Expand Down
14 changes: 14 additions & 0 deletions spec/lrama/lexer/location_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,20 @@
end
end

describe "#generate_error_message" do
it "returns decorated error message" do
path = fixture_path("lexer/location.y")
location = Lrama::Lexer::Location.new(grammar_file_path: path, first_line: 33, first_column: 12, last_line: 33, last_column: 15)
expected = <<-TEXT
#{path}:33:12: ERROR
| expr '+' expr { $$ = $1 + $3; }
^^^
TEXT

expect(location.generate_error_message("ERROR")).to eq expected
end
end

describe "#line_with_carrets" do
it "returns line text with carrets" do
path = fixture_path("lexer/location.y")
Expand Down
14 changes: 4 additions & 10 deletions spec/lrama/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1817,7 +1817,6 @@ class : keyword_class tSTRING %prec tPLUS keyword_end { code 1 }
#{file.path}:31:42: ident after %prec
class : keyword_class tSTRING %prec tPLUS keyword_end { code 1 }
^^^^^^^^^^^
ERROR
end
end
Expand All @@ -1842,7 +1841,6 @@ class : keyword_class { code 2 } tSTRING %prec "=" '!' keyword_end { code 3 }
#{file.path}:31:51: char after %prec
class : keyword_class { code 2 } tSTRING %prec "=" '!' keyword_end { code 3 }
^^^
ERROR
end
end
Expand All @@ -1867,7 +1865,6 @@ class : keyword_class { code 4 } tSTRING '?' keyword_end %prec tEQ { code 5 } {
#{file.path}:31:78: multiple User_code after %prec
class : keyword_class { code 4 } tSTRING '?' keyword_end %prec tEQ { code 5 } { code 6 }
^
ERROR
end
end
Expand Down Expand Up @@ -2392,13 +2389,13 @@ class : keyword_class tSTRING keyword_end { code 1 }
;
INPUT

expected = <<-ERROR
Referring symbol `results` is not found.
create_grammar_file("parse.y", y) do |file, content|
expected = <<-ERROR
#{file.path}:25:17: Referring symbol `results` is not found.
{ $results = $left + $right; }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ERROR
ERROR

create_grammar_file("parse.y", y) do |file, content|
expect { Lrama::Parser.new(content, file.path).parse }.to raise_error(expected)
end
end
Expand Down Expand Up @@ -2429,7 +2426,6 @@ class : keyword_class tSTRING keyword_end { code 1 }
#{file.path}:5:8: parse error on value 'invalid' (IDENTIFIER)
%expect invalid
^^^^^^^
ERROR
end
end
Expand Down Expand Up @@ -2457,7 +2453,6 @@ class : keyword_class tSTRING keyword_end { code 1 }
#{file.path}:5:10: parse error on value 10 (INTEGER)
%expect 0 10
^^
ERROR
end
end
Expand Down Expand Up @@ -2485,7 +2480,6 @@ class : keyword_class tSTRING keyword_end { code 1 }
#{file.path}:5:9: parse error on value 'invalid' (IDENTIFIER)
%expect\t\tinvalid
\t\t^^^^^^^
ERROR
end
end
Expand Down

0 comments on commit 1d487d6

Please sign in to comment.