|
Warning: this is an htmlized version!
The original is here, and the conversion rules are here. |
-- This file:
-- http://angg.twu.net/LUA/LpegRex1.lua.html
-- http://angg.twu.net/LUA/LpegRex1.lua
-- (find-angg "LUA/LpegRex1.lua")
-- Author: Eduardo Ochs <eduardoochs@gmail.com>
--
-- Tests for lpegrex.
-- See: (find-es "lpeg" "lpegrex")
-- (find-lpegrexpage)
-- (find-lpegrextext)
-- (find-lpegrexpage 6 "__options =")
-- (find-lpegrextext 6 "__options =")
--
-- (defun a () (interactive) (find-angg "LUA/Arith4.lua"))
-- (defun l () (interactive) (find-angg "LUA/LpegRex1.lua"))
--
-- «.rectfromast» (to "rectfromast")
Path.prepend("cpath", "~/usrc/lpeglabel/?.so")
Path.prepend("path", "~/usrc/lpegrex/?.lua")
lpegrex = require "lpegrex"
Grammar = [==[
top <-- plusexpr
plusexpr <-| timesexpr ({"+"} timesexpr)*
timesexpr <-| norp ({"*"} norp)*
norp <-| num / ("(" plusexpr ")")
num <-- {%d}
]==]
patt = lpegrex.compile(Grammar)
bigstr = "(1+2)*3"
ast, errlabel, errpos = patt:match(bigstr)
-- PPPV(ast, errlabel, errpos)
__options = {}
t0 = function (str)
local gram,subj = str:match("^(.-)::%s*(.*)$")
-- local patt = lpegrex.compile(gram)
local patt = lpegrex.compile(gram, {__options = __options})
ast,errlabel,errpos = patt:match(subj)
PP(ast)
end
simplify = function (o)
if type(o) ~= "table" then return o end
if #o == 1 then return simplify(o[1]) end
return map(simplify, o)
end
-- «rectfromast» (to ".rectfromast")
-- (find-angg "LUA/Rect.lua" "Rect")
-- (find-angg "LUA/Rect.lua" "SynTree")
--
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
--[==[
* (eepitch-lua52)
* (eepitch-kill)
* (eepitch-lua52)
dofile "LpegRex1.lua"
= rectfromast(42)
= rectfromast(42):syn1()
= Rect.from(42):syn1()
= rectfromast({42})
= rectfromast({2, "+", 3, "-", 4})
= rectfromast({2, "+", {5, "*", 6}, "-", 4})
= a
= a:syn1()
--]==]
--[==[
* (eepitch-lua52)
* (eepitch-kill)
* (eepitch-lua52)
dofile "LpegRex1.lua"
t0 [=[ top <-- {%d} :: 42 ]=]
t0 [=[ top <-| {%d} :: 42 ]=]
t0 [=[ top <== {%d} :: 42 ]=]
t0 [=[ top : foo <== {%d}* :: 42 ]=]
t0 [=[ top <== num*
num <-- {%d}
:: 42
]=]
t0 [=[ top <-| num*
num <== {%d}
:: 42
]=]
t0 [=[ top : nums <== num*
num <== {%d}
:: 42
]=]
t0 [=[ top <-- addexpr
norp <-- num / "(" addexpr ")"
addexpr <== mulexpr ({"+"} mulexpr)*
mulexpr <== norp ({"*"} norp)*
num <== {%d+}
:: (1+2*3*4+5)*6+7
]=]
PPPV(ast)
asts = simplify(ast)
astr = rectfromast(asts)
PPP(asts)
= astr
-- Ugly:
= rectfromast(ast)
--]==]
--[==[
* (eepitch-lua52)
* (eepitch-kill)
* (eepitch-lua52)
dofile "LpegRex1.lua"
-- (find-lpegrexpage 6 "__options =")
-- (find-lpegrextext 6 "__options =")
__options = {
tag = function(tag, node)
print('new node', tag)
node.tag = tag
return node
end
}
t0 [=[ top : nums <== num*
num <== {%d}
:: 42
]=]
--]==]
-- Local Variables:
-- coding: utf-8-unix
-- End: