Skip to content

Commit

Permalink
Fix an incompatibility with the parser translator
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Earlopain committed Dec 21, 2024
1 parent e1e5b55 commit 72909b4
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 29 deletions.
6 changes: 3 additions & 3 deletions lib/prism/translation/parser/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1692,7 +1692,7 @@ def visit_string_node(node)
start_offset = node.content_loc.start_offset

[content_lines, unescaped_lines].transpose.map do |content_line, unescaped_line|
end_offset = start_offset + content_line.length
end_offset = start_offset + content_line.bytesize
offsets = srange_offsets(start_offset, end_offset)
start_offset = end_offset

Expand Down Expand Up @@ -1747,7 +1747,7 @@ def visit_symbol_node(node)
start_offset = node.value_loc.start_offset

node.value.lines.map do |line|
end_offset = start_offset + line.length
end_offset = start_offset + line.bytesize
offsets = srange_offsets(start_offset, end_offset)
start_offset = end_offset

Expand Down Expand Up @@ -1890,7 +1890,7 @@ def visit_x_string_node(node)
start_offset = node.content_loc.start_offset

node.unescaped.lines.map do |line|
end_offset = start_offset + line.length
end_offset = start_offset + line.bytesize
offsets = srange_offsets(start_offset, end_offset)
start_offset = end_offset

Expand Down
2 changes: 1 addition & 1 deletion lib/prism/translation/parser/lexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ def to_a
adjustment = 0
end

end_offset = start_offset + adjusted_line.length + adjustment
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
end
Expand Down
3 changes: 3 additions & 0 deletions test/prism/fixtures/dstring.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,6 @@ foo\\\\
"
foo\\\\\
"

"
’"
3 changes: 3 additions & 0 deletions test/prism/fixtures/dsym_str.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
:"foo
bar"

:"
’"
3 changes: 3 additions & 0 deletions test/prism/fixtures/xstring.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@
``

%x{}

`
’`
1 change: 1 addition & 0 deletions test/prism/ruby/ruby_parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class RubyParserTest < TestCase
# https://github.com/seattlerb/ruby_parser/issues/344
failures = [
"alias.txt",
"dsym_str.txt",
"dos_endings.txt",
"heredocs_with_ignored_newlines.txt",
"method_calls.txt",
Expand Down
22 changes: 14 additions & 8 deletions test/prism/snapshots/dstring.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 15 additions & 9 deletions test/prism/snapshots/dsym_str.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 14 additions & 8 deletions test/prism/snapshots/xstring.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 72909b4

Please sign in to comment.