diff options
author | Earlopain <[email protected]> | 2025-01-10 16:18:56 +0100 |
---|---|---|
committer | Kevin Newton <[email protected]> | 2025-01-11 19:09:05 -0500 |
commit | 9c962ea7926e06a57a738bd1e0232ccf09e32771 (patch) | |
tree | c37fc681e32823ced7ad31675abb274799159619 | |
parent | 110461c509a3f93060cf66c82ed44baa00e182a1 (diff) |
[ruby/prism] Fix parser translator tokens for backslashes in single-quoted strings and word arrays
These are not line continuations. They either should be taken literally,
or allow the word array to contain the following whitespace (newlines in this case)
Before:
```
0...1: tSTRING_BEG => "'"
1...12: tSTRING_CONTENT => "foobar\\\n"
12...16: tSTRING_CONTENT => "baz\n"
16...17: tSTRING_END => "'"
17...18: tNL => nil
```
After:
```
0...1: tSTRING_BEG => "'"
1...6: tSTRING_CONTENT => "foo\\\n"
6...12: tSTRING_CONTENT => "bar\\\n"
12...16: tSTRING_CONTENT => "baz\n"
16...17: tSTRING_END => "'"
17...18: tNL => nil
```
https://github.com/ruby/prism/commit/b6554ad64e
-rw-r--r-- | lib/prism/translation/parser/lexer.rb | 19 | ||||
-rw-r--r-- | test/prism/fixtures/strings.txt | 5 |
2 files changed, 24 insertions, 0 deletions
diff --git a/lib/prism/translation/parser/lexer.rb b/lib/prism/translation/parser/lexer.rb index 71eafe5a1a..270f0cf56d 100644 --- a/lib/prism/translation/parser/lexer.rb +++ b/lib/prism/translation/parser/lexer.rb @@ -333,6 +333,7 @@ module Prism lines.map do |line| newline = line.end_with?("\r\n") ? "\r\n" : "\n" chomped_line = line.chomp +<<<<<<< HEAD if match = chomped_line.match(/(?<backslashes>\\+)\z/) adjustment = match[:backslashes].size / 2 adjusted_line = chomped_line.delete_suffix("\\" * adjustment) @@ -342,6 +343,24 @@ module Prism else adjusted_line << newline end +======= + backslash_count = chomped_line[/\\{1,}\z/]&.length || 0 + is_interpolation = interpolation?(quote_stack.last) + is_percent_array = percent_array?(quote_stack.last) + + if backslash_count.odd? && (is_interpolation || is_percent_array) + if is_percent_array + # Remove the last backslash, keep potential newlines + current_line << line.sub(/(\\)(\r?\n)\z/, '\2') + adjustment += 1 + else + chomped_line.delete_suffix!("\\") + current_line << chomped_line + adjustment += 2 + end + # If the string ends with a line continuation emit the remainder + emit = index == lines.count - 1 +>>>>>>> b6554ad64e (Fix parser translator tokens for backslashes in single-quoted strings and word arrays) else adjusted_line = line adjustment = 0 diff --git a/test/prism/fixtures/strings.txt b/test/prism/fixtures/strings.txt index a8861be687..83f38cb606 100644 --- a/test/prism/fixtures/strings.txt +++ b/test/prism/fixtures/strings.txt @@ -83,6 +83,11 @@ b\nar '\\ foo \\ bar' +'foo\ +bar\\ +baz +' + "#$foo" "#@foo" |