lua-users home
lua-l archive

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


On Mon, Jan 30, 2012 at 6:50 AM, Jay Carlson <nop@nop.com> wrote:

> csv = t.grammar{
>   record={t.group{
>          t.exp[[field]]
>          t.star{",", t.exp[[field]]}
>       }, t.choice{t.nl, t.eof}
>     },
>   field=t.choice{t.exp[[escaped]], t.exp[[nonescaped]]},
>   nonescaped={t.range'^,"%nl'.star().capture()},
>   escaped={'"',
>       t.choice{ t.range'^"', t.cap('""').as('"') }.star().capture()
>     '"'}
> }

Table constructors are distinctly lacking sugar. This is the second
time I've wanted letrec. Let's imagine:

csv = t.grammar{
  -- imagine: local field, nonescaped, escaped
  record={t.group{
         field,
         t.star{",", field}
      }, t.choice{t.nl, t.eof}
    },
  field=t.choice{escaped, nonescaped},
  nonescaped={t.range'^,"%nl'.star().capture()},
  escaped={'"',
      t.choice{ t.range'^"', t.cap('""').as('"') }.star().capture()
    '"'}
  -- and all the interior references get fixed up
}

...not that I'm volunteering to code any of this mess...