Skip to content

Commit

Permalink
[ruby/prism] Regexp terminator escapes
Browse files Browse the repository at this point in the history
  • Loading branch information
kddnewton authored and matzbot committed Dec 11, 2023
1 parent c65de63 commit 4095e7d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
19 changes: 17 additions & 2 deletions prism/prism.c
Original file line number Diff line number Diff line change
Expand Up @@ -9535,7 +9535,9 @@ parser_lex(pm_parser_t *parser) {
case '\r':
parser->current.end++;
if (peek(parser) != '\n') {
pm_token_buffer_push(&token_buffer, '\\');
if (lex_mode->as.regexp.terminator != '\r') {
pm_token_buffer_push(&token_buffer, '\\');
}
pm_token_buffer_push(&token_buffer, '\r');
break;
}
Expand Down Expand Up @@ -9563,7 +9565,20 @@ parser_lex(pm_parser_t *parser) {
escape_read(parser, &token_buffer.buffer, PM_ESCAPE_FLAG_REGEXP);
break;
default:
if (lex_mode->as.regexp.terminator == '/' && peeked == '/') {
if (lex_mode->as.regexp.terminator == peeked) {
// Some characters when they are used as the
// terminator also receive an escape. They are
// enumerated here.
switch (peeked) {
case '$': case ')': case '*': case '+':
case '.': case '>': case '?': case ']':
case '^': case '|': case '}':
pm_token_buffer_push(&token_buffer, '\\');
break;
default:
break;
}

pm_token_buffer_push(&token_buffer, peeked);
parser->current.end++;
break;
Expand Down
2 changes: 1 addition & 1 deletion test/prism/snapshots/seattlerb/bug190.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
├── opening_loc: (1,0)-(1,3) = "%r'"
├── content_loc: (1,3)-(1,5) = "\\'"
├── closing_loc: (1,5)-(1,6) = "'"
└── unescaped: "\\'"
└── unescaped: "'"

0 comments on commit 4095e7d

Please sign in to comment.