lua-users home
lua-l archive

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


2012/2/20 William Ahern <william@25thandclement.com>:
> On Mon, Feb 20, 2012 at 06:21:17PM +0100, Christophe Jorssen wrote:
>> Hello all,
>>
>> I'm trying to convert a string like
>>
>>   'a?(b?c:d):e'
>>
>> to another string
>>
>> 'ifthenelse(a,ifthenelse(b,c,d),e)'
>>

Thanks William, it works like a charm (do you want me to publish your
solution to stackoverflow, with due credits of course ?).

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

?

> 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.

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

> 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?

Many thanks.

Best regards

-- 
Christophe