Skip to content

Commit

Permalink
fix: field and expose all operators, remove test_operator from arithm…
Browse files Browse the repository at this point in the history
…etic expansions
  • Loading branch information
amaanq committed Aug 24, 2023
1 parent 309307b commit 9c62d8c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
28 changes: 20 additions & 8 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,24 +203,24 @@ module.exports = grammar({
$._c_expression,
),
_c_unary_expression: $ => prec.left(seq(
choice('++', '--'),
field('operator', choice('++', '--')),
$._c_expression_not_assignment,
)),
_c_binary_expression: $ => prec.right(seq(
$._c_expression_not_assignment,
choice(
field('operator', choice(
'+=', '-=', '*=', '/=', '%=', '**=',
'<<=', '>>=', '&=', '^=', '|=',
'==', '!=', '<=', '>=', '&&', '||',
'<<', '>>',
'+', '-', '*', '/', '%', '**',
'<', '>',
),
)),
$._c_expression_not_assignment,
)),
_c_postfix_expression: $ => seq(
$._c_expression_not_assignment,
choice('++', '--'),
field('operator', choice('++', '--')),
),
_c_parenthesized_expression: $ => seq(
'(',
Expand Down Expand Up @@ -525,7 +525,7 @@ module.exports = grammar({

unary_expression: $ => choice(
prec(1, seq(
token(prec(1, choice('-', '+', '~', '++', '--'))),
field('operator', tokenLiterals(1, '-', '+', '~', '++', '--')),
$._expression,
)),
prec.right(1, seq(
Expand All @@ -536,7 +536,7 @@ module.exports = grammar({

postfix_expression: $ => seq(
$._expression,
choice('++', '--'),
field('operator', choice('++', '--')),
),

parenthesized_expression: $ => seq(
Expand Down Expand Up @@ -590,7 +590,6 @@ module.exports = grammar({

_arithmetic_literal: $ => prec(1, choice(
$.number,
$.test_operator,
$.subscript,
$.simple_expansion,
$.expansion,
Expand All @@ -611,7 +610,6 @@ module.exports = grammar({
'<<', '>>', '<<=', '>>=',
'&', '|', '^',
'&=', '|=', '^=',
$.test_operator,
)),
field('right', $._arithmetic_expression),
),
Expand Down Expand Up @@ -968,3 +966,17 @@ function commaSep1(rule) {
function immediateLiterals(...literals) {
return choice(...literals.map(l => token.immediate(l)));
}

/**
*
* Turns a list of rules into a choice of aliased token rules
*
* @param {number} precedence
*
* @param {(RegExp|String)[]} literals
*
* @return {ChoiceRule}
*/
function tokenLiterals(precedence, ...literals) {
return choice(...literals.map(l => token(prec(precedence, l))));
}
3 changes: 2 additions & 1 deletion test/corpus/literals.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1115,7 +1115,8 @@ echo $((foo-- || bar++))
(binary_expression
(binary_expression
(unary_expression
(test_operator))
(unary_expression
(variable_name)))
(unary_expression
(unary_expression
(variable_name))))
Expand Down

0 comments on commit 9c62d8c

Please sign in to comment.