fix: arithmetic expansions

pull/504/merge
Amaan Qureshi 2023-08-23 05:11:13 +07:00
parent a1fead6eb7
commit d22ee263e6
4 changed files with 87 additions and 25 deletions

@ -562,7 +562,7 @@ module.exports = grammar({
$.brace_expression, $.brace_expression,
), ),
arithmetic_expansion: $ => seq('$((', $._arithmetic_expression, '))'), arithmetic_expansion: $ => seq(choice('$((', '(('), commaSep1($._arithmetic_expression), '))'),
brace_expression: $ => seq( brace_expression: $ => seq(
alias($._brace_start, '{'), alias($._brace_start, '{'),
@ -583,10 +583,12 @@ module.exports = grammar({
_arithmetic_literal: $ => prec(1, choice( _arithmetic_literal: $ => prec(1, choice(
$.number, $.number,
$.test_operator,
$.subscript, $.subscript,
$.simple_expansion, $.simple_expansion,
$.expansion, $.expansion,
$._simple_variable_name, $._simple_variable_name,
$.variable_name,
)), )),
_arithmetic_binary_expression: $ => prec.left(2, choice( _arithmetic_binary_expression: $ => prec.left(2, choice(
@ -619,12 +621,12 @@ module.exports = grammar({
), ),
_arithmetic_unary_expression: $ => choice( _arithmetic_unary_expression: $ => choice(
prec(1, seq( prec(3, seq(
token(prec(1, choice('-', '+', '~', '++', '--'))), token(prec(1, choice('-', '+', '~', '++', '--'))),
$._arithmetic_expression, $._arithmetic_expression,
)), )),
prec.right(1, seq( prec.right(3, seq(
choice('!', $.test_operator), '!',
$._arithmetic_expression, $._arithmetic_expression,
)), )),
), ),

@ -469,15 +469,13 @@ ${array[((number+1))]}
(expansion (expansion
(subscript (subscript
(variable_name) (variable_name)
(parenthesized_expression (arithmetic_expansion (binary_expression (simple_expansion (variable_name)) (number)))))))
(parenthesized_expression
(concatenation (simple_expansion (variable_name)) (word))))))))
(command (command
(command_name (command_name
(expansion (expansion
(subscript (subscript
(variable_name) (variable_name)
(parenthesized_expression (parenthesized_expression (word)))))))) (arithmetic_expansion (binary_expression (variable_name) (number))))))))
========================================== ==========================================
Bare $ Bare $

@ -843,6 +843,11 @@ Arithmetic expansions
echo $((1 + 2 - 3 * 4 / 5)) echo $((1 + 2 - 3 * 4 / 5))
a=$((6 % 7 ** 8 << 9 >> 10 & 11 | 12 ^ 13)) a=$((6 % 7 ** 8 << 9 >> 10 & 11 | 12 ^ 13))
$(((${1:-${SECONDS}} % 12) + 144)) $(((${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 (expansion
(variable_name))) (variable_name)))
(number))) (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 Concatenation with double backticks

@ -393,11 +393,13 @@ done )
(simple_expansion (simple_expansion
(variable_name))) (variable_name)))
(if_statement (if_statement
(test_command (command
(command_name
(arithmetic_expansion
(binary_expression (binary_expression
(simple_expansion (simple_expansion
(variable_name)) (variable_name))
(regex))) (number)))))
(command (command
(command_name (command_name
(word)))))) (word))))))
@ -421,8 +423,12 @@ done )
(command_substitution (command_substitution
(redirected_statement (redirected_statement
(list (list
(test_command (command
(word)) (command_name
(arithmetic_expansion
(binary_expression
(variable_name)
(number)))))
(command (command
(command_name (command_name
(word)) (word))
@ -470,8 +476,12 @@ done )
(command_substitution (command_substitution
(redirected_statement (redirected_statement
(list (list
(test_command (command
(word)) (command_name
(arithmetic_expansion
(binary_expression
(variable_name)
(number)))))
(command (command
(command_name (command_name
(word)) (word))
@ -935,13 +945,15 @@ fi
(program (program
(if_statement (if_statement
(test_command (command
(command_name
(arithmetic_expansion
(ternary_expression (ternary_expression
(binary_expression (binary_expression
(number) (number)
(number)) (number))
(number) (number)
(number))) (number)))))
(command (command
(command_name (command_name
(word)) (word))