Skip to content

Commit

Permalink
Add support for alternative operator representations (#179)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdrouhard authored Oct 3, 2022
1 parent 9a877b6 commit d5e90fb
Show file tree
Hide file tree
Showing 5 changed files with 308,613 additions and 273,728 deletions.
53 changes: 45 additions & 8 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -1028,15 +1028,36 @@ module.exports = grammar(C, {
),
)),

binary_expression: ($, original) => choice(
unary_expression: ($, original) => choice(
original,
prec.left(PREC.THREE_WAY, seq(
field('left', $._expression),
field('operator', '<=>'),
field('right', $._expression)
prec.left(PREC.UNARY, seq(
field('operator', choice('not', 'compl')),
field('argument', $._expression)
))
),

binary_expression: ($, original) => {
const table = [
['<=>', PREC.THREE_WAY],
['or', PREC.LOGICAL_OR],
['and', PREC.LOGICAL_AND],
['bitor', PREC.INCLUSIVE_OR],
['xor', PREC.EXCLUSIVE_OR],
['bitand', PREC.BITWISE_AND],
['not_eq', PREC.EQUAL],
];

return choice(
...original.members,
...table.map(([operator, precedence]) => {
return prec.left(precedence, seq(
field('left', $._expression),
field('operator', operator),
field('right', $._expression)
))
}));
},

argument_list: $ => seq(
'(',
commaSep(choice($._expression, $.initializer_list)),
Expand Down Expand Up @@ -1111,6 +1132,19 @@ module.exports = grammar(C, {
$.qualified_identifier,
),

assignment_expression: ($, original) => choice(
original,
prec.right(PREC.ASSIGNMENT, seq(
field('left', $._assignment_left_expression),
field('operator', choice(
'and_eq',
'or_eq',
'xor_eq'
)),
field('right', $._expression)
))
),

operator_name: $ => prec(1, seq(
'operator',
choice(
Expand All @@ -1128,10 +1162,13 @@ module.exports = grammar(C, {
'->*',
'->',
'()', '[]',
'xor', 'bitand', 'bitor', 'compl',
'not', 'xor_eq', 'and_eq', 'or_eq', 'not_eq',
'and', 'or',
seq(choice('new', 'delete'), optional('[]')),
seq('""', $.identifier)
)
)),
)
)),

this: $ => 'this',
nullptr: $ => 'nullptr',
Expand All @@ -1141,7 +1178,7 @@ module.exports = grammar(C, {
repeat1(choice($.raw_string_literal, $.string_literal))
),

literal_suffix: $ => token.immediate(/[a-zA-Z_]\w*/),
literal_suffix: $ => token.immediate(/[a-zA-Z_]\w*/),

user_defined_literal: $ => seq(
choice(
Expand Down
Loading

0 comments on commit d5e90fb

Please sign in to comment.