Literate blocks parses; TODO: validation
This commit is contained in:
parent
f6f39ee093
commit
2ec9e0bd4b
|
@ -6268,11 +6268,17 @@ end
|
||||||
|
|
||||||
----------------------------------- LITERATE ----------------------------------
|
----------------------------------- LITERATE ----------------------------------
|
||||||
|
|
||||||
|
-- Types functions extensions
|
||||||
|
|
||||||
|
function string:linearize()
|
||||||
|
return self:gsub("\n", "\\\\n")
|
||||||
|
end
|
||||||
|
|
||||||
-- Variable for all literate stuff
|
-- Variable for all literate stuff
|
||||||
local lit = {}
|
local lit = {}
|
||||||
|
|
||||||
-- Ordered collection of fn call, fn declarations or literal elements
|
-- Error collector
|
||||||
lit.collection = {}
|
lit.e = {}
|
||||||
|
|
||||||
-- Grammars
|
-- Grammars
|
||||||
lit.g = {}
|
lit.g = {}
|
||||||
|
@ -6283,19 +6289,18 @@ lit.space = lpeg.S" \t"
|
||||||
lit.anyspace = lpeg.S" \t\r\n"
|
lit.anyspace = lpeg.S" \t\r\n"
|
||||||
lit.spot = (1 - lit.anyspace)
|
lit.spot = (1 - lit.anyspace)
|
||||||
lit.any = (lit.spot + lit.space)
|
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_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^-1
|
||||||
lit.yaml_footer = lit.space^0 * lpeg.P"..." * lit.space^0 * lit.newline
|
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
|
-- Blocks grammar
|
||||||
lit.g.block = lpeg.P {
|
lit.g.block = lpeg.P {
|
||||||
"Block";
|
"Block";
|
||||||
Block = lpeg.C(lpeg.V"YAML" * lpeg.V"Code");
|
Block = lpeg.Ct(lpeg.V"YAML" * lpeg.V"Code", "name");
|
||||||
YAML = lit.yaml_header * lit.yaml_body^0 * lit.yaml_footer;
|
YAML = lpeg.C(lit.yaml_header * lit.yaml_body^0 * lit.yaml_footer);
|
||||||
Code = (lit.any + lit.newline)^0;
|
Code = lpeg.C((lit.any + lit.newline)^0);
|
||||||
}
|
}
|
||||||
|
|
||||||
-- Evals Lisp code
|
-- Evals Lisp code
|
||||||
|
@ -6315,12 +6320,50 @@ function lit.eval(code)
|
||||||
end
|
end
|
||||||
]]--
|
]]--
|
||||||
|
|
||||||
function lit.parse_blocks(codeblock)
|
function lit.debug_level(str)
|
||||||
parsed = lpeg.match(lit.g.block, codeblock.text)
|
if str == "ERROR" then
|
||||||
if codeblock.text ~= parsed then
|
return 2
|
||||||
print(codeblock.text)
|
elseif str == "WARNING" then
|
||||||
print(parsed)
|
return 1
|
||||||
|
else
|
||||||
|
return 0
|
||||||
end
|
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
|
return codeblock
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,16 @@
|
||||||
----------------------------------- LITERATE ----------------------------------
|
----------------------------------- LITERATE ----------------------------------
|
||||||
|
|
||||||
|
-- Types functions extensions
|
||||||
|
|
||||||
|
function string:linearize()
|
||||||
|
return self:gsub("\n", "\\\\n")
|
||||||
|
end
|
||||||
|
|
||||||
-- Variable for all literate stuff
|
-- Variable for all literate stuff
|
||||||
local lit = {}
|
local lit = {}
|
||||||
|
|
||||||
-- Ordered collection of fn call, fn declarations or literal elements
|
-- Error collector
|
||||||
lit.collection = {}
|
lit.e = {}
|
||||||
|
|
||||||
-- Grammars
|
-- Grammars
|
||||||
lit.g = {}
|
lit.g = {}
|
||||||
|
@ -15,19 +21,18 @@ lit.space = lpeg.S" \t"
|
||||||
lit.anyspace = lpeg.S" \t\r\n"
|
lit.anyspace = lpeg.S" \t\r\n"
|
||||||
lit.spot = (1 - lit.anyspace)
|
lit.spot = (1 - lit.anyspace)
|
||||||
lit.any = (lit.spot + lit.space)
|
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_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^-1
|
||||||
lit.yaml_footer = lit.space^0 * lpeg.P"..." * lit.space^0 * lit.newline
|
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
|
-- Blocks grammar
|
||||||
lit.g.block = lpeg.P {
|
lit.g.block = lpeg.P {
|
||||||
"Block";
|
"Block";
|
||||||
Block = lpeg.C(lpeg.V"YAML" * lpeg.V"Code");
|
Block = lpeg.Ct(lpeg.V"YAML" * lpeg.V"Code", "name");
|
||||||
YAML = lit.yaml_header * lit.yaml_body^0 * lit.yaml_footer;
|
YAML = lpeg.C(lit.yaml_header * lit.yaml_body^0 * lit.yaml_footer);
|
||||||
Code = (lit.any + lit.newline)^0;
|
Code = lpeg.C((lit.any + lit.newline)^0);
|
||||||
}
|
}
|
||||||
|
|
||||||
-- Evals Lisp code
|
-- Evals Lisp code
|
||||||
|
@ -47,12 +52,50 @@ function lit.eval(code)
|
||||||
end
|
end
|
||||||
]]--
|
]]--
|
||||||
|
|
||||||
function lit.parse_blocks(codeblock)
|
function lit.debug_level(str)
|
||||||
parsed = lpeg.match(lit.g.block, codeblock.text)
|
if str == "ERROR" then
|
||||||
if codeblock.text ~= parsed then
|
return 2
|
||||||
print(codeblock.text)
|
elseif str == "WARNING" then
|
||||||
print(parsed)
|
return 1
|
||||||
|
else
|
||||||
|
return 0
|
||||||
end
|
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
|
return codeblock
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -22,6 +22,11 @@ Empty code:
|
||||||
id: fn1
|
id: fn1
|
||||||
...
|
...
|
||||||
|
|
||||||
|
Empty YAML and code:
|
||||||
|
|
||||||
|
---
|
||||||
|
...
|
||||||
|
|
||||||
Misses arg:
|
Misses arg:
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
- `fn3(n: false)`
|
- `fn3(n: false)`
|
||||||
- `fn4(b: 4, a: 5)`
|
- `fn4(b: 4, a: 5)`
|
||||||
|
|
||||||
# Blocks
|
# Literate Blocks
|
||||||
|
|
||||||
All literate blocks should be print on `--verbose`.
|
All literate blocks should be print on `--verbose`.
|
||||||
|
|
||||||
|
@ -137,6 +137,13 @@ With inner inner function:
|
||||||
...
|
...
|
||||||
#a + #fn4(#a, #fn1())
|
#a + #fn4(#a, #fn1())
|
||||||
|
|
||||||
|
# Code Blocks
|
||||||
|
|
||||||
|
Always ignored:
|
||||||
|
|
||||||
|
---
|
||||||
|
echo "Ignore me!"
|
||||||
|
|
||||||
# Inserts and Data Types
|
# Inserts and Data Types
|
||||||
|
|
||||||
- `fn3(true)`
|
- `fn3(true)`
|
||||||
|
|
|
@ -5,6 +5,8 @@
|
||||||
...
|
...
|
||||||
1 + 2 + 3
|
1 + 2 + 3
|
||||||
|
|
||||||
|
Override:
|
||||||
|
|
||||||
---
|
---
|
||||||
id: fn1
|
id: fn1
|
||||||
...
|
...
|
||||||
|
|
Loading…
Reference in New Issue