Ok.. Here's a trick... If you "as" something twice.. you can transform the
inner one for 'nil' (if it's optional) to something you want.

https://gist.github.com/NigelThorne/9425445#file-example2-rb

parser...

    rule(:format)               { (offset.maybe.as(:offset_val).as(:offset) >>
format_code).as(:format) }

transform

        rule({:offset_val => simple(:v)})       {  v || 5 }


---
"No man is an island... except Philip"


On Sun, Mar 9, 2014 at 12:23 AM, Foster, Ron <[email protected]>wrote:

> Nigel,
>
>
>
> Thank you very much for putting that together.
>
>
>
> I was wondering if there might be a more general technique for optionals.
>
> The truth of the matter is that this is just a small piece of the grammar.
>
> There are several more elements, and some of those will also be optional.
>
>
>
> I was trying to avoid a combinatorial explosion to deal with every case.
>
> It sounds as if you were thinking the same, but hit some of the snags I
> ran into.
>
>
>
> You gave me some new ideas I hadn't thought of. That's always good.
>
> I very much appreciate that you looked into my question.
>
>
>
> *From:* [email protected] [mailto:[email protected]] *On
> Behalf Of *Nigel Thorne
> *Sent:* Friday, March 07, 2014 10:42 PM
> *To:* [email protected]
> *Subject:* Re: [ruby.parslet] transformation on parser with optional
> fields
>
>
>
> see https://gist.github.com/NigelThorne/9425445#file-example-rb
>
>
>
> Have two rules, one for the default v ersion and one for the 'with offset'
> version.
>
>
>
> I thought you could transform to add a default value, then have other
> rules just match it... but that didn't work for me.
>
>
>
> Cheers
>
> Nigel
>
>
>
>
>
>
>
>
>
>
>
>
> ---
>
> "Man, I'm going to have so many chickens when this lot hatch!"
>
>
>
> On Sat, Mar 8, 2014 at 4:25 AM, Foster, Ron <[email protected]>
> wrote:
>
>
>
> Hi.
>
> Am having some trouble composing a transform on a grammar that has an
> optional field.
>
>
>
> A simple example:
>
>
>
> I& #8217;m looking for a formatting code: e.g. "    d ",
>
> But it could include an optional offset specification: "    [16]  d "
>
>
>
> If that optional field is there, I need to grab the number between the
> brackets.
>
> Ot herwise, I can assume it to be zero.
>
>
>
> I can parse both forms ok,
>
>
>
> But haven't figured out how to build a transform to handle both the
> presence or absence of the optional digits in brackets.
>
>
>
> Any suggestions would be greatly appreciated.
>
>
>
>
>
> Thanks.
>
>
>

Reply via email to