Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion brush-parser/src/word.rs
Original file line number Diff line number Diff line change
Expand Up @@ -872,14 +872,21 @@ peg::parser! {
legacy_arithmetic_expansion() /
command_substitution() /
parameter_expansion() /
heredoc_line_continuation() /
heredoc_escape_sequence() /
heredoc_literal_text()

// In a here-document whose delimiter is unquoted, a backslash-newline pair is a
// line continuation: it is removed outright, joining the two lines. (A delimiter
// that *is* quoted suppresses expansion altogether, so this rule never sees it.)
rule heredoc_line_continuation() -> WordPiece =
"\\\n" { WordPiece::Text(String::new()) }

rule heredoc_escape_sequence() -> WordPiece =
s:$("\\" ['$' | '`' | '\\']) { WordPiece::EscapeSequence(s.to_owned()) }

rule heredoc_literal_text() -> WordPiece =
s:$((!heredoc_escape_sequence() !dollar_sign_word_piece() [^'`'])+) {
s:$((!heredoc_line_continuation() !heredoc_escape_sequence() !dollar_sign_word_piece() [^'`'])+) {
WordPiece::Text(s.to_owned())
}

Expand Down
36 changes: 35 additions & 1 deletion brush-shell/tests/cases/compat/here.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -306,4 +306,38 @@ cases:
i=$((i + 1))
done
out=$(cat <<<"$payload")
echo "${#out}"
echo "${#out}"

- name: "Here doc with line continuation"
stdin: |
cat <<EOF
alpha \
beta
EOF

- name: "Here doc with line continuation and quoted tag"
stdin: |
cat <<'EOF'
alpha \
beta
EOF

- name: "Here doc with line continuation before expansion"
stdin: |
v=world
cat <<EOF
hello \
$v
EOF

- name: "Here doc escapes alongside line continuation"
stdin: |
v=world
cat <<EOF
plain $v
dollar \$v
backslash \\ end
backtick \` end
cont \
joined
EOF
Loading