-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.lua
138 lines (128 loc) · 3.36 KB
/
debug.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
function w( ... )
local ok,fmt = pcall(string.format,...)
if ok == false then
print("-(e)-> " .. fmt)
print(debug.traceback())
else
print("-----> " .. fmt)
end
io.stdout:flush()
end
if not log then
log = function (...)
print(string.format(...))
end
end
function toktostring(tok)
if tok == 0 then
return "TOK_VOID"
elseif tok == 1 then
return "TOK_WORD"
elseif tok == 2 then
return "TOK_VAR"
elseif tok == 3 then
return "TOK_OPENPAREN"
elseif tok == 4 then
return "TOK_CLOSEPAREN"
elseif tok == 7 then
return "TOK_STRING"
elseif tok == 8 then
return "TOK_COMMENT"
elseif tok == 9 then
return "TOK_NUMBER"
elseif tok == 10 then
return "TOK_OPERATOR"
elseif tok == 11 then
return "TOK_OCCURRENCEINDICATOR"
elseif tok == 12 then
return "TOK_OPENBRACKET"
elseif tok == 13 then
return "TOK_CLOSEBRACKET"
elseif tok == 14 then
return "TOK_QNAME"
elseif tok == 15 then
return "TOK_NCNAME"
end
end
do
local level = 0
function enterStep(infotbl,where)
local nexttok = infotbl.peek()
nexttok = nexttok or {0,""}
w("%s%s (next: %s|%q)",string.rep(" ",level), where,toktostring(nexttok[1]),nexttok[2])
level = level + 1
end
function leaveStep(infotbl,where)
local nexttok = infotbl.peek()
level = level - 1
nexttok = nexttok or {0,""}
w("%s%s ... done (next: %s|%q)",string.rep(" ",level),where,toktostring(nexttok[1]),nexttok[2])
end
end
local function cmpkeys( a,b )
if type(a) == type(b) then
if a == "elementname" then return true end
if b == "elementname" then return false end
if type(a) == "table" then return true end
return a < b
end
if type(a) == "number" then return false end
return true
end
do
local function indent(level)
return string.rep( " ", level )
end
function printtable (ind,tbl_to_print,level,depth)
if depth and depth <= level then return end
if type(tbl_to_print) ~= "table" then
log("printtable: %q is not a table, it is a %s (%q)",tostring(ind),type(tbl_to_print),tostring(tbl_to_print))
return
end
level = level or 0
local k,l
local key
if level > 0 then
if type(ind) == "number" then
key = string.format("[%d]",ind)
elseif type(ind) == "table" then
key = "table"
else
key = string.format("[%q]",ind)
end
else
key = ind
end
log(indent(level) .. tostring(key) .. " = {")
level=level+1
local keys = {}
for k,_ in pairs(tbl_to_print) do
keys[#keys + 1] = k
end
table.sort(keys,cmpkeys)
for i=1,#keys do
local k = keys[i]
local l = tbl_to_print[k]
if type(l) == "userdata" and node.is_node(l) then
l = "⬖".. nodelist_tostring(l) .. "⬗"
end
if type(l)=="table" then
if k ~= ".__parent" and k ~= ".__context" then
printtable(k,l,level,depth)
else
if k == ".__parent" then
log("%s[\".__parent\"] = <%s>", indent(level),l[".__local_name"])
end
end
else
if type(k) == "number" then
key = string.format("[%d]",k)
else
key = string.format("[%q]",tostring(k))
end
log("%s%s = %q", indent(level), key,tostring(l))
end
end
log(indent(level-1) .. "},")
end
end