Skip to content

Commit

Permalink
fix: correct parsing of parens in arithmetic command (#270)
Browse files Browse the repository at this point in the history
  • Loading branch information
reubeno authored Nov 26, 2024
1 parent 6ac93c4 commit bad1842
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
8 changes: 4 additions & 4 deletions brush-parser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ peg::parser! {

// N.B. The arithmetic command is a non-sh extension.
// N.B. The arithmetic for clause command is a non-sh extension.
rule compound_command() -> ast::CompoundCommand =
pub(crate) rule compound_command() -> ast::CompoundCommand =
non_posix_extensions_enabled() a:arithmetic_command() { ast::CompoundCommand::Arithmetic(a) } /
b:brace_group() { ast::CompoundCommand::BraceGroup(b) } /
s:subshell() { ast::CompoundCommand::Subshell(s) } /
Expand All @@ -281,16 +281,16 @@ peg::parser! {
non_posix_extensions_enabled() c:arithmetic_for_clause() { ast::CompoundCommand::ArithmeticForClause(c) } /
expected!("compound command")

rule arithmetic_command() -> ast::ArithmeticCommand =
pub(crate) rule arithmetic_command() -> ast::ArithmeticCommand =
specific_operator("(") specific_operator("(") expr:arithmetic_expression() specific_operator(")") specific_operator(")") {
ast::ArithmeticCommand { expr }
}

rule arithmetic_expression() -> ast::UnexpandedArithmeticExpr =
pub(crate) rule arithmetic_expression() -> ast::UnexpandedArithmeticExpr =
raw_expr:$(arithmetic_expression_piece()*) { ast::UnexpandedArithmeticExpr { value: raw_expr } }

rule arithmetic_expression_piece() =
specific_operator("(") arithmetic_expression_piece()* specific_operator(")") {} /
specific_operator("(") (!specific_operator(")") arithmetic_expression_piece())* specific_operator(")") {} /
!arithmetic_end() [_] {}

// TODO: evaluate arithmetic end; the semicolon is used in arithmetic for loops.
Expand Down
5 changes: 5 additions & 0 deletions brush-shell/tests/cases/compound_cmds/arithmetic.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,8 @@ cases:
stdin: |
(( (0) )) && echo "0"
(( (1) )) && echo "1"
- name: "Arithmetic statements with parens and operators"
stdin: |
(( (0) == 0 )) && echo "0 == 0"
(( (1) != 0 )) && echo "1 != 0"

0 comments on commit bad1842

Please sign in to comment.