Skip to content

Commit

Permalink
Add support for alternative operator representations
Browse files Browse the repository at this point in the history
This adds support for the alternative operator representations in unary,
binary, and assignment expressions, as well as when using in explicit
operator calls and overload declarations/definitions.

This does _not_ support full token substitution the preprocessor
performs (these alternate tokens can be substituted _anywhere_ the
tokens they represent appear, but in this grammar they are only
supported in the above expressions).

This also does _not_ add support for alternate token representations of
the brace tokens ('{', '}', '[', ']') or preprocessor directives
('#', '##')
  • Loading branch information
jdrouhard committed Oct 3, 2022
1 parent 88ad63b commit 3fb539d
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 3fb539d

Please sign in to comment.