lua-users home
lua-l archive

Generating a pretty AST with LPeg

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


Hi!
I've nearly finished my first parsing task using LPeg (which was parsing
SAS-code to visualize the "data flow") and it was fun.
As I'm lacking some experience my "AST" resulting from the parsing step
is not too pretty. Let me give you a simplified example of what my "AST"
looks now. Consider parsing "function calls" in some hypothetical
language:
Using
 local WS = lpeg.S' \n\t'^0
 local NAME = lpeg.C( lpeg.R'az'^1 )
 local PLIST = WS * ( NAME * ( WS * ',' * WS * NAME )^0 )^-1
 local FCALL = lpeg.Cc'FCALL'
 * lpeg.Ct( WS * NAME * '(' * PLIST * WS * ')' )
 local ALL = lpeg.Ct( FCALL^0 )
with
 lpeg.match( ALL, [[
 foo( bar, baz )
 asdf( qwer )]] )
results in the table
 {
 'FCALL', { 'foo', 'bar', 'baz' },
 'FCALL', { 'asdf', 'qwer' }
 }
which can be interpreted as the list of expression types ('FCALL'),
followed by a data structure containing the function name and the
arguments.
For better readability and "type safety" I'd rather like it to be
 {
 'FCALL', { name = 'foo', args = { 'bar', 'baz' } },
 'FCALL', { name = 'asdf', args = { 'qwer' } }
 }
Currently, I'm achieving this by a second transformation step.
I'm pretty sure this can be directly done by some clever trick in the
LPeg expressions above.
Please drop me a hint on how to do it.
Any other suggestions are welcome, even a "This is not the way to do it,
usually you do [...]".
Regards,
Matthias

AltStyle によって変換されたページ (->オリジナル) /