Skip to content

Commit

Permalink
Merge pull request #3146 from ruby/fix-up-escape
Browse files Browse the repository at this point in the history
Fix up errors on invalid escape character syntax
  • Loading branch information
kddnewton authored Oct 7, 2024
2 parents 1653317 + 14c8559 commit 13313f3
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions src/prism.c
Original file line number Diff line number Diff line change
Expand Up @@ -9885,6 +9885,10 @@ escape_read(pm_parser_t *parser, pm_buffer_t *buffer, pm_buffer_t *regular_expre
}
case 'c': {
parser->current.end++;
if (flags & PM_ESCAPE_FLAG_CONTROL) {
pm_parser_err_current(parser, PM_ERR_ESCAPE_INVALID_CONTROL_REPEAT);
}

if (parser->current.end == parser->end) {
pm_parser_err_current(parser, PM_ERR_ESCAPE_INVALID_CONTROL);
return;
Expand All @@ -9898,10 +9902,6 @@ escape_read(pm_parser_t *parser, pm_buffer_t *buffer, pm_buffer_t *regular_expre
return;
}
case '\\':
if (flags & PM_ESCAPE_FLAG_CONTROL) {
pm_parser_err_current(parser, PM_ERR_ESCAPE_INVALID_CONTROL_REPEAT);
return;
}
parser->current.end++;

if (match(parser, 'u') || match(parser, 'U')) {
Expand Down Expand Up @@ -9935,6 +9935,10 @@ escape_read(pm_parser_t *parser, pm_buffer_t *buffer, pm_buffer_t *regular_expre
}
case 'C': {
parser->current.end++;
if (flags & PM_ESCAPE_FLAG_CONTROL) {
pm_parser_err_current(parser, PM_ERR_ESCAPE_INVALID_CONTROL_REPEAT);
}

if (peek(parser) != '-') {
size_t width = parser->encoding->char_width(parser->current.end, parser->end - parser->current.end);
pm_parser_err(parser, parser->current.start, parser->current.end + width, PM_ERR_ESCAPE_INVALID_CONTROL);
Expand All @@ -9955,10 +9959,6 @@ escape_read(pm_parser_t *parser, pm_buffer_t *buffer, pm_buffer_t *regular_expre
return;
}
case '\\':
if (flags & PM_ESCAPE_FLAG_CONTROL) {
pm_parser_err_current(parser, PM_ERR_ESCAPE_INVALID_CONTROL_REPEAT);
return;
}
parser->current.end++;

if (match(parser, 'u') || match(parser, 'U')) {
Expand Down Expand Up @@ -9993,6 +9993,10 @@ escape_read(pm_parser_t *parser, pm_buffer_t *buffer, pm_buffer_t *regular_expre
}
case 'M': {
parser->current.end++;
if (flags & PM_ESCAPE_FLAG_META) {
pm_parser_err_current(parser, PM_ERR_ESCAPE_INVALID_META_REPEAT);
}

if (peek(parser) != '-') {
size_t width = parser->encoding->char_width(parser->current.end, parser->end - parser->current.end);
pm_parser_err(parser, parser->current.start, parser->current.end + width, PM_ERR_ESCAPE_INVALID_META);
Expand All @@ -10008,10 +10012,6 @@ escape_read(pm_parser_t *parser, pm_buffer_t *buffer, pm_buffer_t *regular_expre
uint8_t peeked = peek(parser);
switch (peeked) {
case '\\':
if (flags & PM_ESCAPE_FLAG_META) {
pm_parser_err_current(parser, PM_ERR_ESCAPE_INVALID_META_REPEAT);
return;
}
parser->current.end++;

if (match(parser, 'u') || match(parser, 'U')) {
Expand Down Expand Up @@ -10054,6 +10054,8 @@ escape_read(pm_parser_t *parser, pm_buffer_t *buffer, pm_buffer_t *regular_expre
default: {
if (parser->current.end < parser->end) {
escape_write_escape_encoded(parser, buffer);
} else {
pm_parser_err_current(parser, PM_ERR_INVALID_ESCAPE_CHARACTER);
}
return;
}
Expand Down

0 comments on commit 13313f3

Please sign in to comment.