diff options
author | Koichi ITO <koic.ito@gmail.com> | 2024-03-02 00:43:15 +0900 |
---|---|---|
committer | git <svn-admin@ruby-lang.org> | 2024-03-12 10:45:56 +0000 |
commit | 2af6bc26c593951fc591a9d77461d092fab728d8 (patch) | |
tree | 751289b0bba03140e42ddb91c3d5493c8b7db840 /lib/prism/translation | |
parent | 76bd586330d98b8a0023ff35c7fa6014ba3a4715 (diff) |
[ruby/prism] Fix an AST and token incompatibility for `Prism::Translation::Parser`
Fixes https://github.com/ruby/prism/pull/2515.
This PR fixes an AST and token incompatibility between Parser gem and `Prism::Translation::Parser`
for string literal with line breaks.
https://github.com/ruby/prism/commit/c58466e5bf
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 | 2 |
2 files changed, 16 insertions, 2 deletions
diff --git a/lib/prism/translation/parser/compiler.rb b/lib/prism/translation/parser/compiler.rb index d64e382c78..bf5b85441b 100644 --- a/lib/prism/translation/parser/compiler.rb +++ b/lib/prism/translation/parser/compiler.rb @@ -1487,9 +1487,23 @@ module Prism elsif node.opening == "?" builder.character([node.unescaped, srange(node.location)]) else + parts = if node.unescaped.lines.count <= 1 + [builder.string_internal([node.unescaped, srange(node.content_loc)])] + else + start_offset = node.content_loc.start_offset + + node.unescaped.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.string_compose( token(node.opening_loc), - [builder.string_internal([node.unescaped, srange(node.content_loc)])], + parts, token(node.closing_loc) ) end diff --git a/lib/prism/translation/parser/lexer.rb b/lib/prism/translation/parser/lexer.rb index b28273b03f..8bb3084577 100644 --- a/lib/prism/translation/parser/lexer.rb +++ b/lib/prism/translation/parser/lexer.rb @@ -281,7 +281,7 @@ module Prism value = "" location = Range.new(source_buffer, offset_cache[next_location.start_offset], offset_cache[next_location.end_offset]) index += 1 - elsif ["\"", "'"].include?(value) && (next_token = lexed[index][0]) && next_token.type == :STRING_CONTENT && (next_next_token = lexed[index + 1][0]) && next_next_token.type == :STRING_END + elsif ["\"", "'"].include?(value) && (next_token = lexed[index][0]) && next_token.type == :STRING_CONTENT && next_token.value.lines.count <= 1 && (next_next_token = lexed[index + 1][0]) && next_next_token.type == :STRING_END next_location = token.location.join(next_next_token.location) type = :tSTRING value = next_token.value |