serpent.lua 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. local n, v = "serpent", "0.302" -- (C) 2012-18 Paul Kulchenko; MIT License
  2. local c, d = "Paul Kulchenko", "Lua serializer and pretty printer"
  3. local snum = {[tostring(1/0)]='1/0 --[[math.huge]]',[tostring(-1/0)]='-1/0 --[[-math.huge]]',[tostring(0/0)]='0/0'}
  4. local badtype = {thread = true, userdata = true, cdata = true}
  5. local getmetatable = debug and debug.getmetatable or getmetatable
  6. local pairs = function(t) return next, t end -- avoid using __pairs in Lua 5.2+
  7. local keyword, globals, G = {}, {}, (_G or _ENV)
  8. for _,k in ipairs({'and', 'break', 'do', 'else', 'elseif', 'end', 'false',
  9. 'for', 'function', 'goto', 'if', 'in', 'local', 'nil', 'not', 'or', 'repeat',
  10. 'return', 'then', 'true', 'until', 'while'}) do keyword[k] = true end
  11. for k,v in pairs(G) do globals[v] = k end -- build func to name mapping
  12. for _,g in ipairs({'coroutine', 'debug', 'io', 'math', 'string', 'table', 'os'}) do
  13. for k,v in pairs(type(G[g]) == 'table' and G[g] or {}) do globals[v] = g..'.'..k end end
  14. local function s(t, opts)
  15. local name, indent, fatal, maxnum = opts.name, opts.indent, opts.fatal, opts.maxnum
  16. local sparse, custom, huge = opts.sparse, opts.custom, not opts.nohuge
  17. local space, maxl = (opts.compact and '' or ' '), (opts.maxlevel or math.huge)
  18. local maxlen, metatostring = tonumber(opts.maxlength), opts.metatostring
  19. local iname, comm = '_'..(name or ''), opts.comment and (tonumber(opts.comment) or math.huge)
  20. local numformat = opts.numformat or "%.17g"
  21. local seen, sref, syms, symn = {}, {'local '..iname..'={}'}, {}, 0
  22. local function gensym(val) return '_'..(tostring(tostring(val)):gsub("[^%w]",""):gsub("(%d%w+)",
  23. -- tostring(val) is needed because __tostring may return a non-string value
  24. function(s) if not syms[s] then symn = symn+1; syms[s] = symn end return tostring(syms[s]) end)) end
  25. local function safestr(s) return type(s) == "number" and tostring(huge and snum[tostring(s)] or numformat:format(s))
  26. or type(s) ~= "string" and tostring(s) -- escape NEWLINE/010 and EOF/026
  27. or ("%q"):format(s):gsub("\010","n"):gsub("\026","\\026") end
  28. local function comment(s,l) return comm and (l or 0) < comm and ' --[['..select(2, pcall(tostring, s))..']]' or '' end
  29. local function globerr(s,l) return globals[s] and globals[s]..comment(s,l) or not fatal
  30. and safestr(select(2, pcall(tostring, s))) or error("Can't serialize "..tostring(s)) end
  31. local function safename(path, name) -- generates foo.bar, foo[3], or foo['b a r']
  32. local n = name == nil and '' or name
  33. local plain = type(n) == "string" and n:match("^[%l%u_][%w_]*$") and not keyword[n]
  34. local safe = plain and n or '['..safestr(n)..']'
  35. return (path or '')..(plain and path and '.' or '')..safe, safe end
  36. local alphanumsort = type(opts.sortkeys) == 'function' and opts.sortkeys or function(k, o, n) -- k=keys, o=originaltable, n=padding
  37. local maxn, to = tonumber(n) or 12, {number = 'a', string = 'b'}
  38. local function padnum(d) return ("%0"..tostring(maxn).."d"):format(tonumber(d)) end
  39. table.sort(k, function(a,b)
  40. -- sort numeric keys first: k[key] is not nil for numerical keys
  41. return (k[a] ~= nil and 0 or to[type(a)] or 'z')..(tostring(a):gsub("%d+",padnum))
  42. < (k[b] ~= nil and 0 or to[type(b)] or 'z')..(tostring(b):gsub("%d+",padnum)) end) end
  43. local function val2str(t, name, indent, insref, path, plainindex, level)
  44. local ttype, level, mt = type(t), (level or 0), getmetatable(t)
  45. local spath, sname = safename(path, name)
  46. local tag = plainindex and
  47. ((type(name) == "number") and '' or name..space..'='..space) or
  48. (name ~= nil and sname..space..'='..space or '')
  49. if seen[t] then -- already seen this element
  50. sref[#sref+1] = spath..space..'='..space..seen[t]
  51. return tag..'nil'..comment('ref', level) end
  52. -- protect from those cases where __tostring may fail
  53. if type(mt) == 'table' and metatostring ~= false then
  54. local to, tr = pcall(function() return mt.__tostring(t) end)
  55. local so, sr = pcall(function() return mt.__serialize(t) end)
  56. if (to or so) then -- knows how to serialize itself
  57. seen[t] = insref or spath
  58. t = so and sr or tr
  59. ttype = type(t)
  60. end -- new value falls through to be serialized
  61. end
  62. if ttype == "table" then
  63. if level >= maxl then return tag..'{}'..comment('maxlvl', level) end
  64. seen[t] = insref or spath
  65. if next(t) == nil then return tag..'{}'..comment(t, level) end -- table empty
  66. if maxlen and maxlen < 0 then return tag..'{}'..comment('maxlen', level) end
  67. local maxn, o, out = math.min(#t, maxnum or #t), {}, {}
  68. for key = 1, maxn do o[key] = key end
  69. if not maxnum or #o < maxnum then
  70. local n = #o -- n = n + 1; o[n] is much faster than o[#o+1] on large tables
  71. for key in pairs(t) do if o[key] ~= key then n = n + 1; o[n] = key end end end
  72. if maxnum and #o > maxnum then o[maxnum+1] = nil end
  73. if opts.sortkeys and #o > maxn then alphanumsort(o, t, opts.sortkeys) end
  74. local sparse = sparse and #o > maxn -- disable sparsness if only numeric keys (shorter output)
  75. for n, key in ipairs(o) do
  76. local value, ktype, plainindex = t[key], type(key), n <= maxn and not sparse
  77. if opts.valignore and opts.valignore[value] -- skip ignored values; do nothing
  78. or opts.keyallow and not opts.keyallow[key]
  79. or opts.keyignore and opts.keyignore[key]
  80. or opts.valtypeignore and opts.valtypeignore[type(value)] -- skipping ignored value types
  81. or sparse and value == nil then -- skipping nils; do nothing
  82. elseif ktype == 'table' or ktype == 'function' or badtype[ktype] then
  83. if not seen[key] and not globals[key] then
  84. sref[#sref+1] = 'placeholder'
  85. local sname = safename(iname, gensym(key)) -- iname is table for local variables
  86. sref[#sref] = val2str(key,sname,indent,sname,iname,true) end
  87. sref[#sref+1] = 'placeholder'
  88. local path = seen[t]..'['..tostring(seen[key] or globals[key] or gensym(key))..']'
  89. sref[#sref] = path..space..'='..space..tostring(seen[value] or val2str(value,nil,indent,path))
  90. else
  91. out[#out+1] = val2str(value,key,indent,nil,seen[t],plainindex,level+1)
  92. if maxlen then
  93. maxlen = maxlen - #out[#out]
  94. if maxlen < 0 then break end
  95. end
  96. end
  97. end
  98. local prefix = string.rep(indent or '', level)
  99. local head = indent and '{\n'..prefix..indent or '{'
  100. local body = table.concat(out, ','..(indent and '\n'..prefix..indent or space))
  101. local tail = indent and "\n"..prefix..'}' or '}'
  102. return (custom and custom(tag,head,body,tail,level) or tag..head..body..tail)..comment(t, level)
  103. elseif badtype[ttype] then
  104. seen[t] = insref or spath
  105. return tag..globerr(t, level)
  106. elseif ttype == 'function' then
  107. seen[t] = insref or spath
  108. if opts.nocode then return tag.."function() --[[..skipped..]] end"..comment(t, level) end
  109. local ok, res = pcall(string.dump, t)
  110. local func = ok and "((loadstring or load)("..safestr(res)..",'@serialized'))"..comment(t, level)
  111. return tag..(func or globerr(t, level))
  112. else return tag..safestr(t) end -- handle all other types
  113. end
  114. local sepr = indent and "\n" or ";"..space
  115. local body = val2str(t, name, indent) -- this call also populates sref
  116. local tail = #sref>1 and table.concat(sref, sepr)..sepr or ''
  117. local warn = opts.comment and #sref>1 and space.."--[[incomplete output with shared/self-references skipped]]" or ''
  118. return not name and body..warn or "do local "..body..sepr..tail.."return "..name..sepr.."end"
  119. end
  120. local function deserialize(data, opts)
  121. local env = (opts and opts.safe == false) and G
  122. or setmetatable({}, {
  123. __index = function(t,k) return t end,
  124. __call = function(t,...) error("cannot call functions") end
  125. })
  126. local f, res = (loadstring or load)('return '..data, nil, nil, env)
  127. if not f then f, res = (loadstring or load)(data, nil, nil, env) end
  128. if not f then return f, res end
  129. if setfenv then setfenv(f, env) end
  130. return pcall(f)
  131. end
  132. local function merge(a, b) if b then for k,v in pairs(b) do a[k] = v end end; return a; end
  133. if false then
  134. return { _NAME = n, _COPYRIGHT = c, _DESCRIPTION = d, _VERSION = v, serialize = s,
  135. load = deserialize,
  136. dump = function(a, opts) return s(a, merge({name = '_', compact = true, sparse = true}, opts)) end,
  137. line = function(a, opts) return s(a, merge({sortkeys = true, comment = true}, opts)) end,
  138. block = function(a, opts) return s(a, merge({indent = ' ', sortkeys = true, comment = true}, opts)) end }
  139. end
  140. modpol.serpent.load = deserialize
  141. modpol.serpent.dump = function(a, opts) return s(a, merge({name = '_', compact = true, sparse = true}, opts)) end
  142. modpol.serpent.line = function(a, opts) return s(a, merge({sortkeys = true, comment = true}, opts)) end
  143. modpol.serpent.block = function(a, opts) return s(a, merge({indent = ' ', sortkeys = true, comment = true}, opts)) end