Ready for parsing functions
This commit is contained in:
parent
30100855cf
commit
270a5ceb22
|
@ -6264,12 +6264,15 @@ local nat = {}
|
||||||
|
|
||||||
----------------------------------- LITERATE ----------------------------------
|
----------------------------------- LITERATE ----------------------------------
|
||||||
|
|
||||||
-- TODO
|
-- Variable for all literate stuff
|
||||||
local lit = {}
|
local lit = {}
|
||||||
|
|
||||||
-- Ordered collection of fn call, fn declarations or literal elements
|
-- Ordered collection of fn call, fn declarations or literal elements
|
||||||
lit.collection = {}
|
lit.collection = {}
|
||||||
|
|
||||||
|
-- Grammars
|
||||||
|
lit.g = {}
|
||||||
|
|
||||||
-- Lexical elements
|
-- Lexical elements
|
||||||
lit.space = lpeg.S" \t\r\n"
|
lit.space = lpeg.S" \t\r\n"
|
||||||
lit.spot = (1 - lit.space)
|
lit.spot = (1 - lit.space)
|
||||||
|
@ -6288,8 +6291,8 @@ function lit.add_lit(str)
|
||||||
table.insert(lit.collection, {literal = str})
|
table.insert(lit.collection, {literal = str})
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Grammar
|
-- Inlines grammar
|
||||||
lit.G = lpeg.P {
|
lit.g.inlines = lpeg.P {
|
||||||
"Doc";
|
"Doc";
|
||||||
Doc = (lpeg.V"Call" + lpeg.V"Declaration" + lpeg.V"Literal")^0;
|
Doc = (lpeg.V"Call" + lpeg.V"Declaration" + lpeg.V"Literal")^0;
|
||||||
Call = lit.t1^1 / lit.add_call;
|
Call = lit.t1^1 / lit.add_call;
|
||||||
|
@ -6298,6 +6301,7 @@ lit.G = lpeg.P {
|
||||||
}
|
}
|
||||||
|
|
||||||
-- Evals Lisp code
|
-- Evals Lisp code
|
||||||
|
--[[
|
||||||
function lit.eval(code)
|
function lit.eval(code)
|
||||||
local is_passed, out = pcall (
|
local is_passed, out = pcall (
|
||||||
function () return fennel.eval(code) end,
|
function () return fennel.eval(code) end,
|
||||||
|
@ -6311,49 +6315,48 @@ function lit.eval(code)
|
||||||
end
|
end
|
||||||
return {is_passed = is_passed, preview = preview, out = out, lua = lua}
|
return {is_passed = is_passed, preview = preview, out = out, lua = lua}
|
||||||
end
|
end
|
||||||
|
]]--
|
||||||
|
|
||||||
function lit.parse(raw)
|
function lit.parse_declarations(codeblock)
|
||||||
lpeg.match(lit.G, raw) -- TODO
|
print(codeblock)
|
||||||
|
return codeblock
|
||||||
|
end
|
||||||
|
|
||||||
|
function lit.parse_calls(inlines)
|
||||||
|
str = pandoc.utils.stringify(inlines)
|
||||||
|
print(str)
|
||||||
|
-- lpeg.match(lit.g.inlines, raw)
|
||||||
|
--[[
|
||||||
local doc = ""
|
local doc = ""
|
||||||
for i, t in ipairs(lit.collection) do
|
for i, t in ipairs(lit.collection) do
|
||||||
for k, v in pairs(t) do
|
for k, v in pairs(t) do
|
||||||
-- print(i, k, v)
|
print(i, k, v)
|
||||||
doc = doc .. v
|
doc = doc .. v
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return doc
|
return doc
|
||||||
|
]]--
|
||||||
|
return inlines
|
||||||
end
|
end
|
||||||
|
|
||||||
------------------------------------ PANDOC -----------------------------------
|
------------------------------------ PANDOC -----------------------------------
|
||||||
|
|
||||||
local function is_chosen(block)
|
|
||||||
chosen, content = true, block.content
|
|
||||||
if content == nil or pandoc.utils.type(content) ~= "Inlines" then
|
|
||||||
chosen = false
|
|
||||||
end
|
|
||||||
return chosen
|
|
||||||
end
|
|
||||||
|
|
||||||
local function sanitize(inlines)
|
|
||||||
for i, inline in ipairs(inlines) do
|
|
||||||
if inline.tag == "Code" then
|
|
||||||
inlines:remove(i)
|
|
||||||
elseif inline.tag == "Quoted" then
|
|
||||||
str = pandoc.utils.stringify(inline.content):gsub('"', '\\"')
|
|
||||||
inlines[i] = '"' .. str .. '"'
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return pandoc.utils.stringify(inlines)
|
|
||||||
end
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
{
|
{
|
||||||
Block = function (block)
|
-- Parses LiNP declarations
|
||||||
if is_chosen(block) then
|
CodeBlock = function(codeblock)
|
||||||
raw = sanitize(block.content)
|
return lit.parse_declarations(codeblock)
|
||||||
doc = lit.parse(raw)
|
end,
|
||||||
-- print(doc)
|
},
|
||||||
end
|
{
|
||||||
|
-- Avoids quotes conversion in stringify
|
||||||
|
Quoted = function(quoted)
|
||||||
|
str = pandoc.utils.stringify(quoted.content):gsub('"', '\\"')
|
||||||
|
return '"' .. str .. '"'
|
||||||
|
end,
|
||||||
|
-- Parses and does LiNP calls
|
||||||
|
Inlines = function(inlines)
|
||||||
|
return lit.parse_calls(inlines)
|
||||||
end,
|
end,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,11 @@
|
||||||
|
# Makes JSON test files
|
||||||
|
|
||||||
|
# Variables
|
||||||
|
FILTER=dist/linp.lua
|
||||||
|
FILES="tests/*.md"
|
||||||
|
|
||||||
|
for file in $FILES; do
|
||||||
|
if [ -f $file.json ]; then
|
||||||
|
pandoc -L $FILTER -o $file.json $file
|
||||||
|
fi
|
||||||
|
done
|
|
@ -21,7 +21,7 @@ echo_help () {
|
||||||
|
|
||||||
# Obtains result as "pass" | "fail" | "diff" (AST doesn't match)
|
# Obtains result as "pass" | "fail" | "diff" (AST doesn't match)
|
||||||
get_result () {
|
get_result () {
|
||||||
result=$([[ $1 -eq 0 ]] && echo "pass" || echo "fail")
|
result=$([[ $1 -ne 0 ]] && echo "fail" || echo "pass")
|
||||||
if [ -f $2.json ]; then
|
if [ -f $2.json ]; then
|
||||||
diff1=$(cat tmp.json | jq)
|
diff1=$(cat tmp.json | jq)
|
||||||
diff2=$(cat $2.json | jq)
|
diff2=$(cat $2.json | jq)
|
||||||
|
@ -55,15 +55,15 @@ sh scripts/make_dist.sh
|
||||||
|
|
||||||
# Does tests
|
# Does tests
|
||||||
clear && echo "🐾 Starting tests"
|
clear && echo "🐾 Starting tests"
|
||||||
for testfile in $FILES; do
|
for file in $FILES; do
|
||||||
echo "⚗️ $testfile:"
|
echo "⚗️ $file:"
|
||||||
expectation=${testfile:6:4}
|
expectation=${file:6:4}
|
||||||
ast=$(pandoc -L $FILTER -t json -o tmp.json $testfile)
|
ast=$(pandoc -L $FILTER -t json -o tmp.json $file)
|
||||||
result=$(get_result $? $testfile)
|
result=$(get_result $? $file)
|
||||||
echo " Expect: $expectation"
|
echo " Expect: $expectation"
|
||||||
echo " Result: $result"
|
echo " Result: $result"
|
||||||
if [ "$VERBOSE" = true ]; then
|
if [ "$VERBOSE" = true ]; then
|
||||||
pandoc -L $FILTER -t native $testfile
|
pandoc -L $FILTER -t native $file
|
||||||
fi
|
fi
|
||||||
rm tmp.json
|
rm tmp.json
|
||||||
done
|
done
|
||||||
|
|
|
@ -1,11 +1,14 @@
|
||||||
----------------------------------- LITERATE ----------------------------------
|
----------------------------------- LITERATE ----------------------------------
|
||||||
|
|
||||||
-- TODO
|
-- Variable for all literate stuff
|
||||||
local lit = {}
|
local lit = {}
|
||||||
|
|
||||||
-- Ordered collection of fn call, fn declarations or literal elements
|
-- Ordered collection of fn call, fn declarations or literal elements
|
||||||
lit.collection = {}
|
lit.collection = {}
|
||||||
|
|
||||||
|
-- Grammars
|
||||||
|
lit.g = {}
|
||||||
|
|
||||||
-- Lexical elements
|
-- Lexical elements
|
||||||
lit.space = lpeg.S" \t\r\n"
|
lit.space = lpeg.S" \t\r\n"
|
||||||
lit.spot = (1 - lit.space)
|
lit.spot = (1 - lit.space)
|
||||||
|
@ -24,8 +27,8 @@ function lit.add_lit(str)
|
||||||
table.insert(lit.collection, {literal = str})
|
table.insert(lit.collection, {literal = str})
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Grammar
|
-- Inlines grammar
|
||||||
lit.G = lpeg.P {
|
lit.g.inlines = lpeg.P {
|
||||||
"Doc";
|
"Doc";
|
||||||
Doc = (lpeg.V"Call" + lpeg.V"Declaration" + lpeg.V"Literal")^0;
|
Doc = (lpeg.V"Call" + lpeg.V"Declaration" + lpeg.V"Literal")^0;
|
||||||
Call = lit.t1^1 / lit.add_call;
|
Call = lit.t1^1 / lit.add_call;
|
||||||
|
@ -34,6 +37,7 @@ lit.G = lpeg.P {
|
||||||
}
|
}
|
||||||
|
|
||||||
-- Evals Lisp code
|
-- Evals Lisp code
|
||||||
|
--[[
|
||||||
function lit.eval(code)
|
function lit.eval(code)
|
||||||
local is_passed, out = pcall (
|
local is_passed, out = pcall (
|
||||||
function () return fennel.eval(code) end,
|
function () return fennel.eval(code) end,
|
||||||
|
@ -47,17 +51,28 @@ function lit.eval(code)
|
||||||
end
|
end
|
||||||
return {is_passed = is_passed, preview = preview, out = out, lua = lua}
|
return {is_passed = is_passed, preview = preview, out = out, lua = lua}
|
||||||
end
|
end
|
||||||
|
]]--
|
||||||
|
|
||||||
function lit.parse(raw)
|
function lit.parse_declarations(codeblock)
|
||||||
lpeg.match(lit.G, raw) -- TODO
|
print(codeblock)
|
||||||
|
return codeblock
|
||||||
|
end
|
||||||
|
|
||||||
|
function lit.parse_calls(inlines)
|
||||||
|
str = pandoc.utils.stringify(inlines)
|
||||||
|
print(str)
|
||||||
|
-- lpeg.match(lit.g.inlines, raw)
|
||||||
|
--[[
|
||||||
local doc = ""
|
local doc = ""
|
||||||
for i, t in ipairs(lit.collection) do
|
for i, t in ipairs(lit.collection) do
|
||||||
for k, v in pairs(t) do
|
for k, v in pairs(t) do
|
||||||
-- print(i, k, v)
|
print(i, k, v)
|
||||||
doc = doc .. v
|
doc = doc .. v
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return doc
|
return doc
|
||||||
|
]]--
|
||||||
|
return inlines
|
||||||
end
|
end
|
||||||
|
|
||||||
return lit
|
return lit
|
||||||
|
|
|
@ -1,33 +1,21 @@
|
||||||
------------------------------------ PANDOC -----------------------------------
|
------------------------------------ PANDOC -----------------------------------
|
||||||
|
|
||||||
local function is_chosen(block)
|
|
||||||
chosen, content = true, block.content
|
|
||||||
if content == nil or pandoc.utils.type(content) ~= "Inlines" then
|
|
||||||
chosen = false
|
|
||||||
end
|
|
||||||
return chosen
|
|
||||||
end
|
|
||||||
|
|
||||||
local function sanitize(inlines)
|
|
||||||
for i, inline in ipairs(inlines) do
|
|
||||||
if inline.tag == "Code" then
|
|
||||||
inlines:remove(i)
|
|
||||||
elseif inline.tag == "Quoted" then
|
|
||||||
str = pandoc.utils.stringify(inline.content):gsub('"', '\\"')
|
|
||||||
inlines[i] = '"' .. str .. '"'
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return pandoc.utils.stringify(inlines)
|
|
||||||
end
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
{
|
{
|
||||||
Block = function (block)
|
-- Parses LiNP declarations
|
||||||
if is_chosen(block) then
|
CodeBlock = function(codeblock)
|
||||||
raw = sanitize(block.content)
|
return lit.parse_declarations(codeblock)
|
||||||
doc = lit.parse(raw)
|
end,
|
||||||
-- print(doc)
|
},
|
||||||
end
|
{
|
||||||
|
-- Avoids quotes conversion in stringify
|
||||||
|
Quoted = function(quoted)
|
||||||
|
str = pandoc.utils.stringify(quoted.content):gsub('"', '\\"')
|
||||||
|
return '"' .. str .. '"'
|
||||||
|
end,
|
||||||
|
-- Parses and does LiNP calls
|
||||||
|
Inlines = function(inlines)
|
||||||
|
return lit.parse_calls(inlines)
|
||||||
end,
|
end,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,11 +13,8 @@
|
||||||
|
|
||||||
# Invalid Calls
|
# Invalid Calls
|
||||||
|
|
||||||
- fn1(1, $stdout: "true") has invalid rkwarg value
|
- fn1(1, $action: "foo") has invalid rkwarg value
|
||||||
- fn1(1, $act: "return") has invalid rkwarg
|
- fn1(1, $act: "shift") has invalid rkwarg
|
||||||
- fn1(1, $eval: "/path/does/not/exists") has invalid path
|
- fn1(fn4()); \fn4() is not declared
|
||||||
- fn1(1, $code: "/path/does/not/exists") has invalid path
|
|
||||||
- fn1(1, $lua: "/path/does/not/exists") has invalid path
|
|
||||||
- fn1(fn4()); `fn4()` is not declared
|
|
||||||
|
|
||||||
Invalid calls and declarations generate and collects errors.
|
Invalid calls and declarations generate and collects errors.
|
||||||
|
|
|
@ -28,23 +28,15 @@
|
||||||
|
|
||||||
Valid calls:
|
Valid calls:
|
||||||
|
|
||||||
- fn1($stdout: true) gets stdout instead of evaluation result.
|
- fn1($results: "value") gets value of the last expression as result; it is the default setting.
|
||||||
- fn1($action: "wipe") wipes calls and declarations of this function from
|
- fn1($results: "output") gets stdout as result.
|
||||||
source document after all calls.
|
- fn2($action: "shift") shifts call for result.
|
||||||
- fn2($action: "return") returns result after call.
|
- fn2($action: "quote") quotes it instead of calling it.
|
||||||
- fn2($action: "hide") hides call from source document after call.
|
- fn3(4, $action: "shift") has arg and rkwarg.
|
||||||
- fn2($action: "quote") quotes it without call.
|
- fn3($action: "shift", 5) has rkwarg and arg.
|
||||||
- fn2($eval: "fn2.txt", $code: "fn2.fnl", $lua: "fn2.lua") writes
|
- fn4($action: "shift", a: 1, b: 2) has rkwarg and kwargs.
|
||||||
evaluation results, Fennel code and Lua code.
|
- fn4(a: 1, $action: "shift", b: 2) has kwarg, rkwarg and kwarg.
|
||||||
- fn3(4, $action: "return") has arg and rkwarg.
|
- fn4(a: 1, b: 2, $action: "shift") has kwargs and rkwarg.
|
||||||
- fn3($action: "return", 5) has rkwarg and arg.
|
|
||||||
- fn3($eval: "fn3-4.txt", $code: "fn3-4.fnl", $lua: "fn3-4.lua") writes in
|
|
||||||
same files than below.
|
|
||||||
- fn4(a: 1, b: 2, $action: "return") has kwargs and rkwarg.
|
|
||||||
- fn4($action: "return", a: 1, b: 2) has rkwarg and kwargs.
|
|
||||||
- fn4(a: 1, $action: "return", b: 2) has kwarg, rkwarg and kwarg.
|
|
||||||
- fn4($eval: "fn3-4.txt", $code: "fn3-4.fnl", $lua: "fn3-4.lua") writes in
|
|
||||||
same files than above.
|
|
||||||
- fn5(10) calls another function inside.
|
- fn5(10) calls another function inside.
|
||||||
- fn6(9, 8) calls another quoted function inside.
|
- fn6(9, 8) calls another quoted function inside.
|
||||||
- fn7(fn7(1)) calls several functions.
|
- fn7(fn7(1)) calls several functions.
|
||||||
|
|
File diff suppressed because one or more lines are too long
|
@ -26,4 +26,3 @@
|
||||||
- fn4 () space before `(`
|
- fn4 () space before `(`
|
||||||
- fn5) misses `(`
|
- fn5) misses `(`
|
||||||
- fn6 misses `()`
|
- fn6 misses `()`
|
||||||
- `fn1()` as inline code
|
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
{"pandoc-api-version":[1,23],"meta":{},"blocks":[{"t":"Header","c":[1,["not-declarations",[],[]],[{"t":"Str","c":"Not"},{"t":"Space"},{"t":"Str","c":"declarations"}]]},{"t":"CodeBlock","c":[["",[],[]],"-- skipped: \\fn1()\n1 + 2\n\n-- doesn't starts with `%a` because of the space: fn 2()\n1 + 2\n\n-- doesn't continue with `%w`: fn-3()\n1 + 2\n\n-- space before `(`: fn4 ()\n1 + 2\n\n-- misses `(`: fn5) \n1 + 2\n\n-- misses `()`: fn6 \n1 + 2"]},{"t":"Header","c":[1,["not-calls",[],[]],[{"t":"Str","c":"Not"},{"t":"Space"},{"t":"Str","c":"calls"}]]},{"t":"BulletList","c":[[{"t":"Plain","c":[{"t":"RawInline","c":["tex","\\fn1"]},{"t":"Str","c":"()"},{"t":"Space"},{"t":"Str","c":"skipped"}]}],[{"t":"Plain","c":[{"t":"Str","c":"fn"},{"t":"Space"},{"t":"Str","c":"2()"},{"t":"Space"},{"t":"Str","c":"doesn’t"},{"t":"Space"},{"t":"Str","c":"starts"},{"t":"Space"},{"t":"Str","c":"with"},{"t":"Space"},{"t":"Code","c":[["",[],[]],"%a"]},{"t":"Space"},{"t":"Str","c":"because"},{"t":"Space"},{"t":"Str","c":"of"},{"t":"Space"},{"t":"Str","c":"the"},{"t":"Space"},{"t":"Str","c":"space"}]}],[{"t":"Plain","c":[{"t":"Str","c":"fn-3()"},{"t":"Space"},{"t":"Str","c":"doesn’t"},{"t":"Space"},{"t":"Str","c":"continue"},{"t":"Space"},{"t":"Str","c":"with"},{"t":"Space"},{"t":"Code","c":[["",[],[]],"%w"]}]}],[{"t":"Plain","c":[{"t":"Str","c":"fn4"},{"t":"Space"},{"t":"Str","c":"()"},{"t":"Space"},{"t":"Str","c":"space"},{"t":"Space"},{"t":"Str","c":"before"},{"t":"Space"},{"t":"Code","c":[["",[],[]],"("]}]}],[{"t":"Plain","c":[{"t":"Str","c":"fn5)"},{"t":"Space"},{"t":"Str","c":"misses"},{"t":"Space"},{"t":"Code","c":[["",[],[]],"("]}]}],[{"t":"Plain","c":[{"t":"Str","c":"fn6"},{"t":"Space"},{"t":"Str","c":"misses"},{"t":"Space"},{"t":"Code","c":[["",[],[]],"()"]}]}],[{"t":"Plain","c":[{"t":"Code","c":[["",[],[]],"fn1()"]},{"t":"Space"},{"t":"Str","c":"as"},{"t":"Space"},{"t":"Str","c":"inline"},{"t":"Space"},{"t":"Str","c":"code"}]}]]}]}
|
{"pandoc-api-version":[1,23],"meta":{},"blocks":[{"t":"Header","c":[1,["not-declarations",[],[]],[{"t":"Str","c":"Not"},{"t":"Space"},{"t":"Str","c":"declarations"}]]},{"t":"CodeBlock","c":[["",[],[]],"-- skipped: \\fn1()\n1 + 2\n\n-- doesn't starts with `%a` because of the space: fn 2()\n1 + 2\n\n-- doesn't continue with `%w`: fn-3()\n1 + 2\n\n-- space before `(`: fn4 ()\n1 + 2\n\n-- misses `(`: fn5) \n1 + 2\n\n-- misses `()`: fn6 \n1 + 2"]},{"t":"Header","c":[1,["not-calls",[],[]],[{"t":"Str","c":"Not"},{"t":"Space"},{"t":"Str","c":"calls"}]]},{"t":"BulletList","c":[[{"t":"Plain","c":[{"t":"RawInline","c":["tex","\\fn1"]},{"t":"Str","c":"()"},{"t":"Space"},{"t":"Str","c":"skipped"}]}],[{"t":"Plain","c":[{"t":"Str","c":"fn"},{"t":"Space"},{"t":"Str","c":"2()"},{"t":"Space"},{"t":"Str","c":"doesn’t"},{"t":"Space"},{"t":"Str","c":"starts"},{"t":"Space"},{"t":"Str","c":"with"},{"t":"Space"},{"t":"Code","c":[["",[],[]],"%a"]},{"t":"Space"},{"t":"Str","c":"because"},{"t":"Space"},{"t":"Str","c":"of"},{"t":"Space"},{"t":"Str","c":"the"},{"t":"Space"},{"t":"Str","c":"space"}]}],[{"t":"Plain","c":[{"t":"Str","c":"fn-3()"},{"t":"Space"},{"t":"Str","c":"doesn’t"},{"t":"Space"},{"t":"Str","c":"continue"},{"t":"Space"},{"t":"Str","c":"with"},{"t":"Space"},{"t":"Code","c":[["",[],[]],"%w"]}]}],[{"t":"Plain","c":[{"t":"Str","c":"fn4"},{"t":"Space"},{"t":"Str","c":"()"},{"t":"Space"},{"t":"Str","c":"space"},{"t":"Space"},{"t":"Str","c":"before"},{"t":"Space"},{"t":"Code","c":[["",[],[]],"("]}]}],[{"t":"Plain","c":[{"t":"Str","c":"fn5)"},{"t":"Space"},{"t":"Str","c":"misses"},{"t":"Space"},{"t":"Code","c":[["",[],[]],"("]}]}],[{"t":"Plain","c":[{"t":"Str","c":"fn6"},{"t":"Space"},{"t":"Str","c":"misses"},{"t":"Space"},{"t":"Code","c":[["",[],[]],"()"]}]}]]}]}
|
||||||
|
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue