local y={_VERSION="\105\110\115\112\101\099\116\046\108\117\097 \051\046\049\046\048",_URL="\104\116\116\112\058\047\047\103\105\116\104\117\098\046\099\111\109\047\107\105\107\105\116\111\047\105\110\115\112\101\099\116\046\108\117\097",_DESCRIPTION="\104\117\109\097\110-\114\101\097\100\097\098\108\101 \114\101\112\114\101\115\101\110\116\097\116\105\111\110\115 \111\102 \116\097\098\108\101\115";_LICENSE="    \077\073\084 \076\073\067\069\078\083\069\n\n    \067\111\112\121\114\105\103\104\116 \040\099\041 \050\048\049\051 \069\110\114\105\113\117\101 \071\097\114\099\195\173\097 \067\111\116\097\n\n    \080\101\114\109\105\115\115\105\111\110 \105\115 \104\101\114\101\098\121 \103\114\097\110\116\101\100\044 \102\114\101\101 \111\102 \099\104\097\114\103\101\044 \116\111 \097\110\121 \112\101\114\115\111\110 \111\098\116\097\105\110\105\110\103 \097\n    \099\111\112\121 \111\102 \116\104\105\115 \115\111\102\116\119\097\114\101 \097\110\100 \097\115\115\111\099\105\097\116\101\100 \100\111\099\117\109\101\110\116\097\116\105\111\110 \102\105\108\101\115 \040\116\104\101\n    \"\083\111\102\116\119\097\114\101\"\041\044 \116\111 \100\101\097\108 \105\110 \116\104\101 \083\111\102\116\119\097\114\101 \119\105\116\104\111\117\116 \114\101\115\116\114\105\099\116\105\111\110\044 \105\110\099\108\117\100\105\110\103\n    \119\105\116\104\111\117\116 \108\105\109\105\116\097\116\105\111\110 \116\104\101 \114\105\103\104\116\115 \116\111 \117\115\101\044 \099\111\112\121\044 \109\111\100\105\102\121\044 \109\101\114\103\101\044 \112\117\098\108\105\115\104\044\n    \100\105\115\116\114\105\098\117\116\101\044 \115\117\098\108\105\099\101\110\115\101\044 \097\110\100\047\111\114 \115\101\108\108 \099\111\112\105\101\115 \111\102 \116\104\101 \083\111\102\116\119\097\114\101\044 \097\110\100 \116\111\n    \112\101\114\109\105\116 \112\101\114\115\111\110\115 \116\111 \119\104\111\109 \116\104\101 \083\111\102\116\119\097\114\101 \105\115 \102\117\114\110\105\115\104\101\100 \116\111 \100\111 \115\111\044 \115\117\098\106\101\099\116 \116\111\n    \116\104\101 \102\111\108\108\111\119\105\110\103 \099\111\110\100\105\116\105\111\110\115\058\n\n    \084\104\101 \097\098\111\118\101 \099\111\112\121\114\105\103\104\116 \110\111\116\105\099\101 \097\110\100 \116\104\105\115 \112\101\114\109\105\115\115\105\111\110 \110\111\116\105\099\101 \115\104\097\108\108 \098\101 \105\110\099\108\117\100\101\100\n    \105\110 \097\108\108 \099\111\112\105\101\115 \111\114 \115\117\098\115\116\097\110\116\105\097\108 \112\111\114\116\105\111\110\115 \111\102 \116\104\101 \083\111\102\116\119\097\114\101\046\n\n    \084\072\069 \083\079\070\084\087\065\082\069 \073\083 \080\082\079\086\073\068\069\068 \"\065\083 \073\083\"\044 \087\073\084\072\079\085\084 \087\065\082\082\065\078\084\089 \079\070 \065\078\089 \075\073\078\068\044 \069\088\080\082\069\083\083\n    \079\082 \073\077\080\076\073\069\068\044 \073\078\067\076\085\068\073\078\071 \066\085\084 \078\079\084 \076\073\077\073\084\069\068 \084\079 \084\072\069 \087\065\082\082\065\078\084\073\069\083 \079\070\n    \077\069\082\067\072\065\078\084\065\066\073\076\073\084\089\044 \070\073\084\078\069\083\083 \070\079\082 \065 \080\065\082\084\073\067\085\076\065\082 \080\085\082\080\079\083\069 \065\078\068 \078\079\078\073\078\070\082\073\078\071\069\077\069\078\084\046\n    \073\078 \078\079 \069\086\069\078\084 \083\072\065\076\076 \084\072\069 \065\085\084\072\079\082\083 \079\082 \067\079\080\089\082\073\071\072\084 \072\079\076\068\069\082\083 \066\069 \076\073\065\066\076\069 \070\079\082 \065\078\089\n    \067\076\065\073\077\044 \068\065\077\065\071\069\083 \079\082 \079\084\072\069\082 \076\073\065\066\073\076\073\084\089\044 \087\072\069\084\072\069\082 \073\078 \065\078 \065\067\084\073\079\078 \079\070 \067\079\078\084\082\065\067\084\044\n    \084\079\082\084 \079\082 \079\084\072\069\082\087\073\083\069\044 \065\082\073\083\073\078\071 \070\082\079\077\044 \079\085\084 \079\070 \079\082 \073\078 \067\079\078\078\069\067\084\073\079\078 \087\073\084\072 \084\072\069\n    \083\079\070\084\087\065\082\069 \079\082 \084\072\069 \085\083\069 \079\082 \079\084\072\069\082 \068\069\065\076\073\078\071\083 \073\078 \084\072\069 \083\079\070\084\087\065\082\069\046\n  "}local g=tostring y.KEY=setmetatable({},{__tostring=function()return"\105\110\115\112\101\099\116\046\075\069\089"end})y.METATABLE=setmetatable({},{__tostring=function()return"\105\110\115\112\101\099\116\046\077\069\084\065\084\065\066\076\069"end})local function H(y)return next,y,nil end local function k(y)if y:match("\"")and not y:match("\'")then return"\'"..(y.."\'")end return"\""..(y:gsub("\"","\092\"").."\"")end local o={["\a"]="\092\097";["\b"]="\092\098";["\012"]="\092\102";["\n"]="\092\110";["\r"]="\092\114";["\t"]="\092\116",["\v"]="\092\118"}local T={}for y=0,31,1 do local g=string.char(y)if not o[g]then o[g]="\092"..y T[g]=string.format("\092\037\048\051\100",y)end end local function w(y)return((y:gsub("\092","\092\092")):gsub("\040\037\099\041\037\102\091\048-\057\093",T)):gsub("\037\099",o)end local function d(y)return type(y)=="\115\116\114\105\110\103"and y:match("\094\091\095\037\097\093\091\095\037\097\037\100\093\042\036")end local function z(y,g)return type(y)=="\110\117\109\098\101\114"and(1<=y and(y<=g and math.floor(y)==y))end local X={number=1,boolean=2,string=3;table=4,["\102\117\110\099\116\105\111\110"]=5,userdata=6;thread=7}local function D(y,g)local H,k=type(y),type(g)if H==k and(H=="\115\116\114\105\110\103"or H=="\110\117\109\098\101\114")then return y<g end local o,T=X[H],X[k]if o and T then return X[H]<X[k]elseif o then return true elseif T then return false end return H<k end local function m(y)local g=1 local H=rawget(y,g)while H~=nil do g=g+1 H=rawget(y,g)end return g-1 end local function b(y)local g,k={},0 local o=m(y)for y,H in H(y)do if not z(y,o)then k=k+1 g[k]=y end end table.sort(g,D)return g,k,o end local function n(y,g)g=g or{}if type(y)=="\116\097\098\108\101"then if not g[y]then g[y]=1 for y,H in H(y)do n(y,g)n(H,g)end n(getmetatable(y),g)else g[y]=g[y]+1 end end return g end local p=function(y)local g,H={},#y for H=1,H,1 do g[H]=y[H]end return g,H end local function e(y,...)local g={...}local H,k=p(y)for y=1,#g,1 do H[k+y]=g[y]end return H end local function J(g,k,o,T)if k==nil then return nil end if T[k]then return T[k]end local w=g(k,o)if type(w)=="\116\097\098\108\101"then local d={}T[k]=d local z for H,k in H(w)do z=J(g,H,e(o,H,y.KEY),T)if z~=nil then d[z]=J(g,k,e(o,z),T)end end local X=J(g,getmetatable(w),e(o,y.METATABLE),T)if type(X)~="\116\097\098\108\101"then X=nil end setmetatable(d,X)w=d end return w end local A={}local B={__index=A}function A.puts(y,...)local g={...}local H=y.buffer local k=#H for y=1,#g,1 do k=k+1 H[k]=g[y]end end function A.down(g,y)g.level=g.level+1 y()g.level=g.level-1 end function A.tabify(y)y:puts(y.newline,string.rep(y.indent,y.level))end function A.alreadyVisited(g,y)return g.ids[y]~=nil end function A.getId(H,y)local k=H.ids[y]if not k then local g=type(y)k=(H.maxIds[g]or 0)+1 H.maxIds[g]=k H.ids[y]=k end return g(k)end function A.putKey(g,y)if d(y)then return g:puts(y)end g:puts("\091")g:putValue(y)g:puts("\093")end function A.putTable(k,H)if H==y.KEY or H==y.METATABLE then k:puts(g(H))elseif k:alreadyVisited(H)then k:puts("\060\116\097\098\108\101 ",k:getId(H),"\062")elseif k.level>=k.depth then k:puts("\123\046\046\046\125")else if k.tableAppearances[H]>1 then k:puts("\060",k:getId(H),"\062")end local y,g,o=b(H)local T=getmetatable(H)k:puts("\123")k:down(function()local w=0 for y=1,o,1 do if w>0 then k:puts("\044")end k:puts(" ")k:putValue(H[y])w=w+1 end for g=1,g,1 do local o=y[g]if w>0 then k:puts("\044")end k:tabify()k:putKey(o)k:puts(" \061 ")k:putValue(H[o])w=w+1 end if type(T)=="\116\097\098\108\101"then if w>0 then k:puts("\044")end k:tabify()k:puts("\060\109\101\116\097\116\097\098\108\101\062 \061 ")k:putValue(T)end end)if g>0 or type(T)=="\116\097\098\108\101"then k:tabify()elseif o>0 then k:puts(" ")end k:puts("\125")end end function A.putValue(H,y)local o=type(y)if o=="\115\116\114\105\110\103"then H:puts(k(w(y)))elseif o=="\110\117\109\098\101\114"or o=="\098\111\111\108\101\097\110"or o=="\110\105\108"or o=="\099\100\097\116\097"or o=="\099\116\121\112\101"then H:puts(g(y))elseif o=="\116\097\098\108\101"then H:putTable(y)else H:puts("\060",o," ",H:getId(y),"\062")end end function y.inspect(y,g)g=g or{}local H=g.depth or math.huge local k=g.newline or"\n"local o=g.indent or"  "local T=g.process if T then y=J(T,y,{},{})end local w=setmetatable({depth=H;level=0,buffer={};ids={};maxIds={},newline=k;indent=o;tableAppearances=n(y)},B)w:putValue(y)return table.concat(w.buffer)end setmetatable(y,{__call=function(g,...)return y.inspect(...)end})return y.inspect
local inspect ={
  _VERSION = 'inspect.lua 3.1.0',
  _URL     = 'http://github.com/kikito/inspect.lua',
  _DESCRIPTION = 'human-readable representations of tables',
  _LICENSE = [[
    MIT LICENSE

    Copyright (c) 2013 Enrique García Cota

    Permission is hereby granted, free of charge, to any person obtaining a
    copy of this software and associated documentation files (the
    "Software"), to deal in the Software without restriction, including
    without limitation the rights to use, copy, modify, merge, publish,
    distribute, sublicense, and/or sell copies of the Software, and to
    permit persons to whom the Software is furnished to do so, subject to
    the following conditions:

    The above copyright notice and this permission notice shall be included
    in all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
    OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
    IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
    CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
    TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
    SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  ]]
}

local tostring = tostring

inspect.KEY       = setmetatable({}, {__tostring = function() return 'inspect.KEY' end})
inspect.METATABLE = setmetatable({}, {__tostring = function() return 'inspect.METATABLE' end})

local function rawpairs(t)
  return next, t, nil
end

-- Apostrophizes the string if it has quotes, but not aphostrophes
-- Otherwise, it returns a regular quoted string
local function smartQuote(str)
  if str:match('"') and not str:match("'") then
    return "'" .. str .. "'"
  end
  return '"' .. str:gsub('"', '\\"') .. '"'
end

-- \a => '\\a', \0 => '\\0', 31 => '\31'
local shortControlCharEscapes = {
  ["\a"] = "\\a",  ["\b"] = "\\b", ["\f"] = "\\f", ["\n"] = "\\n",
  ["\r"] = "\\r",  ["\t"] = "\\t", ["\v"] = "\\v"
}
local longControlCharEscapes = {} -- \a => nil, \0 => \000, 31 => \031
for i=0, 31 do
  local ch = string.char(i)
  if not shortControlCharEscapes[ch] then
    shortControlCharEscapes[ch] = "\\"..i
    longControlCharEscapes[ch]  = string.format("\\%03d", i)
  end
end

local function escape(str)
  return (str:gsub("\\", "\\\\")
             :gsub("(%c)%f[0-9]", longControlCharEscapes)
             :gsub("%c", shortControlCharEscapes))
end

local function isIdentifier(str)
  return type(str) == 'string' and str:match( "^[_%a][_%a%d]*$" )
end

local function isSequenceKey(k, sequenceLength)
  return type(k) == 'number'
     and 1 <= k
     and k <= sequenceLength
     and math.floor(k) == k
end

local defaultTypeOrders = {
  ['number']   = 1, ['boolean']  = 2, ['string'] = 3, ['table'] = 4,
  ['function'] = 5, ['userdata'] = 6, ['thread'] = 7
}

local function sortKeys(a, b)
  local ta, tb = type(a), type(b)

  -- strings and numbers are sorted numerically/alphabetically
  if ta == tb and (ta == 'string' or ta == 'number') then return a < b end

  local dta, dtb = defaultTypeOrders[ta], defaultTypeOrders[tb]
  -- Two default types are compared according to the defaultTypeOrders table
  if dta and dtb then return defaultTypeOrders[ta] < defaultTypeOrders[tb]
  elseif dta     then return true  -- default types before custom ones
  elseif dtb     then return false -- custom types after default ones
  end

  -- custom types are sorted out alphabetically
  return ta < tb
end

-- For implementation reasons, the behavior of rawlen & # is "undefined" when
-- tables aren't pure sequences. So we implement our own # operator.
local function getSequenceLength(t)
  local len = 1
  local v = rawget(t,len)
  while v ~= nil do
    len = len + 1
    v = rawget(t,len)
  end
  return len - 1
end

local function getNonSequentialKeys(t)
  local keys, keysLength = {}, 0
  local sequenceLength = getSequenceLength(t)
  for k,_ in rawpairs(t) do
    if not isSequenceKey(k, sequenceLength) then
      keysLength = keysLength + 1
      keys[keysLength] = k
    end
  end
  table.sort(keys, sortKeys)
  return keys, keysLength, sequenceLength
end

local function countTableAppearances(t, tableAppearances)
  tableAppearances = tableAppearances or {}

  if type(t) == 'table' then
    if not tableAppearances[t] then
      tableAppearances[t] = 1
      for k,v in rawpairs(t) do
        countTableAppearances(k, tableAppearances)
        countTableAppearances(v, tableAppearances)
      end
      countTableAppearances(getmetatable(t), tableAppearances)
    else
      tableAppearances[t] = tableAppearances[t] + 1
    end
  end

  return tableAppearances
end

local copySequence = function(s)
  local copy, len = {}, #s
  for i=1, len do copy[i] = s[i] end
  return copy, len
end

local function makePath(path, ...)
  local keys = {...}
  local newPath, len = copySequence(path)
  for i=1, #keys do
    newPath[len + i] = keys[i]
  end
  return newPath
end

local function processRecursive(process, item, path, visited)
  if item == nil then return nil end
  if visited[item] then return visited[item] end

  local processed = process(item, path)
  if type(processed) == 'table' then
    local processedCopy = {}
    visited[item] = processedCopy
    local processedKey

    for k,v in rawpairs(processed) do
      processedKey = processRecursive(process, k, makePath(path, k, inspect.KEY), visited)
      if processedKey ~= nil then
        processedCopy[processedKey] = processRecursive(process, v, makePath(path, processedKey), visited)
      end
    end

    local mt  = processRecursive(process, getmetatable(processed), makePath(path, inspect.METATABLE), visited)
    if type(mt) ~= 'table' then mt = nil end -- ignore not nil/table __metatable field
    setmetatable(processedCopy, mt)
    processed = processedCopy
  end
  return processed
end



-------------------------------------------------------------------

local Inspector = {}
local Inspector_mt = {__index = Inspector}

function Inspector:puts(...)
  local args   = {...}
  local buffer = self.buffer
  local len    = #buffer
  for i=1, #args do
    len = len + 1
    buffer[len] = args[i]
  end
end

function Inspector:down(f)
  self.level = self.level + 1
  f()
  self.level = self.level - 1
end

function Inspector:tabify()
  self:puts(self.newline, string.rep(self.indent, self.level))
end

function Inspector:alreadyVisited(v)
  return self.ids[v] ~= nil
end

function Inspector:getId(v)
  local id = self.ids[v]
  if not id then
    local tv = type(v)
    id              = (self.maxIds[tv] or 0) + 1
    self.maxIds[tv] = id
    self.ids[v]     = id
  end
  return tostring(id)
end

function Inspector:putKey(k)
  if isIdentifier(k) then return self:puts(k) end
  self:puts("[")
  self:putValue(k)
  self:puts("]")
end

function Inspector:putTable(t)
  if t == inspect.KEY or t == inspect.METATABLE then
    self:puts(tostring(t))
  elseif self:alreadyVisited(t) then
    self:puts('<table ', self:getId(t), '>')
  elseif self.level >= self.depth then
    self:puts('{...}')
  else
    if self.tableAppearances[t] > 1 then self:puts('<', self:getId(t), '>') end

    local nonSequentialKeys, nonSequentialKeysLength, sequenceLength = getNonSequentialKeys(t)
    local mt                = getmetatable(t)

    self:puts('{')
    self:down(function()
      local count = 0
      for i=1, sequenceLength do
        if count > 0 then self:puts(',') end
        self:puts(' ')
        self:putValue(t[i])
        count = count + 1
      end

      for i=1, nonSequentialKeysLength do
        local k = nonSequentialKeys[i]
        if count > 0 then self:puts(',') end
        self:tabify()
        self:putKey(k)
        self:puts(' = ')
        self:putValue(t[k])
        count = count + 1
      end

      if type(mt) == 'table' then
        if count > 0 then self:puts(',') end
        self:tabify()
        self:puts('<metatable> = ')
        self:putValue(mt)
      end
    end)

    if nonSequentialKeysLength > 0 or type(mt) == 'table' then -- result is multi-lined. Justify closing }
      self:tabify()
    elseif sequenceLength > 0 then -- array tables have one extra space before closing }
      self:puts(' ')
    end

    self:puts('}')
  end
end

function Inspector:putValue(v)
  local tv = type(v)

  if tv == 'string' then
    self:puts(smartQuote(escape(v)))
  elseif tv == 'number' or tv == 'boolean' or tv == 'nil' or
         tv == 'cdata' or tv == 'ctype' then
    self:puts(tostring(v))
  elseif tv == 'table' then
    self:putTable(v)
  else
    self:puts('<', tv, ' ', self:getId(v), '>')
  end
end

-------------------------------------------------------------------

function inspect.inspect(root, options)
  options       = options or {}

  local depth   = options.depth   or math.huge
  local newline = options.newline or '\n'
  local indent  = options.indent  or '  '
  local process = options.process

  if process then
    root = processRecursive(process, root, {}, {})
  end

  local inspector = setmetatable({
    depth            = depth,
    level            = 0,
    buffer           = {},
    ids              = {},
    maxIds           = {},
    newline          = newline,
    indent           = indent,
    tableAppearances = countTableAppearances(root)
  }, Inspector_mt)

  inspector:putValue(root)

  return table.concat(inspector.buffer)
end

setmetatable(inspect, { __call = function(_, ...) return inspect.inspect(...) end })

return inspect.inspect
