mirror of
https://github.com/leafo/moonscript.git
synced 2025-01-09 00:04:22 +00:00
move line decorator transformation out of the parser
This commit is contained in:
parent
2134a3320a
commit
99e1c9d38b
@ -254,25 +254,7 @@ end
|
|||||||
-- transforms a statement that has a line decorator
|
-- transforms a statement that has a line decorator
|
||||||
local function wrap_decorator(stm, dec)
|
local function wrap_decorator(stm, dec)
|
||||||
if not dec then return stm end
|
if not dec then return stm end
|
||||||
|
return { "decorated", stm, dec }
|
||||||
local arg = {stm, dec}
|
|
||||||
|
|
||||||
if dec[1] == "if" then
|
|
||||||
local _, cond, fail = unpack(dec)
|
|
||||||
if fail then fail = {"else", {fail}} end
|
|
||||||
stm = {"if", cond, {stm}, fail}
|
|
||||||
elseif dec[1] == "unless" then
|
|
||||||
stm = {
|
|
||||||
"if",
|
|
||||||
{"not", {"parens", dec[2]}},
|
|
||||||
{stm}
|
|
||||||
}
|
|
||||||
elseif dec[1] == "comprehension" then
|
|
||||||
local _, clauses = unpack(dec)
|
|
||||||
stm = {"comprehension", stm, clauses}
|
|
||||||
end
|
|
||||||
|
|
||||||
return stm
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- wrap if statement if there is a conditional decorator
|
-- wrap if statement if there is a conditional decorator
|
||||||
|
@ -549,6 +549,45 @@ Statement = Transformer({
|
|||||||
end
|
end
|
||||||
return node
|
return node
|
||||||
end,
|
end,
|
||||||
|
decorated = function(self, node)
|
||||||
|
local stm, dec = unpack(node, 2)
|
||||||
|
local _exp_0 = dec[1]
|
||||||
|
if "if" == _exp_0 then
|
||||||
|
local cond, fail = unpack(dec, 2)
|
||||||
|
if fail then
|
||||||
|
fail = {
|
||||||
|
"else",
|
||||||
|
{
|
||||||
|
fail
|
||||||
|
}
|
||||||
|
}
|
||||||
|
end
|
||||||
|
return {
|
||||||
|
"if",
|
||||||
|
cond,
|
||||||
|
{
|
||||||
|
stm
|
||||||
|
},
|
||||||
|
fail
|
||||||
|
}
|
||||||
|
elseif "unless" == _exp_0 then
|
||||||
|
return {
|
||||||
|
"unless",
|
||||||
|
dec[2],
|
||||||
|
{
|
||||||
|
stm
|
||||||
|
}
|
||||||
|
}
|
||||||
|
elseif "comprehension" == _exp_0 then
|
||||||
|
return {
|
||||||
|
"comprehension",
|
||||||
|
stm,
|
||||||
|
dec[2]
|
||||||
|
}
|
||||||
|
else
|
||||||
|
return error("Unknown decorator " .. dec[1])
|
||||||
|
end
|
||||||
|
end,
|
||||||
unless = function(self, node)
|
unless = function(self, node)
|
||||||
return {
|
return {
|
||||||
"if",
|
"if",
|
||||||
|
@ -244,6 +244,21 @@ Statement = Transformer {
|
|||||||
node[2] = apply_to_last node[2], ret if ret
|
node[2] = apply_to_last node[2], ret if ret
|
||||||
node
|
node
|
||||||
|
|
||||||
|
decorated: (node) =>
|
||||||
|
stm, dec = unpack node, 2
|
||||||
|
|
||||||
|
switch dec[1]
|
||||||
|
when "if"
|
||||||
|
cond, fail = unpack dec, 2
|
||||||
|
fail = { "else", { fail } } if fail
|
||||||
|
{ "if", cond, { stm }, fail }
|
||||||
|
when "unless"
|
||||||
|
{ "unless", dec[2], { stm } }
|
||||||
|
when "comprehension"
|
||||||
|
{ "comprehension", stm, dec[2] }
|
||||||
|
else
|
||||||
|
error "Unknown decorator " .. dec[1]
|
||||||
|
|
||||||
unless: (node) =>
|
unless: (node) =>
|
||||||
{ "if", {"not", {"parens", node[2]}}, unpack node, 3 }
|
{ "if", {"not", {"parens", node[2]}}, unpack node, 3 }
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user