From 27f1905764518fe9a09594b97c8b826e78136e7f Mon Sep 17 00:00:00 2001 From: uga-rosa Date: Mon, 24 Apr 2023 00:15:24 +0900 Subject: [PATCH] feat: support new grammar falsy operator --- grammar.js | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/grammar.js b/grammar.js index 7e0af62..4a55542 100644 --- a/grammar.js +++ b/grammar.js @@ -83,15 +83,16 @@ const ENCODING = [ ]; const PREC = { - TERNARY: 1, //=> expr ? expr : expr - OR: 2, //=> or - AND: 3, //=> and - COMPARE: 4, //=> < <= == ~= >= > and all - PLUS: 5, //=> + - - CONCAT: 5, //=> .. . - MULTI: 6, //=> * / % - UNARY: 7, //=> ! - + - CALL: 8, //expr[n] expr[n:m] expr.name expr(...) + FALSY: 1, //=> expr ?? expr + TERNARY: 2, //=> expr ? expr : expr + OR: 3, //=> or + AND: 4, //=> and + COMPARE: 5, //=> < <= == ~= >= > and all + PLUS: 6, //=> + - + CONCAT: 6, //=> .. . + MULTI: 7, //=> * / % + UNARY: 8, //=> ! - + + CALL: 9, //expr[n] expr[n:m] expr.name expr(...) }; module.exports = grammar({ @@ -924,6 +925,7 @@ module.exports = grammar({ choice( $._variable, $.ternary_expression, + $.falsy_expression, $.index_expression, $.slice_expression, $.binary_operation, @@ -946,6 +948,16 @@ module.exports = grammar({ ) ), + falsy_expression: ($) => + prec.left( + PREC.FALSY, + seq( + field("left", $._expression), + "??", + field("right", $._expression), + ) + ), + // Shamelessly stolen from tree-sitter-lua match_case: ($) => choice("#", "?"),