lua-users home
lua-l archive

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


It was thus said that the Great Andrew Starks once stated:
> On Tuesday, April 7, 2015, Sean Conner <sean@conman.org> wrote:
> 
> > It was thus said that the Great Daurnimator once stated:
> > > On 8 April 2015 at 10:12, Hisham <h@hisham.hm <javascript:;>> wrote:
> > > > Ah! NO LPEG ALLOWED! :)
> > >
> > > Why not?
> >
> >   Way too easy.
> >
> >         lpeg = require "lpeg"
> >         id = lpeg.R("AZ","az","09","__")^1 / variables
> >         expand_variables = lpeg.Cs((
> >                    (lpeg.P"${") / "" * id * (lpeg.P"}"/"")
> >                  + (lpeg.P"$")  / "" * id
> >                  + lpeg.C(lpeg.P(1))
> >                )^0)
> >
> >   -spc (Could be made even smaller ... )
> >
> >
> >
> Right. Too easy and too readable. :)

  Okay, how about:

re = require "re"

expand_variables = re.compile([[
        expand          <-      {~ (full / partial / .)* ~}
        full            <-      '${' -> '' id '}' -> ''
        partial         <-      '$'  -> '' id
        id              <-      [A-Za-z-0-9_]+ -> variables
  ]],{ variables = variables })

  -spc (A more or less literal translation ... )