From 2ec9e0bd4b31f3aa6e682e986738d37b670a552f Mon Sep 17 00:00:00 2001 From: perro Date: Fri, 17 Mar 2023 19:04:41 -0700 Subject: [PATCH] Literate blocks parses; TODO: validation --- dist/lin.lua | 73 +++++++++++++++++++++++++++++++--------- src/literate.lua | 73 +++++++++++++++++++++++++++++++--------- tests/fail.lit.errors.md | 5 +++ tests/pass.lit.infos.md | 9 ++++- tests/pass.lit.warns.md | 2 ++ 5 files changed, 131 insertions(+), 31 deletions(-) diff --git a/dist/lin.lua b/dist/lin.lua index c180dc2..cbd8630 100644 --- a/dist/lin.lua +++ b/dist/lin.lua @@ -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 diff --git a/src/literate.lua b/src/literate.lua index b8c42bc..45281cc 100644 --- a/src/literate.lua +++ b/src/literate.lua @@ -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 diff --git a/tests/fail.lit.errors.md b/tests/fail.lit.errors.md index ade041c..03a8f00 100644 --- a/tests/fail.lit.errors.md +++ b/tests/fail.lit.errors.md @@ -22,6 +22,11 @@ Empty code: id: fn1 ... +Empty YAML and code: + + --- + ... + Misses arg: --- diff --git a/tests/pass.lit.infos.md b/tests/pass.lit.infos.md index d5c629e..0e358c7 100644 --- a/tests/pass.lit.infos.md +++ b/tests/pass.lit.infos.md @@ -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)` diff --git a/tests/pass.lit.warns.md b/tests/pass.lit.warns.md index 708e634..39a6921 100644 --- a/tests/pass.lit.warns.md +++ b/tests/pass.lit.warns.md @@ -5,6 +5,8 @@ ... 1 + 2 + 3 +Override: + --- id: fn1 ...