[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Lpeg: a question about function captures and named captures
- From: Sean Conner <sean@...>
- Date: Thu, 13 Apr 2023 00:08:28 -0400
It was thus said that the Great Andrew Gierth once stated:
> >>>>> "Sean" == Sean Conner <sean@conman.org> writes:
>
> >> > (Cc"a" * Cc"b" / f):Cg"c" :ptmatch("ccc")
> >> {"c"="ab"}
> >>
> >> How do I modify the ".../f" above to make it put the first result of f
> >> into :Cg"c" and the second result into :Cg"d"?
>
> Sean> If I understand your question correctly, for
>
> Sean> Cc'a' * Cc'b' / f
>
> Sean> you want the first result of f() to go into a field named 'c',
> Sean> and the second result of f() to go into a field named 'd'. Yes,
> Sean> that isn't going to work.
>
> Well... sorta.
>
> ((Cc"a" * Cc"b" / f):Cg"c" * (Cb"c" / 2):Cg"d"):ptmatch("ccc");
This is hurting my brain. It just doesn't look right to me, as I would
write this as:
Ct(
Cg(Cc"a" * Cc"b" / f , 'c')
* Cg(Cb"c" / 2, 'd')
)
> This puts the list of captures returned by f into named capture "c", and
> then extracts the second one into "d" by using a back capture. When Ct()
> is applied to the result, only the first value of "c" is used, so the
> result is { c="ab", d="ba" }.
I'll admit, I'm surprised! I haven't used "pattern / number" in any LPEG
code I've written so far.
> Note, though, that in my tests (on 5.3) this ended up evaluating f()
> twice
I think this is more on the LPEG version and less on the Lua version.
-spc