diff options
author | Koichi ITO <koic.ito@gmail.com> | 2024-03-03 21:25:10 +0900 |
---|---|---|
committer | git <svn-admin@ruby-lang.org> | 2024-03-07 14:05:20 +0000 |
commit | 0e4bfd08e548e90faf5d91bddccf418dc9a62a42 (patch) | |
tree | 12ac92dffdedf181274593cf0f53798a1e8bb4de /lib/prism/translation | |
parent | 78725f14b287bff19d48dea076be169c74e18b77 (diff) |
[ruby/prism] Fix an AST and token incompatibility for `Prism::Translation::Parser`
Fixes https://github.com/ruby/prism/pull/2506.
This PR fixes an AST and token incompatibility between Parser gem and `Prism::Translation::Parser`
for symbols quoted with line breaks.
https://github.com/ruby/prism/commit/06ab4df8cd
Diffstat (limited to 'lib/prism/translation')
-rw-r--r-- | lib/prism/translation/parser/compiler.rb | 16 | ||||
-rw-r--r-- | lib/prism/translation/parser/lexer.rb | 10 |
2 files changed, 25 insertions, 1 deletions
diff --git a/lib/prism/translation/parser/compiler.rb b/lib/prism/translation/parser/compiler.rb index 8170e1953b..d64e382c78 100644 --- a/lib/prism/translation/parser/compiler.rb +++ b/lib/prism/translation/parser/compiler.rb @@ -1528,9 +1528,23 @@ module Prism builder.symbol([node.unescaped, srange(node.location)]) end else + 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.length + offsets = srange_offsets(start_offset, end_offset) + start_offset = end_offset + + builder.string_internal([line, offsets]) + end + end + builder.symbol_compose( token(node.opening_loc), - [builder.string_internal([node.unescaped, srange(node.value_loc)])], + parts, token(node.closing_loc) ) end diff --git a/lib/prism/translation/parser/lexer.rb b/lib/prism/translation/parser/lexer.rb index 9a0a48a00f..62ce17c7ea 100644 --- a/lib/prism/translation/parser/lexer.rb +++ b/lib/prism/translation/parser/lexer.rb @@ -291,6 +291,16 @@ module Prism quote = value[2] == "-" || value[2] == "~" ? value[3] : value[2] value = "<<#{quote == "'" || quote == "\"" ? quote : "\""}" end + when :tSTRING_CONTENT + unless (lines = token.value.lines).one? + start_offset = offset_cache[token.location.start_offset] + lines.map do |line| + end_offset = start_offset + line.length + tokens << [:tSTRING_CONTENT, [line, Range.new(source_buffer, start_offset, offset_cache[end_offset])]] + start_offset = end_offset + end + next + end when :tSTRING_DVAR value = nil when :tSTRING_END |