Ready for parsing functions

This commit is contained in:
perro tuerto 2023-03-15 16:33:53 -07:00
parent 30100855cf
commit 270a5ceb22
11 changed files with 103 additions and 98 deletions

67
dist/linp.lua vendored
View File

@ -6264,12 +6264,15 @@ local nat = {}
----------------------------------- LITERATE ----------------------------------
-- TODO
-- Variable for all literate stuff
local lit = {}
-- Ordered collection of fn call, fn declarations or literal elements
lit.collection = {}
-- Grammars
lit.g = {}
-- Lexical elements
lit.space = lpeg.S" \t\r\n"
lit.spot = (1 - lit.space)
@ -6288,8 +6291,8 @@ function lit.add_lit(str)
table.insert(lit.collection, {literal = str})
end
-- Grammar
lit.G = lpeg.P {
-- Inlines grammar
lit.g.inlines = lpeg.P {
"Doc";
Doc = (lpeg.V"Call" + lpeg.V"Declaration" + lpeg.V"Literal")^0;
Call = lit.t1^1 / lit.add_call;
@ -6298,6 +6301,7 @@ lit.G = lpeg.P {
}
-- Evals Lisp code
--[[
function lit.eval(code)
local is_passed, out = pcall (
function () return fennel.eval(code) end,
@ -6311,49 +6315,48 @@ function lit.eval(code)
end
return {is_passed = is_passed, preview = preview, out = out, lua = lua}
end
]]--
function lit.parse(raw)
lpeg.match(lit.G, raw) -- TODO
function lit.parse_declarations(codeblock)
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 = ""
for i, t in ipairs(lit.collection) do
for k, v in pairs(t) do
-- print(i, k, v)
print(i, k, v)
doc = doc .. v
end
end
return doc
]]--
return inlines
end
------------------------------------ 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 {
{
Block = function (block)
if is_chosen(block) then
raw = sanitize(block.content)
doc = lit.parse(raw)
-- print(doc)
end
-- Parses LiNP declarations
CodeBlock = function(codeblock)
return lit.parse_declarations(codeblock)
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,
}
}

11
scripts/make_jsons.sh Normal file
View File

@ -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

View File

@ -21,7 +21,7 @@ echo_help () {
# Obtains result as "pass" | "fail" | "diff" (AST doesn't match)
get_result () {
result=$([[ $1 -eq 0 ]] && echo "pass" || echo "fail")
result=$([[ $1 -ne 0 ]] && echo "fail" || echo "pass")
if [ -f $2.json ]; then
diff1=$(cat tmp.json | jq)
diff2=$(cat $2.json | jq)
@ -55,15 +55,15 @@ sh scripts/make_dist.sh
# Does tests
clear && echo "🐾 Starting tests"
for testfile in $FILES; do
echo "⚗️ $testfile:"
expectation=${testfile:6:4}
ast=$(pandoc -L $FILTER -t json -o tmp.json $testfile)
result=$(get_result $? $testfile)
for file in $FILES; do
echo "⚗️ $file:"
expectation=${file:6:4}
ast=$(pandoc -L $FILTER -t json -o tmp.json $file)
result=$(get_result $? $file)
echo " Expect: $expectation"
echo " Result: $result"
if [ "$VERBOSE" = true ]; then
pandoc -L $FILTER -t native $testfile
pandoc -L $FILTER -t native $file
fi
rm tmp.json
done

View File

@ -1,11 +1,14 @@
----------------------------------- LITERATE ----------------------------------
-- TODO
-- Variable for all literate stuff
local lit = {}
-- Ordered collection of fn call, fn declarations or literal elements
lit.collection = {}
-- Grammars
lit.g = {}
-- Lexical elements
lit.space = lpeg.S" \t\r\n"
lit.spot = (1 - lit.space)
@ -24,8 +27,8 @@ function lit.add_lit(str)
table.insert(lit.collection, {literal = str})
end
-- Grammar
lit.G = lpeg.P {
-- Inlines grammar
lit.g.inlines = lpeg.P {
"Doc";
Doc = (lpeg.V"Call" + lpeg.V"Declaration" + lpeg.V"Literal")^0;
Call = lit.t1^1 / lit.add_call;
@ -34,6 +37,7 @@ lit.G = lpeg.P {
}
-- Evals Lisp code
--[[
function lit.eval(code)
local is_passed, out = pcall (
function () return fennel.eval(code) end,
@ -47,17 +51,28 @@ function lit.eval(code)
end
return {is_passed = is_passed, preview = preview, out = out, lua = lua}
end
]]--
function lit.parse(raw)
lpeg.match(lit.G, raw) -- TODO
function lit.parse_declarations(codeblock)
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 = ""
for i, t in ipairs(lit.collection) do
for k, v in pairs(t) do
-- print(i, k, v)
print(i, k, v)
doc = doc .. v
end
end
return doc
]]--
return inlines
end
return lit

View File

@ -1,33 +1,21 @@
------------------------------------ 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 {
{
Block = function (block)
if is_chosen(block) then
raw = sanitize(block.content)
doc = lit.parse(raw)
-- print(doc)
end
-- Parses LiNP declarations
CodeBlock = function(codeblock)
return lit.parse_declarations(codeblock)
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,
}
}

View File

@ -13,11 +13,8 @@
# Invalid Calls
- fn1(1, $stdout: "true") has invalid rkwarg value
- fn1(1, $act: "return") has invalid rkwarg
- fn1(1, $eval: "/path/does/not/exists") has invalid path
- 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
- fn1(1, $action: "foo") has invalid rkwarg value
- fn1(1, $act: "shift") has invalid rkwarg
- fn1(fn4()); \fn4() is not declared
Invalid calls and declarations generate and collects errors.

View File

@ -28,23 +28,15 @@
Valid calls:
- fn1($stdout: true) gets stdout instead of evaluation result.
- fn1($action: "wipe") wipes calls and declarations of this function from
source document after all calls.
- fn2($action: "return") returns result after call.
- fn2($action: "hide") hides call from source document after call.
- fn2($action: "quote") quotes it without call.
- fn2($eval: "fn2.txt", $code: "fn2.fnl", $lua: "fn2.lua") writes
evaluation results, Fennel code and Lua code.
- fn3(4, $action: "return") has arg 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.
- fn1($results: "value") gets value of the last expression as result; it is the default setting.
- fn1($results: "output") gets stdout as result.
- fn2($action: "shift") shifts call for result.
- fn2($action: "quote") quotes it instead of calling it.
- fn3(4, $action: "shift") has arg and rkwarg.
- fn3($action: "shift", 5) has rkwarg and arg.
- fn4($action: "shift", a: 1, b: 2) has rkwarg and kwargs.
- fn4(a: 1, $action: "shift", b: 2) has kwarg, rkwarg and kwarg.
- fn4(a: 1, b: 2, $action: "shift") has kwargs and rkwarg.
- fn5(10) calls another function inside.
- fn6(9, 8) calls another quoted function inside.
- fn7(fn7(1)) calls several functions.

File diff suppressed because one or more lines are too long

View File

@ -26,4 +26,3 @@
- fn4 () space before `(`
- fn5) misses `(`
- fn6 misses `()`
- `fn1()` as inline code

View File

@ -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":"doesnt"},{"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":"doesnt"},{"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":"doesnt"},{"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":"doesnt"},{"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