2019-04-20 17:38:53 -07:00
|
|
|
local function view_quote(str)
|
2018-10-17 18:32:39 -07:00
|
|
|
return ("\"" .. str:gsub("\"", "\\\"") .. "\"")
|
|
|
|
end
|
2019-04-20 17:38:53 -07:00
|
|
|
local short_control_char_escapes = {["\11"] = "\\v", ["\12"] = "\\f", ["\13"] = "\\r", ["\7"] = "\\a", ["\8"] = "\\b", ["\9"] = "\\t", ["\n"] = "\\n"}
|
2019-09-22 15:35:39 -07:00
|
|
|
local long_control_char_escapes = nil
|
2019-04-20 17:38:53 -07:00
|
|
|
do
|
|
|
|
local long = {}
|
2018-10-17 18:32:39 -07:00
|
|
|
for i = 0, 31 do
|
|
|
|
local ch = string.char(i)
|
2019-04-20 17:38:53 -07:00
|
|
|
if not short_control_char_escapes[ch] then
|
|
|
|
short_control_char_escapes[ch] = ("\\" .. i)
|
|
|
|
long[ch] = ("\\%03d"):format(i)
|
2018-10-17 18:32:39 -07:00
|
|
|
end
|
|
|
|
end
|
2019-09-22 15:35:39 -07:00
|
|
|
long_control_char_escapes = long
|
2018-10-17 18:32:39 -07:00
|
|
|
end
|
2019-04-20 17:38:53 -07:00
|
|
|
local function escape(str)
|
2020-09-18 12:13:53 -07:00
|
|
|
return str:gsub("\\", "\\\\"):gsub("(%c)%f[0-9]", long_control_char_escapes):gsub("%c", short_control_char_escapes)
|
2018-10-17 18:32:39 -07:00
|
|
|
end
|
2019-04-20 17:38:53 -07:00
|
|
|
local function sequence_key_3f(k, len)
|
|
|
|
return ((type(k) == "number") and (1 <= k) and (k <= len) and (math.floor(k) == k))
|
2018-10-17 18:32:39 -07:00
|
|
|
end
|
2019-04-20 17:38:53 -07:00
|
|
|
local type_order = {["function"] = 5, boolean = 2, number = 1, string = 3, table = 4, thread = 7, userdata = 6}
|
2020-12-23 10:33:09 -08:00
|
|
|
local function sort_keys(_0_0, _1_0)
|
|
|
|
local _1_ = _0_0
|
|
|
|
local a = _1_[1]
|
|
|
|
local _2_ = _1_0
|
|
|
|
local b = _2_[1]
|
2018-10-17 18:32:39 -07:00
|
|
|
local ta = type(a)
|
|
|
|
local tb = type(b)
|
2020-12-23 10:33:09 -08:00
|
|
|
if ((ta == tb) and ((ta == "string") or (ta == "number"))) then
|
2019-04-20 17:38:53 -07:00
|
|
|
return (a < b)
|
2018-10-17 18:32:39 -07:00
|
|
|
else
|
2020-12-23 10:33:09 -08:00
|
|
|
local dta = type_order[ta]
|
|
|
|
local dtb = type_order[tb]
|
2018-10-17 18:32:39 -07:00
|
|
|
if (dta and dtb) then
|
2019-04-20 17:38:53 -07:00
|
|
|
return (dta < dtb)
|
2018-10-17 18:32:39 -07:00
|
|
|
elseif dta then
|
|
|
|
return true
|
|
|
|
elseif dtb then
|
|
|
|
return false
|
|
|
|
elseif "else" then
|
2019-04-20 17:38:53 -07:00
|
|
|
return (ta < tb)
|
2018-10-17 18:32:39 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-04-20 17:38:53 -07:00
|
|
|
local function get_sequence_length(t)
|
2020-12-23 10:33:09 -08:00
|
|
|
local len = 0
|
2018-10-17 18:32:39 -07:00
|
|
|
for i in ipairs(t) do
|
|
|
|
len = i
|
|
|
|
end
|
|
|
|
return len
|
|
|
|
end
|
2019-04-20 17:38:53 -07:00
|
|
|
local function get_nonsequential_keys(t)
|
|
|
|
local keys = {}
|
2018-10-17 18:32:39 -07:00
|
|
|
local sequence_length = get_sequence_length(t)
|
2020-12-23 10:33:09 -08:00
|
|
|
for k, v in pairs(t) do
|
2019-04-20 17:38:53 -07:00
|
|
|
if not sequence_key_3f(k, sequence_length) then
|
2020-12-23 10:33:09 -08:00
|
|
|
table.insert(keys, {k, v})
|
2018-10-17 18:32:39 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
table.sort(keys, sort_keys)
|
|
|
|
return keys, sequence_length
|
|
|
|
end
|
2019-04-20 17:38:53 -07:00
|
|
|
local function count_table_appearances(t, appearances)
|
|
|
|
if (type(t) == "table") then
|
|
|
|
if not appearances[t] then
|
|
|
|
appearances[t] = 1
|
|
|
|
for k, v in pairs(t) do
|
|
|
|
count_table_appearances(k, appearances)
|
|
|
|
count_table_appearances(v, appearances)
|
2018-10-17 18:32:39 -07:00
|
|
|
end
|
2020-12-23 10:33:09 -08:00
|
|
|
else
|
2019-04-20 17:38:53 -07:00
|
|
|
appearances[t] = ((appearances[t] or 0) + 1)
|
|
|
|
end
|
2018-10-17 18:32:39 -07:00
|
|
|
end
|
|
|
|
return appearances
|
|
|
|
end
|
|
|
|
local put_value = nil
|
2019-04-20 17:38:53 -07:00
|
|
|
local function puts(self, ...)
|
|
|
|
for _, v in ipairs({...}) do
|
2018-10-17 18:32:39 -07:00
|
|
|
table.insert(self.buffer, v)
|
|
|
|
end
|
|
|
|
return nil
|
|
|
|
end
|
2019-04-20 17:38:53 -07:00
|
|
|
local function tabify(self)
|
2019-09-22 15:35:39 -07:00
|
|
|
return puts(self, "\n", (self.indent):rep(self.level))
|
2018-10-17 18:32:39 -07:00
|
|
|
end
|
2019-04-20 17:38:53 -07:00
|
|
|
local function already_visited_3f(self, v)
|
|
|
|
return (self.ids[v] ~= nil)
|
2018-10-17 18:32:39 -07:00
|
|
|
end
|
2019-04-20 17:38:53 -07:00
|
|
|
local function get_id(self, v)
|
2018-10-17 18:32:39 -07:00
|
|
|
local id = self.ids[v]
|
2019-04-20 17:38:53 -07:00
|
|
|
if not id then
|
|
|
|
local tv = type(v)
|
|
|
|
id = ((self["max-ids"][tv] or 0) + 1)
|
|
|
|
self["max-ids"][tv] = id
|
|
|
|
self.ids[v] = id
|
2018-10-17 18:32:39 -07:00
|
|
|
end
|
|
|
|
return tostring(id)
|
|
|
|
end
|
2019-09-22 15:35:39 -07:00
|
|
|
local function put_sequential_table(self, t, len)
|
2018-10-17 18:32:39 -07:00
|
|
|
puts(self, "[")
|
|
|
|
self.level = (self.level + 1)
|
2020-12-23 10:33:09 -08:00
|
|
|
for k, v in ipairs(t) do
|
|
|
|
local _2_ = (1 + len)
|
|
|
|
if ((1 < k) and (k < _2_)) then
|
2020-03-07 17:41:37 -08:00
|
|
|
puts(self, " ")
|
|
|
|
end
|
2020-12-23 10:33:09 -08:00
|
|
|
put_value(self, v)
|
2018-10-17 18:32:39 -07:00
|
|
|
end
|
|
|
|
self.level = (self.level - 1)
|
2020-03-07 17:41:37 -08:00
|
|
|
return puts(self, "]")
|
2018-10-17 18:32:39 -07:00
|
|
|
end
|
2019-04-20 17:38:53 -07:00
|
|
|
local function put_key(self, k)
|
2019-09-22 15:35:39 -07:00
|
|
|
if ((type(k) == "string") and k:find("^[-%w?\\^_!$%&*+./@:|<=>]+$")) then
|
2018-10-17 18:32:39 -07:00
|
|
|
return puts(self, ":", k)
|
|
|
|
else
|
|
|
|
return put_value(self, k)
|
|
|
|
end
|
|
|
|
end
|
2019-09-22 15:35:39 -07:00
|
|
|
local function put_kv_table(self, t, ordered_keys)
|
2018-10-17 18:32:39 -07:00
|
|
|
puts(self, "{")
|
|
|
|
self.level = (self.level + 1)
|
2020-12-23 10:33:09 -08:00
|
|
|
for i, _2_0 in ipairs(ordered_keys) do
|
|
|
|
local _3_ = _2_0
|
|
|
|
local k = _3_[1]
|
|
|
|
local v = _3_[2]
|
2020-09-18 12:13:53 -07:00
|
|
|
if (self["table-edges"] or (i ~= 1)) then
|
|
|
|
tabify(self)
|
|
|
|
end
|
2018-10-17 18:32:39 -07:00
|
|
|
put_key(self, k)
|
|
|
|
puts(self, " ")
|
2020-12-23 10:33:09 -08:00
|
|
|
put_value(self, v)
|
2019-09-22 15:35:39 -07:00
|
|
|
end
|
|
|
|
for i, v in ipairs(t) do
|
|
|
|
tabify(self)
|
|
|
|
put_key(self, i)
|
|
|
|
puts(self, " ")
|
2018-10-17 18:32:39 -07:00
|
|
|
put_value(self, v)
|
|
|
|
end
|
|
|
|
self.level = (self.level - 1)
|
2020-09-18 12:13:53 -07:00
|
|
|
if self["table-edges"] then
|
|
|
|
tabify(self)
|
|
|
|
end
|
2018-10-17 18:32:39 -07:00
|
|
|
return puts(self, "}")
|
|
|
|
end
|
2019-04-20 17:38:53 -07:00
|
|
|
local function put_table(self, t)
|
2020-03-07 17:41:37 -08:00
|
|
|
local metamethod = nil
|
2020-12-23 10:33:09 -08:00
|
|
|
local function _3_()
|
|
|
|
local _2_0 = t
|
|
|
|
if _2_0 then
|
|
|
|
local _4_0 = getmetatable(_2_0)
|
|
|
|
if _4_0 then
|
|
|
|
return _4_0.__fennelview
|
2020-03-07 17:41:37 -08:00
|
|
|
else
|
2020-12-23 10:33:09 -08:00
|
|
|
return _4_0
|
2020-03-07 17:41:37 -08:00
|
|
|
end
|
|
|
|
else
|
2020-12-23 10:33:09 -08:00
|
|
|
return _2_0
|
2020-03-07 17:41:37 -08:00
|
|
|
end
|
|
|
|
end
|
2020-12-23 10:33:09 -08:00
|
|
|
metamethod = (self["metamethod?"] and _3_())
|
2019-09-22 15:35:39 -07:00
|
|
|
if (already_visited_3f(self, t) and self["detect-cycles?"]) then
|
2020-12-23 10:33:09 -08:00
|
|
|
return puts(self, "#<table @", get_id(self, t), ">")
|
2019-04-20 17:38:53 -07:00
|
|
|
elseif (self.level >= self.depth) then
|
2018-10-17 18:32:39 -07:00
|
|
|
return puts(self, "{...}")
|
2020-03-07 17:41:37 -08:00
|
|
|
elseif metamethod then
|
|
|
|
return puts(self, metamethod(t, self.fennelview))
|
2018-10-17 18:32:39 -07:00
|
|
|
elseif "else" then
|
2019-09-22 15:35:39 -07:00
|
|
|
local non_seq_keys, len = get_nonsequential_keys(t)
|
2018-10-17 18:32:39 -07:00
|
|
|
local id = get_id(self, t)
|
2019-09-22 15:35:39 -07:00
|
|
|
if ((1 < (self.appearances[t] or 0)) and self["detect-cycles?"]) then
|
2020-12-23 10:33:09 -08:00
|
|
|
puts(self, "@", id)
|
|
|
|
end
|
|
|
|
if ((#non_seq_keys == 0) and (#t == 0)) then
|
|
|
|
local function _5_()
|
2020-09-18 12:13:53 -07:00
|
|
|
if self["empty-as-square"] then
|
|
|
|
return "[]"
|
|
|
|
else
|
|
|
|
return "{}"
|
|
|
|
end
|
|
|
|
end
|
2020-12-23 10:33:09 -08:00
|
|
|
return puts(self, _5_())
|
2019-04-20 17:38:53 -07:00
|
|
|
elseif (#non_seq_keys == 0) then
|
2019-09-22 15:35:39 -07:00
|
|
|
return put_sequential_table(self, t, len)
|
2018-10-17 18:32:39 -07:00
|
|
|
elseif "else" then
|
2019-09-22 15:35:39 -07:00
|
|
|
return put_kv_table(self, t, non_seq_keys)
|
2018-10-17 18:32:39 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-12-23 10:33:09 -08:00
|
|
|
local function _2_(self, v)
|
2018-10-17 18:32:39 -07:00
|
|
|
local tv = type(v)
|
2019-04-20 17:38:53 -07:00
|
|
|
if (tv == "string") then
|
|
|
|
return puts(self, view_quote(escape(v)))
|
|
|
|
elseif ((tv == "number") or (tv == "boolean") or (tv == "nil")) then
|
2018-10-17 18:32:39 -07:00
|
|
|
return puts(self, tostring(v))
|
2020-12-23 10:33:09 -08:00
|
|
|
else
|
|
|
|
local _4_
|
|
|
|
do
|
|
|
|
local _3_0 = getmetatable(v)
|
|
|
|
if _3_0 then
|
|
|
|
_4_ = _3_0.__fennelview
|
|
|
|
else
|
|
|
|
_4_ = _3_0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if ((tv == "table") or ((tv == "userdata") and (nil ~= _4_))) then
|
|
|
|
return put_table(self, v)
|
|
|
|
elseif "else" then
|
|
|
|
return puts(self, "#<", tostring(v), ">")
|
|
|
|
end
|
2018-10-17 18:32:39 -07:00
|
|
|
end
|
|
|
|
end
|
2020-12-23 10:33:09 -08:00
|
|
|
put_value = _2_
|
2019-09-22 15:35:39 -07:00
|
|
|
local function one_line(str)
|
|
|
|
local ret = str:gsub("\n", " "):gsub("%[ ", "["):gsub(" %]", "]"):gsub("%{ ", "{"):gsub(" %}", "}"):gsub("%( ", "("):gsub(" %)", ")")
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
local function fennelview(x, options)
|
2020-03-07 17:41:37 -08:00
|
|
|
local options0 = (options or {})
|
|
|
|
local inspector = nil
|
2020-12-23 10:33:09 -08:00
|
|
|
local function _3_(_241)
|
2020-03-07 17:41:37 -08:00
|
|
|
return fennelview(_241, options0)
|
|
|
|
end
|
2020-12-23 10:33:09 -08:00
|
|
|
local function _4_()
|
2020-03-07 17:41:37 -08:00
|
|
|
if options0["one-line"] then
|
2019-09-22 15:35:39 -07:00
|
|
|
return ""
|
|
|
|
else
|
|
|
|
return " "
|
|
|
|
end
|
|
|
|
end
|
2020-12-23 10:33:09 -08:00
|
|
|
inspector = {["detect-cycles?"] = not (false == options0["detect-cycles?"]), ["empty-as-square"] = options0["empty-as-square"], ["max-ids"] = {}, ["metamethod?"] = not (false == options0["metamethod?"]), ["table-edges"] = (options0["table-edges"] ~= false), appearances = count_table_appearances(x, {}), buffer = {}, depth = (options0.depth or 128), fennelview = _3_, ids = {}, indent = (options0.indent or _4_()), level = 0}
|
2019-09-22 15:35:39 -07:00
|
|
|
put_value(inspector, x)
|
2020-09-18 12:13:53 -07:00
|
|
|
local str = table.concat(inspector.buffer)
|
|
|
|
if options0["one-line"] then
|
|
|
|
return one_line(str)
|
|
|
|
else
|
|
|
|
return str
|
2019-09-22 15:35:39 -07:00
|
|
|
end
|
2018-10-17 18:32:39 -07:00
|
|
|
end
|
2019-04-20 17:38:53 -07:00
|
|
|
return fennelview
|