Skip to content

Commit

Permalink
try to improve escaped char handling
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfv committed Sep 7, 2024
1 parent 738725a commit 761e862
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
19 changes: 9 additions & 10 deletions crates/deno_task_shell/src/grammar.pest
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// grammar.pest

// Whitespace and comments
WHITESPACE = _{ " " | "\t" | ("\\" ~ WHITESPACE* ~ NEWLINE) }
WHITESPACE = _{ " " | "\t" | ESCAPED_NEWLINE }
ESCAPED_NEWLINE = _{ "\\" ~ NEWLINE }
COMMENT = _{ "#" ~ (!NEWLINE ~ ANY)* }

// Basic tokens
Expand All @@ -25,11 +24,13 @@ QUOTED_PENDING_WORD = ${ (
QUOTED_ESCAPE_CHAR |
SUB_COMMAND |
("$" ~ VARIABLE) |
QUOTED_CHAR
QUOTED_CHAR |
NEWLINE |
ESCAPED_NEWLINE
)* }

UNQUOTED_ESCAPE_CHAR = ${ ("\\" ~ "$" | "$" ~ !"(" ~ !VARIABLE) | "\\" ~ (" " | "`" | "\"" | "(" | ")")* }
QUOTED_ESCAPE_CHAR = ${ "\\" ~ "$" | "$" ~ !"(" ~ !VARIABLE | "\\" ~ ("`" | "\"" | "(" | ")" | "'")* }
QUOTED_ESCAPE_CHAR = @{ "\\" ~ ("$" | "`" | "\"" | "(" | ")" | "'" | "\n" | "\r\n") }

UNQUOTED_CHAR = ${ ("\\" ~ " ") | !("(" | ")" | "{" | "}" | "<" | ">" | "|" | "&" | ";" | "\"" | "'" | "$") ~ ANY }
QUOTED_CHAR = ${ !"\"" ~ ANY }
Expand All @@ -38,12 +39,11 @@ VARIABLE = ${ (ASCII_ALPHANUMERIC | "_")+ }
SUB_COMMAND = { "$(" ~ complete_command ~ ")" }

DOUBLE_QUOTED = @{ "\"" ~ QUOTED_PENDING_WORD ~ "\"" }
SINGLE_QUOTED = @{ "'" ~ (!"'" ~ ANY)* ~ "'" }
SINGLE_QUOTED = @{ "'" ~ (!"'" ~ (NEWLINE | ESCAPED_NEWLINE | ANY))* ~ "'" }

NAME = ${ (ASCII_ALPHA | "_") ~ (ASCII_ALPHANUMERIC | "_")* }
ASSIGNMENT_WORD = { NAME ~ "=" ~ UNQUOTED_PENDING_WORD? }
IO_NUMBER = @{ ASCII_DIGIT+ }

// Special tokens
AND_IF = { "&&" }
OR_IF = { "||" }
Expand All @@ -60,7 +60,6 @@ CLOBBER = { ">|" }
AMPERSAND = { "&" }
EXIT_STATUS = ${ "$?" }


// Operators
OPERATOR = _{
AND_IF | OR_IF | DSEMI | DLESS | DGREAT | LESSAND | GREATAND | LESSGREAT | DLESSDASH | CLOBBER |
Expand Down Expand Up @@ -203,8 +202,8 @@ filename = _{ FILE_NAME_PENDING_WORD }
io_here = !{ (DLESS | DLESSDASH) ~ here_end }
here_end = @{ ("\"" ~ UNQUOTED_PENDING_WORD ~ "\"") | UNQUOTED_PENDING_WORD }

newline_list = _{ NEWLINE+ }
linebreak = _{ NEWLINE* }
newline_list = _{ (NEWLINE | ESCAPED_NEWLINE)+ }
linebreak = _{ (NEWLINE | ESCAPED_NEWLINE)* }
separator_op = { "&" | ";" }
separator = _{ separator_op ~ linebreak | newline_list }
sequential_sep = !{ ";" ~ linebreak | newline_list }
Expand Down
11 changes: 7 additions & 4 deletions crates/deno_task_shell/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,6 @@ fn parse_subshell(pair: Pair<Rule>) -> Result<Command> {

fn parse_word(pair: Pair<Rule>) -> Result<Word> {
let mut parts = Vec::new();

match pair.as_rule() {
Rule::UNQUOTED_PENDING_WORD => {
for part in pair.into_inner() {
Expand Down Expand Up @@ -748,6 +747,7 @@ fn parse_word(pair: Pair<Rule>) -> Result<Word> {
for part in pair.into_inner() {
match part.as_rule() {
Rule::UNQUOTED_ESCAPE_CHAR => {
println!("part: {:?}", part);
if let Some(WordPart::Text(ref mut text)) = parts.last_mut() {
text.push(part.as_str().chars().next().unwrap());
} else {
Expand Down Expand Up @@ -803,10 +803,14 @@ fn parse_quoted_word(pair: Pair<Rule>) -> Result<WordPart> {
match part.as_rule() {
Rule::EXIT_STATUS => parts.push(WordPart::Text("$?".to_string())),
Rule::QUOTED_ESCAPE_CHAR => {
let val = &part.as_str()[1..];
if val == "\n" {
continue;
}
if let Some(WordPart::Text(ref mut s)) = parts.last_mut() {
s.push_str(part.as_str());
s.push_str(val);
} else {
parts.push(WordPart::Text(part.as_str().to_string()));
parts.push(WordPart::Text(val.to_string()));
}
}
Rule::SUB_COMMAND => {
Expand Down Expand Up @@ -990,7 +994,6 @@ mod test {
.map_err(|e| anyhow::Error::msg(e.to_string()))?
.next()
.unwrap();
// println!("pairs: {:?}", pairs);
parse_complete_command(pairs)
};

Expand Down
4 changes: 3 additions & 1 deletion scripts/multiline.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ echo "foo \
bla"

echo "foo
bla"
bla"

echo "foo \" bar"

0 comments on commit 761e862

Please sign in to comment.