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 Sean Conner once stated:
> 
>   My current attempt:
> 
> 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"))
> print(cmd:match("bar -t %t",   1,"/tmp/foo.bar","application/x-bar"))
> 
>   This outputs:
> 
> foo -t application/x-foo /tmp/bar.foo   true
> bar -t application/x-bar        true
> 
> I'd like the output to be:
> 
> foo -t application/x-foo /tmp/bar.foo   false
> bar -t application/x-bar        true
> 
>   I'm using a named group to track if I need redirection or not, and since a
> named group does not return a value, it shouldn't affect the substitution
> capture (and it doesn't).  But the group capture in the char expression
> seems to be ignored.
> 
>   What's going on here?  Am I misunderstanding the documentation?

  I think I'm misunderstanding the documention.  lpeg.Cb() states:

	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.

  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.

  -spc (I would still like to find an LPeg solution, but not hopeful ... )