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

Implement operator precedence. #22

Merged
merged 1 commit into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading