Skip to content
This repository has been archived by the owner on Dec 12, 2024. It is now read-only.

Commit

Permalink
Implement operator precedence.
Browse files Browse the repository at this point in the history
I just took a look on how tree-sitter-c parser does it.

This also adds some missing operators.

Parenthesis still not implemented.
  • Loading branch information
hugopl committed Dec 5, 2024
1 parent 5d5dc70 commit 85843ac
Show file tree
Hide file tree
Showing 6 changed files with 24,302 additions and 18,027 deletions.
67 changes: 58 additions & 9 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,20 @@ const const_start = /[A-Z]/,
['{', '}', '\\}'],
['<', '>', '>'],
['|', '|', '\\|'],
];
],
PREC = {
DEFAULT: 0,
LOGICAL_OR: 1,
LOGICAL_AND: 2,
INCLUSIVE_OR: 3,
EXCLUSIVE_OR: 4,
BITWISE_AND: 5,
EQUAL: 6,
RELATIONAL: 7,
SHIFT: 9,
ADD: 10,
MULTIPLY: 11,
};

module.exports = grammar({
name: 'crystal',
Expand Down Expand Up @@ -446,21 +459,56 @@ module.exports = grammar({
// prec('splat_operator', seq('**', token.immediate($._expression))),

// https://github.com/will/tree-sitter-crystal/blob/15597b307b18028b04d288561f9c29794621562b/grammar.js#L545
binary_operation: $ =>
prec.left(
seq(
$._expression,
alias($._binary_operator, $.operator),
$._expression,
),
),
binary_operation: $ => {
const table = [
['+', PREC.ADD],
['&+', PREC.ADD],
['-', PREC.ADD],
['&-', PREC.ADD],
['*', PREC.MULTIPLY],
['&*', PREC.MULTIPLY],
['/', PREC.MULTIPLY],
['//', PREC.MULTIPLY],
['%', PREC.MULTIPLY],
['||', PREC.LOGICAL_OR],
['&&', PREC.LOGICAL_AND],
['|', PREC.INCLUSIVE_OR],
['^', PREC.EXCLUSIVE_OR],
['&', PREC.BITWISE_AND],
['==', PREC.EQUAL],
['=~', PREC.EQUAL],
['!~', PREC.EQUAL],
['===', PREC.EQUAL],
['<=>', PREC.EQUAL],
['!=', PREC.EQUAL],
['>', PREC.RELATIONAL],
['>=', PREC.RELATIONAL],
['<=', PREC.RELATIONAL],
['<', PREC.RELATIONAL],
['<<', PREC.SHIFT],
['>>', PREC.SHIFT],
];

return choice(...table.map(([operator, precedence]) => {
return prec.left(precedence, seq(
field('left', $._expression),
// @ts-ignore
field('operator', operator),
field('right', $._expression),
));
}));
},

_binary_operator: $ =>
choice(
'+',
'&+',
'-',
'&-',
'*',
'&*',
'/',
'//',
'%',
'&',
'|',
Expand All @@ -477,6 +525,7 @@ module.exports = grammar({
'<=>',
'===',
'=~',
'!~',
),
},
});
Loading

0 comments on commit 85843ac

Please sign in to comment.