Skip to content

Commit c7ea494

Browse files
committed
Enable remaining heredoc unescape tests
1 parent fc49acf commit c7ea494

File tree

3 files changed

+23
-14
lines changed

3 files changed

+23
-14
lines changed

src/prism.c

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8482,6 +8482,7 @@ parser_lex(pm_parser_t *parser) {
84828482

84838483
const uint8_t *breakpoint = pm_strpbrk(parser, parser->current.end, breakpoints, parser->end - parser->current.end);
84848484
pm_token_buffer_t token_buffer = { 0 };
8485+
bool was_escaped_newline = false;
84858486

84868487
while (breakpoint != NULL) {
84878488
switch (*breakpoint) {
@@ -8509,6 +8510,7 @@ parser_lex(pm_parser_t *parser) {
85098510
// content. Then, the next time a token is lexed, it will match
85108511
// again and return the end of the heredoc.
85118512
if (
8513+
!was_escaped_newline &&
85128514
(start + ident_length <= parser->end) &&
85138515
(memcmp(start, ident_start, ident_length) == 0)
85148516
) {
@@ -8550,6 +8552,9 @@ parser_lex(pm_parser_t *parser) {
85508552
case '\r':
85518553
parser->current.end++;
85528554
if (peek(parser) != '\n') {
8555+
if (quote == PM_HEREDOC_QUOTE_SINGLE) {
8556+
pm_token_buffer_push(&token_buffer, '\\');
8557+
}
85538558
pm_token_buffer_push(&token_buffer, '\r');
85548559
break;
85558560
}
@@ -8559,25 +8564,19 @@ parser_lex(pm_parser_t *parser) {
85598564
// to leave the escaped newline in place so that
85608565
// it can be removed later when we dedent the
85618566
// heredoc.
8562-
if (lex_mode->as.heredoc.indent == PM_HEREDOC_INDENT_TILDE) {
8567+
if (quote == PM_HEREDOC_QUOTE_SINGLE || lex_mode->as.heredoc.indent == PM_HEREDOC_INDENT_TILDE) {
85638568
pm_token_buffer_push(&token_buffer, '\\');
85648569
pm_token_buffer_push(&token_buffer, '\n');
85658570
}
85668571

8567-
if (parser->heredoc_end) {
8568-
// ... if we are on the same line as a heredoc,
8569-
// flush the heredoc and continue parsing after
8570-
// heredoc_end.
8571-
parser_flush_heredoc_end(parser);
8572-
pm_token_buffer_copy(parser, &token_buffer);
8573-
LEX(PM_TOKEN_STRING_CONTENT);
8574-
} else {
8575-
// ... else track the newline.
8576-
pm_newline_list_append(&parser->newline_list, parser->current.end);
8572+
token_buffer.cursor = parser->current.end + 1;
8573+
breakpoint = parser->current.end;
8574+
8575+
if (quote != PM_HEREDOC_QUOTE_SINGLE) {
8576+
was_escaped_newline = true;
85778577
}
85788578

8579-
parser->current.end++;
8580-
break;
8579+
continue;
85818580
default:
85828581
if (quote == PM_HEREDOC_QUOTE_SINGLE) {
85838582
pm_token_buffer_push(&token_buffer, '\\');
@@ -8616,6 +8615,8 @@ parser_lex(pm_parser_t *parser) {
86168615
default:
86178616
assert(false && "unreachable");
86188617
}
8618+
8619+
was_escaped_newline = false;
86198620
}
86208621

86218622
// If we've hit the end of the string, then this is an unterminated

test/prism/snapshots/whitequark/parser_slash_slash_n_escaping_in_literals.txt

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/prism/unescape_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,14 @@ def prism_result(escape) = prism(escape, &:unescaped)
102102
[Context::String.new("%[", "]"), escapes],
103103
[Context::String.new("`", "`"), escapes],
104104
[Context::String.new("%x[", "]"), escapes],
105+
[Context::String.new("<<H\n", "\nH"), escapes],
106+
[Context::String.new("<<'H'\n", "\nH"), escapes],
107+
[Context::String.new("<<\"H\"\n", "\nH"), escapes],
108+
[Context::String.new("<<`H`\n", "\nH"), escapes],
109+
[Context::String.new("<<-H\n", "\nH"), escapes],
110+
[Context::String.new("<<-'H'\n", "\nH"), escapes],
111+
[Context::String.new("<<-\"H\"\n", "\nH"), escapes],
112+
[Context::String.new("<<-`H`\n", "\nH"), escapes],
105113
# [Context::String.new("<<~H\n", "\nH"), escapes],
106114
# [Context::String.new("<<~'H'\n", "\nH"), escapes],
107115
# [Context::String.new("<<~\"H\"\n", "\nH"), escapes],

0 commit comments

Comments
 (0)