Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- экранировать символы, которые могут привести к ошибке синтаксиса
- function string:escape()
- return (self:gsub("\\", "\\\\"):gsub("\"", "\\\""):
- gsub("\r", "\\r"):gsub("\n", "\\n"):gsub("\t", "\\t"):
- gsub("%c", function(s) return "\\" .. s:byte() end))
- end
- -- развернуть переменную
- -- [*] - уже выводили, [@] - не вывели т.к. skipt
- -- nil, boolean, number, string, function, userdata, thread, table
- local function _trace( essence, lib, reclvl, skipt )
- local t = type(essence)
- if t == "string" then -- mt строки не нужна
- essence = essence:escape()
- io.write('"' .. essence .. '"')
- else
- local mt = getmetatable(essence)
- if mt and mt.class then
- io.write("class: ", mt.class)
- else
- io.write(tostring(essence))
- end
- local extra_mt = false -- вывести метатаблицу, если true
- if t == "table" then
- if lib[essence] then -- уже выводили
- io.write(" [*t]")
- elseif skipt then -- не разворачивать
- io.write(" [@t]")
- else
- lib[essence] = true
- extra_mt = true -- уже добавили в lib, но метатаблицу ещё не вывели, оповестить mt
- for k,v in pairs(essence) do
- io.write("\n")
- io.write(string.rep("\t", reclvl))
- _trace(k, lib, reclvl+1, true)
- io.write(" => ")
- _trace(v, lib, reclvl+1)
- end
- end
- end
- if mt then
- if lib[essence] and not extra_mt then
- io.write(" [*mt]")
- elseif skipt then
- io.write(" [@mt]")
- else
- lib[essence] = true
- io.write("\n")
- io.write(string.rep("\t", reclvl))
- io.write("*mt* => ")
- _trace(mt, lib, reclvl+1)
- end
- end
- end
- end
- function trace( ... )
- local n = select("#", ...) -- сколько аргументов
- local lib = {}
- for i=1,n do
- local essence = (select(i, ...))
- _trace(essence, lib, 1)
- io.write("\n")
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement