diff --git a/prism/prism.c b/prism/prism.c index 9922f5772291fd..9bed7b19d8ce32 100644 --- a/prism/prism.c +++ b/prism/prism.c @@ -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; } @@ -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; diff --git a/test/prism/snapshots/seattlerb/bug190.txt b/test/prism/snapshots/seattlerb/bug190.txt index ef4411ddb7937a..f7eaefa5c67574 100644 --- a/test/prism/snapshots/seattlerb/bug190.txt +++ b/test/prism/snapshots/seattlerb/bug190.txt @@ -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: "'"