gists/pt.lua

144 lines
3.4 KiB
Lua
Raw Normal View History

2015-11-21 20:01:40 -08:00
require 'extra'
2014-08-20 17:44:41 -07:00
local pt = {}
pt.__index = pt
setmetatable(pt, pt)
function rawstr(v)
if v == nil then return 'nil' end
local mt = getmetatable(v)
local ts = mt and rawget(mt, '__tostring')
if not ts then return tostring(v) end
mt.__tostring = nil
local s = tostring(v)
mt.__tostring = ts
return s
end
function getaddr(t)
return rawstr(t):sub(#type(t) + 3)
end
2015-11-21 20:01:40 -08:00
function copy(t)
-- shallow copy
if type(t) ~= 'table' then return end
local new = {}
for key, value in pairs(t) do
new[key] = value
end
return new
end
function pt.__call(pt, args)
2014-08-20 17:44:41 -07:00
-- print a table as semi-valid YAML
-- with references to prevent recursion/duplication
2015-11-21 20:01:40 -08:00
local t = args.table or args[1]
2014-08-20 17:44:41 -07:00
local self = {}
setmetatable(self, pt)
2015-11-21 20:01:40 -08:00
self.seen = copy(args.seen) or {}
self.skipped = copy(args.skipped) or {}
self.seen_elsewhere = args.seen or {}
self.depth = args.depth or 16
self.writer = args.writer or io.write
self.skeleton = args.skeleton or false
2014-08-20 17:44:41 -07:00
self.queued = {}
self:inner('__root__', t, '')
return self.seen
end
function pt:write(...)
self.writer(...)
end
function pt.safekey(k)
if type(k) == 'table' then
return 't'..getaddr(k)
end
local s = tostring(k)
s = s:gsub('[\r\n]', '')
return s:find('[^%w_]') and ('%q'):format(s) or s
end
function pt.safeval(v, indent)
if type(v) == 'function' then
return 'f'..getaddr(v)
end
local s = tostring(v)
if type(v) == 'number' then
return s
end
s = s:find('[\r\n]') and ('\n'..s):gsub('[\r\n]', '\n'..indent..' ') or s
2015-11-21 20:01:40 -08:00
--local safe = ('%q'):format(s)
--return s == safe:sub(2, -2) and s or safe
-- TODO: finish matching valid characters
return s:find('[^%w_()[]{}.]') and ('%q'):format(s) or s
2014-08-20 17:44:41 -07:00
end
function pt:inner(k, v, indent)
if type(v) ~= 'table' then
2015-11-21 20:01:40 -08:00
if self.skeleton then return end
2014-08-20 17:44:41 -07:00
self:write(indent, pt.safekey(k), ': ')
self:write(pt.safeval(v, indent), '\n')
return
end
local addr = getaddr(v)
self:write(indent, pt.safekey(k))
2015-11-21 20:01:40 -08:00
if #indent > self.depth or self.skipped[addr] then
--self.skipped[addr] = true -- TODO: extra logics
self:write(': #t', addr, '\n')
return
end
2014-08-20 17:44:41 -07:00
if self.seen[addr] or self.queued[addr] then
2015-11-21 20:01:40 -08:00
self:write(': *t', addr, self.seen_elsewhere[addr] and ' #\n' or '\n')
2014-08-20 17:44:41 -07:00
return
end
self.seen[addr] = true
self:write(': &t', addr, '\n')
self:outer(v, indent..' ')
end
function pt:outer(t, indent)
if type(t) ~= "table" then
self:write(indent, pt.safeval(t, indent), '\n')
return
end
local ours = {}
local not_ours = {}
for k,v in opairs(t) do
if type(v) == 'table' then
local addr = getaddr(v)
2015-11-21 20:01:40 -08:00
if not self.queued[addr] and not self.seen[addr] and not self.skipped[addr] then
2014-08-20 17:44:41 -07:00
self.queued[addr] = true
ours[k] = v
else
not_ours[k] = v
end
else
self:inner(k, v, indent)
end
end
for k,v in opairs(not_ours) do
self:inner(k, v, indent)
end
for k,v in opairs(ours) do
self.queued[getaddr(v)] = nil
self:inner(k, v, indent)
end
local mt = getmetatable(t)
if mt ~= nil then
self:inner('__metatable', mt, indent)
end
end
return pt