mirror of
https://github.com/leafo/moonscript.git
synced 2025-01-09 00:04:22 +00:00
expressions inside of class body is parsed
This commit is contained in:
parent
7ff11584f5
commit
255a92f8ee
@ -422,7 +422,15 @@ local build_grammar = wrap_env(function()
|
|||||||
TableBlockInner = Ct(KeyValueLine * (SpaceBreak^1 * KeyValueLine)^0),
|
TableBlockInner = Ct(KeyValueLine * (SpaceBreak^1 * KeyValueLine)^0),
|
||||||
TableBlock = SpaceBreak^1 * Advance * TableBlockInner * PopIndent / mark"table",
|
TableBlock = SpaceBreak^1 * Advance * TableBlockInner * PopIndent / mark"table",
|
||||||
|
|
||||||
ClassDecl = key"class" * Name * (key"extends" * Exp + C"")^-1 * TableBlock / mark"class",
|
ClassDecl = key"class" * Name * (key"extends" * Exp + C"")^-1 * ClassBlock / mark"class",
|
||||||
|
|
||||||
|
ClassBlock = SpaceBreak^1 * Advance *
|
||||||
|
Ct(ClassLine * (SpaceBreak^1 * ClassLine)^0) * PopIndent,
|
||||||
|
ClassLine = CheckIndent * ((
|
||||||
|
KeyValueList / mark"props" +
|
||||||
|
Exp / mark"stm"
|
||||||
|
) * sym","^-1),
|
||||||
|
|
||||||
Export = key"export" * (
|
Export = key"export" * (
|
||||||
Cc"class" * ClassDecl +
|
Cc"class" * ClassDecl +
|
||||||
op"*" + op"^" +
|
op"*" + op"^" +
|
||||||
|
@ -536,20 +536,36 @@ Statement = Transformer({
|
|||||||
})
|
})
|
||||||
end,
|
end,
|
||||||
class = function(self, node)
|
class = function(self, node)
|
||||||
local _, name, parent_val, tbl = unpack(node)
|
local _, name, parent_val, body = unpack(node)
|
||||||
|
local statements = { }
|
||||||
|
local properties = { }
|
||||||
|
local _list_0 = body
|
||||||
|
for _index_0 = 1, #_list_0 do
|
||||||
|
local item = _list_0[_index_0]
|
||||||
|
local _exp_0 = item[1]
|
||||||
|
if "stm" == _exp_0 then
|
||||||
|
insert(statements, item[2])
|
||||||
|
elseif "props" == _exp_0 then
|
||||||
|
local _list_1 = item
|
||||||
|
for _index_0 = 2, #_list_1 do
|
||||||
|
local tuple = _list_1[_index_0]
|
||||||
|
insert(properties, tuple)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
local constructor = nil
|
local constructor = nil
|
||||||
local properties = (function()
|
properties = (function()
|
||||||
local _accum_0 = { }
|
local _accum_0 = { }
|
||||||
local _len_0 = 0
|
local _len_0 = 0
|
||||||
local _list_0 = tbl[2]
|
local _list_1 = properties
|
||||||
for _index_0 = 1, #_list_0 do
|
for _index_0 = 1, #_list_1 do
|
||||||
local entry = _list_0[_index_0]
|
local tuple = _list_1[_index_0]
|
||||||
local _value_0
|
local _value_0
|
||||||
if entry[1] == constructor_name then
|
if tuple[1] == constructor_name then
|
||||||
constructor = entry[2]
|
constructor = tuple[2]
|
||||||
_value_0 = nil
|
_value_0 = nil
|
||||||
else
|
else
|
||||||
_value_0 = entry
|
_value_0 = tuple
|
||||||
end
|
end
|
||||||
if _value_0 ~= nil then
|
if _value_0 ~= nil then
|
||||||
_len_0 = _len_0 + 1
|
_len_0 = _len_0 + 1
|
||||||
@ -558,7 +574,6 @@ Statement = Transformer({
|
|||||||
end
|
end
|
||||||
return _accum_0
|
return _accum_0
|
||||||
end)()
|
end)()
|
||||||
tbl[2] = properties
|
|
||||||
local parent_cls_name = NameProxy("parent")
|
local parent_cls_name = NameProxy("parent")
|
||||||
local base_name = NameProxy("base")
|
local base_name = NameProxy("base")
|
||||||
local self_name = NameProxy("self")
|
local self_name = NameProxy("self")
|
||||||
@ -660,9 +675,9 @@ Statement = Transformer({
|
|||||||
local slice = (function()
|
local slice = (function()
|
||||||
local _accum_0 = { }
|
local _accum_0 = { }
|
||||||
local _len_0 = 0
|
local _len_0 = 0
|
||||||
local _list_0 = chain
|
local _list_1 = chain
|
||||||
for _index_0 = 3, #_list_0 do
|
for _index_0 = 3, #_list_1 do
|
||||||
local item = _list_0[_index_0]
|
local item = _list_1[_index_0]
|
||||||
_len_0 = _len_0 + 1
|
_len_0 = _len_0 + 1
|
||||||
_accum_0[_len_0] = item
|
_accum_0[_len_0] = item
|
||||||
end
|
end
|
||||||
@ -710,9 +725,9 @@ Statement = Transformer({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
local _list_0 = slice
|
local _list_1 = slice
|
||||||
for _index_0 = 1, #_list_0 do
|
for _index_0 = 1, #_list_1 do
|
||||||
local item = _list_0[_index_0]
|
local item = _list_1[_index_0]
|
||||||
insert(new_chain, item)
|
insert(new_chain, item)
|
||||||
end
|
end
|
||||||
return new_chain
|
return new_chain
|
||||||
@ -722,7 +737,10 @@ Statement = Transformer({
|
|||||||
end)
|
end)
|
||||||
end),
|
end),
|
||||||
_with_0.assign_one(parent_cls_name, parent_val == "" and "nil" or parent_val),
|
_with_0.assign_one(parent_cls_name, parent_val == "" and "nil" or parent_val),
|
||||||
_with_0.assign_one(base_name, tbl),
|
_with_0.assign_one(base_name, {
|
||||||
|
"table",
|
||||||
|
properties
|
||||||
|
}),
|
||||||
_with_0.assign_one(base_name:chain("__index"), base_name),
|
_with_0.assign_one(base_name:chain("__index"), base_name),
|
||||||
build["if"]({
|
build["if"]({
|
||||||
cond = parent_cls_name,
|
cond = parent_cls_name,
|
||||||
|
@ -302,17 +302,27 @@ Statement = Transformer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class: (node) =>
|
class: (node) =>
|
||||||
_, name, parent_val, tbl = unpack node
|
_, name, parent_val, body = unpack node
|
||||||
|
|
||||||
|
-- split apart properties and statements
|
||||||
|
statements = {}
|
||||||
|
properties = {}
|
||||||
|
for item in *body
|
||||||
|
switch item[1]
|
||||||
|
when "stm"
|
||||||
|
insert statements, item[2]
|
||||||
|
when "props"
|
||||||
|
for tuple in *item[2,]
|
||||||
|
insert properties, tuple
|
||||||
|
|
||||||
|
-- find constructor
|
||||||
constructor = nil
|
constructor = nil
|
||||||
properties = for entry in *tbl[2]
|
properties = for tuple in *properties
|
||||||
if entry[1] == constructor_name
|
if tuple[1] == constructor_name
|
||||||
constructor = entry[2]
|
constructor = tuple[2]
|
||||||
nil
|
nil
|
||||||
else
|
else
|
||||||
entry
|
tuple
|
||||||
|
|
||||||
tbl[2] = properties
|
|
||||||
|
|
||||||
parent_cls_name = NameProxy "parent"
|
parent_cls_name = NameProxy "parent"
|
||||||
base_name = NameProxy "base"
|
base_name = NameProxy "base"
|
||||||
@ -398,7 +408,7 @@ Statement = Transformer {
|
|||||||
parent_cls_name
|
parent_cls_name
|
||||||
|
|
||||||
.assign_one parent_cls_name, parent_val == "" and "nil" or parent_val
|
.assign_one parent_cls_name, parent_val == "" and "nil" or parent_val
|
||||||
.assign_one base_name, tbl
|
.assign_one base_name, {"table", properties}
|
||||||
.assign_one base_name\chain"__index", base_name
|
.assign_one base_name\chain"__index", base_name
|
||||||
|
|
||||||
build["if"] {
|
build["if"] {
|
||||||
|
Loading…
Reference in New Issue
Block a user