Multiple captures with patt/function in LPEG

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Multiple captures with patt/function in LPEG

Paul K-2
Hi All,

I have this small issue with an LPEG capture pattern I'm working with
and hope someone can explain what may be going on. I'm missing
something simple here, but I can't figure out why I only get one
capture with the following:

lpeg.match(((lpeg.Cp() * lpeg.R('04')) / print)^1, "4123")

This prints 1 2 3 4 (four lines), which is what the positions of the
matches are. If I remove Cp, I get 4 1 2 3 (four lines), which is what
I expect. Why don't I get
1 4
2 1
3 2
4 3
from the first example? I expect "print" to get "all captures made by
patt as arguments", which are 1 (from Cp) and 4 from R('04'), no?
Thank you.

Paul.

Reply | Threaded
Open this post in threaded view
|

Re: Multiple captures with patt/function in LPEG

Coroutines
On Thu, Aug 28, 2014 at 11:42 PM, Paul K <[hidden email]> wrote:

> Hi All,
>
> I have this small issue with an LPEG capture pattern I'm working with
> and hope someone can explain what may be going on. I'm missing
> something simple here, but I can't figure out why I only get one
> capture with the following:
>
> lpeg.match(((lpeg.Cp() * lpeg.R('04')) / print)^1, "4123")
>
> This prints 1 2 3 4 (four lines), which is what the positions of the
> matches are. If I remove Cp, I get 4 1 2 3 (four lines), which is what
> I expect. Why don't I get
> 1 4
> 2 1
> 3 2
> 4 3
> from the first example? I expect "print" to get "all captures made by
> patt as arguments", which are 1 (from Cp) and 4 from R('04'), no?
> Thank you.
>
> Paul.
>

I'm not sure but I think you are currently matching the range of
characters "lpeg.R('04')" but not capturing them?  lpeg isn't
available through homebrew and I don't feel like going through the
work of installing it :(

try this?  lpeg.match(((lpeg.Cp() * lpeg.C(lpeg.R('04'))) / print)^1, "4123")

Reply | Threaded
Open this post in threaded view
|

Re: Multiple captures with patt/function in LPEG

Paul K-2
> try this?  lpeg.match(((lpeg.Cp() * lpeg.C(lpeg.R('04'))) / print)^1, "4123")

Indeed; that fixes it. Thank you.

Paul.

Reply | Threaded
Open this post in threaded view
|

Re: Multiple captures with patt/function in LPEG

Coroutines
On Fri, Aug 29, 2014 at 12:07 AM, Paul K <[hidden email]> wrote:
>> try this?  lpeg.match(((lpeg.Cp() * lpeg.C(lpeg.R('04'))) / print)^1, "4123")
>
> Indeed; that fixes it. Thank you.

Yay I'm useful :D  So... yeah you were matching those numbers but not
capturing them alongside the position of the match :3

Good luck to ya mate <3

Reply | Threaded
Open this post in threaded view
|

Re: Multiple captures with patt/function in LPEG

Dirk Laurie-2
In reply to this post by Coroutines
2014-08-29 8:54 GMT+02:00 Coroutines <[hidden email]>:
> On Thu, Aug 28, 2014 at 11:42 PM, Paul K <[hidden email]> wrote:
>> lpeg.match(((lpeg.Cp() * lpeg.R('04')) / print)^1, "4123")
>>
>> Why don't I get
>> 1 4
>> 2 1
>> 3 2
>> 4 3
> try this?  lpeg.match(((lpeg.Cp() * lpeg.C(lpeg.R('04'))) / print)^1, "4123")

Equivalently:

lpeg.match(((lpeg.Cp() * (lpeg.R'04'/1)) / print)^1, "4123")