Skip to content

Commit

Permalink
fix: compare with 'in' operator (Wilfred#15)
Browse files Browse the repository at this point in the history
* fix: compare with 'in' operator

* fix: use field in object for loop

* test: add examples from Wilfred#13 as tests

* test: update test case following suggestion in Wilfred#13
  • Loading branch information
Duologic authored Aug 9, 2023
1 parent af22de3 commit 26d9699
Show file tree
Hide file tree
Showing 4 changed files with 314 additions and 14 deletions.
27 changes: 13 additions & 14 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ module.exports = grammar({
[PREC.multiplicative, choice("*", "/", "%")],
[PREC.additive, choice("+", "-")],
[PREC.bitshift, choice("<<", ">>")],
[PREC.comparison, choice("<", "<=", ">", ">=")],
[PREC.comparison, choice("<", "<=", ">", ">=", "in")],
[PREC.equality, choice("==", "!=")],
[PREC.bitand, "&"],
[PREC.bitxor, "^"],
Expand Down Expand Up @@ -210,26 +210,25 @@ module.exports = grammar({
// error expr
expr_error: ($) => prec.right(seq("error", $.expr)),

_expr_super: ($) => seq($.expr, "in", $.super),
_expr_super: ($) => prec(PREC.comparison, seq($.expr, "in", $.super)),

_parenthesis: ($) => seq("(", $.expr, ")"),

objinside: ($) =>
choice(
// seq($.member, repeat(seq(",", $.member)), optional(",")),
commaSep1($.member, true),
seq(
repeat(seq($.objlocal, ",")),
"[",
$.expr,
"]",
":",
$.expr,
repeat(seq(",", $.objlocal)),
optional(","),
$.forspec,
optional($.compspec),
),
$._objforloop,
),

_objforloop: ($) =>
seq(
repeat(seq($.objlocal, ",")),
$.field,
repeat(seq(",", $.objlocal)),
optional(","),
$.forspec,
optional($.compspec),
),

member: ($) =>
Expand Down
155 changes: 155 additions & 0 deletions test/corpus/fieldname.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
===============================
Resolve expr in fieldname key 1
===============================

local a = 'a';
{ [a]: 'a' }

---

(document
(expr
(local_bind
(local)
(bind
(id)
(expr
(string
(string_start)
(string_content)
(string_end))))
(expr
(member
(field
(fieldname
(expr
(id)))
(expr
(string
(string_start)
(string_content)
(string_end)))))))))

===============================
Resolve expr in fieldname key 2
===============================

local prefix = 'Happy Hour ';
{
[prefix + 'Mimosa']: 'Champagne',
}

---

(document
(expr
(local_bind
(local)
(bind
(id)
(expr
(string
(string_start)
(string_content)
(string_end))))
(expr
(member
(field
(fieldname
(expr
(expr (id))
(expr
(string
(string_start)
(string_content)
(string_end)))))
(expr
(string
(string_start)
(string_content)
(string_end)))))))))

===============================
Resolve expr in fieldname key 3
===============================

local Margarita(salted) = {
[if salted then 'garnish']: 'Salt',
ingredients: [
{ kind: 'Tequila Blanco', qty: 2 },
],
};
{
Margarita: Margarita(true),
'Margarita Unsalted': Margarita(false),
}

---

(document
(expr
(local_bind
(local)
(bind
function: (id)
params:
(params
(param
identifier: (id)))
body: (expr
(member
(field
(fieldname
(expr
condition: (expr (id))
consequence:
(expr
(string
(string_start)
(string_content)
(string_end)))))
(expr
(string
(string_start)
(string_content)
(string_end)))))
(member
(field
(fieldname (id))
(expr
(expr
(member
(field
(fieldname (id))
(expr
(string
(string_start)
(string_content)
(string_end)))))
(member
(field
(fieldname
(id))
(expr
(number))))))))
))
(expr
(member
(field
(fieldname (id))
(expr
(expr (id))
(args
(expr (true))))))
(member
(field
(fieldname
(string
(string_start)
(string_content)
(string_end)))
(expr
(expr (id))
(args
(expr
(false))))))))))
78 changes: 78 additions & 0 deletions test/corpus/forloop.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
======================
For loop in array
======================

[
i
for i in std.range(0,10)
]

---

(document
(expr
(expr (id))
(forspec
(id)
(expr (expr (expr (id)) (id))
(args (expr (number)) (expr (number)))))))

======================
For loop in object
======================

{
['item' + i]: i
for i in std.range(0,10)
}

---

(document
(expr
(field
(fieldname
(expr
(expr
(string
(string_start)
(string_content)
(string_end)))
(expr (id))))
(expr (id)))
(forspec
(id)
(expr (expr (expr (id)) (id))
(args (expr (number)) (expr (number)))))))

================================
For loop in object with function
================================

{
['item' + i](v): i + v
for i in std.range(0,10)
}

---

(document
(expr
(field
(fieldname
(expr
(expr
(string
(string_start)
(string_content)
(string_end)))
(expr (id))))
(params
(param (id)))
(expr
(expr (id))
(expr (id))))
(forspec
(id)
(expr (expr (expr (id)) (id))
(args (expr (number)) (expr (number)))))))
68 changes: 68 additions & 0 deletions test/corpus/inoperator.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
======================
In operator on object
======================

local obj = { a: null };
'a' in obj

---

(document
(expr
(local_bind
(local)
(bind
(id)
(expr
(member
(field
(fieldname
(id))
(expr
(null))))))
(expr
(expr
(string
(string_start)
(string_content)
(string_end)))
(expr
(id))))))

======================
In operator on super
======================

local obj = { a: null };
obj + { b: 'a' in super }

---

(document
(expr
(local_bind
(local)
(bind
(id)
(expr
(member
(field
(fieldname
(id))
(expr
(null))))))
(expr
(expr
(id))
(expr
(member
(field
(fieldname
(id))
(expr
(expr
(string
(string_start)
(string_content)
(string_end)))
(super)))))))))

0 comments on commit 26d9699

Please sign in to comment.