Skip to content

Commit

Permalink
update grammar / helix highlights
Browse files Browse the repository at this point in the history
  • Loading branch information
gruhn committed Oct 5, 2024
1 parent a70b328 commit 0caf7d5
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 31 deletions.
2 changes: 0 additions & 2 deletions examples/bank.qnt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ module bank {

pure val ADDRESSES = Set("alice", "bob", "charlie")

pure val test = _ => 42

action deposit(account, amount) = {
// Increment balance of account by amount
balances' = balances.setBy(account, curr => curr + amount)
Expand Down
33 changes: 33 additions & 0 deletions examples/counter.qnt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

module counters {
var n: int

action Init = {
n' = 1
}

action Even = all {
n % 2 == 0,
n' = n / 2,
}

action ByThree = all {
n % 3 == 0,
n' = 2 * n
}

action Positive = all {
n > 0,
n' = n + 1,
}

action Next = any {
Even, ByThree, Positive
}

run run1 = (n' = 1).then(n' = 2).then(n' = 3).then(n' = 6).then(n' = 3)

run run2 = (Init).then(Positive).then(Positive).then(ByThree).then(Even)

run run3 = (Init).then(Next).then(Next).then(Next).then(Next)
}
21 changes: 15 additions & 6 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ module.exports = grammar({
$.record_literal,
$.tuple_literal,
$.list_literal,
'1 to 10', // TODO
$.match_expr,
),

well_known_set: $ => choice('Bool', 'Int', 'Nat'),
Expand Down Expand Up @@ -246,20 +246,25 @@ module.exports = grammar({
prec.left ('comparison' , seq($.expr, '<=' , $.expr)),
prec.left ('comparison' , seq($.expr, '==' , $.expr)),
prec.left ('comparison' , seq($.expr, '!=' , $.expr)),
$.infix_and,
$.infix_or,
$.infix_iff,
$.infix_implies,
prec.left ('delayed_assign', seq($.expr, '\' =' , $.expr)),
prec.left ('infix_and' , seq($.expr, 'and' , $.expr)),
prec.left ('infix_or' , seq($.expr, 'or' , $.expr)),
prec.left ('infix_iff' , seq($.expr, 'iff' , $.expr)),
prec.left ('infix_implies' , seq($.expr, 'implies', $.expr)),
prec.left ('pair' , seq($.expr, '->' , $.expr)),
),

infix_and: $ => prec.left('infix_and', seq($.expr, 'and', $.expr)),
infix_or: $ => prec.left('infix_or', seq($.expr, 'or', $.expr)),
infix_iff: $ => prec.left('infix_iff', seq($.expr, 'iff', $.expr)),
infix_implies: $ => prec.left ('infix_implies' , seq($.expr, 'implies', $.expr)),

braced_and: $ => prec('braced_and', seq('and', withBraces(sepEndBy(',', $.expr)))),
braced_or: $ => prec('braced_or', seq('or', withBraces(sepEndBy(',', $.expr)))),
braced_all: $ => prec('braced_all', seq('all', withBraces(sepEndBy(',', $.expr)))),
braced_any: $ => prec('braced_any', seq('any', withBraces(sepEndBy(',', $.expr)))),

if_else_condition: $ => prec('if_else', seq('if', withParens($.expr), 'then', $.expr, 'else', $.expr)),
if_else_condition: $ => prec('if_else', seq('if', withParens($.expr), $.expr, 'else', $.expr)),

nondet_choice: $ => prec.right('nondet_choice', seq(
'nondet', $.identifier, '=', $.expr, choice(';', '\n'), $.expr
Expand All @@ -277,6 +282,10 @@ module.exports = grammar({

tuple_literal: $ => prec('tuple', withParens(sepBy(',', $.expr))),

match_expr: $ => seq('match', $.expr, withBraces(
repeat(seq('|', $.identifier, withParens($.expr), '=>', $.expr))
)),

list_literal: $ => withBrackets(sepBy(',', $.expr)),

// TODO: nested operator definitions
Expand Down
41 changes: 29 additions & 12 deletions queries/helix-highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,6 @@
"module"
; "from"
; "as"
; "not"
"or"
"and"
"implies"
"iff"
"all"
"any"
"type"
"assume"
"const"
Expand All @@ -22,12 +15,24 @@
"pure"
"action"
"temporal"
"run"
] @keyword

[
"if"
"else"
] @keyword.control.conditional
(match_expr "match" @keyword.control.conditional)

(if_else_condition
"if" @keyword.control.conditional
"else" @keyword.control.conditional)

; [
; "or"
; "and"
; "implies"
; "iff"
; "all"
; "any"
; ; "not"
; ] @keyword.operator

; [
; "import"
Expand Down Expand Up @@ -64,9 +69,18 @@
">="
"^"
"->"
; TODO: and, or, iff, implies
] @operator

(infix_and "and" @operator)
(infix_or "or" @operator)
(infix_iff "iff" @operator)
(infix_implies "implies" @operator)

(braced_and "and" @keyword)
(braced_or "or" @keyword)
(braced_all "all" @keyword)
(braced_any "any" @keyword)

[
"("
")"
Expand All @@ -76,6 +90,9 @@
"}"
] @punctuation.bracket

(polymorphic_type
(type) @type.parameter)

(type) @type
(int_literal) @constant.numeric.integer
(comment) @comment
Expand Down
5 changes: 4 additions & 1 deletion queries/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@
"pure"
"action"
"temporal"
; "run"
"run"
] @property

(match_expr "match" @property)


[
"true"
"false"
Expand Down
57 changes: 47 additions & 10 deletions test/corpus/expressions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -269,22 +269,21 @@ action _ = {
(expr
(identifier)))))))

=========================================
========================
If-Else Condition
=========================================

val _ = if (p) then e1 else e2
========================

val _ = if (p) e1 else e2
val _ =
if (p) then
if (p)
e1
else if (p2) then
else if (p2)
e2
else
e3

---

(source_file
(operator_definition
(identifier)
Expand All @@ -311,8 +310,7 @@ val _ =
(expr
(identifier))
(expr
(identifier))))))))

(identifier))))))))

========================
Non-Deterministic Choice
Expand Down Expand Up @@ -491,6 +489,45 @@ val _ = (e_1, e_2, e_3)
(expr
(identifier))))))

============================
Match Expression
============================

val _ =
match L_k(x) {
| L_1(x_1) => e_1
| L_2(x_2) => e_2
| L_3(x_3) => e_3
}

---

(source_file
(operator_definition
(identifier)
(expr
(match_expr
(expr
(operator_application
(identifier)
(expr
(identifier))))
(identifier)
(expr
(identifier))
(expr
(identifier))
(identifier)
(expr
(identifier))
(expr
(identifier))
(identifier)
(expr
(identifier))
(expr
(identifier))))))

============================
List Constructor
============================
Expand Down

0 comments on commit 0caf7d5

Please sign in to comment.