diff --git a/grammar.js b/grammar.js index 7e0af62..c0a9596 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,12 @@ 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("#", "?"), diff --git a/queries/highlights.scm b/queries/highlights.scm index ce25b13..880ed61 100644 --- a/queries/highlights.scm +++ b/queries/highlights.scm @@ -273,6 +273,8 @@ (ternary_expression ["?" ":"] @conditional.ternary) +(falsy_expression "??" @conditional) + ; Options ((set_value) @number (#match? @number "^[0-9]+(\.[0-9]+)?$"))