[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: LPeg back captures
- From: Andrew Starks <andrew.starks@...>
- Date: Wed, 23 Oct 2013 23:36:51 -0500
On Wed, Oct 23, 2013 at 10:55 PM, Andrew Starks <andrew.starks@trms.com> wrote:
> On Wed, Oct 23, 2013 at 10:10 PM, Sean Conner <sean@conman.org> wrote:
>>> > DIGIT = R"09"
>>> > delim_char = P"!" -- Cb("delim")
>>> > flags = P"i"
>>> > backref = P"\\" * DIGIT
>>> > escapeddelim = P"\\" * delim_char
>>> > anychar = P(1) - delim_char
>>> > string = (escapeddelim + anychar)^1
>>> > repl = C((string + backref)^0)
>>> > ere = C((P(1) - delim_char)^0)
>>> > idelim_char = Cg(P"/" + P"!" + (P(1) - (DIGIT + flags)),"delim")
>>> > regexp = Ct(
>>> > idelim_char
>>> > * Cg(ere,"re")
>>> > * delim_char
>>> > * Cg(repl,"replace")
>>> > * delim_char
>>> > * Cg(flags^0,"flags")
>>> > )
>
> Man... I'm sorry. I thought I was pretty sure on this. I did some
> tests on a simpler example and I was wrong again. Order of declaration
> doesn't matter....
>
>
> ```
> lpeg = require'lpeg'
>
> P, Cg, Cb, C = lpeg.P, lpeg.Cg, lpeg.Cb, lpeg.C
>
> the_forb_back_there = Cb("forb")
> forb_cap = Cg(P(P"foo" + "bar"), "forb")
>
> main_patt = C(forb_cap * (P(1))^1 * the_forb_back_there)
>
> print(main_patt:match("barnillydissle"))
> -->barnillydissle bar
> ```
>
> bar is coming out as a separate return value.... I think that may be
> the problem. I'm gonna dig more.
I'm gonna work on a solution, but at least I think I know the problem:
Cb doesn't work on nested captures. So your other statements (ere,
escapeddelim, etc) are "out of scope" with delim_char.
-Andrew