From f2ce353b4cc69c8e7d326d19615a8ced01011004 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 28 Aug 2011 23:40:51 -0700 Subject: [PATCH] added `:` prefix operator for self assignment in table lits --- moonscript/parse.lua | 7 ++++++- tests/inputs/tables.moon | 7 +++++++ tests/outputs/tables.lua | 18 +++++++++++++++++- 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/moonscript/parse.lua b/moonscript/parse.lua index 36be661..8c662f1 100644 --- a/moonscript/parse.lua +++ b/moonscript/parse.lua @@ -251,6 +251,11 @@ local build_grammar = wrap(function() return #left == #right end + -- :name in table literal + local function self_assign(name) + return {name, name} + end + -- can't have P(false) because it causes preceding patterns not to run local Cut = P(function() return false end) @@ -402,7 +407,7 @@ local build_grammar = wrap(function() ClassDecl = key"class" * Name * (key"extends" * Exp + C"")^-1 * TableBlock / mark"class", Export = key"export" * Ct(NameList) / mark"export", - KeyValue = Ct((SimpleName + sym"[" * Exp * sym"]") * symx":" * (Exp + TableBlock)), + KeyValue = (sym":" * Name) / self_assign + Ct((SimpleName + sym"[" * Exp * sym"]") * symx":" * (Exp + TableBlock)), KeyValueList = KeyValue * (sym"," * KeyValue)^0, KeyValueLine = CheckIndent * KeyValueList * sym","^-1, diff --git a/tests/inputs/tables.moon b/tests/inputs/tables.moon index 0143fd8..ec4d321 100644 --- a/tests/inputs/tables.moon +++ b/tests/inputs/tables.moon @@ -81,5 +81,12 @@ x = -- okay +x = { :something, something: something } +y = { + :hi, :there, :how, :you + :thing +} + +call_me "hello", :x, :y, :z diff --git a/tests/outputs/tables.lua b/tests/outputs/tables.lua index 049b7e3..0238be0 100644 --- a/tests/outputs/tables.lua +++ b/tests/outputs/tables.lua @@ -100,4 +100,20 @@ x = { name = function(self, node) return self:value(node) end -} \ No newline at end of file +} +x = { + something = something, + something = something +} +local y = { + hi = hi, + there = there, + how = how, + you = you, + thing = thing +} +call_me("hello", { + x = x, + y = y, + z = z +}) \ No newline at end of file