lua-users home
lua-l archive

Re: Generating a pretty AST with LPeg

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


On Wed, Jan 29, 2014 at 10:41 AM, Matthias Kluwe <mkluwe@gmail.com> wrote:
>
> {
> 'FCALL', { 'foo', 'bar', 'baz' },
> 'FCALL', { 'asdf', 'qwer' }
> }
>
>
> For better readability and "type safety" I'd rather like it to be
>
> {
> 'FCALL', { name = 'foo', args = { 'bar', 'baz' } },
> 'FCALL', { name = 'asdf', args = { 'qwer' } }
> }
>
It looks like the feature you want is lpeg.Cg(patt, name) [1].
For your example, here is an alternative version of FCALL using Cg:
> 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 NAME_f = lpeg.Cg(NAME, 'name')
 local ARGS_f = lpeg.Cg(lpeg.Ct(PLIST), 'args')
 local FCALL = lpeg.Cc'FCALL'
 * lpeg.Ct( WS * NAME_f * '(' * ARGS_f * WS * ')' )
[1] http://www.inf.puc-rio.br/~roberto/lpeg/#cap-g
-Duncan

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