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 = {}
-- 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
lit.newline = lpeg.P"\r"^-1 * lpeg.P"\n"
lit.space = lpeg.S" \t"
lit.anyspace = lpeg.S" \t\r\n"
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)
table.insert(lit.collection, {declaration = str})
end
function lit.add_lit(str)
table.insert(lit.collection, {literal = str})
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;
-- Blocks grammar
lit.g.block = lpeg.P {
"Block";
Block = lpeg.C(lpeg.V"YAML" * lpeg.V"Code");
YAML = lit.yaml_header * lit.yaml_body^0 * lit.yaml_footer;
Code = (lit.any + lit.newline)^0;
}
-- Evals Lisp code
@ -6322,23 +6316,16 @@ end
]]--
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
end
function lit.parse_inserts(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
]]--
-- print(code)
return code
end

View File

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

View File

@ -10,30 +10,24 @@ lit.collection = {}
lit.g = {}
-- 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
lit.newline = lpeg.P"\r"^-1 * lpeg.P"\n"
lit.space = lpeg.S" \t"
lit.anyspace = lpeg.S" \t\r\n"
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)
table.insert(lit.collection, {declaration = str})
end
function lit.add_lit(str)
table.insert(lit.collection, {literal = str})
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;
-- Blocks grammar
lit.g.block = lpeg.P {
"Block";
Block = lpeg.C(lpeg.V"YAML" * lpeg.V"Code");
YAML = lit.yaml_header * lit.yaml_body^0 * lit.yaml_footer;
Code = (lit.any + lit.newline)^0;
}
-- Evals Lisp code
@ -54,23 +48,16 @@ end
]]--
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
end
function lit.parse_inserts(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
]]--
-- print(code)
return code
end