Skip to content

emit $eof token with eof...eof+1 range to properly render diagnostic #786

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

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion lib/parser/lexer.rl
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ class Parser::Lexer
[ false, [ '$error'.freeze, range(p - 1, p) ] ]
else
eof = @source_pts.size
[ false, [ '$eof'.freeze, range(eof, eof) ] ]
[ false, [ '$eof'.freeze, range(eof, eof + 1) ] ]
end
end

Expand Down
12 changes: 12 additions & 0 deletions test/test_diagnostic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,16 @@ def test_bug_error_on_newline
'(string):2: ^'
], diag.render)
end

def test_render_eof_error
location = Parser::Source::Range.new(@buffer, @buffer.source.length, @buffer.source.length + 1)

diag = Parser::Diagnostic.new(:error, :unexpected_token, { token: '$end' }, location, [])

assert_equal([
'(string):1:34: error: unexpected token $end',
'(string):1: if (this is some bad code + bugs)',
'(string):1: ^'
], diag.render)
end
end
2 changes: 1 addition & 1 deletion test/test_lexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2620,7 +2620,7 @@ def test_float_suffix
def test_eof
assert_scanned("self",
:kSELF, "self", [0, 4])
assert_equal([false, ["$eof", Parser::Source::Range.new(@lex.source_buffer, 4, 4)]],
assert_equal([false, ["$eof", Parser::Source::Range.new(@lex.source_buffer, 4, 5)]],
@lex.advance)
end

Expand Down