Skip to content

Commit

Permalink
Merge pull request #3338 from Earlopain/parser-translator-offset-cache
Browse files Browse the repository at this point in the history
  • Loading branch information
kddnewton authored Dec 21, 2024
2 parents a08cde8 + a651126 commit 79873f2
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 30 deletions.
8 changes: 4 additions & 4 deletions lib/prism/translation/parser/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1091,7 +1091,7 @@ def visit_interpolated_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 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 79873f2

Please sign in to comment.