Warning: this is an htmlized version!
The original is here, and the conversion rules are here. |
-- This file: -- http://anggtwu.net/LUA/Capts1.lua.html -- http://anggtwu.net/LUA/Capts1.lua -- (find-angg "LUA/Capts1.lua") -- Author: Eduardo Ochs <eduardoochs@gmail.com> -- -- (defun e () (interactive) (find-angg "LUA/Capts1.lua")) -- (find-angggrep "grep --color=auto -nH --null -e ELpeg1 LUA/*.lua") -- (find-angg "LUA/Subst1.lua" "totex-tests") -- (find-LATEX "2023lpegcaptures.tex") -- Superseded by: -- (find-angg "LUA/Und1.lua") -- (find-angg "LUA/LPex1.lua") -- «.lpexes_bigstr» (to "lpexes_bigstr") -- «.lpexes_parse» (to "lpexes_parse") -- «.lpexes_parse-tests» (to "lpexes_parse-tests") -- «.totex» (to "totex") -- «.totex-tests» (to "totex-tests") -- «.uparse» (to "uparse") -- «.uparse-tests» (to "uparse-tests") -- «.LPex» (to "LPex") -- «.LPex-tests» (to "LPex-tests") -- «.LPexes» (to "LPexes") -- «.LPexes-tests» (to "LPexes-tests") require "Co1" -- (find-angg "LUA/Co1.lua") require "ELpeg1" -- (find-angg "LUA/ELpeg1.lua") require "ToTeX1" -- (find-angg "LUA/ToTeX1.lua") -- require "Subst1" -- (find-angg "LUA/Subst1.lua") -- require "Show2" -- (find-angg "LUA/Show2.lua") -- «lpexes_bigstr» (to ".lpexes_bigstr") lpexes_bigstr = [=[ name: P_1 subj: "abc" code: P"a" * P"b" * P"c" diag: {<a>_<> <b>_<> <c>_<>}_<> name: C_1 subj: "abcd" code: (P(1) * P"b" * C(1) * C"d"):C() diag: {<a>_<> <b>_<> <c>_<"c"> <c>_<"d">}_<"abcd" "c" "d"> name: Cs_1 subj: "abc" code: (P"a" * (P"b" / "bb") * P"c"):Cs() diag: {<a>_<> <b>_<"bb"> <c>_<>}_<"abbc"> name: Cc_1 subj: "cd" code: Cc("a","b") * P"cd" * Cc("e","f") diag: {<...>_<"a" "b"> <cd>_<> <...>_<"e" "f">}_<"a" "b" "e" "f"> name: Cg_1 subj: "" code: Cc"a" * Cc("b","c"):Cg"d" * Cc"e" diag: {<.>_<"a"> <.....>_<"b" "c">_<d={."b" "c".}> <.>_<"e">} : _<"a" d={."b" "c".} "e"> : _<"a" "e"> name: Cg_2 subj: "" code: (Cc"a" * Cc("b","c"):Cg"d" * Cc"e"):Ct() diag: {<.>_<"a"> <.....>_<"b" "c">_<d={."b" "c".}> <.>_<"e">} : _<{"a", d={"b", "c"}, "e"}> name: Cg_3 subj: "" code: (Cc"a" * Cc("b","c"):Cg"d" * Cc"e" * Cc("f", "g"):Cg"d"):Ct() diag: {<.>_<"a"> <.....>_<"b" "c">_<d={."b" "c".}> : <.>_<"e"> <.....>_<"f" "g">_<d={."f" "g".}>} : _<"a" d={."b" "c".} "e" d={."f" "g".}> : _<{"a", d={"b", "c"}, "e", d={"f"}}> : _<{"a", "e", d={"f"}}> name: Cg_4 subj: "" code: Cc"a" * Cc("b","c"):Cg"d" * Cc"e" * Cc("f", "g"):Cg"d" diag: {<.>_<"a"> <.....>_<"b" "c">_<d={."b" "c".}> : <.>_<"e"> <.....>_<"f" "g">_<d={."f" "g".}>} : _<"a" d={."b" "c".} "e" d={."f" "g".}> : _<"a" "e"> name: Cb_1 subj: "" code: (Cc"a" * Cc("c","d"):Cg"b" * Cc("e", "f"):Cg"b" * Cc"g" * Cb"b"):Ct() diag: {{<.>_<"a"> <.....>_<"c" "d">_<b={."c" "d".}> : <.....>_<"e" "f">_<b={."e" "f".}> : <.>_<"g">} : _<"a" b={."c" "d".} b={."e" "f".} "g"> : _<"a" b={."e" "f".} "g"> : <.>_<["b"]>} : _<"a" b={."e" "f".} "g" "e" "f"> : _<{"a", b={"e", "f"}, "g", "e", "f"}> name: Cb_2 subj: "" code: (Cc"a":Cg"b" * Cc"c":Cg"d" * Cc"e":Cg"f") * Cb"b" diag: {{<..>_<"a">_<b="a"> : <..>_<"c">_<d="c"> : <..>_<"e">_<f="e">}_<b="a" d="c" f="e"> : <..>_<["b"]> : }_<b="a" d="c" f="e" ["b"]> : _<b="a" d="c" f="e" "a"> name: Cb_3 subj: "" code: Cc"a":Cg"b" * (Cc"c":Cg"d" * Cc"e":Cg"f" * Cb"b") diag: {<..>_<"a">_<b="a"> : {<..>_<"c">_<d="c"> : <..>_<"e">_<f="e"> : <..>_<["b"]>}_<d="c" f="e" ["b"]>} : _<b="a" d="c" f="e" ["b"]> : _<b="a" d="c" f="e" "a"> name: Cb_4 subj: "" code: (Cc"a" * (Cc("b", "c") * Cc"d":Cg"e"):Cg"f" * Cb"f"):Ct() diag: {{<..>_<"a"> <....>_<"b" "c"> <..>_<"d">_<e="d">} : _<"a" "b" "c" e="d"> : _<"a" "b" "c"> : _<f={."a" "b" "c".}> : <..>_<["f"]> : }_<f={."a" "b" "c".} ["f"]> : _<f={."a" "b" "c".} "a" "b" "c"> : _<{f={"a"}, "a", "b", "c"}> ]=] -- _ -- | |_ __ _____ _____ ___ _ __ __ _ _ __ ___ ___ -- | | '_ \ / _ \ \/ / _ \/ __| | '_ \ / _` | '__/ __|/ _ \ -- | | |_) | __/> < __/\__ \ | |_) | (_| | | \__ \ __/ -- |_| .__/ \___/_/\_\___||___/____| .__/ \__,_|_| |___/\___| -- |_| |_____|_| -- -- «lpexes_parse» (to ".lpexes_parse") gr,V,VA = Gram.new() V.nl = P"\n" V.restofline = (1-P"\n")^0 V.colonline = (S" "^0 * (R"az"^0):C() * ":" * P" "^-1 * V.restofline:C() * V.nl):Ct() V.nameline = V.colonline:Cobeying(function (o) return o[1] > "" end) V.contline = V.colonline:Cobeying(function (o) return o[1] == "" end) V.otherline = (-V.colonline) * V.restofline * V.nl V.named0 = V.nameline * V.contline^0 V.named = V.named0:Ct() / function (A) local f = function (i) return A[i][2] end return {A[1][1], mapconcat(f, seq(1,#A), "\n")} end V.block0 = V.otherline^0 * V.named^1 V.block = V.block0:Ct() / function (B) local C = VTable {} for _,A in ipairs(B) do C[A[1]] = A[2] end return C end V.blocks = (V.block^1):Ct() lpexes_pat = gr:compile("blocks") lpexes_parse = function (bigstr) return lpexes_pat:match(bigstr.."\n") end lpex_tostring = function (B) local f = function (s) return (s:gsub("\n", "\n ")) end local g = function (k) return format("%s: %s", k, f(B[k])) end return mapconcat(g, sorted(keys(B)), "\n") end -- «lpexes_parse-tests» (to ".lpexes_parse-tests") --[==[ * (eepitch-lua51) * (eepitch-kill) * (eepitch-lua51) dofile "Capts1.lua" bigstr = [=[ name: test 1 subj: "" code: (Cc"a" * Cc("c","d")) diag: {{<.>_<"a"> <.....>_<"c" "d">_<b={."c" "d".}> : <.....>_<"e" "f">_<b={."e" "f".}> name: test 2 subj: "" code: (Cc"a" * Cc("c","d")) * foo diag: {{<.>_<"a"> <.....>_<"c" "d">_<b={."c" "d".}> ]=] D = lpexes_parse(bigstr) PP(D) = lpex_tostring(D[1]) = lpex_tostring(D[2]) D = lpexes_parse(lpexes_bigstr) = lpex_tostring(D[1]) = lpex_tostring(D[2]) --]==] -- _ _ -- | |_ ___ | |_ _____ __ -- | __/ _ \| __/ _ \ \/ / -- | || (_) | || __/> < -- \__\___/ \__\___/_/\_\ -- -- «totex» (to ".totex") co0 = Co.new(" %_{}", "\\^") cot = co0:translator() usepackages = [[ \usepackage{edrx21} \input edrxaccents.tex \input edrx21chars.tex \def\und#1#2{\underbrace{#1}_{#2}} ]] dotsp = function (str, verbose) local a,b,c = str:match("^(%.*)(.-)(%.*)$") local d = (" "):rep(#(a..c)) if verbose then PP(a,b,c,d) end return d..b..d end tex_arrayl = function (body) return format("\\begin{array}{l} %s\n\\end{array}", body) end tex_example0 = function (a,b) return tex_arrayl(format("%s \\\\\n \\Rightarrow \\;\\; %s", a, b)) end fmts = VTable {} fmts["co"] = "\\texttt{<cot(dotsp(o[1]))>}" fmts["und"] = "\\und{<1>}{<2>}" fmts["curly"] = "{<1>}" fmts["curly"] = "{<mapconcat(totex, o)>}" fmts["other"] = "<1>" fmts["example"] = [[ \begin{array}{l} <1> \\ \Rightarrow \;\; <2> \\ \end{array} ]] fmts["example"] = "<tex_example0(totex(o[1]), totex(o[2]))>" co = function (str) return mkast("co", str) end und = function (a,b) return mkast("und", a, b) end -- «totex-tests» (to ".totex-tests") --[==[ ** (find-code-show2 "~/LATEX/Show2.tex") * (code-show2 "~/LATEX/Show2.tex") * (eepitch-lua51) * (eepitch-kill) * (eepitch-lua51) dofile "Capts1.lua" = dotsp("..ab..cd...", "verbose") = tex_arrayl ("foo") = tex_example0("foo", "bar") o = und(co"a", co'{."a" b="c" d.}') o = und(co"a....", co'{."a" b="c" d.}') o = mkast("example", co("foo"), und("bar", "plic")) = o = totex(o) = o:show(4) * (etv) = Show.log = Show.bigstr --]==] -- -- _ _ _ __ __ _ _ __ ___ ___ -- | | | | '_ \ / _` | '__/ __|/ _ \ -- | |_| | |_) | (_| | | \__ \ __/ -- \__,_| .__/ \__,_|_| |___/\___| -- |_| -- «uparse» (to ".uparse") gr,V,VA,VE,PE = Gram.new() V.S = (S" \t\n")^0 _ = V.S V.co1 = R" ~" - S"<>" V.other1 = R" ~" - S"<>{}_" V.other1 = P(1) - S"<>{}_" V.und1 = V.co + V.curly V.longitem = V.unds + V.other VA.co = P"<" * (V.co1^0):C() * P">" VA.curly = P"{" * V.longitem^0 * P"}" VA.other = (V.other1^1):Cs() V.unds = assocl((V.co + V.curly), P"_"/"und") uparse_pat = gr:compile("longitem") uparse = function (str) return uparse_pat:match(str) end utex = function (str) return totex(uparse(str)) end example = function (bigstr) local a0,b0 = bigstr:match("^%s*([^\n]*)\n(.*)") local a,b = bitrim(a0), bitrim(b0) return mkast("example", co(a), uparse(b)) end -- «uparse-tests» (to ".uparse-tests") --[==[ * (eepitch-lua51) * (eepitch-kill) * (eepitch-lua51) dofile "Capts1.lua" = gr:cm("co", "<foo>") = gr:cm("other", "abc") = gr:cm("unds", "<a>_<b>_<c>") = gr:cm("unds", "<aa>_<b>_{c}") = gr:cm("unds", "<aa>_<b>_{<c>}") = gr:cm("unds", "<aa>_<b>_{<c>_<d>}") = gr:cm("unds", "<aa>_<b>_{<c>_<d>_<e>}") = gr:cm("unds", "<aa>_<b>_{<c>_<d>_<e>fg}") = uparse '<abcde>_<"b" "d">_<"db">' = uparse '<abcde>_<"b" "d">_<"db">' :show(4) = uparse '<abcde>_<"b" "d">_{db}' :show(4) = uparse '{<a><b>_<> <c>}' :show(4) = uparse '{<a>_<><b>_<> <c>_<>}_<"abc">' :show(4) = uparse '{<a><b>_<> <c>}_<"abc">' :show(4) = uparse '{<a><b>_<""><c>}' :show(4) = uparse '{<a><b>_<""><c>}_<ac>' :show(4) = uparse '{{<a><b> _<"bb"><c>}}_<"abbc">' :show(4) = uparse [[{{<a><b> _<"bb"><c>}}_<"abbc">]] :show(4) = uparse [[{<P"a" * P"b":Cc("bb") * P"c"> :: {<a><b> _<"bb"><c>}_<"abbc"> }]] :show(1) * (etv) o = mkast("example", "foo", "bar") = o = o:show() * (etv) = uparse '{ <a><b...>_<"bb" "BB"><c>}' :show(4) = uparse '{{<a><b...>_<"bb" "BB"><c>}}_<"abbBBc">' :show(4) * (etv) = Show.log = Show.bigstr * (etv) o = uparse "<foo>_<bar>_<plic>" o = uparse '<abcde>_<"b" "d">_<"db">' = o = o:show(4) * (etv) --]==] -- _ ____ -- | | | _ \ _____ __ -- | | | |_) / _ \ \/ / -- | |___| __/ __/> < -- |_____|_| \___/_/\_\ -- -- «LPex» (to ".LPex") LPex = Class { type = "LPex", from = function (tbl) return LPex(tbl) end, __tostring = function (lpex) return lpex_tostring(lpex) end, __index = { example = function (lpex) return mkast("example", co(lpex.code), uparse(lpex.diag)) end, show = function (lpex) return lpex:example():show() end, pat = function (lpex) return expr(lpex.code) end, test = function (lpex) PP(lpex:pat():match(expr(lpex.subj))) end, short = function (lpex) return format("%s :: %s", lpex.name, lpex.code) end, sa = function (lpex) local name,texcode = lpex.name, totex(lpex:example()) local fmt = "\\sa{%s}{%s}" return format(fmt, name, texcode) end, }, } -- «LPex-tests» (to ".LPex-tests") --[==[ * (eepitch-lua51) * (eepitch-kill) * (eepitch-lua51) dofile "Capts1.lua" D = lpexes_parse(lpexes_bigstr) o = LPex.from(D[3]) = o = o:example() = o.code = o:test() = o:short() = o:sa() = o:show() * (etv) --]==] -- _ ____ -- | | | _ \ _____ _____ ___ -- | | | |_) / _ \ \/ / _ \/ __| -- | |___| __/ __/> < __/\__ \ -- |_____|_| \___/_/\_\___||___/ -- -- «LPexes» (to ".LPexes") LPexes = Class { type = "LPexes", from = function (bigstr) return LPexes({}):add(bigstr) end, __tostring = function (lps) local f = function (lp) return lp:short() end return mapconcat(f, lps, "\n") end, __index = { add = function (lps,bigstr) for _,lp0 in ipairs(lpexes_parse(bigstr)) do local lp = LPex.from(lp0) table.insert(lps, lp) lps[lp.name] = lp end return lps end, sas = function (lps) local f = function (lp) return lp:sa() end return mapconcat(f, lps, "\n\n") end, names0 = function (lps,fmt,sep,medstr) local f = function (lp) return format(fmt,lp.name) end local A = medstr and split(medstr) or lps return mapconcat(f, A, sep) end, names = function (lps) return lps:names0("%s", " ") end, gas = function (lps,sep,medstr) return lps:names0("\\ga{%s}", sep or "\n", medstr) end, }, } -- «LPexes-tests» (to ".LPexes-tests") --[==[ * (eepitch-lua51) * (eepitch-kill) * (eepitch-lua51) dofile "Capts1.lua" lps = LPexes.from(lpexes_bigstr) = lps = lps[1] = lps[2] = lps["C_1"] = lps:sas() = lps:names() = lps:gas() = lps:gas(" \\\\\n") -- (find-es "lpeg" "lpeg-unnamed") fparen = function (...) return "("..table.concat({...}, ",")..")" end lps:add [=[ name: Cf_1 subj: "abcdef" code: ((C(1)*C(1)):Cg() * (C(1)*C(1)):Cg() * (C(1)*C(1)):Cg()):Cf(fparen) diag: {{<a.>_<"a"> <b.>_<"b">}_<{."a" "b".}> : {<c.>_<"c"> <d.>_<"d">}_<{."c" "d".}> : {<e.>_<"e"> <f.>_<"f">}_<{."e" "f".}>} : _<"((a,c,d),e,f)"> ]=] = lps["Cf_1"] = lps["Cf_1"]:test() = lps["Cf_1"]:show() * (etv) --]==]