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
8 changes: 8 additions & 0 deletions src/prism.c
Original file line number Diff line number Diff line change
Expand Up @@ -10700,6 +10700,14 @@ parser_lex(pm_parser_t *parser) {
// We'll check if we're at the end of the file. If we are, then we
// need to return the EOF token.
if (parser->current.end >= parser->end) {
// If we hit EOF, but the EOF came immediately after a newline,
// set the start of the token to the newline. This way any EOF
// errors will be reported as happening on that line rather than
// a line after. For example "foo(\n" should report an error
// on line 1 even though EOF technically occurs on line 2.
if (parser->current.start > parser->start && (*(parser->current.start - 1) == '\n')) {
parser->current.start -= 1;
}
LEX(PM_TOKEN_EOF);
}

Expand Down
4 changes: 4 additions & 0 deletions test/prism/errors/unterminated_block.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
foo {
^ unexpected end-of-input, assuming it is closing the parent top level context
^ expected a block beginning with `{` to end with `}`

3 changes: 3 additions & 0 deletions test/prism/errors/unterminated_method_parameters.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
foo(
^ unexpected end-of-input; expected a `)` to close the arguments

11 changes: 11 additions & 0 deletions test/prism/errors_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ class ErrorsTest < TestCase
end
end

def test_newline_preceding_eof
err = Prism.parse("foo(").errors.first
assert_equal 1, err.location.start_line

err = Prism.parse("foo(\n").errors.first
assert_equal 1, err.location.start_line

err = Prism.parse("foo(\n\n\n\n\n").errors.first
assert_equal 5, err.location.start_line
end

def test_embdoc_ending
source = <<~RUBY
=begin\n=end
Expand Down
Loading