Skip to content

Commit 1ddbf15

Browse files
Fix operator precedence (tree-sitter#190)
* Fix and/or operator precedence * Fix shift operator precedence * Fix equality operator precedence * Fix unary operator precedence * Regenerate files
1 parent 8dfe22e commit 1ddbf15

File tree

4 files changed

+33760
-32221
lines changed

4 files changed

+33760
-32221
lines changed

grammar.js

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,20 @@ module.exports = grammar({
4242
'member',
4343
'call',
4444
$.update_expression,
45-
'unary_not',
4645
'unary_void',
4746
'binary_exp',
4847
'binary_times',
4948
'binary_plus',
49+
'binary_shift',
5050
'binary_compare',
5151
'binary_relation',
52-
'binary_in',
53-
'binary_and',
54-
'binary_or',
52+
'binary_equality',
53+
'bitwise_and',
54+
'bitwise_xor',
55+
'bitwise_or',
56+
'logical_and',
57+
'logical_or',
5558
'ternary',
56-
$.await_expression,
5759
$.sequence_expression,
5860
$.arrow_function
5961
],
@@ -716,10 +718,10 @@ module.exports = grammar({
716718
field('arguments', optional(prec.dynamic(1, $.arguments)))
717719
)),
718720

719-
await_expression: $ => seq(
721+
await_expression: $ => prec('unary_void', seq(
720722
'await',
721723
$.expression
722-
),
724+
)),
723725

724726
member_expression: $ => prec('member', seq(
725727
field('object', choice($.expression, $.primary_expression)),
@@ -786,14 +788,14 @@ module.exports = grammar({
786788

787789
binary_expression: $ => choice(
788790
...[
789-
['&&', 'binary_and'],
790-
['||', 'binary_or'],
791-
['>>', 'binary_times'],
792-
['>>>', 'binary_times'],
793-
['<<', 'binary_times'],
794-
['&', 'binary_and'],
795-
['^', 'binary_or'],
796-
['|', 'binary_or'],
791+
['&&', 'logical_and'],
792+
['||', 'logical_or'],
793+
['>>', 'binary_shift'],
794+
['>>>', 'binary_shift'],
795+
['<<', 'binary_shift'],
796+
['&', 'bitwise_and'],
797+
['^', 'bitwise_xor'],
798+
['|', 'bitwise_or'],
797799
['+', 'binary_plus'],
798800
['-', 'binary_plus'],
799801
['*', 'binary_times'],
@@ -802,15 +804,15 @@ module.exports = grammar({
802804
['**', 'binary_exp'],
803805
['<', 'binary_relation'],
804806
['<=', 'binary_relation'],
805-
['==', 'binary_relation'],
806-
['===', 'binary_relation'],
807-
['!=', 'binary_relation'],
808-
['!==', 'binary_relation'],
807+
['==', 'binary_equality'],
808+
['===', 'binary_equality'],
809+
['!=', 'binary_equality'],
810+
['!==', 'binary_equality'],
809811
['>=', 'binary_relation'],
810812
['>', 'binary_relation'],
811813
['??', 'ternary'],
812814
['instanceof', 'binary_relation'],
813-
['in', 'binary_in'],
815+
['in', 'binary_relation'],
814816
].map(([operator, precedence]) =>
815817
prec.left(precedence, seq(
816818
field('left', $.expression),
@@ -820,19 +822,9 @@ module.exports = grammar({
820822
)
821823
),
822824

823-
unary_expression: $ => choice(...[
824-
['!', 'unary_not'],
825-
['~', 'unary_not'],
826-
['-', 'unary_not'],
827-
['+', 'unary_not'],
828-
['typeof', 'unary_void'],
829-
['void', 'unary_void'],
830-
['delete', 'unary_void'],
831-
].map(([operator, precedence]) =>
832-
prec.left(precedence, seq(
833-
field('operator', operator),
834-
field('argument', $.expression)
835-
))
825+
unary_expression: $ => prec.left('unary_void', seq(
826+
field('operator', choice('!', '~', '-', '+', 'typeof', 'void', 'delete')),
827+
field('argument', $.expression)
836828
)),
837829

838830
update_expression: $ => prec.left(choice(

0 commit comments

Comments
 (0)