lua-users home
lua-l archive

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


It was thus said that the Great Roberto Ierusalimschy once stated:
> > [...]
> > 
> >   So even if I were to use lpeg.Cmt() to force evaluation of all nested
> > captures, I'm still not garenteed to get what I want (I think---I tried and
> > no, it still didn't work, but I would like to hear from Roberto if I'm
> > interpreting this correctly.
> 
> I am not sure I understood exactly what is your question.

  The question is the exepected behavior of lpeg.Cg() in the following code:

lpeg = require "lpeg"

char = lpeg.P"%s" * lpeg.Carg(1) / "%1" * lpeg.Cg(lpeg.Cc(false),'redirect')
     + lpeg.P"%t" * lpeg.Carg(2) / "%1"
     + lpeg.R" ~"
cmd  = lpeg.Cg(lpeg.Cc(true),'redirect')
     * lpeg.Cs(char^1)
     * lpeg.Cb'redirect'

print(cmd:match("foo -t %t %s",1,"/tmp/bar.foo","application/x-foo"))

  I was expecting the match to return false as the second capture, and it
was as if the call to lpeg.Cg() in the char expression was being dropped. 
The description of lpeg.Cb():

        Creates a back capture. This pattern matches the empty string and
        produces the values produced by the most recent group capture named
        name (where name can be any Lua value).

        Most recent means the last complete outermost group capture with the
        given name. A Complete capture means that the entire pattern
        corresponding to the capture has matched. An Outermost capture means
        that the capture is not inside another complete capture.

        In the same way that LPeg does not specify when it evaluates
        captures, it does not specify whether it reuses values previously
        produced by the group or re-evaluates them.

seems to indicate that indeed, the use of lpeg.Cg() within the context of
lpeg.Cs() means it is ignored when using lpeg.Cb() to retrieve the value (if
I read everything right), and that even using lpeg.Cmt() (which forces
evaluations of all captures at that time) won't work either.

  -spc