Warning: this is an htmlized version!
The original is here, and
the conversion rules are here.
-- This file:
--   http://angg.twu.net/LUA/Lpeg2.lua.html
--   http://angg.twu.net/LUA/Lpeg2.lua
--           (find-angg "LUA/Lpeg2.lua")
-- Author: Eduardo Ochs <eduardoochs@gmail.com>
--
-- (defun e () (interactive) (find-angg "LUA/Lpeg2.lua"))

-- «.download»	(to "download")
-- «.tests»	(to "tests")

-- «download»  (to ".download")
--[[
* (eepitch-shell)
* (eepitch-kill)
* (eepitch-shell)
rm -v    ~/LUA/lua50init.lua
mkdir -p ~/LUA/
cd       ~/LUA/
wget http://angg.twu.net/LUA/lua50init.lua
# (setenv "LUA_INIT" (concat "@" (ee-expand "~/LUA/lua50init.lua")))

rm -Rv /tmp/parser/
mkdir  /tmp/parser/
cd     /tmp/parser/
wget http://angg.twu.net/LUA/Lpeg2.lua
# (find-anchor  "/tmp/parser/Lpeg2.lua")

--]]


lpeg = require("lpeg")

-- (find-es "lpeg" "globals")
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

-- (find-angg "LUA/LpegRex1.lua" "rectfromast")
rectfromast = function (o)
    if type(o) == "number" then return Rect.from(o) end
    if type(o) == "string" then return Rect.from(o) end
    if type(o) == "table" then
      local out = rectfromast(o[#o]):syn1()
      for i=#o-2,1,-2 do
        local lowerleft = rectfromast(o[i])
        local left = lowerleft:syn1(o[i+1])
        local left_ = left:pad0(1, left:width()+2, "_")
        out = left_ .. out
      end
      return out
    end
    PP("rectfromast can't handle this", o)
  end

Ast = Class {
  type = "Ast",
  from = function (...) return Ast({...}) end,
  fromsimp = function (...)
      local L = {...}                  -- if ... has a single argument
      if #L == 1 then return L[1] end  -- then return a simplified result.
      return Ast.from(...)
    end,
  __tostring = function (o) return rectfromast(o):tostring() end,
  __index = {
  },
}

--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "Lpeg1.lua"
= Ast.from(2, "+", 4, "-", 6)
= Ast.from(2, "_[_]", 4)
= Ast.fromsimp(2, "+", 4, "-", 6)
= Ast.from    (Ast.from    (2, "+", 4, "-", 6))
= Ast.fromsimp(Ast.fromsimp(2, "+", 4, "-", 6))
= Ast.from    (Ast.from    (2, "+", 4, "-", 6), "*", 3)
= Ast.fromsimp(Ast.fromsimp(2, "+", 4, "-", 6), "*", 3)

--]]

-- (find-es "lpeg" "lpeg-quickref")
pskip  = P(" ")^0
pnum   = (R("09")^1):C() * pskip
plit   = function (str) return P(str)     * pskip end
ptok   = function (str) return P(str):C() * pskip end
passoc = function (pexp, ptok)
    return (pexp * (ptok * pexp)^0) / Ast.fromsimp
  end

pexpr = P({
  "add",
   add    = passoc(V"mul",  ptok("+") + ptok("-")),
   mul    = passoc(V"norp", ptok("*") + ptok("/")),
   norp   = pnum + plit("(") * V("add") * plit(")"),
})

-- «tests»  (to ".tests")
--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "Lpeg2.lua"
= pexpr:match( "1*2 + 3 + 4*5*6 + 7")
= pexpr:match("(1*2 + 3 + 4*5*6 + 7)")
= pexpr:match("(1*2 + 3 + 4*5*6 + 7) * 8")

PPP(pexpr:match( "1*2 + 3 + 4*5*6 + 7"))

--]]








-- Local Variables:
-- coding:  utf-8-unix
-- End: