-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutil.lua
217 lines (217 loc) · 4.86 KB
/
util.lua
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
local concat
concat = table.concat
local unpack = unpack or table.unpack
local type = type
local moon = {
is_object = function(value)
return type(value) == "table" and value.__class
end,
is_a = function(thing, t)
if not (type(thing) == "table") then
return false
end
local cls = thing.__class
while cls do
if cls == t then
return true
end
cls = cls.__parent
end
return false
end,
type = function(value)
local base_type = type(value)
if base_type == "table" then
local cls = value.__class
if cls then
return cls
end
end
return base_type
end
}
local pos_to_line
pos_to_line = function(str, pos)
local line = 1
for _ in str:sub(1, pos):gmatch("\n") do
line = line + 1
end
return line
end
local trim
trim = function(str)
return str:match("^%s*(.-)%s*$")
end
local get_line
get_line = function(str, line_num)
for line in str:gmatch("([^\n]*)\n?") do
if line_num == 1 then
return line
end
line_num = line_num - 1
end
end
local get_closest_line
get_closest_line = function(str, line_num)
local line = get_line(str, line_num)
if (not line or trim(line) == "") and line_num > 1 then
return get_closest_line(str, line_num - 1)
else
return line, line_num
end
end
local split
split = function(str, delim)
if str == "" then
return { }
end
str = str .. delim
local _accum_0 = { }
local _len_0 = 1
for m in str:gmatch("(.-)" .. delim) do
_accum_0[_len_0] = m
_len_0 = _len_0 + 1
end
return _accum_0
end
local dump
dump = function(what)
local seen = { }
local _dump
_dump = function(what, depth)
if depth == nil then
depth = 0
end
local t = type(what)
if t == "string" then
return '"' .. what .. '"\n'
elseif t == "table" then
if seen[what] then
return "recursion(" .. tostring(what) .. ")...\n"
end
seen[what] = true
depth = depth + 1
local lines
do
local _accum_0 = { }
local _len_0 = 1
for k, v in pairs(what) do
_accum_0[_len_0] = (" "):rep(depth * 4) .. "[" .. tostring(k) .. "] = " .. _dump(v, depth)
_len_0 = _len_0 + 1
end
lines = _accum_0
end
seen[what] = false
local class_name
if what.__class then
class_name = "<" .. tostring(what.__class.__name) .. ">"
end
return tostring(class_name or "") .. "{\n" .. concat(lines) .. (" "):rep((depth - 1) * 4) .. "}\n"
else
return tostring(what) .. "\n"
end
end
return _dump(what)
end
local debug_posmap
debug_posmap = function(posmap, moon_code, lua_code)
local tuples
do
local _accum_0 = { }
local _len_0 = 1
for k, v in pairs(posmap) do
_accum_0[_len_0] = {
k,
v
}
_len_0 = _len_0 + 1
end
tuples = _accum_0
end
table.sort(tuples, function(a, b)
return a[1] < b[1]
end)
local lines
do
local _accum_0 = { }
local _len_0 = 1
for _index_0 = 1, #tuples do
local pair = tuples[_index_0]
local lua_line, pos = unpack(pair)
local moon_line = pos_to_line(moon_code, pos)
local lua_text = get_line(lua_code, lua_line)
local moon_text = get_closest_line(moon_code, moon_line)
local _value_0 = tostring(pos) .. "\t " .. tostring(lua_line) .. ":[ " .. tostring(trim(lua_text)) .. " ] >> " .. tostring(moon_line) .. ":[ " .. tostring(trim(moon_text)) .. " ]"
_accum_0[_len_0] = _value_0
_len_0 = _len_0 + 1
end
lines = _accum_0
end
return concat(lines, "\n")
end
local setfenv = setfenv or function(fn, env)
local name
local i = 1
while true do
name = debug.getupvalue(fn, i)
if not name or name == "_ENV" then
break
end
i = i + 1
end
if name then
debug.upvaluejoin(fn, i, (function()
return env
end), 1)
end
return fn
end
local getfenv = getfenv or function(fn)
local i = 1
while true do
local name, val = debug.getupvalue(fn, i)
if not (name) then
break
end
if name == "_ENV" then
return val
end
i = i + 1
end
return nil
end
local get_options
get_options = function(...)
local count = select("#", ...)
local opts = select(count, ...)
if type(opts) == "table" then
return opts, unpack({
...
}, nil, count - 1)
else
return { }, ...
end
end
local safe_module
safe_module = function(name, tbl)
return setmetatable(tbl, {
__index = function(self, key)
return error("Attempted to import non-existent `" .. tostring(key) .. "` from " .. tostring(name))
end
})
end
return {
moon = moon,
pos_to_line = pos_to_line,
get_closest_line = get_closest_line,
get_line = get_line,
trim = trim,
split = split,
dump = dump,
debug_posmap = debug_posmap,
getfenv = getfenv,
setfenv = setfenv,
get_options = get_options,
unpack = unpack,
safe_module = safe_module
}