lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


> Anyway, in the code below the tests C1..C3 and D1..D4 are based in
> your examples. In the block D1..D4 only D4 yields an error, and that
> makes sense to me - but in the block C1..C3 the tests C1 and C3 yield
> errors but C2 does not. Is this behavior of C1..C3 something that is
> explained in the documentation? How? I admit that I'm still struggling
> with some terms, sorry...
> 
> The code:
> 
>   [...]
> 
>   (Cb"c"      )     :pm()                   -- C1
>   (Cb"c":Cg"c")     :pm()                   -- C2
>   (Cb"c":Cg"c"):Ct():pm()                   -- C3

C2 is really subtle. I first thought that was a bug :-)

All three cases are wrong. In C2, however, as the back reference is
inside a named group, the named group just throws away its inner
capture, without evaluating it. The manual allows that:

  "Usually, LPeg does not specify when (and if) it evaluates its captures."

As the back reference is not evaluated, no error is raised. (Again, this
highlights that captures are computations; it is hard to express them
statically...)

In C3, the table capture does use the named group, and therefore
it needs to evaluate its inner capture, and that raises the error.

Note that the following case also does not raise an error:

  > lpeg.Cg(lpeg.Cb("c"), "b"):match("")

-- Roberto