From 5c46359f4fa543e24e5d4a0a327d41bf8a9ff7c8 Mon Sep 17 00:00:00 2001 From: Hans Hagen Date: Thu, 22 Mar 2018 19:57:53 +0100 Subject: 2018-03-22 19:49:00 --- tex/context/base/mkiv/l-macro.lua | 72 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 71 insertions(+), 1 deletion(-) (limited to 'tex/context/base/mkiv/l-macro.lua') diff --git a/tex/context/base/mkiv/l-macro.lua b/tex/context/base/mkiv/l-macro.lua index d2b8c7ae4..30c7cbec6 100644 --- a/tex/context/base/mkiv/l-macro.lua +++ b/tex/context/base/mkiv/l-macro.lua @@ -14,7 +14,8 @@ if not modules then modules = { } end modules ['l-macros'] = { local S, P, R, V, C, Cs, Cc, Ct, Carg = lpeg.S, lpeg.P, lpeg.R, lpeg.V, lpeg.C, lpeg.Cs, lpeg.Cc, lpeg.Ct, lpeg.Carg local lpegmatch = lpeg.match local concat = table.concat -local next = next +local format, sub = string.format, string.sub +local next, load, type = next, load, type local newline = S("\n\r")^1 local continue = P("\\") * newline @@ -41,6 +42,15 @@ local definitions = { } local resolve local subparser +local report_lua = function(...) + if logs and logs.reporter then + report_lua = logs.reporter("system","lua") + report_lua(...) + else + print(format(...)) + end +end + -- todo: zero case resolve = C(C(name) * arguments^-1) / function(raw,s,a) @@ -160,6 +170,66 @@ function macros.resolving() return next(patterns) end +local function loaded(name,trace,detail) + -- local c = io.loaddata(fullname) -- not yet available + local f = io.open(name,"rb") + if not f then + return false, format("file '%s' not found",name) + end + local c = f:read("*a") + if not c then + return false, format("file '%s' is invalid",name) + end + f:close() + local n = lpegmatch(parser,c) + if trace then + if #n ~= #c then + report_lua("macros expanded in '%s' (%i => %i bytes)",name,#c,#n) + if detail then + report_lua() + report_lua(n) + report_lua() + end + elseif detail then + report_lua("no macros expanded in '%s'",name) + end + end + if #name > 30 then + n = "--[[" .. sub(name,-30) .. "]] " .. n + else + n = "--[[" .. name .. "]] " .. n + end + return load(n) +end + +macros.loaded = loaded + +function required(name,trace) + local filename = file.addsuffix(name,"lua") + local fullname = resolvers and resolvers.find_file(filename) or filename + if not fullname or fullname == "" then + return false + end + local codeblob = package.loaded[fullname] + if codeblob then + return codeblob + end + local code, message = loaded(fullname,macros,trace,trace) + if type(code) == "function" then + code = code() + else + report_lua("error when loading '%s'",fullname) + return false, message + end + if code == nil then + code = false + end + package.loaded[fullname] = code + return code +end + +macros.required = required + -- local str = [[ -- #define check(p,q) (p ~= 0) and (p > q) -- -- cgit v1.2.3