Skip to content

Commit 0073b30

Browse files
committed
Fix error messages for unterminated ( and {
If we hit an EOF token, and the character before the EOF is a newline, we need to rewind the error to the newline so that error messages contain the correct newline. Before this commit, we would report an error on the "line of the EOF" which would come right after the newline
1 parent ff50a14 commit 0073b30

File tree

4 files changed

+23
-1
lines changed

4 files changed

+23
-1
lines changed

src/prism.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,14 @@ pm_parser_err_token(pm_parser_t *parser, const pm_token_t *token, pm_diagnostic_
476476
* given token and a format string.
477477
*/
478478
#define PM_PARSER_ERR_TOKEN_FORMAT(parser, token, diag_id, ...) \
479-
PM_PARSER_ERR_FORMAT(parser, (token).start, (token).end, diag_id, __VA_ARGS__)
479+
do { \
480+
pm_token_t errtok = (token); \
481+
if (errtok.type == PM_TOKEN_EOF && errtok.start > parser->start && (*(errtok.start - 1) == '\n')) { \
482+
PM_PARSER_ERR_FORMAT(parser, errtok.start - 1, (token).end, diag_id, __VA_ARGS__); \
483+
} else { \
484+
PM_PARSER_ERR_FORMAT(parser, (token).start, (token).end, diag_id, __VA_ARGS__); \
485+
} \
486+
} while(0)
480487

481488
/**
482489
* Append an error to the list of errors on the parser using the location of the
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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ 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+
end
42+
3543
def test_embdoc_ending
3644
source = <<~RUBY
3745
=begin\n=end

0 commit comments

Comments
 (0)