Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Fix" transpose issue in parser compiler #2714

Merged
merged 1 commit into from
Apr 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 20 additions & 10 deletions lib/prism/translation/parser/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1535,19 +1535,29 @@ def visit_string_node(node)
elsif node.opening == "?"
builder.character([node.unescaped, srange(node.location)])
else
parts = if node.content.lines.count <= 1 || node.unescaped.lines.count <= 1
[builder.string_internal([node.unescaped, srange(node.content_loc)])]
else
start_offset = node.content_loc.start_offset
content_lines = node.content.lines
unescaped_lines = node.unescaped.lines

[node.content.lines, node.unescaped.lines].transpose.map do |content_line, unescaped_line|
end_offset = start_offset + content_line.length
offsets = srange_offsets(start_offset, end_offset)
start_offset = end_offset
parts =
if content_lines.length <= 1 || unescaped_lines.length <= 1
[builder.string_internal([node.unescaped, srange(node.content_loc)])]
elsif content_lines.length != unescaped_lines.length
# This occurs when we have line continuations in the string. We
# need to come back and fix this, but for now this stops the
# code from breaking when we encounter it because of trying to
# transpose arrays of different lengths.
[builder.string_internal([node.unescaped, srange(node.content_loc)])]
else
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
offsets = srange_offsets(start_offset, end_offset)
start_offset = end_offset

builder.string_internal([unescaped_line, offsets])
builder.string_internal([unescaped_line, offsets])
end
end
end

builder.string_compose(
token(node.opening_loc),
Expand Down