[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: lpeg: subset of a grammar
- From: Andreas Matthias <andreas.matthias@...>
- Date: Wed, 18 Aug 2010 23:02:14 +0200
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
- Prev by Date:
Re: Basic, no fuss, no magic, foolproof module pattern. [Was: require, module, globals and "magic"]
- Next by Date:
Re: Basic, no fuss, no magic, foolproof module pattern. [Was: require, module, globals and "magic"]
- Previous by thread:
Re: lpeg: subset of a grammar
- Next by thread:
Re: lpeg: subset of a grammar
- Index(es):