• Subject: lpeg-list and nested substitutions
• From: Wesley Smith <wesley.hoke@...>
• Date: Sun, 28 Mar 2010 10:23:49 +0100

```Is it possible to apply capture substitutions in nested lists with
lpeg-list?  I've tried a number of things, but just can'
t seem to get it to work.  For example, given this grammar:

local p = P{
"patt",
one = L(L"one"),
patt = (V"one" + L(V"patt") + P(1))^0
}
local res = { p:match(code) }

I want to transform

local code = { "something", { "something", { "one" } } }

into

local code = { "something", { "something", "two" } }

I tried this:

local code = { "something", { "something", { "one" } } }
local p = P{
"patt",
one = L(L"one"),
patt = (Cs(V"one"/ "two") + L(V"patt") + P(1))^0
}
local res = { p:match(code) }

but it only gives me the capture for match on {"one"} of course.  When
I try to move the substitution out like this:

local code = { "something", { "something", { "one" } } }
local p = P{
"patt",
one = L(L"one"),
patt = Cs((V"one"/ "two" + L(V"patt") + P(1))^0)
}
local res = { p:match(code) }

I'm confronted with "can't mix different streams on substitution
capture".  It's easy to substitute a nested table with a singly nested
list:

local code = {"a", "b", {"x", "y"}, "c"}
local patt = Cs(P"a" * P"b"* (L(L"x" * L"y")/"zipper") * P"c")

but I've not found a way to generalize this to arbitrarily nested
lists because the recursive nature of the grammar generates the error
message above when substitutions encompass the recursion, preventing
me from marking the location in the parent list where the substitution
should occur (at least this  is my current theory).  Is there any way
around this?  Perhaps by enabling the mixing of different streams on
substitution captures?

thanks,
wes

```