[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: LPeg back captures
- From: Sean Conner <sean@...>
- Date: Thu, 24 Oct 2013 01:04:25 -0400
It was thus said that the Great Andrew Starks once stated:
> 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.
I've simplified the LPeg code. What I want is the behavior of this:
delim = P"/"
field = C((P(1) - delim)^0)
regexp = Ct(
delim
* Cg(field,"re")
* delim
* Cg(field,"replace")
* delim
* Cg(field^-1,"flags")
)
But where delim is defined as the first character of the string (instead of
the hardcoded pattern I have here). This is harder than it looks.
-spc