Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error message for invalid named references #96

Merged
merged 1 commit into from
Sep 12, 2023
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
9 changes: 7 additions & 2 deletions lib/lrama/lexer/token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@ def numberize_references(lhs, rhs)
if lhs.referred_by?(ref_name)
'$'
else
# TODO: Better error message for wrong reference
rhs.find_index {|token| token.referred_by?(ref_name) } + 1
index = rhs.find_index {|token| token.referred_by?(ref_name) }

if index
index + 1
else
raise "'#{ref_name}' is invalid name."
end
end
[ref[0], value, ref[2], ref[3], ref[4]]
else
Expand Down
37 changes: 37 additions & 0 deletions spec/lrama/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1277,6 +1277,43 @@ class : keyword_class tSTRING keyword_end { code 1 }
])
end
end

context "includes invalid named references" do
it "raise an error" do
y = <<~INPUT
%{
// Prologue
%}

%union {
int i;
}

%token NUM
%type <val> expr

%%

input : /* empty */
| input line
;

line : '\\n'
| expr '\\n'
{ printf("\\t%.10g\\n", $expr); }
;

expr[result]: NUM
| expr[left] expr[right] '+'
{ $results = $left + $right; }
| expr expr '-'
{ $$ = $1 - $2; }
;
INPUT

expect { Lrama::Parser.new(y).parse }.to raise_error("'results' is invalid name.")
end
end
end
end
end
Expand Down