
On Thu, Dec 17, 2009 at 11:44 AM, Kenny Riddile
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::vectorstd::string stringList; };
BOOST_FUSION_ADAPT_STRUCT( Foo, (std::string, someString) (std::string, someOtherString) (std::vectorstd::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::vectorstd::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.