diff options
author | Philipp Gesang <phg42.2a@gmail.com> | 2013-04-12 13:59:39 +0200 |
---|---|---|
committer | Philipp Gesang <phg42.2a@gmail.com> | 2013-04-12 13:59:39 +0200 |
commit | 0e9c876d4af89655cc0dfa5e2fd2758be39dacc3 (patch) | |
tree | 4e0b8dece0771408ca3b1b6ace9ecf123db597b4 | |
parent | 790222c6c4698786b322138770a3afbc8647d01e (diff) | |
download | lualibs-0e9c876d4af89655cc0dfa5e2fd2758be39dacc3.tar.gz |
import libary files from Context
-rw-r--r-- | lualibs-trac-inf.lua | 196 | ||||
-rw-r--r-- | lualibs-util-deb.lua | 128 | ||||
-rw-r--r-- | lualibs-util-env.lua | 258 | ||||
-rw-r--r-- | lualibs-util-sta.lua | 342 | ||||
-rw-r--r-- | lualibs-util-tpl.lua | 174 |
5 files changed, 1098 insertions, 0 deletions
diff --git a/lualibs-trac-inf.lua b/lualibs-trac-inf.lua new file mode 100644 index 0000000..fdc07d9 --- /dev/null +++ b/lualibs-trac-inf.lua @@ -0,0 +1,196 @@ +if not modules then modules = { } end modules ['trac-inf'] = { + version = 1.001, + comment = "companion to trac-inf.mkiv", + author = "Hans Hagen, PRAGMA-ADE, Hasselt NL", + copyright = "PRAGMA ADE / ConTeXt Development Team", + license = "see context related readme files" +} + +-- As we want to protect the global tables, we no longer store the timing +-- in the tables themselves but in a hidden timers table so that we don't +-- get warnings about assignments. This is more efficient than using rawset +-- and rawget. + +local type, tonumber = type, tonumber +local format, lower = string.format, string.lower +local concat = table.concat +local clock = os.gettimeofday or os.clock -- should go in environment + +statistics = statistics or { } +local statistics = statistics + +statistics.enable = true +statistics.threshold = 0.01 + +local statusinfo, n, registered, timers = { }, 0, { }, { } + +table.setmetatableindex(timers,function(t,k) + local v = { timing = 0, loadtime = 0 } + t[k] = v + return v +end) + +local function hastiming(instance) + return instance and timers[instance] +end + +local function resettiming(instance) + timers[instance or "notimer"] = { timing = 0, loadtime = 0 } +end + +local function starttiming(instance) + local timer = timers[instance or "notimer"] + local it = timer.timing or 0 + if it == 0 then + timer.starttime = clock() + if not timer.loadtime then + timer.loadtime = 0 + end + end + timer.timing = it + 1 +end + +local function stoptiming(instance, report) + local timer = timers[instance or "notimer"] + local it = timer.timing + if it > 1 then + timer.timing = it - 1 + else + local starttime = timer.starttime + if starttime then + local stoptime = clock() + local loadtime = stoptime - starttime + timer.stoptime = stoptime + timer.loadtime = timer.loadtime + loadtime + if report then + statistics.report("load time %0.3f",loadtime) + end + timer.timing = 0 + return loadtime + end + end + return 0 +end + +local function elapsed(instance) + if type(instance) == "number" then + return instance or 0 + else + local timer = timers[instance or "notimer"] + return timer and timer.loadtime or 0 + end +end + +local function elapsedtime(instance) + return format("%0.3f",elapsed(instance)) +end + +local function elapsedindeed(instance) + return elapsed(instance) > statistics.threshold +end + +local function elapsedseconds(instance,rest) -- returns nil if 0 seconds + if elapsedindeed(instance) then + return format("%0.3f seconds %s", elapsed(instance),rest or "") + end +end + +statistics.hastiming = hastiming +statistics.resettiming = resettiming +statistics.starttiming = starttiming +statistics.stoptiming = stoptiming +statistics.elapsed = elapsed +statistics.elapsedtime = elapsedtime +statistics.elapsedindeed = elapsedindeed +statistics.elapsedseconds = elapsedseconds + +-- general function .. we might split this module + +function statistics.register(tag,fnc) + if statistics.enable and type(fnc) == "function" then + local rt = registered[tag] or (#statusinfo + 1) + statusinfo[rt] = { tag, fnc } + registered[tag] = rt + if #tag > n then n = #tag end + end +end + +local report = logs.reporter("mkiv lua stats") + +function statistics.show() + if statistics.enable then + -- this code will move + local register = statistics.register + register("luatex banner", function() + return lower(status.banner) + end) + register("control sequences", function() + return format("%s of %s + %s", status.cs_count, status.hash_size,status.hash_extra) + end) + register("callbacks", function() + local total, indirect = status.callbacks or 0, status.indirect_callbacks or 0 + return format("%s direct, %s indirect, %s total", total-indirect, indirect, total) + end) + if jit then + local status = { jit.status() } + if status[1] then + register("luajit status", function() + return concat(status," ",2) + end) + end + end + -- so far + -- collectgarbage("collect") + register("current memory usage",statistics.memused) + register("runtime",statistics.runtime) + logs.newline() -- initial newline + for i=1,#statusinfo do + local s = statusinfo[i] + local r = s[2]() + if r then + report("%s: %s",s[1],r) + end + end + -- logs.newline() -- final newline + statistics.enable = false + end +end + +function statistics.memused() -- no math.round yet -) + local round = math.round or math.floor + return format("%s MB (ctx: %s MB)",round(collectgarbage("count")/1000), round(status.luastate_bytes/1000000)) +end + +starttiming(statistics) + +function statistics.formatruntime(runtime) -- indirect so it can be overloaded and + return format("%s seconds", runtime) -- indeed that happens in cure-uti.lua +end + +function statistics.runtime() + stoptiming(statistics) + return statistics.formatruntime(elapsedtime(statistics)) +end + +local report = logs.reporter("system") + +function statistics.timed(action) + starttiming("run") + action() + stoptiming("run") + report("total runtime: %s",elapsedtime("run")) +end + +-- where, not really the best spot for this: + +commands = commands or { } + +function commands.resettimer(name) + resettiming(name or "whatever") + starttiming(name or "whatever") +end + +function commands.elapsedtime(name) + stoptiming(name or "whatever") + context(elapsedtime(name or "whatever")) +end diff --git a/lualibs-util-deb.lua b/lualibs-util-deb.lua new file mode 100644 index 0000000..785373f --- /dev/null +++ b/lualibs-util-deb.lua @@ -0,0 +1,128 @@ +if not modules then modules = { } end modules ['util-deb'] = { + version = 1.001, + comment = "companion to luat-lib.mkiv", + author = "Hans Hagen, PRAGMA-ADE, Hasselt NL", + copyright = "PRAGMA ADE / ConTeXt Development Team", + license = "see context related readme files" +} + +-- the <anonymous> tag is kind of generic and used for functions that are not +-- bound to a variable, like node.new, node.copy etc (contrary to for instance +-- node.has_attribute which is bound to a has_attribute local variable in mkiv) + +local debug = require "debug" + +local getinfo = debug.getinfo +local type, next, tostring = type, next, tostring +local format, find = string.format, string.find +local is_boolean = string.is_boolean + +utilities = utilities or { } +local debugger = utilities.debugger or { } +utilities.debugger = debugger + +local counters = { } +local names = { } + +local report = logs.reporter("debugger") + +-- one + +local function hook() + local f = getinfo(2) -- "nS" + if f then + local n = "unknown" + if f.what == "C" then + n = f.name or '<anonymous>' + if not names[n] then + names[n] = format("%42s",n) + end + else + -- source short_src linedefined what name namewhat nups func + n = f.name or f.namewhat or f.what + if not n or n == "" then + n = "?" + end + if not names[n] then + names[n] = format("%42s : % 5i : %s",n,f.linedefined or 0,f.short_src or "unknown source") + end + end + counters[n] = (counters[n] or 0) + 1 + end +end + +function debugger.showstats(printer,threshold) -- hm, something has changed, rubish now + printer = printer or report + threshold = threshold or 0 + local total, grandtotal, functions = 0, 0, 0 + local dataset = { } + for name, count in next, counters do + dataset[#dataset+1] = { name, count } + end + table.sort(dataset,function(a,b) return a[2] == b[2] and b[1] > a[1] or a[2] > b[2] end) + for i=1,#dataset do + local d = dataset[i] + local name = d[1] + local count = d[2] + if count > threshold and not find(name,"for generator") then -- move up + printer(format("%8i %s\n", count, names[name])) + total = total + count + end + grandtotal = grandtotal + count + functions = functions + 1 + end + printer("\n") + printer(format("functions : % 10i\n", functions)) + printer(format("total : % 10i\n", total)) + printer(format("grand total: % 10i\n", grandtotal)) + printer(format("threshold : % 10i\n", threshold)) +end + +function debugger.savestats(filename,threshold) + local f = io.open(filename,'w') + if f then + debugger.showstats(function(str) f:write(str) end,threshold) + f:close() + end +end + +function debugger.enable() + debug.sethook(hook,"c") +end + +function debugger.disable() + debug.sethook() +--~ counters[debug.getinfo(2,"f").func] = nil +end + +--~ debugger.enable() + +--~ print(math.sin(1*.5)) +--~ print(math.sin(1*.5)) +--~ print(math.sin(1*.5)) +--~ print(math.sin(1*.5)) +--~ print(math.sin(1*.5)) + +--~ debugger.disable() + +--~ print("") +--~ debugger.showstats() +--~ print("") +--~ debugger.showstats(print,3) + +-- from the lua book: + +function traceback() + local level = 1 + while true do + local info = debug.getinfo(level, "Sl") + if not info then + break + elseif info.what == "C" then + print(format("%3i : C function",level)) + else + print(format("%3i : [%s]:%d",level,info.short_src,info.currentline)) + end + level = level + 1 + end +end diff --git a/lualibs-util-env.lua b/lualibs-util-env.lua new file mode 100644 index 0000000..283b91c --- /dev/null +++ b/lualibs-util-env.lua @@ -0,0 +1,258 @@ +if not modules then modules = { } end modules ['util-env'] = { + version = 1.001, + comment = "companion to luat-lib.mkiv", + author = "Hans Hagen, PRAGMA-ADE, Hasselt NL", + copyright = "PRAGMA ADE / ConTeXt Development Team", + license = "see context related readme files" +} + +local allocate, mark = utilities.storage.allocate, utilities.storage.mark + +local format, sub, match, gsub, find = string.format, string.sub, string.match, string.gsub, string.find +local unquoted, quoted = string.unquoted, string.quoted +local concat, insert, remove = table.concat, table.insert, table.remove + +environment = environment or { } +local environment = environment + +-- precautions + +os.setlocale(nil,nil) -- useless feature and even dangerous in luatex + +function os.setlocale() + -- no way you can mess with it +end + +-- dirty tricks (we will replace the texlua call by luatex --luaonly) + +local validengines = allocate { + ["luatex"] = true, + ["luajittex"] = true, + -- ["luatex.exe"] = true, + -- ["luajittex.exe"] = true, +} + +local basicengines = allocate { + ["luatex"] = "luatex", + ["texlua"] = "luatex", + ["texluac"] = "luatex", + ["luajittex"] = "luajittex", + ["texluajit"] = "luajittex", + -- ["texlua.exe"] = "luatex", + -- ["texluajit.exe"] = "luajittex", +} + +local luaengines=allocate { + ["lua"] = true, + ["luajit"] = true, +} + +environment.validengines = validengines +environment.basicengines = basicengines + +-- [-1] = binary +-- [ 0] = self +-- [ 1] = argument 1 ... + +-- instead we could set ranges + +if not arg then + -- used as library +elseif luaengines[file.removesuffix(arg[-1])] then +-- arg[-1] = arg[0] +-- arg[ 0] = arg[1] +-- for k=2,#arg do +-- arg[k-1] = arg[k] +-- end +-- remove(arg) -- last +elseif validengines[file.removesuffix(arg[0])] then + if arg[1] == "--luaonly" then + arg[-1] = arg[0] + arg[ 0] = arg[2] + for k=3,#arg do + arg[k-2] = arg[k] + end + remove(arg) -- last + remove(arg) -- pre-last + else + -- tex run + end + + -- This is an ugly hack but it permits symlinking a script (say 'context') to 'mtxrun' as in: + -- + -- ln -s /opt/minimals/tex/texmf-linux-64/bin/mtxrun context + -- + -- The special mapping hack is needed because 'luatools' boils down to 'mtxrun --script base' + -- but it's unlikely that there will be more of this + + local originalzero = file.basename(arg[0]) + local specialmapping = { luatools == "base" } + + if originalzero ~= "mtxrun" and originalzero ~= "mtxrun.lua" then + arg[0] = specialmapping[originalzero] or originalzero + insert(arg,0,"--script") + insert(arg,0,"mtxrun") + end + +end + +-- environment + +environment.arguments = allocate() +environment.files = allocate() +environment.sortedflags = nil + +-- context specific arguments (in order not to confuse the engine) + +function environment.initializearguments(arg) + local arguments, files = { }, { } + environment.arguments, environment.files, environment.sortedflags = arguments, files, nil + for index=1,#arg do + local argument = arg[index] + if index > 0 then + local flag, value = match(argument,"^%-+(.-)=(.-)$") + if flag then + flag = gsub(flag,"^c:","") + arguments[flag] = unquoted(value or "") + else + flag = match(argument,"^%-+(.+)") + if flag then + flag = gsub(flag,"^c:","") + arguments[flag] = true + else + files[#files+1] = argument + end + end + end + end + environment.ownname = file.reslash(environment.ownname or arg[0] or 'unknown.lua') +end + +function environment.setargument(name,value) + environment.arguments[name] = value +end + +-- todo: defaults, better checks e.g on type (boolean versus string) +-- +-- tricky: too many hits when we support partials unless we add +-- a registration of arguments so from now on we have 'partial' + +function environment.getargument(name,partial) + local arguments, sortedflags = environment.arguments, environment.sortedflags + if arguments[name] then + return arguments[name] + elseif partial then + if not sortedflags then + sortedflags = allocate(table.sortedkeys(arguments)) + for k=1,#sortedflags do + sortedflags[k] = "^" .. sortedflags[k] + end + environment.sortedflags = sortedflags + end + -- example of potential clash: ^mode ^modefile + for k=1,#sortedflags do + local v = sortedflags[k] + if find(name,v) then + return arguments[sub(v,2,#v)] + end + end + end + return nil +end + +environment.argument = environment.getargument + +function environment.splitarguments(separator) -- rather special, cut-off before separator + local done, before, after = false, { }, { } + local originalarguments = environment.originalarguments + for k=1,#originalarguments do + local v = originalarguments[k] + if not done and v == separator then + done = true + elseif done then + after[#after+1] = v + else + before[#before+1] = v + end + end + return before, after +end + +function environment.reconstructcommandline(arg,noquote) + arg = arg or environment.originalarguments + if noquote and #arg == 1 then + -- we could just do: return unquoted(resolvers.resolve(arg[i])) + local a = arg[1] + a = resolvers.resolve(a) + a = unquoted(a) + return a + elseif #arg > 0 then + local result = { } + for i=1,#arg do + -- we could just do: result[#result+1] = format("%q",unquoted(resolvers.resolve(arg[i]))) + local a = arg[i] + a = resolvers.resolve(a) + a = unquoted(a) + a = gsub(a,'"','\\"') -- tricky + if find(a," ") then + result[#result+1] = quoted(a) + else + result[#result+1] = a + end + end + return concat(result," ") + else + return "" + end +end + +-- -- to be tested: +-- +-- function environment.reconstructcommandline(arg,noquote) +-- arg = arg or environment.originalarguments +-- if noquote and #arg == 1 then +-- return unquoted(resolvers.resolve(arg[1])) +-- elseif #arg > 0 then +-- local result = { } +-- for i=1,#arg do +-- result[#result+1] = format("%q",unquoted(resolvers.resolve(arg[i]))) -- always quote +-- end +-- return concat(result," ") +-- else +-- return "" +-- end +-- end + +if arg then + + -- new, reconstruct quoted snippets (maybe better just remove the " then and add them later) + local newarg, instring = { }, false + + for index=1,#arg do + local argument = arg[index] + if find(argument,"^\"") then + newarg[#newarg+1] = gsub(argument,"^\"","") + if not find(argument,"\"$") then + instring = true + end + elseif find(argument,"\"$") then + newarg[#newarg] = newarg[#newarg] .. " " .. gsub(argument,"\"$","") + instring = false + elseif instring then + newarg[#newarg] = newarg[#newarg] .. " " .. argument + else + newarg[#newarg+1] = argument + end + end + for i=1,-5,-1 do + newarg[i] = arg[i] + end + + environment.initializearguments(newarg) + + environment.originalarguments = mark(newarg) + environment.rawarguments = mark(arg) + + arg = { } -- prevent duplicate handling + +end diff --git a/lualibs-util-sta.lua b/lualibs-util-sta.lua new file mode 100644 index 0000000..1a61ec4 --- /dev/null +++ b/lualibs-util-sta.lua @@ -0,0 +1,342 @@ +if not modules then modules = { } end modules ['util-sta'] = { + version = 1.001, + comment = "companion to util-ini.mkiv", + author = "Hans Hagen, PRAGMA-ADE, Hasselt NL", + copyright = "PRAGMA ADE / ConTeXt Development Team", + license = "see context related readme files" +} + +local insert, remove, fastcopy, concat = table.insert, table.remove, table.fastcopy, table.concat +local format = string.format +local select, tostring = select, tostring + +local trace_stacker = false trackers.register("stacker.resolve", function(v) trace_stacker = v end) + +local stacker = stacker or { } + +utilities.stacker = stacker + +local function start(s,t,first,last) + if s.mode == "switch" then + local n = tostring(t[last]) + if trace_stacker then + s.report("start: %s",n) + end + return n + else + local r = { } + for i=first,last do + r[#r+1] = tostring(t[i]) + end + local n = concat(r," ") + if trace_stacker then + s.report("start: %s",n) + end + return n + end +end + +local function stop(s,t,first,last) + if s.mode == "switch" then + local n = tostring(false) + if trace_stacker then + s.report("stop: %s",n) + end + return n + else + local r = { } + for i=last,first,-1 do + r[#r+1] = tostring(false) + end + local n = concat(r," ") + if trace_stacker then + s.report("stop: %s",n) + end + return n + end +end + +local function change(s,t1,first1,last1,t2,first2,last2) + if s.mode == "switch" then + local n = tostring(t2[last2]) + if trace_stacker then + s.report("change: %s",n) + end + return n + else + local r = { } + for i=last1,first1,-1 do + r[#r+1] = tostring(false) + end + local n = concat(r," ") + for i=first2,last2 do + r[#r+1] = tostring(t2[i]) + end + if trace_stacker then + s.report("change: %s",n) + end + return n + end +end + +function stacker.new(name) + + local s + + local stack = { } + local list = { } + local ids = { } + local hash = { } + + local hashing = true + + local function push(...) + for i=1,select("#",...) do + insert(stack,(select(i,...))) -- watch the () + end + if hashing then + local c = concat(stack,"|") + local n = hash[c] + if not n then + n = #list+1 + hash[c] = n + list[n] = fastcopy(stack) + end + insert(ids,n) + return n + else + local n = #list+1 + list[n] = fastcopy(stack) + insert(ids,n) + return n + end + end + + local function pop() + remove(stack) + remove(ids) + return ids[#ids] or s.unset or -1 + end + + local function clean() + if #stack == 0 then + if trace_stacker then + s.report("%s list entries, %s stack entries",#list,#stack) + end + end + end + + local tops = { } + local top, switch + + local function resolve_begin(mode) + if mode then + switch = mode == "switch" + else + switch = s.mode == "switch" + end + top = { switch = switch } + insert(tops,top) + end + + local function resolve_step(ti) -- keep track of changes outside function ! + -- todo: optimize for n=1 etc + local result = nil + local noftop = #top + if ti > 0 then + local current = list[ti] + if current then + local noflist = #current + local nofsame = 0 + if noflist > noftop then + for i=1,noflist do + if current[i] == top[i] then + nofsame = i + else + break + end + end + else + for i=1,noflist do + if current[i] == top[i] then + nofsame = i + else + break + end + end + end + local plus = nofsame + 1 + if plus <= noftop then + if plus <= noflist then + if switch then + result = s.change(s,top,plus,noftop,current,nofsame,noflist) + else + result = s.change(s,top,plus,noftop,current,plus,noflist) + end + else + if switch then + result = s.change(s,top,plus,noftop,current,nofsame,noflist) + else + result = s.stop(s,top,plus,noftop) + end + end + elseif plus <= noflist then + if switch then + result = s.start(s,current,nofsame,noflist) + else + result = s.start(s,current,plus,noflist) + end + end + top = current + else + if 1 <= noftop then + result = s.stop(s,top,1,noftop) + end + top = { } + end + return result + else + if 1 <= noftop then + result = s.stop(s,top,1,noftop) + end + top = { } + return result + end + end + + local function resolve_end() + -- resolve_step(s.unset) + local noftop = #top + if noftop > 0 then + local result = s.stop(s,top,1,#top) + remove(tops) + top = tops[#tops] + switch = top and top.switch + return result + end + end + + local function resolve(t) + resolve_begin() + for i=1,#t do + resolve_step(t[i]) + end + resolve_end() + end + + local report = logs.reporter("stacker",name or nil) + + s = { + name = name or "unknown", + unset = -1, + report = report, + start = start, + stop = stop, + change = change, + push = push, + pop = pop, + clean = clean, + resolve = resolve, + resolve_begin = resolve_begin, + resolve_step = resolve_step, + resolve_end = resolve_end, + } + + return s -- we can overload functions + +end + +-- local s = utilities.stacker.new("demo") +-- +-- local unset = s.unset +-- local push = s.push +-- local pop = s.pop +-- +-- local t = { +-- unset, +-- unset, +-- push("a"), -- a +-- push("b","c"), -- a b c +-- pop(), -- a b +-- push("d"), -- a b d +-- pop(), -- a b +-- unset, +-- pop(), -- a +-- pop(), -- b +-- unset, +-- unset, +-- } +-- +-- s.resolve(t) + +-- demostacker = utilities.stacker.new("demos") +-- +-- local whatever = { +-- one = "1 0 0 RG 1 0 0 rg", +-- two = "1 1 0 RG 1 1 0 rg", +-- [false] = "0 G 0 g", +-- } +-- +-- local concat = table.concat +-- +-- local pdfliteral = nodes.pool.pdfliteral +-- +-- function demostacker.start(s,t,first,last) +-- local n = whatever[t[last]] +-- -- s.report("start: %s",n) +-- return pdfliteral(n) +-- end +-- +-- function demostacker.stop(s,t,first,last) +-- local n = whatever[false] +-- -- s.report("stop: %s",n) +-- return pdfliteral(n) +-- end +-- +-- function demostacker.change(s,t1,first1,last1,t2,first2,last2) +-- local n = whatever[t2[last2]] +-- -- s.report("change: %s",n) +-- return pdfliteral(n) +-- end +-- +-- demostacker.mode = "switch" +-- +-- local whatever = { +-- one = "/OC /test1 BDC", +-- two = "/OC /test2 BDC", +-- [false] = "EMC", +-- } +-- +-- demostacker = utilities.stacker.new("demos") +-- +-- function demostacker.start(s,t,first,last) +-- local r = { } +-- for i=first,last do +-- r[#r+1] = whatever[t[i]] +-- end +-- -- s.report("start: %s",concat(r," ")) +-- return pdfliteral(concat(r," ")) +-- end +-- +-- function demostacker.stop(s,t,first,last) +-- local r = { } +-- for i=last,first,-1 do +-- r[#r+1] = whatever[false] +-- end +-- -- s.report("stop: %s",concat(r," ")) +-- return pdfliteral(concat(r," ")) +-- end +-- +-- function demostacker.change(s,t1,first1,last1,t2,first2,last2) +-- local r = { } +-- for i=last1,first1,-1 do +-- r[#r+1] = whatever[false] +-- end +-- for i=first2,last2 do +-- r[#r+1] = whatever[t2[i]] +-- end +-- -- s.report("change: %s",concat(r," ")) +-- return pdfliteral(concat(r," ")) +-- end +-- +-- demostacker.mode = "stack" diff --git a/lualibs-util-tpl.lua b/lualibs-util-tpl.lua new file mode 100644 index 0000000..7a6abef --- /dev/null +++ b/lualibs-util-tpl.lua @@ -0,0 +1,174 @@ +if not modules then modules = { } end modules ['util-tpl'] = { + version = 1.001, + comment = "companion to luat-lib.mkiv", + author = "Hans Hagen, PRAGMA-ADE, Hasselt NL", + copyright = "PRAGMA ADE / ConTeXt Development Team", + license = "see context related readme files" +} + +-- This is experimental code. Coming from dos and windows, I've always used %whatever% +-- as template variables so let's stick to it. After all, it's easy to parse and stands +-- out well. A double %% is turned into a regular %. + +utilities.templates = utilities.templates or { } +local templates = utilities.templates + +local trace_template = false trackers.register("templates.trace",function(v) trace_template = v end) +local report_template = logs.reporter("template") + +local tostring = tostring +local format, sub = string.format, string.sub +local P, C, Cs, Carg, lpegmatch = lpeg.P, lpeg.C, lpeg.Cs, lpeg.Carg, lpeg.match + +-- todo: make installable template.new + +local replacer + +local function replacekey(k,t,how,recursive) + local v = t[k] + if not v then + if trace_template then + report_template("unknown key %a",k) + end + return "" + else + v = tostring(v) + if trace_template then + report_template("setting key %a to value %a",k,v) + end + if recursive then + return lpegmatch(replacer,v,1,t,how,recursive) + else + return v + end + end +end + +local sqlescape = lpeg.replacer { + { "'", "''" }, + { "\\", "\\\\" }, + { "\r\n", "\\n" }, + { "\r", "\\n" }, + -- { "\t", "\\t" }, +} + +local sqlquotedescape = lpeg.Cs(lpeg.Cc("'") * sqlescape * lpeg.Cc("'")) + +-- escapeset : \0\1\2\3\4\5\6\7\8\9\10\11\12\13\14\15\16\17\18\19\20\21\22\23\24\25\26\27\28\29\30\31\"\\\127 +-- test string: [[1\0\31test23"\\]] .. string.char(19) .. "23" +-- +-- slow: +-- +-- local luaescape = lpeg.replacer { +-- { '"', [[\"]] }, +-- { '\\', [[\\]] }, +-- { R("\0\9") * #R("09"), function(s) return "\\00" .. byte(s) end }, +-- { R("\10\31") * #R("09"), function(s) return "\\0" .. byte(s) end }, +-- { R("\0\31") , function(s) return "\\" .. byte(s) end }, +-- } +-- +-- slightly faster: +-- +-- local luaescape = Cs (( +-- P('"' ) / [[\"]] + +-- P('\\') / [[\\]] + +-- Cc("\\00") * (R("\0\9") / byte) * #R("09") + +-- Cc("\\0") * (R("\10\31") / byte) * #R("09") + +-- Cc("\\") * (R("\0\31") / byte) + +-- P(1) +-- )^0) + +local escapers = { + lua = function(s) + return sub(format("%q",s),2,-2) + end, + sql = function(s) + return lpegmatch(sqlescape,s) + end, +} + +local quotedescapers = { + lua = function(s) + return format("%q",s) + end, + sql = function(s) + return lpegmatch(sqlquotedescape,s) + end, +} + +lpeg.patterns.sqlescape = sqlescape +lpeg.patterns.sqlescape = sqlquotedescape + +local luaescaper = escapers.lua +local quotedluaescaper = quotedescapers.lua + +local function replacekeyunquoted(s,t,how,recurse) -- ".. \" " + local escaper = how and escapers[how] or luaescaper + return escaper(replacekey(s,t,how,recurse)) +end + +local function replacekeyquoted(s,t,how,recurse) -- ".. \" " + local escaper = how and quotedescapers[how] or quotedluaescaper + return escaper(replacekey(s,t,how,recurse)) +end + +local single = P("%") -- test %test% test : resolves test +local double = P("%%") -- test 10%% test : %% becomes % +local lquoted = P("%[") -- test '%[test]%' test : resolves to test with escaped "'s +local rquoted = P("]%") -- +local lquotedq = P("%(") -- test %(test)% test : resolves to 'test' with escaped "'s +local rquotedq = P(")%") -- + +local escape = double / '%%' +local nosingle = single / '' +local nodouble = double / '' +local nolquoted = lquoted / '' +local norquoted = rquoted / '' +local nolquotedq = lquotedq / '' +local norquotedq = rquotedq / '' + +local key = nosingle * ((C((1-nosingle )^1) * Carg(1) * Carg(2) * Carg(3)) / replacekey ) * nosingle +local quoted = nolquotedq * ((C((1-norquotedq)^1) * Carg(1) * Carg(2) * Carg(3)) / replacekeyquoted ) * norquotedq +local unquoted = nolquoted * ((C((1-norquoted )^1) * Carg(1) * Carg(2) * Carg(3)) / replacekeyunquoted) * norquoted +local any = P(1) + + replacer = Cs((unquoted + quoted + escape + key + any)^0) + +local function replace(str,mapping,how,recurse) + if mapping and str then + return lpegmatch(replacer,str,1,mapping,how or "lua",recurse or false) or str + else + return str + end +end + +-- print(replace("test '%[x]%' test",{ x = [[a 'x' a]] })) +-- print(replace("test '%[x]%' test",{ x = true })) +-- print(replace("test '%[x]%' test",{ x = [[a 'x' a]], y = "oeps" },'sql')) +-- print(replace("test '%[x]%' test",{ x = [[a '%y%' a]], y = "oeps" },'sql',true)) +-- print(replace([[test %[x]% test]],{ x = [[a "x" a]]})) +-- print(replace([[test %(x)% test]],{ x = [[a "x" a]]})) + +templates.replace = replace + +function templates.load(filename,mapping,how,recurse) + local data = io.loaddata(filename) or "" + if mapping and next(mapping) then + return replace(data,mapping,how,recurse) + else + return data + end +end + +function templates.resolve(t,mapping,how,recurse) + if not mapping then + mapping = t + end + for k, v in next, t do + t[k] = replace(v,mapping,how,recurse) + end + return t +end + +-- inspect(utilities.templates.replace("test %one% test", { one = "%two%", two = "two" })) +-- inspect(utilities.templates.resolve({ one = "%two%", two = "two", three = "%three%" })) |