lua-users home
lua-l archive

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

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