Autoformat

This commit is contained in:
perro tuerto 2023-07-01 10:03:35 -07:00
parent 5b9d776dfb
commit ee1d86dc86
2 changed files with 71 additions and 69 deletions

View File

@ -11,48 +11,59 @@ lit.fns = {}
-- Returns specific grammar -- Returns specific grammar
function lit.g(name) function lit.g(name)
-- Stores all grammars -- Stores all grammars
local grammars = {} local grammars = {}
-- Shortcuts -- Shortcuts
local P, S, R, Cf, Cc, Ct, V, Cs, Cg, Cb, B, C, Cmt = local P, S, R, Cf, Cc, Ct, V, Cs, Cg, Cb, B, C, Cmt =
lpeg.P, lpeg.S, lpeg.R, lpeg.Cf, lpeg.Cc, lpeg.Ct, lpeg.V, lpeg.P,
lpeg.Cs, lpeg.Cg, lpeg.Cb, lpeg.B, lpeg.C, lpeg.Cmt lpeg.S,
lpeg.R,
lpeg.Cf,
lpeg.Cc,
lpeg.Ct,
lpeg.V,
lpeg.Cs,
lpeg.Cg,
lpeg.Cb,
lpeg.B,
lpeg.C,
lpeg.Cmt
-- Lexical elements -- Lexical elements
local newline = P"\r"^-1 * P"\n" local newline = P("\r") ^ -1 * P("\n")
local space = S" \t" local space = S(" \t")
local anyspace = S" \t\r\n" local anyspace = S(" \t\r\n")
local spot = (1 - anyspace) local spot = (1 - anyspace)
local any = (spot + space) local any = (spot + space)
local yamlheader = space^0 * P"---" * space^0 * newline local yamlheader = space ^ 0 * P("---") * space ^ 0 * newline
local yamlfooter = space^0 * P"..." * space^0 * newline^-1 local yamlfooter = space ^ 0 * P("...") * space ^ 0 * newline ^ -1
local yamlbody = -yamlfooter * any^0 * newline local yamlbody = -yamlfooter * any ^ 0 * newline
local label = R("az", "AZ") * R("az", "AZ", "09", "__")^0 local label = R("az", "AZ") * R("az", "AZ", "09", "__") ^ 0
local ref = -B"\\" * P"#" * C(label) local ref = -B("\\") * P("#") * C(label)
local notref = (1 - ref)^0 local notref = (1 - ref) ^ 0
-- Function declaration grammar -- Function declaration grammar
grammars["declaration"] = P { grammars["declaration"] = P({
"Declaration"; "Declaration",
Declaration = Ct(V"YAML" * V"Code", "name"); Declaration = Ct(V("YAML") * V("Code"), "name"),
YAML = C(yamlheader * yamlbody^0 * yamlfooter); YAML = C(yamlheader * yamlbody ^ 0 * yamlfooter),
Code = C((any + newline)^0); Code = C((any + newline) ^ 0),
} })
-- Argument references grammar -- Argument references grammar
grammars["references"] = notref * P { grammars["references"] = notref
"References"; * P({
References = Ct((ref * notref)^0); "References",
} * -1 References = Ct((ref * notref) ^ 0),
})
* -1
return grammars[name] return grammars[name]
end end
-- Prints located messages -- Prints located messages
function lit.puts(yaml_key, ...) function lit.puts(yaml_key, ...)
-- Returns debug level as number -- Returns debug level as number
local function debuglevel(str) local function debuglevel(str)
if str == "ERROR" then if str == "ERROR" then
@ -72,7 +83,7 @@ function lit.puts(yaml_key, ...)
for mtype, msgs in pairs(msg) do for mtype, msgs in pairs(msg) do
if msgs[key] then if msgs[key] then
key = (msgs[key][lang] ~= nil and msgs[key][lang] or msgs[key]["en"]) key = (msgs[key][lang] ~= nil and msgs[key][lang] or msgs[key]["en"])
for i, str in ipairs({...}) do for i, str in ipairs({ ... }) do
key = key:gsub("#" .. i, str) key = key:gsub("#" .. i, str)
end end
levelname, level = mtype, debuglevel(mtype) levelname, level = mtype, debuglevel(mtype)
@ -84,20 +95,15 @@ function lit.puts(yaml_key, ...)
local verbosity = debuglevel(PANDOC_STATE.verbosity) local verbosity = debuglevel(PANDOC_STATE.verbosity)
local msg, levelname, level = getmsg(yaml_key, ...) local msg, levelname, level = getmsg(yaml_key, ...)
if level >= verbosity then if level >= verbosity then print("[" .. levelname .. "] [LIT] " .. msg) end
print("[" .. levelname .. "] [LIT] " .. msg)
end
end end
-- Parses and evaluates literate functions in document -- Parses and evaluates literate functions in document
function lit.exam(doc) function lit.exam(doc)
-- Extracts function declarations -- Extracts function declarations
local function extract(codeblock) local function extract(codeblock)
-- Checks function declarations -- Checks function declarations
local function check(match) local function check(match)
-- Stores valid declaration -- Stores valid declaration
local declaration = {} local declaration = {}
@ -105,13 +111,19 @@ function lit.exam(doc)
local metastruct = { local metastruct = {
["mandatory"] = { ["mandatory"] = {
-- Value as table == whole patterns to match -- Value as table == whole patterns to match
["id"] = {"%a[_%w]*"}, ["id"] = { "%a[_%w]*" },
}, },
["optional"] = { ["optional"] = {
["lang"] = { ["lang"] = {
"lua", "fennel", "python", "js", "ruby", "lisp", "graphviz", "lua",
"fennel",
"python",
"js",
"ruby",
"lisp",
"graphviz",
}, },
["cmd"] = {".+"}, ["cmd"] = { ".+" },
-- Value as string == type to match -- Value as string == type to match
["args"] = "table", ["args"] = "table",
["shift"] = "boolean", ["shift"] = "boolean",
@ -130,13 +142,13 @@ function lit.exam(doc)
-- First table indicates one or more internal or external evaluations -- First table indicates one or more internal or external evaluations
-- Second table indicates one or more commands to try -- Second table indicates one or more commands to try
local langcmds = { local langcmds = {
["lua"] = {["int_eval"] = { "CMD1", "CMD2", } }, ["lua"] = { ["int_eval"] = { "CMD1", "CMD2" } },
["fennel"] = {["int_eval"] = { "CMD1", "CMD2", } }, ["fennel"] = { ["int_eval"] = { "CMD1", "CMD2" } },
["python"] = {["ext_eval"] = { "CMD1", "CMD2", } }, ["python"] = { ["ext_eval"] = { "CMD1", "CMD2" } },
["js"] = {["ext_eval"] = { "CMD1", "CMD2", } }, ["js"] = { ["ext_eval"] = { "CMD1", "CMD2" } },
["ruby"] = {["ext_eval"] = { "CMD1", "CMD2", } }, ["ruby"] = { ["ext_eval"] = { "CMD1", "CMD2" } },
["lisp"] = {["ext_eval"] = { "CMD1", "CMD2", } }, ["lisp"] = { ["ext_eval"] = { "CMD1", "CMD2" } },
["graphviz"] = {["ext_eval"] = { "CMD1", "CMD2", } }, ["graphviz"] = { ["ext_eval"] = { "CMD1", "CMD2" } },
} }
-- Prints error -- Prints error
@ -154,17 +166,17 @@ function lit.exam(doc)
-- If lang and cmd are present, ignores lang and use cmd as eval -- If lang and cmd are present, ignores lang and use cmd as eval
if declaration["lang"] and declaration["cmd"] then if declaration["lang"] and declaration["cmd"] then
lit.puts("ignore_lang", declaration["cmd"], declaration["lang"]) lit.puts("ignore_lang", declaration["cmd"], declaration["lang"])
declaration["eval"] = {["ext_eval"] = { declaration["cmd"] } } declaration["eval"] = { ["ext_eval"] = { declaration["cmd"] } }
-- If neither lang or cmd are presents, defaults lang and eval to lua -- If neither lang or cmd are presents, defaults lang and eval to lua
elseif not(declaration["lang"]) and not(declaration["cmd"])then elseif not declaration["lang"] and not declaration["cmd"] then
declaration["lang"] = "lua" declaration["lang"] = "lua"
declaration["eval"] = langcmds["lua"] declaration["eval"] = langcmds["lua"]
-- If only lang present, eval is lang -- If only lang present, eval is lang
elseif declaration["lang"] and not(declaration["cmd"]) then elseif declaration["lang"] and not declaration["cmd"] then
declaration["eval"] = langcmds[declaration["lang"]] declaration["eval"] = langcmds[declaration["lang"]]
-- If only cmd present, eval is cmd -- If only cmd present, eval is cmd
elseif not(declaration["lang"]) and declaration["cmd"] then elseif not declaration["lang"] and declaration["cmd"] then
declaration["eval"] = {["ext_eval"] = { declaration["cmd"] } } declaration["eval"] = { ["ext_eval"] = { declaration["cmd"] } }
end end
end end
@ -174,9 +186,7 @@ function lit.exam(doc)
local cmd = declaration["cmd"]:split()[1] local cmd = declaration["cmd"]:split()[1]
if os.isunix() then if os.isunix() then
local status = io.try("type", cmd) local status = io.try("type", cmd)
if not(status) then if not status then putserr("invalid_cmd", cmd) end
putserr("invalid_cmd", cmd)
end
else else
lit.puts("skip_check", "checkcmd") lit.puts("skip_check", "checkcmd")
end end
@ -188,22 +198,19 @@ function lit.exam(doc)
for key, _ in pairs(declaration) do for key, _ in pairs(declaration) do
local missing1 = metastruct["mandatory"][key] == nil local missing1 = metastruct["mandatory"][key] == nil
local missing2 = metastruct["optional"][key] == nil local missing2 = metastruct["optional"][key] == nil
if missing1 and missing2 then if missing1 and missing2 then putserr("invalid_key", key) end
putserr("invalid_key", key)
end
end end
end end
-- Checks for valid metadata -- Checks for valid metadata
local function checkmeta(kind) local function checkmeta(kind)
-- Checks for valid metadata type -- Checks for valid metadata type
local function checktype(type1, key) local function checktype(type1, key)
local val = declaration[key] local val = declaration[key]
local type2 = type(val) local type2 = type(val)
if type1 ~= type2 then if type1 ~= type2 then
if type1 == "path" then if type1 == "path" then
if not(pandoc.path.directory(val):isdir()) then if not (pandoc.path.directory(val):isdir()) then
putserr("invalid_path", val, key) putserr("invalid_path", val, key)
end end
else else
@ -222,9 +229,7 @@ function lit.exam(doc)
break break
end end
end end
if not(err:isempty()) then if not (err:isempty()) then putserr("invalid_value", val, err) end
putserr("invalid_value", val, err)
end
end end
for key, val in pairs(metastruct[kind]) do for key, val in pairs(metastruct[kind]) do
@ -241,7 +246,7 @@ function lit.exam(doc)
-- Parses metadata -- Parses metadata
local function parsemeta(yaml) local function parsemeta(yaml)
local isok, res = pcall(pandoc.read, yaml) local isok, res = pcall(pandoc.read, yaml)
if isok and not(pandoc.utils.stringify(res.meta):isempty()) then if isok and not (pandoc.utils.stringify(res.meta):isempty()) then
declaration = pandoc.metatotable(res.meta) declaration = pandoc.metatotable(res.meta)
checkmeta("mandatory") checkmeta("mandatory")
checkmeta("optional") checkmeta("optional")
@ -287,7 +292,6 @@ function lit.exam(doc)
-- Calls literate functions -- Calls literate functions
local function call(el) local function call(el)
-- TODO -- TODO
-- Calls literate functions in code inlines -- Calls literate functions in code inlines
local function codecall(code) local function codecall(code)
@ -296,9 +300,9 @@ function lit.exam(doc)
local function metacall(meta) local function metacall(meta)
local function inlinescall(inlines) local function inlinescall(inlines)
return inlines:walk { return inlines:walk({
Code = function(code) return codecall(code) end Code = function(code) return codecall(code) end,
} })
end end
for key, val in pairs(meta) do for key, val in pairs(meta) do
@ -324,14 +328,14 @@ function lit.exam(doc)
-- Asserts literate status -- Asserts literate status
local function assert() local function assert()
if not(lit.status) then if not lit.status then
lit.puts("aborted") lit.puts("aborted")
os.exit(1) os.exit(1)
end end
end end
doc = doc:walk { CodeBlock = function(block) return extract(block) end } doc = doc:walk({ CodeBlock = function(block) return extract(block) end })
doc = doc:walk { Meta = function(meta) return call(meta) end } doc = doc:walk({ Meta = function(meta) return call(meta) end })
doc = doc:walk { Code = function(code) return call(code) end } doc = doc:walk({ Code = function(code) return call(code) end })
return doc:walk { Pandoc = function(_) assert() end } return doc:walk({ Pandoc = function(_) assert() end })
end end

View File

@ -2,8 +2,6 @@
local nat = {} local nat = {}
function nat.get(str) function nat.get(str) return str end
return str
end
return nat return nat