Skip to content

Commit

Permalink
Complex commands parsing fix
Browse files Browse the repository at this point in the history
  • Loading branch information
prsabahrami committed Sep 7, 2024
1 parent ebf9af7 commit 142b9db
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 20 deletions.
38 changes: 20 additions & 18 deletions crates/deno_task_shell/src/grammar.pest
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ COMMENT = _{ "#" ~ (!NEWLINE ~ ANY)* }
// Basic tokens
QUOTED_WORD = { DOUBLE_QUOTED | SINGLE_QUOTED }

UNQUOTED_PENDING_WORD = ${
(!(WHITESPACE | OPERATOR | NEWLINE) ~ (
UNQUOTED_PENDING_WORD = ${ !OPERATOR ~
(!(WHITESPACE | NEWLINE) ~ (
EXIT_STATUS |
UNQUOTED_ESCAPE_CHAR |
SUB_COMMAND |
("$" ~ VARIABLE) |
("$" ~ "{" ~ VARIABLE ~ "}" | "$" ~ VARIABLE) |
UNQUOTED_CHAR |
QUOTED_WORD
)
Expand All @@ -24,18 +24,18 @@ QUOTED_PENDING_WORD = ${ (
EXIT_STATUS |
QUOTED_ESCAPE_CHAR |
SUB_COMMAND |
("$" ~ VARIABLE) |
("$" ~ "{" ~ VARIABLE ~ "}" | "$" ~ VARIABLE) |
QUOTED_CHAR
)* }

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

UNQUOTED_CHAR = ${ ("\\" ~ " ") | !("(" | ")" | "{" | "}" | "<" | ">" | "|" | "&" | ";" | "\"" | "'" | "$") ~ ANY }
UNQUOTED_CHAR = ${ ("\\" ~ " ") | !("]]" | "[[" | "(" | ")" | "<" | ">" | "|" | "&" | ";" | "\"" | "'" | "$") ~ ANY }
QUOTED_CHAR = ${ !"\"" ~ ANY }

VARIABLE = ${ (ASCII_ALPHANUMERIC | "_")+ }
SUB_COMMAND = { "$(" ~ complete_command ~ ")" }
SUB_COMMAND = { "$(" ~ complete_command ~ ")"}

DOUBLE_QUOTED = @{ "\"" ~ QUOTED_PENDING_WORD ~ "\"" }
SINGLE_QUOTED = @{ "'" ~ (!"'" ~ ANY)* ~ "'" }
Expand Down Expand Up @@ -64,7 +64,7 @@ EXIT_STATUS = ${ "$?" }
// Operators
OPERATOR = _{
AND_IF | OR_IF | DSEMI | DLESS | DGREAT | LESSAND | GREATAND | LESSGREAT | DLESSDASH | CLOBBER |
"(" | ")" | "{" | "}" | ";" | "&" | "|" | "<" | ">"
"," |"(" | ")" | "{" | "}" | ";" | "&" | "|" | "<" | ">"
}

// Reserved words
Expand Down Expand Up @@ -102,12 +102,13 @@ pipeline = !{ Bang? ~ pipe_sequence }
pipe_sequence = !{ command ~ ((StdoutStderr | Stdout) ~ linebreak ~ pipe_sequence)? }

command = !{
simple_command |
compound_command ~ redirect_list? |
simple_command |
function_definition
}

compound_command = {
double_square_bracket |
brace_group |
subshell |
for_clause |
Expand All @@ -123,10 +124,12 @@ term = !{ and_or ~ (separator ~ and_or)* }

for_clause = {
For ~ name ~ linebreak ~
(linebreak ~ In ~ wordlist? ~ sequential_sep)? ~
linebreak ~ do_group
(In ~ (brace_group | wordlist)? ~ sequential_sep)? ~
do_group
}

double_square_bracket = !{ "[[" ~ compound_list ~ "]]" }

case_clause = !{
Case ~ UNQUOTED_PENDING_WORD ~ linebreak ~
linebreak ~ In ~ linebreak ~
Expand Down Expand Up @@ -156,14 +159,13 @@ pattern = !{

if_clause = !{
If ~ compound_list ~
Then ~ compound_list ~
else_part? ~
Fi
linebreak ~ Then ~ linebreak ~ compound_list ~ linebreak ~
else_part? ~ linebreak ~ Fi
}

else_part = !{
Elif ~ compound_list ~ Then ~ else_part |
Else ~ compound_list
Elif ~ compound_list ~ Then ~ linebreak ~ else_part |
Else ~ linebreak ~ compound_list
}

while_clause = !{ While ~ compound_list ~ do_group }
Expand All @@ -185,7 +187,7 @@ simple_command = !{

cmd_prefix = !{ (io_redirect | ASSIGNMENT_WORD)+ }
cmd_suffix = !{ (io_redirect | UNQUOTED_PENDING_WORD)+ }
cmd_name = @{ (RESERVED_WORD | UNQUOTED_PENDING_WORD) }
cmd_name = @{ !RESERVED_WORD ~ UNQUOTED_PENDING_WORD }
cmd_word = @{ (ASSIGNMENT_WORD | UNQUOTED_PENDING_WORD) }

redirect_list = !{ io_redirect+ }
Expand Down
2 changes: 0 additions & 2 deletions crates/deno_task_shell/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,8 +332,6 @@ struct ShellParser;
pub fn parse(input: &str) -> Result<SequentialList> {
let mut pairs = ShellParser::parse(Rule::FILE, input)?;

// println!("pairs: {:?}", pairs);

parse_file(pairs.next().unwrap())
}

Expand Down
23 changes: 23 additions & 0 deletions scripts/for_loop.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# for i in {1..10}; do
# echo $i
# done


for i in $(seq 1 2 20); do
echo $i
done


# for i in {1..10..2}; do
# echo $i
# done


# for i in $(1,2,3,4,5); do
# echo $i
# done


for i in $(ls); do
printf "%s\n" "$i"
done
5 changes: 5 additions & 0 deletions scripts/if_else.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
if [[ $FOO == "bar" ]] then
echo "FOO is bar"
else
echo "FOO is not bar"
fi
5 changes: 5 additions & 0 deletions scripts/while_loop.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
COUNTER=-5
while [[ $COUNTER -lt 5 ]]; do
echo The counter is $COUNTER
let COUNTER=COUNTER+1
done

0 comments on commit 142b9db

Please sign in to comment.