[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: lpeg.Cb bug (?) with repeated evaluation of group capture pattern
- From: Roberto Ierusalimschy <roberto@...>
- Date: Thu, 15 Sep 2016 12:47:40 -0300
> [...]
>
> So the first Cb() returns the table returned by Ct(). Why does the second
> call to Cb() return a new table? Cg() groups all values returned by the
> pattern into a single capture, so there should only *be* one capture. Or am
> I misreading the manual? Does it mysteriously "rerun" the pattern each time
> it's called?
The manual does not say *when* (or even *if*) a capture 'Ct' runs. It
only says that, *when* it runs, it returns a table with such and such
elements. (Probably the manual would be clearer if it says that the
capture "returns a table" instead of "creates a table".) So, nothing
prevents it from running several times (therefore, creating several
tables) or not running at all, if its value is not needed.
> And now the two calls to Cb() return the same table. I think the
> confusion here is how
>
> Cg(Ct"","foo")
>
> seems to keep returning new captures for the given pattern for each call to
> Cb(), whereas
>
> Cg(Cmt("",function(s,p,c) return p,{} end),"foo")
>
> returns the same capture for each call to Cb(). What, exactly, is the
> distinction between the two?
The manual says about 'Cmt':
Unlike all other captures, this one is evaluated immediately when a
match occurs. It forces the immediate evaluation of all its nested
captures and then calls function.
This is the only capture that specifies *when* (and *if*) it runs. It
is the only one where you can count on side effects.
-- Roberto