diff --git a/grammar.js b/grammar.js index 810df6aab..a4e8b6f39 100644 --- a/grammar.js +++ b/grammar.js @@ -562,7 +562,7 @@ module.exports = grammar({ $.brace_expression, ), - arithmetic_expansion: $ => seq('$((', $._arithmetic_expression, '))'), + arithmetic_expansion: $ => seq(choice('$((', '(('), commaSep1($._arithmetic_expression), '))'), brace_expression: $ => seq( alias($._brace_start, '{'), @@ -583,10 +583,12 @@ module.exports = grammar({ _arithmetic_literal: $ => prec(1, choice( $.number, + $.test_operator, $.subscript, $.simple_expansion, $.expansion, $._simple_variable_name, + $.variable_name, )), _arithmetic_binary_expression: $ => prec.left(2, choice( @@ -619,12 +621,12 @@ module.exports = grammar({ ), _arithmetic_unary_expression: $ => choice( - prec(1, seq( + prec(3, seq( token(prec(1, choice('-', '+', '~', '++', '--'))), $._arithmetic_expression, )), - prec.right(1, seq( - choice('!', $.test_operator), + prec.right(3, seq( + '!', $._arithmetic_expression, )), ), diff --git a/test/corpus/commands.txt b/test/corpus/commands.txt index 62822431d..6fecc6b52 100644 --- a/test/corpus/commands.txt +++ b/test/corpus/commands.txt @@ -469,15 +469,13 @@ ${array[((number+1))]} (expansion (subscript (variable_name) - (parenthesized_expression - (parenthesized_expression - (concatenation (simple_expansion (variable_name)) (word)))))))) + (arithmetic_expansion (binary_expression (simple_expansion (variable_name)) (number))))))) (command (command_name (expansion (subscript (variable_name) - (parenthesized_expression (parenthesized_expression (word)))))))) + (arithmetic_expansion (binary_expression (variable_name) (number)))))))) ========================================== Bare $ diff --git a/test/corpus/literals.txt b/test/corpus/literals.txt index c95c113b9..938c52144 100644 --- a/test/corpus/literals.txt +++ b/test/corpus/literals.txt @@ -843,6 +843,11 @@ Arithmetic expansions echo $((1 + 2 - 3 * 4 / 5)) a=$((6 % 7 ** 8 << 9 >> 10 & 11 | 12 ^ 13)) $(((${1:-${SECONDS}} % 12) + 144)) +((foo=0)) +echo $((bar=1)) +echo $((-1, 1)) +echo $((! -a || ~ +b || ++c || --d)) +echo $((foo-- || bar++)) -------------------------------------------------------------------------------- @@ -889,7 +894,52 @@ $(((${1:-${SECONDS}} % 12) + 144)) (expansion (variable_name))) (number))) - (number)))))) + (number))))) + (command + (command_name + (arithmetic_expansion + (binary_expression + (variable_name) + (number))))) + (command + (command_name + (word)) + (arithmetic_expansion + (binary_expression + (variable_name) + (number)))) + (command + (command_name + (word)) + (arithmetic_expansion + (unary_expression + (number)) + (number))) + (command + (command_name + (word)) + (arithmetic_expansion + (binary_expression + (binary_expression + (binary_expression + (unary_expression + (test_operator)) + (unary_expression + (unary_expression + (variable_name)))) + (unary_expression + (variable_name))) + (unary_expression + (variable_name))))) + (command + (command_name + (word)) + (arithmetic_expansion + (postfix_expression + (binary_expression + (postfix_expression + (variable_name)) + (variable_name)))))) ================================================================================ Concatenation with double backticks diff --git a/test/corpus/statements.txt b/test/corpus/statements.txt index 0d419b94d..fb35b872c 100644 --- a/test/corpus/statements.txt +++ b/test/corpus/statements.txt @@ -393,11 +393,13 @@ done ) (simple_expansion (variable_name))) (if_statement - (test_command - (binary_expression - (simple_expansion - (variable_name)) - (regex))) + (command + (command_name + (arithmetic_expansion + (binary_expression + (simple_expansion + (variable_name)) + (number))))) (command (command_name (word)))))) @@ -421,8 +423,12 @@ done ) (command_substitution (redirected_statement (list - (test_command - (word)) + (command + (command_name + (arithmetic_expansion + (binary_expression + (variable_name) + (number))))) (command (command_name (word)) @@ -470,8 +476,12 @@ done ) (command_substitution (redirected_statement (list - (test_command - (word)) + (command + (command_name + (arithmetic_expansion + (binary_expression + (variable_name) + (number))))) (command (command_name (word)) @@ -935,13 +945,15 @@ fi (program (if_statement - (test_command - (ternary_expression - (binary_expression - (number) - (number)) - (number) - (number))) + (command + (command_name + (arithmetic_expansion + (ternary_expression + (binary_expression + (number) + (number)) + (number) + (number))))) (command (command_name (word))