Skip to content

Commit 1fbac72

Browse files
committed
Handle CLRF inside heredoc contents
1 parent f89e813 commit 1fbac72

12 files changed

+32
-30
lines changed

src/prism.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11267,11 +11267,11 @@ parser_lex(pm_parser_t *parser) {
1126711267
// Otherwise we'll be parsing string content. These are the places
1126811268
// where we need to split up the content of the heredoc. We'll use
1126911269
// strpbrk to find the first of these characters.
11270-
uint8_t breakpoints[] = "\n\\#";
11270+
uint8_t breakpoints[] = "\r\n\\#";
1127111271

1127211272
pm_heredoc_quote_t quote = lex_mode->as.heredoc.quote;
1127311273
if (quote == PM_HEREDOC_QUOTE_SINGLE) {
11274-
breakpoints[2] = '\0';
11274+
breakpoints[3] = '\0';
1127511275
}
1127611276

1127711277
const uint8_t *breakpoint = pm_strpbrk(parser, parser->current.end, breakpoints, parser->end - parser->current.end, true);
@@ -11285,6 +11285,21 @@ parser_lex(pm_parser_t *parser) {
1128511285
parser->current.end = breakpoint + 1;
1128611286
breakpoint = pm_strpbrk(parser, parser->current.end, breakpoints, parser->end - parser->current.end, true);
1128711287
break;
11288+
case '\r':
11289+
parser->current.end = breakpoint + 1;
11290+
11291+
if (peek_at(parser, breakpoint + 1) != '\n') {
11292+
breakpoint = pm_strpbrk(parser, parser->current.end, breakpoints, parser->end - parser->current.end, true);
11293+
break;
11294+
}
11295+
11296+
// If we hit a \r\n sequence, then we want to replace it
11297+
// with a single \n character in the final string.
11298+
pm_token_buffer_escape(parser, &token_buffer);
11299+
breakpoint++;
11300+
token_buffer.cursor = breakpoint;
11301+
11302+
/* fallthrough */
1128811303
case '\n': {
1128911304
if (parser->heredoc_end != NULL && (parser->heredoc_end > breakpoint)) {
1129011305
parser_flush_heredoc_end(parser);

test/prism/ruby_parser_test.rb

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -52,25 +52,10 @@ class RubyParserTest < TestCase
5252
whitequark/string_concat.txt
5353
]
5454

55-
# These files contain CRLF line endings, which ruby_parser translates into
56-
# LF before it gets back to the node. This means the node actually has the
57-
# wrong contents.
58-
crlf = %w[
59-
dos_endings.txt
60-
heredoc_with_comment.txt
61-
seattlerb/heredoc__backslash_dos_format.txt
62-
seattlerb/heredoc_with_carriage_return_escapes_windows.txt
63-
seattlerb/heredoc_with_extra_carriage_horrible_mix.txt
64-
seattlerb/heredoc_with_extra_carriage_returns_windows.txt
65-
seattlerb/heredoc_with_extra_carriage_returns.txt
66-
seattlerb/heredoc_with_interpolation_and_carriage_return_escapes_windows.txt
67-
seattlerb/heredoc_with_only_carriage_returns_windows.txt
68-
seattlerb/heredoc_with_only_carriage_returns.txt
69-
]
70-
7155
# https://github.com/seattlerb/ruby_parser/issues/344
72-
failures = crlf | %w[
56+
failures = %w[
7357
alias.txt
58+
dos_endings.txt
7459
heredocs_with_ignored_newlines.txt
7560
method_calls.txt
7661
methods.txt
@@ -79,8 +64,10 @@ class RubyParserTest < TestCase
7964
patterns.txt
8065
regex.txt
8166
seattlerb/and_multi.txt
67+
seattlerb/heredoc__backslash_dos_format.txt
8268
seattlerb/heredoc_bad_hex_escape.txt
8369
seattlerb/heredoc_bad_oct_escape.txt
70+
seattlerb/heredoc_with_extra_carriage_horrible_mix.txt
8471
spanning_heredoc_newlines.txt
8572
spanning_heredoc.txt
8673
tilde_heredocs.txt

test/prism/snapshots/dos_endings.txt

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/prism/snapshots/heredoc_with_comment.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/snapshots/seattlerb/heredoc__backslash_dos_format.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/snapshots/seattlerb/heredoc_with_carriage_return_escapes_windows.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/snapshots/seattlerb/heredoc_with_extra_carriage_horrible_mix.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/snapshots/seattlerb/heredoc_with_extra_carriage_returns.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/snapshots/seattlerb/heredoc_with_extra_carriage_returns_windows.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/snapshots/seattlerb/heredoc_with_interpolation_and_carriage_return_escapes_windows.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.

0 commit comments

Comments
 (0)