From 4095e7d2be0ef6426e0cb75a53472f6dc1e5a0af Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Mon, 11 Dec 2023 09:30:24 -0500 Subject: [PATCH] [ruby/prism] Regexp terminator escapes https://github.com/ruby/prism/commit/42a48a2ba9 --- prism/prism.c | 19 +++++++++++++++++-- test/prism/snapshots/seattlerb/bug190.txt | 2 +- 2 files changed, 18 insertions(+), 3 deletions(-) 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: "'"