diff options
author | Earlopain <[email protected]> | 2024-12-21 22:00:56 +0100 |
---|---|---|
committer | Kevin Newton <[email protected]> | 2025-03-18 13:36:53 -0400 |
commit | acf404e20e3d7bc18cbb8abd2b16c62f9c1f996f (patch) | |
tree | 1ed7005309d4ebafb6f0e2338f397cdd7636db76 /lib | |
parent | f49a0114e36cbff434a442561b9637760665897b (diff) |
[ruby/prism] Fix an incompatibility with the parser translator
The offset cache contains an entry for each byte so it can't be accessed via the string length.
Adds tests for all variants except for this:
```
"fo
o" "ba
’"
```
For some reason, this still has the wrong offset.
https://github.com/ruby/prism/commit/a651126458
Diffstat (limited to 'lib')
-rw-r--r-- | lib/prism/translation/parser/compiler.rb | 61 | ||||
-rw-r--r-- | lib/prism/translation/parser/lexer.rb | 6 |
2 files changed, 67 insertions, 0 deletions
diff --git a/lib/prism/translation/parser/compiler.rb b/lib/prism/translation/parser/compiler.rb index 05c3a64c5b..9fd39ca4a2 100644 --- a/lib/prism/translation/parser/compiler.rb +++ b/lib/prism/translation/parser/compiler.rb @@ -1117,6 +1117,29 @@ module Prism return visit_heredoc(node) { |children, closing| builder.string_compose(token(node.opening_loc), children, closing) } end +<<<<<<< HEAD +======= + parts = if node.parts.one? { |part| part.type == :string_node } + node.parts.flat_map do |node| + if node.type == :string_node && node.unescaped.lines.count >= 2 + start_offset = node.content_loc.start_offset + + node.unescaped.lines.map do |line| + end_offset = start_offset + line.bytesize + offsets = srange_offsets(start_offset, end_offset) + start_offset = end_offset + + builder.string_internal([line, offsets]) + end + else + visit(node) + end + end + else + visit_all(node.parts) + end + +>>>>>>> a651126458 (Fix an incompatibility with the parser translator) builder.string_compose( token(node.opening_loc), string_nodes_from_interpolation(node, node.opening), @@ -1700,7 +1723,19 @@ module Prism if node.content.include?("\n") string_nodes_from_line_continuations(node.unescaped, node.content, node.content_loc.start_offset, node.opening) else +<<<<<<< HEAD [builder.string_internal([node.unescaped, srange(node.content_loc)])] +======= + start_offset = node.content_loc.start_offset + + [content_lines, unescaped_lines].transpose.map do |content_line, unescaped_line| + end_offset = start_offset + content_line.bytesize + offsets = srange_offsets(start_offset, end_offset) + start_offset = end_offset + + builder.string_internal([unescaped_line, offsets]) + end +>>>>>>> a651126458 (Fix an incompatibility with the parser translator) end builder.string_compose( @@ -1744,6 +1779,7 @@ module Prism builder.symbol([node.unescaped, srange(node.location)]) end else +<<<<<<< HEAD parts = if node.value == "" [] @@ -1751,6 +1787,19 @@ module Prism string_nodes_from_line_continuations(node.unescaped, node.value, node.value_loc.start_offset, node.opening) else [builder.string_internal([node.unescaped, srange(node.value_loc)])] +======= + parts = if node.value.lines.one? + [builder.string_internal([node.unescaped, srange(node.value_loc)])] + else + start_offset = node.value_loc.start_offset + + node.value.lines.map do |line| + end_offset = start_offset + line.bytesize + offsets = srange_offsets(start_offset, end_offset) + start_offset = end_offset + + builder.string_internal([line, offsets]) +>>>>>>> a651126458 (Fix an incompatibility with the parser translator) end builder.symbol_compose( @@ -1889,7 +1938,19 @@ module Prism elsif node.content.include?("\n") string_nodes_from_line_continuations(node.unescaped, node.content, node.content_loc.start_offset, node.opening) else +<<<<<<< HEAD [builder.string_internal([node.unescaped, srange(node.content_loc)])] +======= + start_offset = node.content_loc.start_offset + + node.unescaped.lines.map do |line| + end_offset = start_offset + line.bytesize + offsets = srange_offsets(start_offset, end_offset) + start_offset = end_offset + + builder.string_internal([line, offsets]) + end +>>>>>>> a651126458 (Fix an incompatibility with the parser translator) end builder.xstring_compose( diff --git a/lib/prism/translation/parser/lexer.rb b/lib/prism/translation/parser/lexer.rb index 7db519499f..4f2ea1855a 100644 --- a/lib/prism/translation/parser/lexer.rb +++ b/lib/prism/translation/parser/lexer.rb @@ -422,6 +422,7 @@ module Prism value = trim_heredoc_whitespace(token.value, current_heredoc) end +<<<<<<< HEAD current_string << unescape_string(value, quote_stack.last) if (backslash_count = token.value[/(\\{1,})\n/, 1]&.length).nil? || backslash_count.even? || !interpolation?(quote_stack.last) tokens << [:tSTRING_CONTENT, [current_string, range(start_offset, start_offset + current_length)]] @@ -466,6 +467,11 @@ module Prism current_line = +"" adjustment = 0 end +======= + end_offset = start_offset + adjusted_line.bytesize + adjustment + tokens << [:tSTRING_CONTENT, [adjusted_line, Range.new(source_buffer, offset_cache[start_offset], offset_cache[end_offset])]] + start_offset = end_offset +>>>>>>> a651126458 (Fix an incompatibility with the parser translator) end end next |