Skip to content

Commit 465a86c

Browse files
Earlopainmatzbot
authored andcommitted
[ruby/prism] Fix %Q with newline delimiter and heredoc interpolation
The lexer did not jump to the `heredoc_end`, causing the heredoc end delimiter to be parsed twice. Normally the heredocs get flushed when a newline is encountered. But because the newline is part of the string delimiter, that codepath is not taken. Fixes [Bug #21758] ruby/prism@7440eb4b11
1 parent 8099e9d commit 465a86c

5 files changed

Lines changed: 44 additions & 0 deletions

File tree

prism/prism.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11612,6 +11612,13 @@ parser_lex(pm_parser_t *parser) {
1161211612
LEX(PM_TOKEN_LABEL_END);
1161311613
}
1161411614

11615+
// When the delimiter itself is a newline, we won't
11616+
// get a chance to flush heredocs in the usual places since
11617+
// the newline is already consumed.
11618+
if (term == '\n' && parser->heredoc_end) {
11619+
parser_flush_heredoc_end(parser);
11620+
}
11621+
1161511622
lex_state_set(parser, PM_LEX_STATE_END);
1161611623
lex_mode_pop(parser);
1161711624
LEX(PM_TOKEN_STRING_END);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
%q
2+
#{<<B}
3+
B
4+
^ unexpected constant, expecting end-of-input
5+
6+
<<A; %q
7+
A
8+
#{<<B}
9+
B
10+
^ unexpected constant, expecting end-of-input
11+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
%Q
2+
#{<<B}
3+
B
4+
5+
%
6+
#{<<B}
7+
B
8+
9+
<<A; %Q
10+
A
11+
#{<<B}
12+
B
13+
14+
<<A; %
15+
A
16+
#{<<B}
17+
B
18+
19+
# \r\n
20+
%Q
21+
#{<<B}
22+
B

test/prism/ruby/parser_test.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ class ParserTest < TestCase
6262
"alias.txt",
6363
"seattlerb/bug_215.txt",
6464

65+
# %Q with newline delimiter and heredoc interpolation
66+
"heredoc_percent_q_newline_delimiter.txt",
67+
6568
# 1.. && 2
6669
"ranges.txt",
6770

test/prism/ruby/ruby_parser_test.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class RubyParserTest < TestCase
3737
"alias.txt",
3838
"dsym_str.txt",
3939
"dos_endings.txt",
40+
"heredoc_percent_q_newline_delimiter.txt",
4041
"heredocs_with_fake_newlines.txt",
4142
"heredocs_with_ignored_newlines.txt",
4243
"method_calls.txt",

0 commit comments

Comments
 (0)