Boost logo

Boost Users :

Subject: Re: [Boost-users] [spirit] help parsing data file into a struct
From: OvermindDL1 (overminddl1_at_[hidden])
Date: 2009-12-18 05:33:34


On Thu, Dec 17, 2009 at 11:44 AM, Kenny Riddile <kfriddile_at_[hidden]> wrote:
> This is my first time using Spirit and I haven't messed with writing EBNF
> grammars since college, so I'm sorry if this is a simple question.  Lets say
> I have a structure that looks like this:
>
> struct Foo
> {
>    std::string someString;
>    std::string someOtherString;
>    std::vector<std::string> stringList;
> };
>
> BOOST_FUSION_ADAPT_STRUCT(
>    Foo,
>    (std::string, someString)
>    (std::string, someOtherString)
>    (std::vector<std::string>, stringList) )
>
> and I have a data file that looks like this:
>
> SOMESTRING hello
> SOMEOTHERSTRING goodbye
>
> STRINGLIST
> {
>    string1
>    string2
>    string3
> }
>
> I have written the following grammar to parse such a file into the struct
> above:
>
> template< typename Iterator >
> struct FooDefinition : spirit::qi::grammar< Iterator, Foo() >
> {
>    FooDefinition()
>        : FooDefinition::base_type( start )
>    {
>        using namespace spirit::qi;
>
>        start = someString >> +space >> someOtherString >> +space >>
> stringList;
>        someString = lit("SOMESTRING") >> +space >> value;
>        someOtherString = lit("SOMEOTHERSTRING") >> +space >> value;
>        stringList = lit("STRINGLIST") >> *space >> lit('{') >> *space >>
> (value % +space) >> *space >> lit('}');
>        value = +char_("a-zA-Z_0-9");
>        space = lit(' ') | lit('\n') | lit('\t');
>    }
>
>    spirit::qi::rule< Iterator, Foo() > start;
>    spirit::qi::rule< Iterator, std::string() > someString;
>    spirit::qi::rule< Iterator, std::string() > someOtherString;
>    spirit::qi::rule< Iterator, std::vector<std::string>() > stringList;
>    spirit::qi::rule< Iterator, std::string() > value;
>    spirit::qi::rule< Iterator > space;
> };
>
> However, I would like the order of the three statements in the data file
> (SOMESTRING, SOMEOTHERSTRING, and STRINGLIST) to be irrelevant.  Right now,
> they must be in the order shown above for parsing to succeed.  How would I
> go about accomplishing this with Spirit?

That is what the permutation operator is for ( ^ ), and it acts like,
well, a permutation. :)

This is also answered faster if asked on the Spirit mailing list.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net