-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patherrors.lua
117 lines (117 loc) · 2.92 KB
/
errors.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
local __util = require("__zk-lib__/lualib/moonscript/util")
local lpeg = require("__zk-lib__/lualib/LuLPeg/lulpeg")
local concat, insert
do
local _obj_0 = table
concat, insert = _obj_0.concat, _obj_0.insert
end
local split, pos_to_line
split, pos_to_line = __util.split, __util.pos_to_line
local user_error
user_error = function(...)
return error({
"user-error",
...
})
end
local lookup_line
lookup_line = function(fname, pos, cache)
if not cache[fname] then
do
local _with_0 = assert(io.open(fname))
cache[fname] = _with_0:read("*a")
_with_0:close()
end
end
return pos_to_line(cache[fname], pos)
end
local reverse_line_number
reverse_line_number = function(fname, line_table, line_num, cache)
for i = line_num, 0, -1 do
if line_table[i] then
return lookup_line(fname, line_table[i], cache)
end
end
return "unknown"
end
local truncate_traceback
truncate_traceback = function(traceback, chunk_func)
if chunk_func == nil then
chunk_func = "moonscript_chunk"
end
traceback = split(traceback, "\n")
local stop = #traceback
while stop > 1 do
if traceback[stop]:match(chunk_func) then
break
end
stop = stop - 1
end
do
local _accum_0 = { }
local _len_0 = 1
local _max_0 = stop
for _index_0 = 1, _max_0 < 0 and #traceback + _max_0 or _max_0 do
local t = traceback[_index_0]
_accum_0[_len_0] = t
_len_0 = _len_0 + 1
end
traceback = _accum_0
end
local rep = "function '" .. chunk_func .. "'"
traceback[#traceback] = traceback[#traceback]:gsub(rep, "main chunk")
return concat(traceback, "\n")
end
local rewrite_traceback
rewrite_traceback = function(text, err)
local line_tables = require("__zk-lib__/lualib/moonscript/line_tables")
local V, S, Ct, C
V, S, Ct, C = lpeg.V, lpeg.S, lpeg.Ct, lpeg.C
local header_text = "stack traceback:"
local Header, Line = V("Header"), V("Line")
local Break = lpeg.S("\n")
local g = lpeg.P({
Header,
Header = header_text * Break * Ct(Line ^ 1),
Line = "\t" * C((1 - Break) ^ 0) * (Break + -1)
})
local cache = { }
local rewrite_single
rewrite_single = function(trace)
local fname, line, msg = trace:match('^(.-):(%d+): (.*)$')
local tbl = line_tables["@" .. tostring(fname)]
if fname and tbl then
return concat({
fname,
":",
reverse_line_number(fname, tbl, line, cache),
": ",
"(",
line,
") ",
msg
})
else
return trace
end
end
err = rewrite_single(err)
local match = g:match(text)
if not (match) then
return nil
end
for i, trace in ipairs(match) do
match[i] = rewrite_single(trace)
end
return concat({
"moon: " .. err,
header_text,
"\t" .. concat(match, "\n\t")
}, "\n")
end
return {
rewrite_traceback = rewrite_traceback,
truncate_traceback = truncate_traceback,
user_error = user_error,
reverse_line_number = reverse_line_number
}