lua-users home
lua-l archive

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


On Tue, Feb 21, 2012 at 11:34:06AM +0100, Christophe Jorssen wrote:
> 2012/2/20 William Ahern <william@25thandclement.com>:
<snip>
> Thanks William, it works like a charm (do you want me to publish your
> solution to stackoverflow, with due credits of course ?).

I have no objections, notwithstanding my dislike for web-based forums ;)

> May I ask you some complementary questions about your code?
> 
> > local var = lpeg.C(lpeg.alpha * (lpeg.alnum^0))
> 
> Is there any difference between
> 
> (lpeg.alnum^0)
> (lpeg.alnum)^0
> lpeg.alnum^0
> ?

No difference. I forgot to remove the inner parentheses after I removed some
debugging code.

> 
> > local E, G = lpeg.V"E", lpeg.V"G"
> 
> I thought that lpeg.V was valid only inside grammars as the manual
> states "The created non-terminal refers to the rule indexed by v in
> the /enclosing/ grammar".  Good to know.

Consider that even if you call lpeg.V within the grammar definition (lpeg.P{
[here] }), the object is still being instantiated before being passed as an
argument to lpeg.P{}. So it doesn't matter whether you call it inside the
table definition or not.

lpeg.V just creates, if I understand things correctly, an open reference.
It's a placeholder for non-terminals (expressions that can reference
themselves), and must be resolved before a match can be run against the PEG.
lpeg.P{} does the resolution by matching the lpeg.V names with indices in
the grammar table.

> Why do you do that? Just to have a shortcut to lpeg.V"E" or for some
> other reasons?

I'm sort of new to LPeg and was just following convention--or what I
perceived as convention. The non-trivial examples in the LPeg manual do it
this way. Also, E and G are the critical elements of the grammar, so it
makes sense for them to be both prominent and terse.

> > local grammar = lpeg.P{ "E",
> >        E = ((var + G) * (lpeg.P"?" * E * lpeg.P":" * E)^-1) / tr,
> >        G = lpeg.P"(" * E * lpeg.P")",
> > }
> 
> So this is done without captures (I mean without lpeg.C*). I
> misunderstood the manual thinking the patt in 'patt / function' was
> necessarily a capture (since a capture is a pattern and it is
> documented in the capture section).
> 
> One thing I do not understand is how function tr is fed by the parser
> without captures? For example, why "?" or  ":" do not end up as
> arguments given to tr?

The captures are coming from `var'.