Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support new grammar falsy operator #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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