Skip to content

Commit ab43b3a

Browse files
committed
Update more escape error messages to match CRuby
1 parent 7b5e75e commit ab43b3a

File tree

3 files changed

+34
-14
lines changed

3 files changed

+34
-14
lines changed

src/prism.c

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9658,7 +9658,8 @@ escape_read(pm_parser_t *parser, pm_buffer_t *buffer, pm_buffer_t *regular_expre
96589658
pm_parser_err(parser, unicode_start, unicode_start + hexadecimal_length, PM_ERR_ESCAPE_INVALID_UNICODE_LONG);
96599659
} else if (hexadecimal_length == 0) {
96609660
// there are not hexadecimal characters
9661-
pm_parser_err(parser, unicode_start, unicode_start + hexadecimal_length, PM_ERR_ESCAPE_INVALID_UNICODE);
9661+
pm_parser_err(parser, parser->current.end, parser->current.end, PM_ERR_ESCAPE_INVALID_UNICODE);
9662+
pm_parser_err(parser, parser->current.end, parser->current.end, PM_ERR_ESCAPE_INVALID_UNICODE_TERM);
96629663
return;
96639664
}
96649665

@@ -9707,10 +9708,6 @@ escape_read(pm_parser_t *parser, pm_buffer_t *buffer, pm_buffer_t *regular_expre
97079708
}
97089709
}
97099710

9710-
if (flags & (PM_ESCAPE_FLAG_CONTROL | PM_ESCAPE_FLAG_META)) {
9711-
pm_parser_err(parser, start, parser->current.end, PM_ERR_INVALID_ESCAPE_CHARACTER);
9712-
}
9713-
97149711
return;
97159712
}
97169713
case 'c': {
@@ -9733,6 +9730,12 @@ escape_read(pm_parser_t *parser, pm_buffer_t *buffer, pm_buffer_t *regular_expre
97339730
return;
97349731
}
97359732
parser->current.end++;
9733+
9734+
if (match(parser, 'u') || match(parser, 'U')) {
9735+
pm_parser_err(parser, parser->current.start, parser->current.end, PM_ERR_INVALID_ESCAPE_CHARACTER);
9736+
return;
9737+
}
9738+
97369739
escape_read(parser, buffer, regular_expression_buffer, flags | PM_ESCAPE_FLAG_CONTROL);
97379740
return;
97389741
case ' ':
@@ -9760,7 +9763,8 @@ escape_read(pm_parser_t *parser, pm_buffer_t *buffer, pm_buffer_t *regular_expre
97609763
case 'C': {
97619764
parser->current.end++;
97629765
if (peek(parser) != '-') {
9763-
pm_parser_err_current(parser, PM_ERR_ESCAPE_INVALID_CONTROL);
9766+
size_t width = parser->encoding->char_width(parser->current.end, parser->end - parser->current.end);
9767+
pm_parser_err(parser, parser->current.start, parser->current.end + width, PM_ERR_ESCAPE_INVALID_CONTROL);
97649768
return;
97659769
}
97669770

@@ -9783,6 +9787,12 @@ escape_read(pm_parser_t *parser, pm_buffer_t *buffer, pm_buffer_t *regular_expre
97839787
return;
97849788
}
97859789
parser->current.end++;
9790+
9791+
if (match(parser, 'u') || match(parser, 'U')) {
9792+
pm_parser_err(parser, parser->current.start, parser->current.end, PM_ERR_INVALID_ESCAPE_CHARACTER);
9793+
return;
9794+
}
9795+
97869796
escape_read(parser, buffer, regular_expression_buffer, flags | PM_ESCAPE_FLAG_CONTROL);
97879797
return;
97889798
case ' ':
@@ -9797,7 +9807,8 @@ escape_read(pm_parser_t *parser, pm_buffer_t *buffer, pm_buffer_t *regular_expre
97979807
return;
97989808
default: {
97999809
if (!char_is_ascii_printable(peeked)) {
9800-
pm_parser_err_current(parser, PM_ERR_ESCAPE_INVALID_CONTROL);
9810+
size_t width = parser->encoding->char_width(parser->current.end, parser->end - parser->current.end);
9811+
pm_parser_err(parser, parser->current.start, parser->current.end + width, PM_ERR_ESCAPE_INVALID_CONTROL);
98019812
return;
98029813
}
98039814

@@ -9810,7 +9821,8 @@ escape_read(pm_parser_t *parser, pm_buffer_t *buffer, pm_buffer_t *regular_expre
98109821
case 'M': {
98119822
parser->current.end++;
98129823
if (peek(parser) != '-') {
9813-
pm_parser_err_current(parser, PM_ERR_ESCAPE_INVALID_META);
9824+
size_t width = parser->encoding->char_width(parser->current.end, parser->end - parser->current.end);
9825+
pm_parser_err(parser, parser->current.start, parser->current.end + width, PM_ERR_ESCAPE_INVALID_META);
98149826
return;
98159827
}
98169828

@@ -9828,6 +9840,12 @@ escape_read(pm_parser_t *parser, pm_buffer_t *buffer, pm_buffer_t *regular_expre
98289840
return;
98299841
}
98309842
parser->current.end++;
9843+
9844+
if (match(parser, 'u') || match(parser, 'U')) {
9845+
pm_parser_err(parser, parser->current.start, parser->current.end, PM_ERR_INVALID_ESCAPE_CHARACTER);
9846+
return;
9847+
}
9848+
98319849
escape_read(parser, buffer, regular_expression_buffer, flags | PM_ESCAPE_FLAG_META);
98329850
return;
98339851
case ' ':
@@ -9842,7 +9860,8 @@ escape_read(pm_parser_t *parser, pm_buffer_t *buffer, pm_buffer_t *regular_expre
98429860
return;
98439861
default:
98449862
if (!char_is_ascii_printable(peeked)) {
9845-
pm_parser_err_current(parser, PM_ERR_ESCAPE_INVALID_META);
9863+
size_t width = parser->encoding->char_width(parser->current.end, parser->end - parser->current.end);
9864+
pm_parser_err(parser, parser->current.start, parser->current.end + width, PM_ERR_ESCAPE_INVALID_META);
98469865
return;
98479866
}
98489867

templates/src/diagnostic.c.erb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ static const pm_diagnostic_data_t diagnostic_messages[PM_DIAGNOSTIC_ID_MAX] = {
167167
[PM_ERR_ESCAPE_INVALID_UNICODE_CM_FLAGS] = { "invalid Unicode escape sequence; Unicode cannot be combined with control or meta flags", PM_ERROR_LEVEL_SYNTAX },
168168
[PM_ERR_ESCAPE_INVALID_UNICODE_LITERAL] = { "invalid Unicode escape sequence; Multiple codepoints at single character literal are disallowed", PM_ERROR_LEVEL_SYNTAX },
169169
[PM_ERR_ESCAPE_INVALID_UNICODE_LONG] = { "invalid Unicode escape sequence; maximum length is 6 digits", PM_ERROR_LEVEL_SYNTAX },
170-
[PM_ERR_ESCAPE_INVALID_UNICODE_TERM] = { "invalid Unicode escape sequence; needs closing `}`", PM_ERROR_LEVEL_SYNTAX },
170+
[PM_ERR_ESCAPE_INVALID_UNICODE_TERM] = { "unterminated Unicode escape", PM_ERROR_LEVEL_SYNTAX },
171171
[PM_ERR_EXPECT_ARGUMENT] = { "expected an argument", PM_ERROR_LEVEL_SYNTAX },
172172
[PM_ERR_EXPECT_EOL_AFTER_STATEMENT] = { "unexpected %s, expecting end-of-input", PM_ERROR_LEVEL_SYNTAX },
173173
[PM_ERR_EXPECT_EXPRESSION_AFTER_AMPAMPEQ] = { "expected an expression after `&&=`", PM_ERROR_LEVEL_SYNTAX },
@@ -330,12 +330,12 @@ static const pm_diagnostic_data_t diagnostic_messages[PM_DIAGNOSTIC_ID_MAX] = {
330330
[PM_ERR_STATEMENT_PREEXE_BEGIN] = { "unexpected a `BEGIN` at a non-statement position", PM_ERROR_LEVEL_SYNTAX },
331331
[PM_ERR_STATEMENT_UNDEF] = { "unexpected an `undef` at a non-statement position", PM_ERROR_LEVEL_SYNTAX },
332332
[PM_ERR_STRING_CONCATENATION] = { "expected a string for concatenation", PM_ERROR_LEVEL_SYNTAX },
333-
[PM_ERR_STRING_INTERPOLATED_TERM] = { "expected a closing delimiter for the interpolated string", PM_ERROR_LEVEL_SYNTAX },
333+
[PM_ERR_STRING_INTERPOLATED_TERM] = { "unterminated string; expected a closing delimiter for the interpolated string", PM_ERROR_LEVEL_SYNTAX },
334334
[PM_ERR_STRING_LITERAL_EOF] = { "unterminated string meets end of file", PM_ERROR_LEVEL_SYNTAX },
335335
[PM_ERR_STRING_LITERAL_TERM] = { "unexpected %s, expected a string literal terminator", PM_ERROR_LEVEL_SYNTAX },
336336
[PM_ERR_SYMBOL_INVALID] = { "invalid symbol", PM_ERROR_LEVEL_SYNTAX }, // TODO expected symbol? prism.c ~9719
337337
[PM_ERR_SYMBOL_TERM_DYNAMIC] = { "unterminated quoted string; expected a closing delimiter for the dynamic symbol", PM_ERROR_LEVEL_SYNTAX },
338-
[PM_ERR_SYMBOL_TERM_INTERPOLATED] = { "expected a closing delimiter for the interpolated symbol", PM_ERROR_LEVEL_SYNTAX },
338+
[PM_ERR_SYMBOL_TERM_INTERPOLATED] = { "unterminated symbol; expected a closing delimiter for the interpolated symbol", PM_ERROR_LEVEL_SYNTAX },
339339
[PM_ERR_TERNARY_COLON] = { "expected a `:` after the true expression of a ternary operator", PM_ERROR_LEVEL_SYNTAX },
340340
[PM_ERR_TERNARY_EXPRESSION_FALSE] = { "expected an expression after `:` in the ternary operator", PM_ERROR_LEVEL_SYNTAX },
341341
[PM_ERR_TERNARY_EXPRESSION_TRUE] = { "expected an expression after `?` in the ternary operator", PM_ERROR_LEVEL_SYNTAX },

test/prism/errors_test.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ def test_unterminated_argument_expression
216216

217217
def test_unterminated_interpolated_symbol
218218
assert_error_messages ":\"#", [
219-
"expected a closing delimiter for the interpolated symbol"
219+
"unterminated symbol; expected a closing delimiter for the interpolated symbol"
220220
]
221221
end
222222

@@ -715,12 +715,13 @@ def test_do_not_allow_characters_other_than_0_9_a_f_and_A_F_in_u_Unicode_charact
715715

716716
assert_errors expected, '"\u{000z}"', [
717717
["invalid Unicode escape sequence", 7..7],
718+
["unterminated Unicode escape", 7..7]
718719
]
719720
end
720721

721722
def test_unterminated_unicode_brackets_should_be_a_syntax_error
722723
assert_errors expression('?\\u{3'), '?\\u{3', [
723-
["invalid Unicode escape sequence; needs closing `}`", 1..5],
724+
["unterminated Unicode escape", 1..5],
724725
]
725726
end
726727

0 commit comments

Comments
 (0)