[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Lpeg: a question about function captures and named captures
- From: Andrew Gierth <andrew@...>
- Date: Wed, 12 Apr 2023 21:41:05 +0100
>>>>> "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 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" }.
Note, though, that in my tests (on 5.3) this ended up evaluating f()
twice (the back captures are intentionally unspecified as to whether
they re-evaluate or reuse a previous evaluation). So this maybe isn't
going to work well if f() is costly or impure.
--
Andrew.