Reputation:
I would like to create a boost spirit rule that parses a string and an float. The strings I would like to parse are formatted like this:
(str astring)(n 123)
or could also be in another order:
(n 123)(str astring)
I would like to create a rule with an attribute of following type:
qi::rule<Iter, boost::fusion::vector<std::string, float>, ascii::space_type> hj;
So far this is my code:
qi::rule<Iter, std::string(), ascii::space_type> attr;
attr = lexeme[*alnum];
qi::rule<Iter, boost::fusion::vector<std::string, float>, ascii::space_type> hj;
hj = (
'(' >> lit("str") >> attr(/*put this at position 0*/) >> ')'
|
'(' >> lit("n") >> float_[/*put this at position 1*/] >> ')'
);
Obviously, this does not compile (boost deducts another attribute type). How can I implement this, especially, what would I put in the commented code?
Upvotes: 2
Views: 158
Reputation: 11181
You can use the permutation operator ^
. Minimum working example:
boost::fusion::vector<std::string,float> ans;
bool res = phrase_parse(first,last,
(lit("(") >> lit("str") >> lexeme[*ascii::alnum] >> lit(")") ) ^
(lit("(") >> lit("n") >> float_ >> lit(")") ),
ascii::space,ans);
This solution is described with more detail here.
Upvotes: 2