Skip to content

Commit 3071c5d

Browse files
Earlopainmatzbot
authored andcommitted
[ruby/prism] Fix parser translator with trailing backslash in %W /%I array
https://docs.ruby-lang.org/en/master/syntax/literals_rdoc.html#label-25w+and+-25W-3A+String-Array+Literals > %W allow escape sequences described in Escape Sequences. However the continuation line <newline> is not usable because it is interpreted as the escaped newline described above. ruby/prism@f5c7460ad5
1 parent 4e5c8c1 commit 3071c5d

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

lib/prism/translation/parser/lexer.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,12 @@ def to_a
425425
end
426426

427427
current_string << unescape_string(value, quote_stack.last)
428-
if (backslash_count = token.value[/(\\{1,})\n/, 1]&.length).nil? || backslash_count.even? || !interpolation?(quote_stack.last)
428+
relevant_backslash_count = if quote_stack.last.start_with?("%W", "%I")
429+
0 # the last backslash escapes the newline
430+
else
431+
token.value[/(\\{1,})\n/, 1]&.length || 0
432+
end
433+
if relevant_backslash_count.even? || !interpolation?(quote_stack.last)
429434
tokens << [:tSTRING_CONTENT, [current_string, range(start_offset, start_offset + current_length)]]
430435
break
431436
end

test/prism/fixtures/strings.txt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,34 @@ bar)
9999
d
100100
]
101101

102+
%w[
103+
foo\nbar baz\n\n\
104+
bat\n\\\n\foo
105+
]
106+
107+
%W[
108+
foo\nbar baz\n\n\
109+
bat\n\\\n\foo
110+
]
111+
112+
%w[foo\
113+
bar
114+
baz\\
115+
bat
116+
1\n
117+
2
118+
3\\n
119+
]
120+
121+
%W[foo\
122+
bar
123+
baz\\
124+
bat
125+
1\n
126+
2
127+
3\\n
128+
]
129+
102130
%W[f\u{006f 006f}]
103131

104132
%W[a b#{c}d e]

0 commit comments

Comments
 (0)