Init parsing literate blocks

This commit is contained in:
perro tuerto 2023-03-16 17:00:24 -07:00
parent 6bd36cfc16
commit f6f39ee093
3 changed files with 57 additions and 81 deletions

57
dist/lin.lua vendored
View File

@ -6278,30 +6278,24 @@ lit.collection = {}
lit.g = {} lit.g = {}
-- Lexical elements -- Lexical elements
lit.space = lpeg.S" \t\r\n" lit.newline = lpeg.P"\r"^-1 * lpeg.P"\n"
lit.spot = (1 - lit.space) lit.space = lpeg.S" \t"
lit.t1 = lpeg.S"()" -- TMP lit.anyspace = lpeg.S" \t\r\n"
lit.t2 = (1 - (lit.t1 + lit.space)) -- TMP lit.spot = (1 - lit.anyspace)
lit.any = (lit.spot + lit.space)
lit.id = lpeg.R("az", "AZ") * lpeg.R("az", "AZ", "09")^0
lit.ref = lpeg.P"#" * lit.id
lit.yaml_header = lit.space^0 * lpeg.P"---" * lit.space^0 * lit.newline
lit.yaml_body = -lpeg.P"." * lit.any^0 * lit.newline
lit.yaml_footer = lit.space^0 * lpeg.P"..." * lit.space^0 * lit.newline
function lit.add_call(str)
table.insert(lit.collection, {call = str})
end
function lit.add_declaration(str) -- Blocks grammar
table.insert(lit.collection, {declaration = str}) lit.g.block = lpeg.P {
end "Block";
Block = lpeg.C(lpeg.V"YAML" * lpeg.V"Code");
function lit.add_lit(str) YAML = lit.yaml_header * lit.yaml_body^0 * lit.yaml_footer;
table.insert(lit.collection, {literal = str}) Code = (lit.any + lit.newline)^0;
end
-- 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;
Declaration = lit.t2^1 / lit.add_declaration;
Literal = lit.space^1 / lit.add_lit;
} }
-- Evals Lisp code -- Evals Lisp code
@ -6322,23 +6316,16 @@ end
]]-- ]]--
function lit.parse_blocks(codeblock) function lit.parse_blocks(codeblock)
print(codeblock) parsed = lpeg.match(lit.g.block, codeblock.text)
if codeblock.text ~= parsed then
print(codeblock.text)
print(parsed)
end
return codeblock return codeblock
end end
function lit.parse_inserts(code) function lit.parse_inserts(code)
print(code) -- print(code)
-- 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)
doc = doc .. v
end
end
return doc
]]--
return code return code
end end

View File

@ -4,18 +4,20 @@
FILTER=dist/lin.lua FILTER=dist/lin.lua
FILES="tests/*.md" FILES="tests/*.md"
VERBOSE=false VERBOSE=false
AST=false
CMD="sh $0" CMD="sh $0"
# Prints help # Prints help
echo_help () { echo_help () {
echo "Usage:" echo "Usage:"
echo " $CMD [-vh] [FILES]" echo " $CMD [-vah] [FILES]"
echo "Examples:" echo "Examples:"
echo " $CMD Tests with 'tests/*.md'" echo " $CMD Tests for 'tests/*.md'"
echo " $CMD -v Verbose tests with 'tests/*.md'" echo " $CMD -v Verbose tests for 'tests/*.md'"
echo " $CMD tests/fail* Tests with 'tests/fail*'" echo " $CMD -a Tests and AST for 'tests/*.md'"
echo " $CMD -v tests/fail* Verbose tests with 'tests/fail*'" echo " $CMD tests/fail* Tests for 'tests/fail*'"
echo " $CMD -h Display this help" echo " $CMD -va tests/fail* Verbose tests and AST for 'tests/fail*'"
echo " $CMD -h Display this help"
exit exit
} }
@ -32,11 +34,12 @@ get_result () {
} }
# Checks options # Checks options
while getopts ':vh' opt; do while getopts ':vah' opt; do
case "$opt" in case "$opt" in
v) VERBOSE=true ; shift ;; v) VERBOSE=true ; shift ;;
a) AST=true ; shift ;;
h) echo_help ;; h) echo_help ;;
?) echo "ERROR: only -v or -h is allowed" ; exit 1 ;; ?) echo "ERROR: only -v, -a or -h is allowed" ; exit 1 ;;
esac esac
done done
@ -62,8 +65,7 @@ for file in $FILES; do
result=$(get_result $? $file) result=$(get_result $? $file)
echo " Expect: $expectation" echo " Expect: $expectation"
echo " Result: $result" echo " Result: $result"
if [ "$VERBOSE" = true ]; then [ "$VERBOSE" = true ] && echo -e "$ast"
pandoc -L $FILTER -t native $file [ "$AST" = true ] && pandoc -L $FILTER -t native $file
fi
rm tmp.json rm tmp.json
done done

View File

@ -10,30 +10,24 @@ lit.collection = {}
lit.g = {} lit.g = {}
-- Lexical elements -- Lexical elements
lit.space = lpeg.S" \t\r\n" lit.newline = lpeg.P"\r"^-1 * lpeg.P"\n"
lit.spot = (1 - lit.space) lit.space = lpeg.S" \t"
lit.t1 = lpeg.S"()" -- TMP lit.anyspace = lpeg.S" \t\r\n"
lit.t2 = (1 - (lit.t1 + lit.space)) -- TMP lit.spot = (1 - lit.anyspace)
lit.any = (lit.spot + lit.space)
lit.id = lpeg.R("az", "AZ") * lpeg.R("az", "AZ", "09")^0
lit.ref = lpeg.P"#" * lit.id
lit.yaml_header = lit.space^0 * lpeg.P"---" * lit.space^0 * lit.newline
lit.yaml_body = -lpeg.P"." * lit.any^0 * lit.newline
lit.yaml_footer = lit.space^0 * lpeg.P"..." * lit.space^0 * lit.newline
function lit.add_call(str)
table.insert(lit.collection, {call = str})
end
function lit.add_declaration(str) -- Blocks grammar
table.insert(lit.collection, {declaration = str}) lit.g.block = lpeg.P {
end "Block";
Block = lpeg.C(lpeg.V"YAML" * lpeg.V"Code");
function lit.add_lit(str) YAML = lit.yaml_header * lit.yaml_body^0 * lit.yaml_footer;
table.insert(lit.collection, {literal = str}) Code = (lit.any + lit.newline)^0;
end
-- 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;
Declaration = lit.t2^1 / lit.add_declaration;
Literal = lit.space^1 / lit.add_lit;
} }
-- Evals Lisp code -- Evals Lisp code
@ -54,23 +48,16 @@ end
]]-- ]]--
function lit.parse_blocks(codeblock) function lit.parse_blocks(codeblock)
print(codeblock) parsed = lpeg.match(lit.g.block, codeblock.text)
if codeblock.text ~= parsed then
print(codeblock.text)
print(parsed)
end
return codeblock return codeblock
end end
function lit.parse_inserts(code) function lit.parse_inserts(code)
print(code) -- print(code)
-- 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)
doc = doc .. v
end
end
return doc
]]--
return code return code
end end