Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 31 additions & 13 deletions lib/prism/translation/parser/lexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -377,12 +377,20 @@ def to_a

lines.each.with_index do |line, index|
chomped_line = line.chomp

# When the line ends with an odd number of backslashes, it must be a line continuation.
if chomped_line[/\\{1,}\z/]&.length&.odd?
chomped_line.delete_suffix!("\\")
current_line << chomped_line
adjustment += 2
backslash_count = chomped_line[/\\{1,}\z/]&.length || 0
is_interpolation = interpolation?(quote_stack.last)
is_percent_array = percent_array?(quote_stack.last)

if backslash_count.odd? && (is_interpolation || is_percent_array)
if is_percent_array
# Remove the last backslash, keep potential newlines
current_line << line.sub(/(\\)(\r?\n)\z/, '\2')
adjustment += 1
else
chomped_line.delete_suffix!("\\")
current_line << chomped_line
adjustment += 2
end
# If the string ends with a line continuation emit the remainder
emit = index == lines.count - 1
else
Expand Down Expand Up @@ -577,7 +585,13 @@ def unescape_string(string, quote)
# TODO: Implement regexp escaping
return string if quote == "/" || quote.start_with?("%r")

if quote == "'" || quote.start_with?("%q") || quote.start_with?("%w") || quote.start_with?("%i")
if interpolation?(quote)
# In interpolation, escape sequences can be written literally. For example, "\\n" becomes "\n",
# and "\\\\n" becomes "\\n". Unknown escapes sequences, like "\\o" simply become "o".
string.gsub(/\\./) do |match|
ESCAPES[match[1]] || match[1]
end
else
if quote == "'"
delimiter = "'"
else
Expand All @@ -586,14 +600,18 @@ def unescape_string(string, quote)

delimiters = Regexp.escape("#{delimiter}#{DELIMITER_SYMETRY[delimiter]}")
string.gsub(/\\([\\#{delimiters}])/, '\1')
else
# When double-quoted, escape sequences can be written literally. For example, "\\n" becomes "\n",
# and "\\\\n" becomes "\\n". Unknown escapes sequences, like "\\o" simply become "o".
string.gsub(/\\./) do |match|
ESCAPES[match[1]] || match[1]
end
end
end

# Determine if characters preceeded by a backslash should be escaped or not
def interpolation?(quote)
quote != "'" && !quote.start_with?("%q", "%w", "%i")
end

# Determine if the string is part of a %-style array.
def percent_array?(quote)
quote.start_with?("%w", "%W", "%i", "%I")
end
end
end
end
Expand Down
5 changes: 5 additions & 0 deletions test/prism/fixtures/strings.txt
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ b\nar

'\\ foo \\ bar'

'foo\
bar\\
baz
'

"#$foo"

"#@foo"
Expand Down
1 change: 0 additions & 1 deletion test/prism/ruby/parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ class ParserTest < TestCase
skip_tokens = [
"comments.txt",
"dash_heredocs.txt",
"dos_endings.txt",
"embdoc_no_newline_at_end.txt",
"heredoc_with_comment.txt",
"heredocs_with_ignored_newlines.txt",
Expand Down
168 changes: 87 additions & 81 deletions test/prism/snapshots/strings.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading