fix: handle parsing complex C-style for loops

pull/504/merge
Amaan Qureshi 2023-08-21 21:44:50 +07:00
parent 8a26c9bc29
commit 59392e7171
2 changed files with 113 additions and 4 deletions

@ -184,6 +184,7 @@ module.exports = grammar({
alias($._c_binary_expression, $.binary_expression),
alias($._c_postfix_expression, $.postfix_expression),
alias($._c_parenthesized_expression, $.parenthesized_expression),
$.command_substitution,
),
_c_variable_assignment: $ => seq(
@ -196,7 +197,7 @@ module.exports = grammar({
$._c_expression_not_assignment,
)),
_c_binary_expression: $ => prec.right(seq(
choice($._c_word, $.simple_expansion),
$._c_expression_not_assignment,
choice(
'+=', '-=', '*=', '/=', '%=', '**=',
'<<=', '>>=', '&=', '^=', '|=',
@ -213,7 +214,7 @@ module.exports = grammar({
),
_c_parenthesized_expression: $ => seq(
'(',
$._c_expression,
commaSep1($._c_expression),
')',
),
_c_word: $ => alias(/[a-zA-Z_][a-zA-Z0-9_]*/, $.word),
@ -591,7 +592,7 @@ module.exports = grammar({
ansi_c_string: _ => /\$'([^']|\\')*'/,
number: _ => /(0x)?[0-9]+(#[0-9A-Za-z@_]+)?/,
number: _ => /-?(0x)?[0-9]+(#[0-9A-Za-z@_]+)?/,
simple_expansion: $ => seq(
'$',

@ -303,6 +303,19 @@ done
for (( i = 4;;i--)) ; do echo $i; if (( $i == 0 )); then break; fi; done
# added post-bash-4.2
for (( i = j = k = 1; i % 9 || (j *= -1, $( ((i%9)) || printf " " >&2; echo 0), k++ <= 10); i += j ))
do
printf "$i"
done
echo
( for (( i = j = k = 1; i % 9 || (j *= -1, $( ((i%9)) || printf " " >&2; echo 0), k++ <= 10); i += j ))
do
printf "$i"
done )
--------------------------------------------------------------------------------
(program
@ -387,7 +400,102 @@ for (( i = 4;;i--)) ; do echo $i; if (( $i == 0 )); then break; fi; done
(regex)))
(command
(command_name
(word)))))))
(word))))))
(comment)
(c_style_for_statement
(variable_assignment
(variable_name)
(variable_assignment
(variable_name)
(variable_assignment
(variable_name)
(number))))
(binary_expression
(word)
(binary_expression
(number)
(parenthesized_expression
(binary_expression
(word)
(number))
(command_substitution
(redirected_statement
(list
(test_command
(word))
(command
(command_name
(word))
(string)))
(file_redirect
(number)))
(command
(command_name
(word))
(number)))
(binary_expression
(postfix_expression
(word))
(number)))))
(binary_expression
(word)
(word))
(do_group
(command
(command_name
(word))
(string
(simple_expansion
(variable_name))))))
(command
(command_name
(word)))
(subshell
(c_style_for_statement
(variable_assignment
(variable_name)
(variable_assignment
(variable_name)
(variable_assignment
(variable_name)
(number))))
(binary_expression
(word)
(binary_expression
(number)
(parenthesized_expression
(binary_expression
(word)
(number))
(command_substitution
(redirected_statement
(list
(test_command
(word))
(command
(command_name
(word))
(string)))
(file_redirect
(number)))
(command
(command_name
(word))
(number)))
(binary_expression
(postfix_expression
(word))
(number)))))
(binary_expression
(word)
(word))
(do_group
(command
(command_name
(word))
(string
(simple_expansion
(variable_name))))))))
================================================================================
If statements