LPeg grammars and metatables

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

LPeg grammars and metatables

Peter Odding
I've been working on a lexer framework based on LPeg and noticed that lpeg.P() does not accept grammars with open references to patterns defined in another grammar shadowed by the current grammar with __index. Has support for __index in grammars been considered for a future version of LPeg? That would allow grammars to inherit one another via Lua's metatables...

Thanks in advance,

 - Peter Odding

Reply | Threaded
Open this post in threaded view
|

Re: LPeg grammars and metatables

Roberto Ierusalimschy
> I've been working on a lexer framework based on LPeg and noticed that 
> lpeg.P() does not accept grammars with open references to patterns defined 
> in another grammar shadowed by the current grammar with __index. Has 
> support for __index in grammars been considered for a future version of 
> LPeg? That would allow grammars to inherit one another via Lua's 
> metatables...

Can you be more specific about what you have in mind? An example, perhaps?

-- Roberto

Reply | Threaded
Open this post in threaded view
|

Re: LPeg grammars and metatables

Peter Odding
Roberto Ierusalimschy wrote:
I've been working on a lexer framework based on LPeg and noticed that lpeg.P() does not accept grammars with open references to patterns defined in another grammar shadowed by the current grammar with __index. Has support for __index in grammars been considered for a future version of LPeg? That would allow grammars to inherit one another via Lua's metatables...

Can you be more specific about what you have in mind? An example, perhaps?

-- Roberto


My lexer framework contains a table of named patterns that all lexers may use. This table contains start/end of string assertions, line boundary assertions, character classes, etc.: patterns I don't want to repeat in each lexer. This table is basically an LPeg grammar without initial rule, though it's never fixed with lpeg.P().

Each lexer is represented by an LPeg grammar. I wanted to be able to refer to patterns in the table described above from each lexer's grammar table via lpeg.V('key'), after having installed a metatable on the grammar table that makes it shadow the table described above (via __index).

My request is to let LPeg honor __index for open references in grammar tables, so I can avoid copying patterns back and forth between grammar tables. But maybe that's not so bad after all: on second thought this might not belong in LPeg...

Totally unrelated but to small for a new thread: Can I match the start of string in LPeg without lpeg.P(function(s, i) return i == 1 and i end)?

Thanks for your time,

 - Peter Odding

Reply | Threaded
Open this post in threaded view
|

Re: LPeg grammars and metatables

Roberto Ierusalimschy
> Totally unrelated but to small for a new thread: Can I match the start of 
> string in LPeg without lpeg.P(function(s, i) return i == 1 and i end)?

(Sorry to take so long to answer...)

The match function in Lpeg only mathes at the start of the string, so
usually you do not know any specific test to know whether you are there.

-- Roberto