Literate blocks parses; TODO: validation

This commit is contained in:
perro tuerto 2023-03-17 19:04:41 -07:00
parent f6f39ee093
commit 2ec9e0bd4b
5 changed files with 131 additions and 31 deletions

73
dist/lin.lua vendored
View File

@ -6268,11 +6268,17 @@ end
----------------------------------- LITERATE ----------------------------------
-- Types functions extensions
function string:linearize()
return self:gsub("\n", "\\\\n")
end
-- Variable for all literate stuff
local lit = {}
-- Ordered collection of fn call, fn declarations or literal elements
lit.collection = {}
-- Error collector
lit.e = {}
-- Grammars
lit.g = {}
@ -6283,19 +6289,18 @@ 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
lit.yaml_footer = lit.space^0 * lpeg.P"..." * lit.space^0 * lit.newline^-1
lit.yaml_body = -lit.yaml_footer * lit.any^0 * lit.newline
lit.id = lpeg.R("az", "AZ") * lpeg.R("az", "AZ", "09")^0
lit.ref = lpeg.P"#" * lit.id
-- 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;
Block = lpeg.Ct(lpeg.V"YAML" * lpeg.V"Code", "name");
YAML = lpeg.C(lit.yaml_header * lit.yaml_body^0 * lit.yaml_footer);
Code = lpeg.C((lit.any + lit.newline)^0);
}
-- Evals Lisp code
@ -6315,12 +6320,50 @@ function lit.eval(code)
end
]]--
function lit.parse_blocks(codeblock)
parsed = lpeg.match(lit.g.block, codeblock.text)
if codeblock.text ~= parsed then
print(codeblock.text)
print(parsed)
function lit.debug_level(str)
if str == "ERROR" then
return 2
elseif str == "WARNING" then
return 1
else
return 0
end
end
function lit.puts(msg, level)
local verbosity = lit.debug_level(PANDOC_STATE.verbosity)
level = lit.debug_level(level)
if level >= verbosity then
print("[" .. PANDOC_STATE.verbosity .. "] " .. msg)
end
end
function lit.add_error(err)
end
function lit.check_yaml(raw_yaml)
local yaml = io.popen("pandoc -t json <<<\"" .. raw_yaml .. "\" 2>&1")
if yaml:read("*l"):sub(1, 1) == "{" then
yaml = pandoc.read(raw_yaml)
return yaml
else
lit.add_error(err)
return nil
end
end
function lit.check_block(parsed)
lit.puts("Parsing " .. table.concat(parsed, "\n"):linearize())
local yaml = lit.check_yaml(parsed[1])
return "TODO"
end
function lit.parse_blocks(codeblock)
local parsed = lpeg.match(lit.g.block, codeblock.text)
local valid = (parsed ~= nil and lit.check_block(parsed) or false)
return codeblock
end

View File

@ -1,10 +1,16 @@
----------------------------------- LITERATE ----------------------------------
-- Types functions extensions
function string:linearize()
return self:gsub("\n", "\\\\n")
end
-- Variable for all literate stuff
local lit = {}
-- Ordered collection of fn call, fn declarations or literal elements
lit.collection = {}
-- Error collector
lit.e = {}
-- Grammars
lit.g = {}
@ -15,19 +21,18 @@ 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
lit.yaml_footer = lit.space^0 * lpeg.P"..." * lit.space^0 * lit.newline^-1
lit.yaml_body = -lit.yaml_footer * lit.any^0 * lit.newline
lit.id = lpeg.R("az", "AZ") * lpeg.R("az", "AZ", "09")^0
lit.ref = lpeg.P"#" * lit.id
-- 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;
Block = lpeg.Ct(lpeg.V"YAML" * lpeg.V"Code", "name");
YAML = lpeg.C(lit.yaml_header * lit.yaml_body^0 * lit.yaml_footer);
Code = lpeg.C((lit.any + lit.newline)^0);
}
-- Evals Lisp code
@ -47,12 +52,50 @@ function lit.eval(code)
end
]]--
function lit.parse_blocks(codeblock)
parsed = lpeg.match(lit.g.block, codeblock.text)
if codeblock.text ~= parsed then
print(codeblock.text)
print(parsed)
function lit.debug_level(str)
if str == "ERROR" then
return 2
elseif str == "WARNING" then
return 1
else
return 0
end
end
function lit.puts(msg, level)
local verbosity = lit.debug_level(PANDOC_STATE.verbosity)
level = lit.debug_level(level)
if level >= verbosity then
print("[" .. PANDOC_STATE.verbosity .. "] " .. msg)
end
end
function lit.add_error(err)
end
function lit.check_yaml(raw_yaml)
local yaml = io.popen("pandoc -t json <<<\"" .. raw_yaml .. "\" 2>&1")
if yaml:read("*l"):sub(1, 1) == "{" then
yaml = pandoc.read(raw_yaml)
return yaml
else
lit.add_error(err)
return nil
end
end
function lit.check_block(parsed)
lit.puts("Parsing " .. table.concat(parsed, "\n"):linearize())
local yaml = lit.check_yaml(parsed[1])
return "TODO"
end
function lit.parse_blocks(codeblock)
local parsed = lpeg.match(lit.g.block, codeblock.text)
local valid = (parsed ~= nil and lit.check_block(parsed) or false)
return codeblock
end

View File

@ -22,6 +22,11 @@ Empty code:
id: fn1
...
Empty YAML and code:
---
...
Misses arg:
---

View File

@ -6,7 +6,7 @@
- `fn3(n: false)`
- `fn4(b: 4, a: 5)`
# Blocks
# Literate Blocks
All literate blocks should be print on `--verbose`.
@ -137,6 +137,13 @@ With inner inner function:
...
#a + #fn4(#a, #fn1())
# Code Blocks
Always ignored:
---
echo "Ignore me!"
# Inserts and Data Types
- `fn3(true)`

View File

@ -5,6 +5,8 @@
...
1 + 2 + 3
Override:
---
id: fn1
...