Embedded skipper design pattern

Jonathan Biggar <[email protected]>
Newsgroups gmane.comp.parsers.spirit.general
Message-ID <CAA=GnWiaUwJ_BA8aBxftpeXf3tt=dAyZHC=D9RqKcbhG=JbMMw@mail.gmail.com>
I like the idea of embedding a skipper rule directly in my grammar, if you
have no good reason not to.  Here's how to do it using Spirit 2.5:

1.  Define your top level grammar without a skipper:

template <typename Iterator>
struct MyGrammar : qi::grammar<Iterator, MyAttribute()>
{
    using Skipper = qi::rule<Iterator>;
    ...
}

2:  Add the skipper to the rules in your grammar that need it:

    qi::rule<IteratorTopAttribute()>  r_start;
    qi::rule<Iterator, AnotherAttribute(), Skipper>  r_definition;
    qi::rule<Iterator, ThirdAttribute()> r_something_no_skip;
    Skipper skipper;

3:  Define your rules like this:

MyGrammar<MyIterator>::MyGrammar() : MyGrammar(base_type(r_start,
"MyGrammar") {
    r_start = skip(skipper.alias()) [ * r_definition ]  >> * skipper ;
// Use this to clean up trailing whitespace
    r_start = skip(skipper.alias()) [ * r_definition ] ;    // Otherwise
use this

    skipper = qi::space ;  // or whatever you need

    r_definition = ... ;  // uses the skipper
    r_something_no_skip = ... ; // doesn't use the skipper
    ...
}

You can also use qi::lexeme or qi::no_skip where useful.

Note that the "skipper.alias()" is very necessary to avoid crashes.

Enjoy!

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot

_______________________________________________
Spirit-general mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/spirit-general
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.