mascarenhas/lpeg-list
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
master
Could not load branches
Nothing to show
Could not load tags
Nothing to show
{{ refName }}
default
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code
-
Clone
Use Git or checkout with SVN using the web URL.
Work fast with our official CLI. Learn more.
- Open with GitHub Desktop
- Download ZIP
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
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.
About
Support for matching lists with LPEG (like OMeta does)
Resources
Stars
Watchers
Forks
Packages 0
No packages published