Skip to content

Commit a122353

Browse files
authored
Merge pull request #3419 from Earlopain/parser-translator-regex-fake-nl
Fix parser translator ast for regex with fake newlines
2 parents 9b4ebf4 + 36d04a7 commit a122353

File tree

3 files changed

+153
-6
lines changed

3 files changed

+153
-6
lines changed

lib/prism/translation/parser/compiler.rb

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,7 +1100,7 @@ def visit_integer_node(node)
11001100
def visit_interpolated_regular_expression_node(node)
11011101
builder.regexp_compose(
11021102
token(node.opening_loc),
1103-
visit_all(node.parts),
1103+
string_nodes_from_interpolation(node, node.opening),
11041104
[node.closing[0], srange_offsets(node.closing_loc.start_offset, node.closing_loc.start_offset + 1)],
11051105
builder.regexp_options([node.closing[1..], srange_offsets(node.closing_loc.start_offset + 1, node.closing_loc.end_offset)])
11061106
)
@@ -2119,6 +2119,7 @@ def string_nodes_from_line_continuations(unescaped, escaped, start_offset, openi
21192119
unescaped = unescaped.lines
21202120
escaped = escaped.lines
21212121
percent_array = opening&.start_with?("%w", "%W", "%i", "%I")
2122+
regex = opening == "/" || opening&.start_with?("%r")
21222123

21232124
# Non-interpolating strings
21242125
if opening&.end_with?("'") || opening&.start_with?("%q", "%s", "%w", "%i")
@@ -2153,11 +2154,18 @@ def string_nodes_from_line_continuations(unescaped, escaped, start_offset, openi
21532154
.chunk_while { |before, after| before[/(\\*)\r?\n$/, 1]&.length&.odd? || false }
21542155
.each do |lines|
21552156
escaped_lengths << lines.sum(&:bytesize)
2156-
unescaped_lines_count = lines.sum do |line|
2157-
count = line.scan(/(\\*)n/).count { |(backslashes)| backslashes&.length&.odd? }
2158-
count -= 1 if !line.end_with?("\n") && count > 0
2159-
count
2160-
end
2157+
2158+
unescaped_lines_count =
2159+
if regex
2160+
0 # Will always be preserved as is
2161+
else
2162+
lines.sum do |line|
2163+
count = line.scan(/(\\*)n/).count { |(backslashes)| backslashes&.length&.odd? }
2164+
count -= 1 if !line.end_with?("\n") && count > 0
2165+
count
2166+
end
2167+
end
2168+
21612169
extra = 1
21622170
extra = lines.count if percent_array # Account for line continuations in percent arrays
21632171

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/
2+
\n
3+
\n
4+
exit
5+
\\n
6+
\n\n\n\n
7+
argh
8+
\\
9+
\\\
10+
foo\nbar
11+
\f
12+
ok
13+
/
14+
15+
%r{
16+
\n
17+
\n
18+
exit
19+
\\n
20+
\n\n\n\n
21+
argh
22+
\\
23+
\\\
24+
foo\nbar
25+
\f
26+
ok
27+
}
28+
29+
%r{
30+
#{123}\n
31+
\n
32+
exit\\\
33+
\\#{123}n
34+
\n#{123}\n\n\n
35+
argh\
36+
\\#{123}baz\\
37+
\\\
38+
foo\nbar
39+
\f
40+
ok
41+
}

test/prism/snapshots/regex_with_fake_newlines.txt

Lines changed: 98 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)