Skip to content

Commit

Permalink
feat: add parsing of arithmetics (#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
prsabahrami authored Sep 19, 2024
1 parent 5462c56 commit 8f14fb5
Show file tree
Hide file tree
Showing 9 changed files with 1,334 additions and 40 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/deno_task_shell/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pest_derive = "2.7.12"
dirs = "5.0.1"
pest_ascii_tree = { git = "https://github.com/prsabahrami/pest_ascii_tree.git", branch = "master" }
miette = "7.2.0"
lazy_static = "1.4.0"

[dev-dependencies]
tempfile = "3.12.0"
Expand Down
96 changes: 90 additions & 6 deletions crates/deno_task_shell/src/grammar.pest
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// Whitespace and comments
WHITESPACE = _{ " " | "\t" | ("\\" ~ WHITESPACE* ~ NEWLINE) }
COMMENT = _{ "#" ~ (!NEWLINE ~ ANY)* }
NUMBER = @{ INT ~ ("." ~ ASCII_DIGIT*)? ~ (^"e" ~ INT)? }
INT = { ("+" | "-")? ~ ASCII_DIGIT+ }

// Basic tokens
QUOTED_WORD = { DOUBLE_QUOTED | SINGLE_QUOTED }
Expand All @@ -11,6 +13,7 @@ UNQUOTED_PENDING_WORD = ${
(TILDE_PREFIX ~ (!(OPERATOR | WHITESPACE | NEWLINE) ~ (
EXIT_STATUS |
UNQUOTED_ESCAPE_CHAR |
"$" ~ ARITHMETIC_EXPRESSION |
SUB_COMMAND |
("$" ~ "{" ~ VARIABLE ~ "}" | "$" ~ VARIABLE) |
UNQUOTED_CHAR |
Expand All @@ -20,6 +23,7 @@ UNQUOTED_PENDING_WORD = ${
(!(OPERATOR | WHITESPACE | NEWLINE) ~ (
EXIT_STATUS |
UNQUOTED_ESCAPE_CHAR |
"$" ~ ARITHMETIC_EXPRESSION |
SUB_COMMAND |
("$" ~ "{" ~ VARIABLE ~ "}" | "$" ~ VARIABLE) |
UNQUOTED_CHAR |
Expand Down Expand Up @@ -69,7 +73,7 @@ QUOTED_ESCAPE_CHAR = ${ "\\" ~ "$" | "$" ~ !"(" ~ !"{" ~ !VARIABLE | "\\" ~ ("`"
UNQUOTED_CHAR = ${ ("\\" ~ " ") | !("]]" | "[[" | "(" | ")" | "<" | ">" | "|" | "&" | ";" | "\"" | "'" | "$") ~ ANY }
QUOTED_CHAR = ${ !"\"" ~ ANY }

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

DOUBLE_QUOTED = @{ "\"" ~ QUOTED_PENDING_WORD ~ "\"" }
Expand Down Expand Up @@ -148,6 +152,7 @@ command = !{

compound_command = {
brace_group |
ARITHMETIC_EXPRESSION |
subshell |
for_clause |
case_clause |
Expand All @@ -156,7 +161,86 @@ compound_command = {
until_clause
}

subshell = !{ "(" ~ compound_list ~ ")" }
ARITHMETIC_EXPRESSION = !{ "((" ~ arithmetic_sequence ~ "))" }
arithmetic_sequence = !{ arithmetic_expr ~ ("," ~ arithmetic_expr)* }
arithmetic_expr = { parentheses_expr | variable_assignment | triple_conditional_expr | binary_arithmetic_expr | binary_conditional_expression | unary_arithmetic_expr | VARIABLE | NUMBER }
parentheses_expr = !{ "(" ~ arithmetic_sequence ~ ")" }

variable_assignment = !{
VARIABLE ~ assignment_operator ~ arithmetic_expr
}

triple_conditional_expr = !{
(parentheses_expr | variable_assignment | binary_arithmetic_expr | binary_conditional_expression | unary_arithmetic_expr | VARIABLE | NUMBER) ~
"?" ~ (parentheses_expr | variable_assignment | binary_arithmetic_expr | binary_conditional_expression | unary_arithmetic_expr | VARIABLE | NUMBER) ~
":" ~ (parentheses_expr | variable_assignment | binary_arithmetic_expr | binary_conditional_expression | unary_arithmetic_expr | VARIABLE | NUMBER)
}

binary_arithmetic_expr = _{
(parentheses_expr | binary_conditional_expression | unary_arithmetic_expr | variable_assignment | VARIABLE | NUMBER) ~
(binary_arithmetic_op ~
(parentheses_expr | variable_assignment | binary_conditional_expression | unary_arithmetic_expr | VARIABLE | NUMBER)
)+
}

binary_arithmetic_op = _{
add | subtract | power | multiply | divide | modulo | left_shift | right_shift |
bitwise_and | bitwise_xor | bitwise_or | logical_and | logical_or
}

add = { "+" }
subtract = { "-" }
multiply = { "*" }
divide = { "/" }
modulo = { "%" }
power = { "**" }
left_shift = { "<<" }
right_shift = { ">>" }
bitwise_and = { "&" }
bitwise_xor = { "^" }
bitwise_or = { "|" }
logical_and = { "&&" }
logical_or = { "||" }

unary_arithmetic_expr = !{
(unary_arithmetic_op | post_arithmetic_op) ~ (parentheses_expr | VARIABLE | NUMBER) |
(parentheses_expr | VARIABLE | NUMBER) ~ post_arithmetic_op
}

unary_arithmetic_op = _{
unary_plus | unary_minus | logical_not | bitwise_not
}

unary_plus = { "+" }
unary_minus = { "-" }
logical_not = { "!" }
bitwise_not = { "~" }

post_arithmetic_op = !{
increment | decrement
}

increment = { "++" }
decrement = { "--" }

assignment_operator = _{
assign | multiply_assign | divide_assign | modulo_assign | add_assign | subtract_assign |
left_shift_assign | right_shift_assign | bitwise_and_assign | bitwise_xor_assign | bitwise_or_assign
}

assign = { "=" }
multiply_assign = { "*=" }
divide_assign = { "/=" }
modulo_assign = { "%=" }
add_assign = { "+=" }
subtract_assign = { "-=" }
left_shift_assign = { "<<=" }
right_shift_assign = { ">>=" }
bitwise_and_assign = { "&=" }
bitwise_xor_assign = { "^=" }
bitwise_or_assign = { "|=" }

subshell = !{ "(" ~ compound_list ~ ")" }
compound_list = !{ (newline_list? ~ term ~ separator?)+ }
term = !{ and_or ~ (separator ~ and_or)* }

Expand Down Expand Up @@ -232,16 +316,16 @@ string_conditional_op = !{

binary_conditional_expression = !{
UNQUOTED_PENDING_WORD ~ (
binary_string_conditional_op |
binary_arithmetic_conditional_op
binary_bash_conditional_op |
binary_posix_conditional_op
) ~ UNQUOTED_PENDING_WORD
}

binary_string_conditional_op = !{
binary_bash_conditional_op = !{
"==" | "=" | "!=" | "<" | ">"
}

binary_arithmetic_conditional_op = !{
binary_posix_conditional_op = !{
"-eq" | "-ne" | "-lt" | "-le" | "-gt" | "-ge"
}

Expand Down
Loading

0 comments on commit 8f14fb5

Please sign in to comment.