Skip to content
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
6 changes: 5 additions & 1 deletion lib/lrama/grammar/symbols/resolver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,11 @@ def find_symbol_by_id(id)

# @rbs (Lexer::Token::Base id) -> Grammar::Symbol
def find_symbol_by_id!(id)
find_symbol_by_id(id) || (raise "Symbol not found. #{id}")
symbol = find_symbol_by_id(id)
return symbol if symbol

message = "symbol #{id.s_value} is used, but is not defined as a token and has no rules"
raise(id.location&.generate_error_message(message) || message)
end
Comment thread
ydah marked this conversation as resolved.

# @rbs (Integer token_id) -> Grammar::Symbol?
Expand Down
2 changes: 1 addition & 1 deletion lib/lrama/parser.rb

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

2 changes: 1 addition & 1 deletion parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ rule
| STRING
| "{...}"

string_as_id: STRING { result = Lrama::Lexer::Token::Ident.new(s_value: val[0].s_value) }
string_as_id: STRING { result = Lrama::Lexer::Token::Ident.new(s_value: val[0].s_value, location: val[0].location) }
end

---- inner
Expand Down
17 changes: 14 additions & 3 deletions spec/lrama/grammar/symbols/resolver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,21 @@
end

it "raises error if symbol not found" do
grammar_file = Lrama::Lexer::GrammarFile.new("foo/basic.y", "")
location = Lrama::Lexer::Location.new(grammar_file: grammar_file, first_line: 1, first_column: 2, last_line: 3, last_column: 4)
grammar_file = Lrama::Lexer::GrammarFile.new("foo/basic.y", " alias")
location = Lrama::Lexer::Location.new(grammar_file: grammar_file, first_line: 1, first_column: 2, last_line: 1, last_column: 7)
symbol = Lrama::Grammar::Symbol.new(id: Lrama::Lexer::Token::Ident.new(s_value: "alias", location: location), alias_name: "alias", term: true)
expect { resolver.find_symbol_by_id!(symbol.id) }.to raise_error("Symbol not found. value: `alias`, location: foo/basic.y (1,2)-(3,4)")
expect { resolver.find_symbol_by_id!(symbol.id) }.to raise_error(<<~ERROR)
foo/basic.y:1:2: symbol alias is used, but is not defined as a token and has no rules
1 | alias
| ^~~~~
ERROR
end

it "raises error if symbol has no location" do
id = Lrama::Lexer::Token::Ident.new(s_value: "alias")

expect { resolver.find_symbol_by_id!(id) }
.to raise_error("symbol alias is used, but is not defined as a token and has no rules")
end
end

Expand Down
19 changes: 19 additions & 0 deletions spec/lrama/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4434,6 +4434,25 @@ class : keyword_class tSTRING keyword_end { code 1 }
end

describe "error messages" do
context "when the start symbol is not defined" do
it "reports the symbol and its location" do
y = <<~INPUT
%start nosuch
%%
program: "a" ;
INPUT

expect do
grammar = Lrama::Parser.new(y, "parse.y").parse
grammar.prepare
end.to raise_error(<<~ERROR)
parse.y:1:7: symbol nosuch is used, but is not defined as a token and has no rules
1 | %start nosuch
| ^~~~~~
ERROR
end
end

context "error_value has line number and column" do
it "contains line number and column" do
y = <<~INPUT
Expand Down