Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Support for matching lists with LPEG (like OMeta does)
JavaScript Lua

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
bench
doc
sblp2009
test
README
index.html
listlpeg-scm-1.rockspec
listre.lua
lpeg.c
makefile
re.lua

README

Support for matching lists with LPEG
(http://www.inf.puc-rio.br/~roberto/lpeg), like OMeta does.

The semantics of list matching are similar to OMeta's: strings and lists
are both streams, strings being streams of characters and lists streams
of items. This means that the pattern "abc" matches both the string
"abc" and the list { "a", "b", "c" }. The special thing about lists is
that its items can themselves be streams (other lists or strings).

List captures are working, the examples below show how they
operate (produce the empty list, the single captured element, or a sublist).
Substitution captures on a list slice produce a list with replaced elements,
dynamic captures receive the list being matched and the current position, and
must return the next position to match as first result (or true to keep in the
same place).

It's backward compatible with LPEG 0.9, but I have a new RE module called listre
that has a more list-friendly syntax, although I am not happy with its syntax yet,
specially with the treatment of strings.

Some examples below (there's a simple expression compiler and evaluator at the end;
list.lua has a lot more, including some tree processing on a simplified HTML). Notice
the lists passed to match have to be wrapped in another list, this looks strange
but is uniform; I will probably just do this wrapping automatically on match later.

--------------------------
re = require "listre"

p = re.compile[[ { "one", <"two">, "three" } ]]

assert(p:match{ { "one" , "two" ,"three" } } == "two")

print("+")

p = re.compile[[
      { "one", < "two", ({ {'foo''bar'*}, "bar" } / { "baz", "boo" })* > ,
     "three" }
]]


assert(p:match{{ "one", "two", "three" }} == "two")
assert(#p:match{{ "one", "two", { "foo", "bar" }, { "baz", "boo" }, "three" }} == 3)
local t = p:match{{ "one", "two", { "foobarbarbar", "bar" }, { "baz", "boo" },
		   "three" }}
assert(t[2][1] == "foobarbarbar")
assert(#p:match{{ "one", "two", { "foobarbar", "bar" }, { "baz", "boo" },
		  "three" }} == 3)

print("+")

p = re.compile([[ { "add", <.>, <.> } -> add ]], { add = function (x, y)
								return x+y
							     end })

assert(p:match{{ "add", 2, 3 }})
assert(p:match{{ "add", 72, 3 }} == 75)
assert(p:match{{ "add", 72.5, 3 }} == 75.5)
assert(not p:match{{ "sub", 72, 3 }})
assert(not p:match{{ "add", 3 }})

print("+")

p = re.compile[[ { "foo", < "bar", (!"baz" .)*, "baz" >, "boo" } ]]

assert(select(4, p:match{{ "foo", "bar", "one", "two", "three", "baz", "boo" }}) == "three")
assert(#p:match{{ "foo", "bar", "one", "two", "three", "baz", "boo" }} == 5)
assert(select(5, p:match{{ "foo", "bar", "one", "two", "three", "baz", "boo" }}) == "baz")
assert(select(2, p:match{{ "foo", "bar", "baz", "boo" }}) == "baz")
assert(#p:match{{ "foo", "bar", "baz", "boo" }} == 2)

print("+")

p = re.compile[[ { "foo", "bar", < (!"baz" .)* >, "baz" , "boo" } ]]

assert(#p:match{{ "foo", "bar", "one", "two", "three", "baz", "boo" }} == 3)
assert(select(3, p:match{{ "foo", "bar", "one", "two", "three", "baz", "boo" }}) == "three")
assert(#p:match{{ "foo", "bar", "baz", "boo" }} == 0)

print("+")

p = re.compile([[
	       exp <- { "add", <exp>, <exp> } -> add
                    / { "sub", <exp>, <exp> } -> sub
		    / { "mul", <exp>, <exp> } -> mul
		    / { "div", <exp>, <exp> } -> div
		    / <.>
    ]], { add = function (x, y) return x + y end, 
          sub = function (x, y) return x - y end,
          mul = function (x, y) return x * y end,
          div = function (x, y) return x / y end, })

assert(p:match{{ "add", { "div", 8, 2 }, 3 }} == 7)
assert(p:match{{ "sub", { "div", 8, { "add", 2, 2 } }, 3 }} == -1)

print("+")

p = re.compile([[ exp <- { . -> ops, <exp>, <exp> } -> eval / { "num", <.> } ]], 
	       { ops = { add = function (x, y) return x + y end, 
                         sub = function (x, y) return x - y end,
                         mul = function (x, y) return x * y end,
                         div = function (x, y) return x / y end, },
                 eval = function (op, x, y) return op(x, y) end })

assert(p:match{{ "add", { "div", { "num", 8 }, { "num", 2 } }, { "num", 3 } }} == 7)
assert(p:match{{ "sub", { "div", { "num", 8 }, { "add", { "num", 2 }, { "num", 2 } } }, 
		{ "num", 3  } }} == -1)

print("+")

local S = m.S(" \n\t")^0

parser = re.compile([[
  exp <- (<mul>:x <aop>:op <exp>:y) -> { op, x, y } / <mul>
  mul <- (<prim>:x <mop>:op <mul>:y) -> { op, x, y } / <prim>
  prim <- ( `( <exp> `) ) / <num>:n -> { 'num', n }
  aop <- (`+ -> 'add') / (`- -> 'sub')
  mop <- (`* -> 'mul') / (`/ -> 'div')
]], { num = S * (m.C(m.P"-"^-1 * m.R("09")^1) / tonumber) * S,
      id = function (x) return x end })

assert(p:match{parser:match("8/2+3")} == 7)
assert(p:match{parser:match("8 / (2 + 2) - 3")} == -1)

print("+")
---------------------------

Use LuaRocks (http://luarocks.org) and the provided .rockspec file to
compile and install. No docs yet, check the test suite list.lua for
more examples.
Something went wrong with that request. Please try again.