Skip to content

Commit

Permalink
feat: support new grammar falsy operator
Browse files Browse the repository at this point in the history
  • Loading branch information
uga-rosa committed Apr 23, 2023
1 parent cd63bd2 commit 9477450
Show file tree
Hide file tree
Showing 5 changed files with 67,026 additions and 65,131 deletions.
26 changes: 17 additions & 9 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -924,6 +925,7 @@ module.exports = grammar({
choice(
$._variable,
$.ternary_expression,
$.falsy_expression,
$.index_expression,
$.slice_expression,
$.binary_operation,
Expand All @@ -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("#", "?"),

Expand Down
2 changes: 2 additions & 0 deletions queries/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,8 @@

(ternary_expression ["?" ":"] @conditional.ternary)

(falsy_expression "??" @conditional)

; Options
((set_value) @number
(#match? @number "^[0-9]+(\.[0-9]+)?$"))
Expand Down
87 changes: 60 additions & 27 deletions src/grammar.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 9477450

Please sign in to comment.