Skip to content

Commit f2a0fe6

Browse files
authored
Merge pull request #3271 from ruby/eof-tok
Fix error messages for unterminated ( and {
2 parents 49c0cba + 60bc43d commit f2a0fe6

File tree

4 files changed

+26
-0
lines changed

4 files changed

+26
-0
lines changed

src/prism.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10700,6 +10700,14 @@ parser_lex(pm_parser_t *parser) {
1070010700
// We'll check if we're at the end of the file. If we are, then we
1070110701
// need to return the EOF token.
1070210702
if (parser->current.end >= parser->end) {
10703+
// If we hit EOF, but the EOF came immediately after a newline,
10704+
// set the start of the token to the newline. This way any EOF
10705+
// errors will be reported as happening on that line rather than
10706+
// a line after. For example "foo(\n" should report an error
10707+
// on line 1 even though EOF technically occurs on line 2.
10708+
if (parser->current.start > parser->start && (*(parser->current.start - 1) == '\n')) {
10709+
parser->current.start -= 1;
10710+
}
1070310711
LEX(PM_TOKEN_EOF);
1070410712
}
1070510713

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
foo {
2+
^ unexpected end-of-input, assuming it is closing the parent top level context
3+
^ expected a block beginning with `{` to end with `}`
4+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
foo(
2+
^ unexpected end-of-input; expected a `)` to close the arguments
3+

test/prism/errors_test.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,17 @@ class ErrorsTest < TestCase
3232
end
3333
end
3434

35+
def test_newline_preceding_eof
36+
err = Prism.parse("foo(").errors.first
37+
assert_equal 1, err.location.start_line
38+
39+
err = Prism.parse("foo(\n").errors.first
40+
assert_equal 1, err.location.start_line
41+
42+
err = Prism.parse("foo(\n\n\n\n\n").errors.first
43+
assert_equal 5, err.location.start_line
44+
end
45+
3546
def test_embdoc_ending
3647
source = <<~RUBY
3748
=begin\n=end

0 commit comments

Comments
 (0)