Upgrade to Fennel 0.3.0.
This commit is contained in:
parent
ed9856df13
commit
7772d40b2e
720
lib/fennel.lua
720
lib/fennel.lua
File diff suppressed because it is too large
Load diff
|
@ -2,7 +2,7 @@ local function view_quote(str)
|
|||
return ("\"" .. str:gsub("\"", "\\\"") .. "\"")
|
||||
end
|
||||
local short_control_char_escapes = {["\11"] = "\\v", ["\12"] = "\\f", ["\13"] = "\\r", ["\7"] = "\\a", ["\8"] = "\\b", ["\9"] = "\\t", ["\n"] = "\\n"}
|
||||
local long_control_char_esapes
|
||||
local long_control_char_escapes = nil
|
||||
do
|
||||
local long = {}
|
||||
for i = 0, 31 do
|
||||
|
@ -12,11 +12,11 @@ do
|
|||
long[ch] = ("\\%03d"):format(i)
|
||||
end
|
||||
end
|
||||
long_control_char_esapes = long
|
||||
long_control_char_escapes = long
|
||||
end
|
||||
local function escape(str)
|
||||
local str = str:gsub("\\", "\\\\")
|
||||
local str = str:gsub("(%c)%f[0-9]", long_control_char_esapes)
|
||||
local str = str:gsub("(%c)%f[0-9]", long_control_char_escapes)
|
||||
return str:gsub("%c", short_control_char_escapes)
|
||||
end
|
||||
local function sequence_key_3f(k, len)
|
||||
|
@ -84,7 +84,7 @@ local function puts(self, ...)
|
|||
return nil
|
||||
end
|
||||
local function tabify(self)
|
||||
return puts(self, "\n", self.indent:rep(self.level))
|
||||
return puts(self, "\n", (self.indent):rep(self.level))
|
||||
end
|
||||
local function already_visited_3f(self, v)
|
||||
return (self.ids[v] ~= nil)
|
||||
|
@ -99,10 +99,10 @@ local function get_id(self, v)
|
|||
end
|
||||
return tostring(id)
|
||||
end
|
||||
local function put_sequential_table(self, t, length)
|
||||
local function put_sequential_table(self, t, len)
|
||||
puts(self, "[")
|
||||
self.level = (self.level + 1)
|
||||
for i = 1, length do
|
||||
for i = 1, len do
|
||||
puts(self, " ")
|
||||
put_value(self, t[i])
|
||||
end
|
||||
|
@ -110,19 +110,25 @@ local function put_sequential_table(self, t, length)
|
|||
return puts(self, " ]")
|
||||
end
|
||||
local function put_key(self, k)
|
||||
if ((type(k) == "string") and k:find("^[-%w?\\^_`!#$%&*+./@~:|<=>]+$")) then
|
||||
if ((type(k) == "string") and k:find("^[-%w?\\^_!$%&*+./@:|<=>]+$")) then
|
||||
return puts(self, ":", k)
|
||||
else
|
||||
return put_value(self, k)
|
||||
end
|
||||
end
|
||||
local function put_kv_table(self, t)
|
||||
local function put_kv_table(self, t, ordered_keys)
|
||||
puts(self, "{")
|
||||
self.level = (self.level + 1)
|
||||
for k, v in pairs(t) do
|
||||
for _, k in ipairs(ordered_keys) do
|
||||
tabify(self)
|
||||
put_key(self, k)
|
||||
puts(self, " ")
|
||||
put_value(self, t[k])
|
||||
end
|
||||
for i, v in ipairs(t) do
|
||||
tabify(self)
|
||||
put_key(self, i)
|
||||
puts(self, " ")
|
||||
put_value(self, v)
|
||||
end
|
||||
self.level = (self.level - 1)
|
||||
|
@ -130,21 +136,21 @@ local function put_kv_table(self, t)
|
|||
return puts(self, "}")
|
||||
end
|
||||
local function put_table(self, t)
|
||||
if already_visited_3f(self, t) then
|
||||
if (already_visited_3f(self, t) and self["detect-cycles?"]) then
|
||||
return puts(self, "#<table ", get_id(self, t), ">")
|
||||
elseif (self.level >= self.depth) then
|
||||
return puts(self, "{...}")
|
||||
elseif "else" then
|
||||
local non_seq_keys, length = get_nonsequential_keys(t)
|
||||
local non_seq_keys, len = get_nonsequential_keys(t)
|
||||
local id = get_id(self, t)
|
||||
if (self.appearances[t] > 1) then
|
||||
return puts(self, "#<", id, ">")
|
||||
if ((1 < (self.appearances[t] or 0)) and self["detect-cycles?"]) then
|
||||
return puts(self, "#<table", id, ">")
|
||||
elseif ((#non_seq_keys == 0) and (#t == 0)) then
|
||||
return puts(self, "{}")
|
||||
elseif (#non_seq_keys == 0) then
|
||||
return put_sequential_table(self, t, length)
|
||||
return put_sequential_table(self, t, len)
|
||||
elseif "else" then
|
||||
return put_kv_table(self, t)
|
||||
return put_kv_table(self, t, non_seq_keys)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -161,10 +167,29 @@ local function _0_(self, v)
|
|||
end
|
||||
end
|
||||
put_value = _0_
|
||||
local function fennelview(root, options)
|
||||
local function one_line(str)
|
||||
local ret = str:gsub("\n", " "):gsub("%[ ", "["):gsub(" %]", "]"):gsub("%{ ", "{"):gsub(" %}", "}"):gsub("%( ", "("):gsub(" %)", ")")
|
||||
return ret
|
||||
end
|
||||
local function fennelview(x, options)
|
||||
local options = (options or {})
|
||||
local inspector = {["max-ids"] = {}, appearances = count_table_appearances(root, {}), buffer = {}, depth = (options.depth or 128), ids = {}, indent = (options.indent or " "), level = 0}
|
||||
put_value(inspector, root)
|
||||
return table.concat(inspector.buffer)
|
||||
local inspector = inspector
|
||||
local function _1_()
|
||||
if options["one-line"] then
|
||||
return ""
|
||||
else
|
||||
return " "
|
||||
end
|
||||
end
|
||||
inspector = {["detect-cycles?"] = not (false == options["detect-cycles?"]), ["max-ids"] = {}, appearances = count_table_appearances(x, {}), buffer = {}, depth = (options.depth or 128), ids = {}, indent = (options.indent or _1_()), level = 0}
|
||||
put_value(inspector, x)
|
||||
do
|
||||
local str = table.concat(inspector.buffer)
|
||||
if options["one-line"] then
|
||||
return one_line(str)
|
||||
else
|
||||
return str
|
||||
end
|
||||
end
|
||||
end
|
||||
return fennelview
|
||||
|
|
Loading…
Reference in a new issue