Skip to content

Commit 06ab4df

Browse files
committed
Fix an AST and token incompatibility for Prism::Translation::Parser
Fixes #2506. This PR fixes an AST and token incompatibility between Parser gem and `Prism::Translation::Parser` for symbols quoted with line breaks.
1 parent 7f5f426 commit 06ab4df

File tree

4 files changed

+38
-1
lines changed

4 files changed

+38
-1
lines changed

lib/prism/translation/parser/compiler.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1526,9 +1526,23 @@ def visit_symbol_node(node)
15261526
builder.symbol([node.unescaped, srange(node.location)])
15271527
end
15281528
else
1529+
parts = if node.value.lines.one?
1530+
[builder.string_internal([node.unescaped, srange(node.value_loc)])]
1531+
else
1532+
start_offset = node.value_loc.start_offset
1533+
1534+
node.value.lines.map do |line|
1535+
end_offset = start_offset + line.length
1536+
offsets = srange_offsets(start_offset, end_offset)
1537+
start_offset = end_offset
1538+
1539+
builder.string_internal([line, offsets])
1540+
end
1541+
end
1542+
15291543
builder.symbol_compose(
15301544
token(node.opening_loc),
1531-
[builder.string_internal([node.unescaped, srange(node.value_loc)])],
1545+
parts,
15321546
token(node.closing_loc)
15331547
)
15341548
end

lib/prism/translation/parser/lexer.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,16 @@ def to_a
285285
quote = value[2] == "-" || value[2] == "~" ? value[3] : value[2]
286286
value = "<<#{quote == "'" || quote == "\"" ? quote : "\""}"
287287
end
288+
when :tSTRING_CONTENT
289+
unless (lines = token.value.lines).one?
290+
start_offset = offset_cache[token.location.start_offset]
291+
lines.map do |line|
292+
end_offset = start_offset + line.length
293+
tokens << [:tSTRING_CONTENT, [line, Range.new(source_buffer, start_offset, offset_cache[end_offset])]]
294+
start_offset = end_offset
295+
end
296+
next
297+
end
288298
when :tSTRING_DVAR
289299
value = nil
290300
when :tSTRING_END

test/prism/fixtures/dsym_str.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:"foo
2+
bar"

test/prism/snapshots/dsym_str.txt

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)