From 50c8e3c0e680fec2b2e2e54d3fb47117ff8dbbb7 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Wed, 1 Jun 2022 17:33:49 +0200 Subject: [PATCH] Centralise precedence values Makes it easier to ensure correct precedence relative to the rest of the grammar later on. --- grammar.js | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/grammar.js b/grammar.js index f4aba54..a25c6bf 100644 --- a/grammar.js +++ b/grammar.js @@ -6,6 +6,14 @@ function sepBy(sep, rule) { return optional(sepBy1(sep, rule)) } +const PREC = { + multiplicative: 5, + additive: 4, + comparative: 3, + and: 2, + or: 1, +}; + module.exports = grammar({ name: "tiger", @@ -55,13 +63,13 @@ module.exports = grammar({ binary_expression: ($) => { const table = [ - [5, prec.left, choice("*", "/")], - [4, prec.left, choice("+", "-")], + [PREC.multiplicative, prec.left, choice("*", "/")], + [PREC.additive, prec.left, choice("+", "-")], // FIXME: comparisons should be non-associative // See https://github.com/tree-sitter/tree-sitter/issues/761 - [3, prec.left, choice(">=", "<=", "=", "<>", "<", ">")], - [2, prec.left, "&"], - [1, prec.left, "|"], + [PREC.comparative, prec.left, choice(">=", "<=", "=", "<>", "<", ">")], + [PREC.and, prec.left, "&"], + [PREC.or, prec.left, "|"], ]; return choice(