-
Notifications
You must be signed in to change notification settings - Fork 195
Expand file tree
/
Copy pathutil.lua
More file actions
307 lines (307 loc) · 6.23 KB
/
util.lua
File metadata and controls
307 lines (307 loc) · 6.23 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
local unpack
unpack = require("moonscript.util").unpack
local P, C, S, Cp, Cmt, V
do
local _obj_0 = require("lpeg")
P, C, S, Cp, Cmt, V = _obj_0.P, _obj_0.C, _obj_0.S, _obj_0.Cp, _obj_0.Cmt, _obj_0.V
end
local ntype
ntype = require("moonscript.types").ntype
local Space
Space = require("moonscript.parse.literals").Space
local Indent = C(S("\t ") ^ 0) / function(str)
do
local sum = 0
for v in str:gmatch("[\t ]") do
local _exp_0 = v
if " " == _exp_0 then
sum = sum + 1
elseif "\t" == _exp_0 then
sum = sum + 4
end
end
return sum
end
end
local Cut = P(function()
return false
end)
local ensure
ensure = function(patt, finally)
return patt * finally + finally * Cut
end
local extract_line
extract_line = function(str, start_pos)
str = str:sub(start_pos)
do
local m = str:match("^(.-)\n")
if m then
return m
end
end
return str:match("^.-$")
end
local show_line_position
show_line_position = function(str, pos, context)
if context == nil then
context = true
end
local lines = {
{ }
}
for c in str:gmatch(".") do
local _update_0 = #lines
lines[_update_0] = lines[_update_0] or { }
table.insert(lines[#lines], c)
if c == "\n" then
lines[#lines + 1] = { }
end
end
for i, line in ipairs(lines) do
lines[i] = table.concat(line)
end
local out
local remaining = pos - 1
for k, line in ipairs(lines) do
if remaining < #line then
local left = line:sub(1, remaining)
local right = line:sub(remaining + 1)
out = {
tostring(left) .. "◉" .. tostring(right)
}
if context then
do
local before = lines[k - 1]
if before then
table.insert(out, 1, before)
end
end
do
local after = lines[k + 1]
if after then
table.insert(out, after)
end
end
end
break
else
remaining = remaining - #line
end
end
if not (out) then
return "-"
end
out = table.concat(out)
return (out:gsub("\n*$", ""))
end
local mark
mark = function(name)
return function(...)
return {
name,
...
}
end
end
local pos
pos = function(patt)
return (Cp() * patt) / function(pos, value)
if type(value) == "table" then
value[-1] = pos
end
return value
end
end
local got
got = function(what, context)
if context == nil then
context = true
end
return Cmt("", function(str, pos)
print("++ got " .. tostring(what), "[" .. tostring(show_line_position(str, pos, context)) .. "]")
return true
end)
end
local flatten_or_mark
flatten_or_mark = function(name)
return function(tbl)
if #tbl == 1 then
return tbl[1]
end
table.insert(tbl, 1, name)
return tbl
end
end
local is_assignable
do
local chain_assignable = {
index = true,
dot = true,
slice = true
}
is_assignable = function(node)
if node == "..." then
return false
end
local _exp_0 = ntype(node)
if "ref" == _exp_0 or "self" == _exp_0 or "value" == _exp_0 or "self_class" == _exp_0 or "table" == _exp_0 then
return true
elseif "chain" == _exp_0 then
return chain_assignable[ntype(node[#node])]
else
return false
end
end
end
local check_assignable
check_assignable = function(str, pos, value)
if is_assignable(value) then
return true, value
else
return false
end
end
local format_assign
do
local flatten_explist = flatten_or_mark("explist")
format_assign = function(lhs_exps, assign)
if not (assign) then
return flatten_explist(lhs_exps)
end
for _index_0 = 1, #lhs_exps do
local assign_exp = lhs_exps[_index_0]
if not (is_assignable(assign_exp)) then
error({
assign_exp,
"left hand expression is not assignable"
})
end
end
local t = ntype(assign)
local _exp_0 = t
if "assign" == _exp_0 then
return {
"assign",
lhs_exps,
unpack(assign, 2)
}
elseif "update" == _exp_0 then
return {
"update",
lhs_exps[1],
unpack(assign, 2)
}
else
return error("unknown assign expression: " .. tostring(t))
end
end
end
local format_single_assign
format_single_assign = function(lhs, assign)
if assign then
return format_assign({
lhs
}, assign)
else
return lhs
end
end
local sym
sym = function(chars)
return Space * chars
end
local symx
symx = function(chars)
return chars
end
local simple_string
simple_string = function(delim, allow_interpolation)
local inner = P("\\" .. tostring(delim)) + "\\\\" + (1 - P(delim))
if allow_interpolation then
local interp = symx('#{') * V("Exp") * sym('}')
inner = (C((inner - interp) ^ 1) + interp / mark("interpolate")) ^ 0
else
inner = C(inner ^ 0)
end
return C(symx(delim)) * inner * sym(delim) / mark("string")
end
local wrap_func_arg
wrap_func_arg = function(value)
return {
"call",
{
value
}
}
end
local join_chain
join_chain = function(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
local wrap_decorator
wrap_decorator = function(stm, dec)
if not (dec) then
return stm
end
return {
"decorated",
stm,
dec
}
end
local check_lua_string
check_lua_string = function(str, pos, right, left)
return #left == #right
end
local self_assign
self_assign = function(name, pos)
return {
{
"key_literal",
name
},
{
"ref",
name,
[-1] = pos
}
}
end
return {
Indent = Indent,
Cut = Cut,
ensure = ensure,
extract_line = extract_line,
mark = mark,
pos = pos,
flatten_or_mark = flatten_or_mark,
is_assignable = is_assignable,
check_assignable = check_assignable,
format_assign = format_assign,
format_single_assign = format_single_assign,
sym = sym,
symx = symx,
simple_string = simple_string,
wrap_func_arg = wrap_func_arg,
join_chain = join_chain,
wrap_decorator = wrap_decorator,
check_lua_string = check_lua_string,
self_assign = self_assign,
got = got,
show_line_position = show_line_position
}