Skip to content

Commit 4ca37be

Browse files
authored
disambiguate fold vs parenthesized assignment (#239)
1 parent 3deebb6 commit 4ca37be

File tree

6 files changed

+385577
-382003
lines changed

6 files changed

+385577
-382003
lines changed

grammar.js

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,23 @@ const FOLD_OPERATORS = [
3333
'or', 'and', 'bitor', 'xor', 'bitand', 'not_eq',
3434
];
3535

36+
const ASSIGNMENT_OPERATORS = [
37+
'=',
38+
'*=',
39+
'/=',
40+
'%=',
41+
'+=',
42+
'-=',
43+
'<<=',
44+
'>>=',
45+
'&=',
46+
'^=',
47+
'|=',
48+
'and_eq',
49+
'or_eq',
50+
'xor_eq',
51+
]
52+
3653
module.exports = grammar(C, {
3754
name: 'cpp',
3855

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

12261243
assignment_expression: $ => prec.right(PREC.ASSIGNMENT, seq(
12271244
field('left', $._assignment_left_expression),
1228-
field('operator', choice(
1229-
'=',
1230-
'*=',
1231-
'/=',
1232-
'%=',
1233-
'+=',
1234-
'-=',
1235-
'<<=',
1236-
'>>=',
1237-
'&=',
1238-
'^=',
1239-
'|=',
1240-
'and_eq',
1241-
'or_eq',
1242-
'xor_eq',
1243-
)),
1245+
field('operator', choice(...ASSIGNMENT_OPERATORS)),
12441246
field('right', choice($._expression, $.initializer_list)),
12451247
)),
12461248

1249+
assignment_expression_lhs_expression: $ => seq(
1250+
field('left', $._expression),
1251+
field('operator', choice(...ASSIGNMENT_OPERATORS)),
1252+
field('right', choice($._expression, $.initializer_list)),
1253+
),
1254+
1255+
// This prevents an ambiguity between fold expressions
1256+
// and assignment expressions within parentheses.
1257+
parenthesized_expression: ($, original) => choice(
1258+
original,
1259+
seq('(', alias($.assignment_expression_lhs_expression, $.assignment_expression), ')')
1260+
),
12471261

12481262
operator_name: $ => prec(1, seq(
12491263
'operator',

0 commit comments

Comments
 (0)