Skip to content

Commit 0759119

Browse files
authored
Merge pull request #751 from ydah/improve-error-message
Improve error messages in Lexer for unexpected tokens and code
2 parents 9f8e620 + 27f0870 commit 0759119

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

lib/lrama/lexer.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ def lex_token
145145
end
146146
return [type, token]
147147
else
148-
raise ParseError, "Unexpected token: #{@scanner.peek(10).chomp}." # steep:ignore UnknownConstant
148+
raise ParseError, location.generate_error_message("Unexpected token") # steep:ignore UnknownConstant
149149
end
150150
end
151151

@@ -186,7 +186,7 @@ def lex_c_code
186186
code << @scanner.getch
187187
end
188188
end
189-
raise ParseError, "Unexpected code: #{code}." # steep:ignore UnknownConstant
189+
raise ParseError, location.generate_error_message("Unexpected code: #{code}") # steep:ignore UnknownConstant
190190
end
191191

192192
private

spec/lrama/lexer_spec.rb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -396,10 +396,14 @@
396396
it do
397397
path = fixture_path("common/unexpected_token.y")
398398
text = File.read(path)
399-
grammar_file = Lrama::Lexer::GrammarFile.new(path, text)
399+
grammar_file = Lrama::Lexer::GrammarFile.new("unexpected_token.y", text)
400400
lexer = Lrama::Lexer.new(grammar_file)
401401

402-
expect { lexer.next_token }.to raise_error(ParseError, "Unexpected token: @invalid.")
402+
expect { lexer.next_token }.to raise_error(ParseError, <<~MSG)
403+
unexpected_token.y:5:0: Unexpected token
404+
5 | @invalid
405+
| ^
406+
MSG
403407
end
404408
end
405409

@@ -410,7 +414,11 @@
410414
lexer.status = :c_declaration
411415
lexer.end_symbol = "%}"
412416

413-
expect { lexer.next_token }.to raise_error(ParseError, "Unexpected code: @invalid.")
417+
expect { lexer.next_token }.to raise_error(ParseError, <<~MSG)
418+
invalid.y:1:0: Unexpected code: @invalid
419+
1 | @invalid
420+
| ^~~~~~~~
421+
MSG
414422
end
415423
end
416424

0 commit comments

Comments
 (0)