-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree.lua
More file actions
91 lines (80 loc) · 2.16 KB
/
Copy pathtree.lua
File metadata and controls
91 lines (80 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
-- Transform helpers for the grammar's Cfn callbacks. The callbacks
-- themselves live as code strings in grammar.lua and require this module
-- at parser load.
--
-- format_assign raises error({node, msg}) for invalid assignment targets;
-- it propagates out of parse() and is formatted by
-- moonscript_parser/init.lua.
local unpack = unpack or table.unpack
local tree = {}
-- never called with nil: that would report "value"
local function ntype(node)
if type(node) == "table" then
return node[1]
end
return "value"
end
tree.ntype = ntype
local chain_assignable = {index = true, dot = true, slice = true}
local function is_assignable(node)
if node == "..." then
return false
end
local t = ntype(node)
if t == "ref" or t == "self" or t == "value" or t == "self_class" or t == "table" then
return true
elseif t == "chain" then
return chain_assignable[ntype(node[#node])]
end
return false
end
tree.is_assignable = is_assignable
local function flatten_or_mark(name)
return function(tbl)
if #tbl == 1 then
return tbl[1]
end
table.insert(tbl, 1, name)
return tbl
end
end
local flatten_explist = flatten_or_mark("explist")
local function format_assign(lhs_exps, assign)
if not assign then
return flatten_explist(lhs_exps)
end
for _, assign_exp in ipairs(lhs_exps) do
if not is_assignable(assign_exp) then
error({assign_exp, "left hand expression is not assignable"})
end
end
local t = ntype(assign)
if t == "assign" then
return {"assign", lhs_exps, unpack(assign, 2)}
elseif t == "update" then
return {"update", lhs_exps[1], unpack(assign, 2)}
else
error("unknown assign expression: " .. tostring(t))
end
end
local function format_single_assign(lhs, assign)
if assign then
return format_assign({lhs}, assign)
end
return lhs
end
tree.format_assign = format_assign
tree.format_single_assign = format_single_assign
local function join_chain(callee, args)
if #args == 0 then
return callee
end
args = {"call", args}
if ntype(callee) == "chain" then
table.insert(callee, args)
return callee
end
return {"chain", callee, args}
end
tree.join_chain = join_chain
return tree