summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lualibs-lpeg.lua2
-rw-r--r--lualibs-lua.lua34
-rw-r--r--lualibs-string.lua7
-rw-r--r--lualibs-table.lua2
-rw-r--r--lualibs-util-sto.lua21
-rw-r--r--lualibs-util-str.lua13
-rw-r--r--lualibs-util-tab.lua67
7 files changed, 107 insertions, 39 deletions
diff --git a/lualibs-lpeg.lua b/lualibs-lpeg.lua
index 55a0d89..5be1246 100644
--- a/lualibs-lpeg.lua
+++ b/lualibs-lpeg.lua
@@ -82,7 +82,7 @@ local lpegtype, lpegmatch, lpegprint = lpeg.type, lpeg.match, lpeg.print
-- let's start with an inspector:
if setinspector then
- setinspector(function(v) if lpegtype(v) then lpegprint(v) return true end end)
+ setinspector("lpeg",function(v) if lpegtype(v) then lpegprint(v) return true end end)
end
-- Beware, we predefine a bunch of patterns here and one reason for doing so
diff --git a/lualibs-lua.lua b/lualibs-lua.lua
index 1a2a987..cb61829 100644
--- a/lualibs-lua.lua
+++ b/lualibs-lua.lua
@@ -129,22 +129,36 @@ local print, select, tostring = print, select, tostring
local inspectors = { }
-function setinspector(inspector) -- global function
- inspectors[#inspectors+1] = inspector
+function setinspector(kind,inspector) -- global function
+ inspectors[kind] = inspector
end
function inspect(...) -- global function
for s=1,select("#",...) do
local value = select(s,...)
- local done = false
- for i=1,#inspectors do
- done = inspectors[i](value)
- if done then
- break
+ if value == nil then
+ print("nil")
+ else
+ local done = false
+ -- type driven (table)
+ local kind = type(value)
+ local inspector = inspectors[kind]
+ if inspector then
+ done = inspector(value)
+ if done then
+ break
+ end
+ end
+ -- whatever driven (token, node, ...)
+ for kind, inspector in next, inspectors do
+ done = inspector(value)
+ if done then
+ break
+ end
+ end
+ if not done then
+ print(tostring(value))
end
- end
- if not done then
- print(tostring(value))
end
end
end
diff --git a/lualibs-string.lua b/lualibs-string.lua
index 70c66f6..e9dc2bb 100644
--- a/lualibs-string.lua
+++ b/lualibs-string.lua
@@ -192,10 +192,11 @@ string.itself = function(s) return s end
-- also handy (see utf variant)
-local pattern = Ct(C(1)^0) -- string and not utf !
+local pattern_c = Ct( C(1) ^0) -- string and not utf !
+local pattern_b = Ct((C(1)/byte)^0)
-function string.totable(str)
- return lpegmatch(pattern,str)
+function string.totable(str,bytes)
+ return lpegmatch(bytes and pattern_b or pattern_c,str)
end
-- handy from within tex:
diff --git a/lualibs-table.lua b/lualibs-table.lua
index b02f210..552097e 100644
--- a/lualibs-table.lua
+++ b/lualibs-table.lua
@@ -1144,7 +1144,7 @@ function table.print(t,...)
end
if setinspector then
- setinspector(function(v) if type(v) == "table" then serialize(print,v,"table") return true end end)
+ setinspector("table",function(v) if type(v) == "table" then serialize(print,v,"table") return true end end)
end
-- -- -- obsolete but we keep them for a while and might comment them later -- -- --
diff --git a/lualibs-util-sto.lua b/lualibs-util-sto.lua
index 8aafca4..d21267d 100644
--- a/lualibs-util-sto.lua
+++ b/lualibs-util-sto.lua
@@ -113,22 +113,16 @@ local f_index = {
["number"] = f_number,
}
-local t_index = {
- ["empty"] = { __index = f_empty },
- ["self"] = { __index = f_self },
- ["table"] = { __index = f_table },
- ["number"] = { __index = f_number },
-}
-
function table.setmetatableindex(t,f)
if type(t) ~= "table" then
f, t = t, { }
end
local m = getmetatable(t)
+ local i = f_index[f] or f
if m then
- m.__index = f_index[f] or f
+ m.__index = i
else
- setmetatable(t,t_index[f] or { __index = f })
+ setmetatable(t,{ __index = i })
end
return t
end
@@ -137,19 +131,16 @@ local f_index = {
["ignore"] = f_ignore,
}
-local t_index = {
- ["ignore"] = { __newindex = f_ignore },
-}
-
function table.setmetatablenewindex(t,f)
if type(t) ~= "table" then
f, t = t, { }
end
local m = getmetatable(t)
+ local i = f_index[f] or f
if m then
- m.__newindex = f_index[f] or f
+ m.__newindex = i
else
- setmetatable(t,t_index[f] or { __newindex = f })
+ setmetatable(t,{ __newindex = i })
end
return t
end
diff --git a/lualibs-util-str.lua b/lualibs-util-str.lua
index c2139b1..95534c8 100644
--- a/lualibs-util-str.lua
+++ b/lualibs-util-str.lua
@@ -6,7 +6,7 @@ if not modules then modules = { } end modules ['util-str'] = {
license = "see context related readme files"
}
-utilities = utilities or {}
+utilities = utilities or { }
utilities.strings = utilities.strings or { }
local strings = utilities.strings
@@ -354,7 +354,16 @@ function string.autosingle(s,sep)
return ("'" .. tostring(s) .. "'")
end
-local tracedchars = { }
+local tracedchars = { [0] =
+ -- the regular bunch
+ "[null]", "[soh]", "[stx]", "[etx]", "[eot]", "[enq]", "[ack]", "[bel]",
+ "[bs]", "[ht]", "[lf]", "[vt]", "[ff]", "[cr]", "[so]", "[si]",
+ "[dle]", "[dc1]", "[dc2]", "[dc3]", "[dc4]", "[nak]", "[syn]", "[etb]",
+ "[can]", "[em]", "[sub]", "[esc]", "[fs]", "[gs]", "[rs]", "[us]",
+ -- plus space
+ "[space]", -- 0x20
+}
+
string.tracedchars = tracedchars
strings.tracers = tracedchars
diff --git a/lualibs-util-tab.lua b/lualibs-util-tab.lua
index 618f34c..d6f3d67 100644
--- a/lualibs-util-tab.lua
+++ b/lualibs-util-tab.lua
@@ -545,6 +545,7 @@ local f_val_str = formatters["%w%q,"]
local f_val_boo = formatters["%w%l,"]
local f_val_not = formatters["%w{},"]
local f_val_seq = formatters["%w{ %, t },"]
+local f_fin_seq = formatters[" %, t }"]
local f_table_return = formatters["return {"]
local f_table_name = formatters["%s={"]
@@ -556,20 +557,21 @@ local f_table_finish = formatters["}"]
local spaces = utilities.strings.newrepeater(" ")
-local serialize = table.serialize -- the extensive one, the one we started with
+local original_serialize = table.serialize -- the extensive one, the one we started with
-- there is still room for optimization: index run, key run, but i need to check with the
-- latest lua for the value of #n (with holes) .. anyway for tracing purposes we want
-- indices / keys being sorted, so it will never be real fast
-function table.serialize(root,name,specification)
+local function serialize(root,name,specification)
if type(specification) == "table" then
- return serialize(root,name,specification) -- the original one
+ return original_serialize(root,name,specification) -- the original one
end
- local t -- = { }
- local n = 1
+ local t -- = { }
+ local n = 1
+ local unknown = false
-- local function simple_table(t)
-- local ts = #t
@@ -614,6 +616,7 @@ function table.serialize(root,name,specification)
return nil
end
end
+ local haszero = t[0]
if n == nt then
local tt = { }
for i=1,nt do
@@ -630,6 +633,23 @@ function table.serialize(root,name,specification)
end
end
return tt
+ elseif haszero and (n == nt + 1) then
+ local tt = { }
+ for i=0,nt do
+ local v = t[i]
+ local tv = type(v)
+ if tv == "number" then
+ tt[i+1] = v -- not needed tostring(v)
+ elseif tv == "string" then
+ tt[i+1] = format("%q",v) -- f_string(v)
+ elseif tv == "boolean" then
+ tt[i+1] = v and "true" or "false"
+ else
+ return nil
+ end
+ end
+ tt[1] = "[0] = " .. tt[1]
+ return tt
end
end
return nil
@@ -680,7 +700,7 @@ function table.serialize(root,name,specification)
elseif tv == "string" then
n = n + 1 t[n] = f_val_str(depth,v)
elseif tv == "table" then
- if next(v) == nil then
+ if next(v) == nil then -- tricky as next is unpredictable in a hash
n = n + 1 t[n] = f_val_not(depth)
else
local st = simple_table(v)
@@ -692,6 +712,8 @@ function table.serialize(root,name,specification)
end
elseif tv == "boolean" then
n = n + 1 t[n] = f_val_boo(depth,v)
+ elseif unknown then
+ n = n + 1 t[n] = f_val_str(depth,tostring(v))
end
elseif tv == "number" then
if tk == "number" then
@@ -700,6 +722,8 @@ function table.serialize(root,name,specification)
n = n + 1 t[n] = f_key_str_value_num(depth,k,v)
elseif tk == "boolean" then
n = n + 1 t[n] = f_key_boo_value_num(depth,k,v)
+ elseif unknown then
+ n = n + 1 t[n] = f_key_str_value_num(depth,tostring(k),v)
end
elseif tv == "string" then
if tk == "number" then
@@ -708,6 +732,8 @@ function table.serialize(root,name,specification)
n = n + 1 t[n] = f_key_str_value_str(depth,k,v)
elseif tk == "boolean" then
n = n + 1 t[n] = f_key_boo_value_str(depth,k,v)
+ elseif unknown then
+ n = n + 1 t[n] = f_key_str_value_str(depth,tostring(k),v)
end
elseif tv == "table" then
if next(v) == nil then
@@ -717,6 +743,8 @@ function table.serialize(root,name,specification)
n = n + 1 t[n] = f_key_str_value_not(depth,k)
elseif tk == "boolean" then
n = n + 1 t[n] = f_key_boo_value_not(depth,k)
+ elseif unknown then
+ n = n + 1 t[n] = f_key_str_value_not(depth,tostring(k))
end
else
local st = simple_table(v)
@@ -728,6 +756,8 @@ function table.serialize(root,name,specification)
n = n + 1 t[n] = f_key_str_value_seq(depth,k,st)
elseif tk == "boolean" then
n = n + 1 t[n] = f_key_boo_value_seq(depth,k,st)
+ elseif unknown then
+ n = n + 1 t[n] = f_key_str_value_seq(depth,tostring(k),st)
end
end
elseif tv == "boolean" then
@@ -737,6 +767,18 @@ function table.serialize(root,name,specification)
n = n + 1 t[n] = f_key_str_value_boo(depth,k,v)
elseif tk == "boolean" then
n = n + 1 t[n] = f_key_boo_value_boo(depth,k,v)
+ elseif unknown then
+ n = n + 1 t[n] = f_key_str_value_boo(depth,tostring(k),v)
+ end
+ else
+ if tk == "number" then
+ n = n + 1 t[n] = f_key_num_value_str(depth,k,tostring(v))
+ elseif tk == "string" then
+ n = n + 1 t[n] = f_key_str_value_str(depth,k,tostring(v))
+ elseif tk == "boolean" then
+ n = n + 1 t[n] = f_key_boo_value_str(depth,k,tostring(v))
+ elseif unknown then
+ n = n + 1 t[n] = f_key_str_value_str(depth,tostring(k),tostring(v))
end
end
end
@@ -775,10 +817,21 @@ function table.serialize(root,name,specification)
end
-- Let's forget about empty tables.
if next(root) ~= nil then
- do_serialize(root,name,1,0)
+ local st = simple_table(root)
+ if st then
+ return t[1] .. f_fin_seq(st) -- todo: move up and in one go
+ else
+ do_serialize(root,name,1,0)
+ end
end
end
n = n + 1
t[n] = f_table_finish()
return concat(t,"\n")
end
+
+table.serialize = serialize
+
+if setinspector then
+ setinspector("table",function(v) if type(v) == "table" then print(serialize(v,"table")) return true end end)
+end