lua-users home
lua-l archive

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


Roberto Ierusalimschy wrote:

> local function select (t)
>   -- succeeds only if extra argument to 'match' is equal to 't'
>   return lpeg.Cmt(lpeg.Carg(1), (function (s, i, c)
>                                    return (c == t)
>                                  end))
> end

Wow. Took me some time to grasp that. Before that, I never
knew why I should ever use lpeg.Cmt. Now I realise it's
a mighty tool.

However the manual just says that the `function' of lpeg.Cmt
should return a new current position or false. I take it that
it can return true as well in which case the current position
is not changed.


Regarding my original example my main concern was the duplicated
code in the two grammars. Then I realised that you can call one
grammar from within another to avoid code duplication. That's nice. 


grammar2 = lpeg.P{
   'rule_c';
   rule_c = lpeg.R('az')/print;
}

grammar1 = lpeg.P{
   'rule_a';
   rule_a = '[' * rule_b * ']';
   rule_b = grammar2 * (',' * rule_b)^0;
}

lpeg.match(grammar1, '[a,b,c]', 1)
lpeg.match(grammar2, 'a')



Ciao
Andreas