Warning: this is an htmlized version!
The original is here, and the conversion rules are here. |
-- This file: -- http://anggtwu.net/LUA/Gram4.lua.html -- http://anggtwu.net/LUA/Gram4.lua -- (find-angg "LUA/Gram4.lua") -- Author: Eduardo Ochs <eduardoochs@gmail.com> -- -- This is a miniature of: -- (find-angg "LUA/ELpeg1.lua") -- -- (defun e () (interactive) (find-angg "LUA/Gram4.lua")) -- See: (find-angg "LUA/LpegRex3.lua" "LpegRex") -- (find-angg "LUA/Re2.lua" "Re") -- «.Gram4» (to "Gram4") -- «.Gram4-tests» (to "Gram4-tests") -- ____ _ _ -- / ___|_ __ __ _ _ __ ___ | || | -- | | _| '__/ _` | '_ ` _ \| || |_ -- | |_| | | | (_| | | | | | |__ _| -- \____|_| \__,_|_| |_| |_| |_| -- -- «Gram4» (to ".Gram4") Gram4 = Class { type = "Gram4", new = function () return Gram4{}:setentries():withV() end, __tostring = function (gr) return gr:tostring() end, __index = { tostring = function (gr, sep) local ks = sorted(keys(gr.entries)) return mapconcat(mytostring, ks, sep or "\n") end, setentries = function (gr,e) gr.entries=(e or VTable{}); return gr end, withV = function (gr) local V,V_mt = {}, { __call = function (v,key) return lpeg.V(key) end, __index = function (v,key) return lpeg.V(key) end, __newindex = function (v,key,val) gr.entries[key] = val end, } setmetatable(V, V_mt) return gr,V end, compile = function (gr, top) local entries = copy(gr.entries) entries[1] = top return lpeg.P(entries) end, cm = function (gr, top, subj, pos) -- compile and match if type(pos) == "string" then pos = subj:match(pos) end return gr:compile(top):match(subj, pos) end, cmp = function (gr, top, subj, pos) -- compile, match, pretty-print return PP(gr:cm(top, subj, pos)) end, } } -- «Gram4-tests» (to ".Gram4-tests") --[==[ * (eepitch-lua51) * (eepitch-kill) * (eepitch-lua51) dofile "Gram4.lua" -- (find-angg "LUA/ELpeg1.lua" "globals") lpeg = lpeg or require "lpeg" B,C,P,R,S,V = lpeg.B,lpeg.C,lpeg.P,lpeg.R,lpeg.S,lpeg.V Cb,Cc,Cf,Cg = lpeg.Cb,lpeg.Cc,lpeg.Cf,lpeg.Cg Cp,Cs,Ct = lpeg.Cp,lpeg.Cs,lpeg.Ct Carg,Cmt = lpeg.Carg,lpeg.Cmt require "Fold1" -- (find-angg "LUA/Fold1.lua") f = function (a,op,b) return format("(%s%s%s)", a, op, b) end g = function (...) return Fold{A={...}}:l2(f) end = g(1) = g(1,2,3,4,5,6,7) gr,V = Gram4.new() _ = S" \t\n"^0 V.N = C(R"09"^1) V.E = V.N V.M = V.E * (_ * C(S"*/") *_* V.E)^0 / g V.A = V.M * (_ * C(S"+-") *_* V.M)^0 / g = gr:cmp("M", "1*2 + 3/4") = gr:cmp("A", "1*2 + 3/4") = gr:cmp("A", "1*2 + 3/(4-5+6)") -- bad V.E = V.N + "("*_*V.A*_*")" = gr:cmp("A", "1*2 + 3/(4-5+6)") -- good = gr = gr:tostring() = gr:tostring(" ") --]==] -- Local Variables: -- coding: utf-8-unix -- End: