[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: intra table reference
- From: spir <denis.spir@...>
- Date: Mon, 30 Nov 2009 18:17:00 +0100
Tony Finch <dot@dotat.at> dixit:
> On Mon, 30 Nov 2009, spir wrote:
> >
> > Thank you. I think this may be a solution, a kind of table factory.
> >
> > The use case is to build parsers/grammars as tables of (named) patterns.
> > This seems to me rather an obvious solution in Lua. However,
> > higher-level patterns need to reference lower-level ones, eg integer =
> > OneOrMore(digit).
>
> I do this when writing LPEG grammars.
>
> The basic pattern is below. Sometimes I return the whole pattern
> environment, in which case match operations look like
> parser.thing:match(str), or I just return the start symbol so match
> operations are parser:match(str)
>
> Note this code uses my LPEG patch so % operators are not necessary to
> access the pattern environment, and I've also used a pythonic % operator.
>
> local function make_parser()
> -- environment for non-terminals
> local env = {}
> local function p(pat)
> return re.compile(pat, env)
> end
> setfenv(1,env)
>
> -- define grammar here
[...]
> return env
> end
> local parser = make_parser()
>
> Tony.
Thank you, that's exactly the approach I was looking for.
Denis
________________________________
la vita e estrany
http://spir.wikidot.com/