64 lines
1.4 KiB
Lua
64 lines
1.4 KiB
Lua
----------------------------------- LITERATE ----------------------------------
|
|
|
|
-- TODO
|
|
local lit = {}
|
|
|
|
-- Ordered collection of fn call, fn declarations or literal elements
|
|
lit.collection = {}
|
|
|
|
-- Lexical elements
|
|
lit.space = lpeg.S" \t\r\n"
|
|
lit.spot = (1 - lit.space)
|
|
lit.t1 = lpeg.S"()" -- TMP
|
|
lit.t2 = (1 - (lit.t1 + lit.space)) -- TMP
|
|
|
|
function lit.add_call(str)
|
|
table.insert(lit.collection, {call = str})
|
|
end
|
|
|
|
function lit.add_declaration(str)
|
|
table.insert(lit.collection, {declaration = str})
|
|
end
|
|
|
|
function lit.add_lit(str)
|
|
table.insert(lit.collection, {literal = str})
|
|
end
|
|
|
|
-- Grammar
|
|
lit.G = lpeg.P {
|
|
"Doc";
|
|
Doc = (lpeg.V"Call" + lpeg.V"Declaration" + lpeg.V"Literal")^0;
|
|
Call = lit.t1^1 / lit.add_call;
|
|
Declaration = lit.t2^1 / lit.add_declaration;
|
|
Literal = lit.space^1 / lit.add_lit;
|
|
}
|
|
|
|
-- Evals Lisp code
|
|
function lit.eval(code)
|
|
local is_passed, out = pcall (
|
|
function () return fennel.eval(code) end,
|
|
function (e) return e end
|
|
)
|
|
local lua = ""
|
|
local out = tostring(out)
|
|
local preview = out:gsub("\n.*", "")
|
|
if is_passed then
|
|
lua = fennel.compileString(code)
|
|
end
|
|
return {is_passed = is_passed, preview = preview, out = out, lua = lua}
|
|
end
|
|
|
|
function lit.parse(raw)
|
|
lpeg.match(lit.G, raw) -- TODO
|
|
local doc = ""
|
|
for i, t in ipairs(lit.collection) do
|
|
for k, v in pairs(t) do
|
|
-- print(i, k, v)
|
|
doc = doc .. v
|
|
end
|
|
end
|
|
return doc
|
|
end
|
|
|
|
return lit
|