Centralise precedence values

Makes it easier to ensure correct precedence relative to the rest of the
grammar later on.
This commit is contained in:
Bruno BELANYI 2022-06-01 17:33:49 +02:00
parent 03fa0427cf
commit 50c8e3c0e6

View file

@ -6,6 +6,14 @@ function sepBy(sep, rule) {
return optional(sepBy1(sep, rule)) return optional(sepBy1(sep, rule))
} }
const PREC = {
multiplicative: 5,
additive: 4,
comparative: 3,
and: 2,
or: 1,
};
module.exports = grammar({ module.exports = grammar({
name: "tiger", name: "tiger",
@ -55,13 +63,13 @@ module.exports = grammar({
binary_expression: ($) => { binary_expression: ($) => {
const table = [ const table = [
[5, prec.left, choice("*", "/")], [PREC.multiplicative, prec.left, choice("*", "/")],
[4, prec.left, choice("+", "-")], [PREC.additive, prec.left, choice("+", "-")],
// FIXME: comparisons should be non-associative // FIXME: comparisons should be non-associative
// See https://github.com/tree-sitter/tree-sitter/issues/761 // See https://github.com/tree-sitter/tree-sitter/issues/761
[3, prec.left, choice(">=", "<=", "=", "<>", "<", ">")], [PREC.comparative, prec.left, choice(">=", "<=", "=", "<>", "<", ">")],
[2, prec.left, "&"], [PREC.and, prec.left, "&"],
[1, prec.left, "|"], [PREC.or, prec.left, "|"],
]; ];
return choice( return choice(