Skip to content

Commit

Permalink
disambiguate fold vs parenthesized assignment (#239)
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonspark authored Jan 26, 2024
1 parent 3deebb6 commit 4ca37be
Show file tree
Hide file tree
Showing 6 changed files with 385,577 additions and 382,003 deletions.
46 changes: 30 additions & 16 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,23 @@ const FOLD_OPERATORS = [
'or', 'and', 'bitor', 'xor', 'bitand', 'not_eq',
];

const ASSIGNMENT_OPERATORS = [
'=',
'*=',
'/=',
'%=',
'+=',
'-=',
'<<=',
'>>=',
'&=',
'^=',
'|=',
'and_eq',
'or_eq',
'xor_eq',
]

module.exports = grammar(C, {
name: 'cpp',

Expand Down Expand Up @@ -1225,25 +1242,22 @@ module.exports = grammar(C, {

assignment_expression: $ => prec.right(PREC.ASSIGNMENT, seq(
field('left', $._assignment_left_expression),
field('operator', choice(
'=',
'*=',
'/=',
'%=',
'+=',
'-=',
'<<=',
'>>=',
'&=',
'^=',
'|=',
'and_eq',
'or_eq',
'xor_eq',
)),
field('operator', choice(...ASSIGNMENT_OPERATORS)),
field('right', choice($._expression, $.initializer_list)),
)),

assignment_expression_lhs_expression: $ => seq(
field('left', $._expression),
field('operator', choice(...ASSIGNMENT_OPERATORS)),
field('right', choice($._expression, $.initializer_list)),
),

// This prevents an ambiguity between fold expressions
// and assignment expressions within parentheses.
parenthesized_expression: ($, original) => choice(
original,
seq('(', alias($.assignment_expression_lhs_expression, $.assignment_expression), ')')
),

operator_name: $ => prec(1, seq(
'operator',
Expand Down
Loading

0 comments on commit 4ca37be

Please sign in to comment.