Upgrade to Fennel 0.3.0.

This commit is contained in:
Phil Hagelberg 2019-09-22 15:35:39 -07:00
parent ed9856df13
commit 7772d40b2e
2 changed files with 664 additions and 145 deletions

File diff suppressed because it is too large Load diff

View file

@ -2,7 +2,7 @@ local function view_quote(str)
return ("\"" .. str:gsub("\"", "\\\"") .. "\"") return ("\"" .. str:gsub("\"", "\\\"") .. "\"")
end end
local short_control_char_escapes = {["\11"] = "\\v", ["\12"] = "\\f", ["\13"] = "\\r", ["\7"] = "\\a", ["\8"] = "\\b", ["\9"] = "\\t", ["\n"] = "\\n"} 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 do
local long = {} local long = {}
for i = 0, 31 do for i = 0, 31 do
@ -12,11 +12,11 @@ do
long[ch] = ("\\%03d"):format(i) long[ch] = ("\\%03d"):format(i)
end end
end end
long_control_char_esapes = long long_control_char_escapes = long
end end
local function escape(str) local function escape(str)
local str = str:gsub("\\", "\\\\") 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) return str:gsub("%c", short_control_char_escapes)
end end
local function sequence_key_3f(k, len) local function sequence_key_3f(k, len)
@ -84,7 +84,7 @@ local function puts(self, ...)
return nil return nil
end end
local function tabify(self) local function tabify(self)
return puts(self, "\n", self.indent:rep(self.level)) return puts(self, "\n", (self.indent):rep(self.level))
end end
local function already_visited_3f(self, v) local function already_visited_3f(self, v)
return (self.ids[v] ~= nil) return (self.ids[v] ~= nil)
@ -99,10 +99,10 @@ local function get_id(self, v)
end end
return tostring(id) return tostring(id)
end end
local function put_sequential_table(self, t, length) local function put_sequential_table(self, t, len)
puts(self, "[") puts(self, "[")
self.level = (self.level + 1) self.level = (self.level + 1)
for i = 1, length do for i = 1, len do
puts(self, " ") puts(self, " ")
put_value(self, t[i]) put_value(self, t[i])
end end
@ -110,19 +110,25 @@ local function put_sequential_table(self, t, length)
return puts(self, " ]") return puts(self, " ]")
end end
local function put_key(self, k) 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) return puts(self, ":", k)
else else
return put_value(self, k) return put_value(self, k)
end end
end end
local function put_kv_table(self, t) local function put_kv_table(self, t, ordered_keys)
puts(self, "{") puts(self, "{")
self.level = (self.level + 1) self.level = (self.level + 1)
for k, v in pairs(t) do for _, k in ipairs(ordered_keys) do
tabify(self) tabify(self)
put_key(self, k) put_key(self, k)
puts(self, " ") 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) put_value(self, v)
end end
self.level = (self.level - 1) self.level = (self.level - 1)
@ -130,21 +136,21 @@ local function put_kv_table(self, t)
return puts(self, "}") return puts(self, "}")
end end
local function put_table(self, t) 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), ">") return puts(self, "#<table ", get_id(self, t), ">")
elseif (self.level >= self.depth) then elseif (self.level >= self.depth) then
return puts(self, "{...}") return puts(self, "{...}")
elseif "else" then 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) local id = get_id(self, t)
if (self.appearances[t] > 1) then if ((1 < (self.appearances[t] or 0)) and self["detect-cycles?"]) then
return puts(self, "#<", id, ">") return puts(self, "#<table", id, ">")
elseif ((#non_seq_keys == 0) and (#t == 0)) then elseif ((#non_seq_keys == 0) and (#t == 0)) then
return puts(self, "{}") return puts(self, "{}")
elseif (#non_seq_keys == 0) then elseif (#non_seq_keys == 0) then
return put_sequential_table(self, t, length) return put_sequential_table(self, t, len)
elseif "else" then elseif "else" then
return put_kv_table(self, t) return put_kv_table(self, t, non_seq_keys)
end end
end end
end end
@ -161,10 +167,29 @@ local function _0_(self, v)
end end
end end
put_value = _0_ 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 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} local inspector = inspector
put_value(inspector, root) local function _1_()
return table.concat(inspector.buffer) 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 end
return fennelview return fennelview