Parsing string with Boost Spirit 2 to populate data in a user-defined structure

I am using Boost.Spirit, which has been distributed since Boost-1.42.0 with VS2005. My problem is this.

I have this line which has been separated by commas. The first three fields are strings, and the rest are numbers. like this.

String1,String2,String3,12.0,12.1,13.0,13.1,12.4 

My rule is like this

 qi::rule<string::iterator, qi::skip_type> stringrule = *(char_ - ',') qi::rule<string::iterator, qi::skip_type> myrule= repeat(3)[*(char_ - ',') >> ','] >> (double_ % ',') ; 

I am trying to save data in such a structure.

 struct MyStruct { vector<string> stringVector ; vector<double> doubleVector ; } ; MyStruct var ; 

I wrapped it in BOOST_FUSION_ADAPT_STRUCTURE to use it with spirit.

 BOOST_FUSION_ADAPT_STRUCT (MyStruct, (vector<string>, stringVector) (vector<double>, doubleVector)) 

My parse function parses the string and returns true and after

 qi::phrase_parse (iterBegin, iterEnd, myrule, boost::spirit::ascii::space, var) ; 

I expect var.stringVector and var.doubleVector to be populated correctly. but this is not so.

What is going wrong?

Sample code is here

Thanks in advance, Surya

+4
source share
1 answer

qi::skip_type is not something you can use a skipper. qi :: skip_type is a qi::skip placeholder type that is applicable only to the skip[] directive (to enable skipping inside lexeme[] or to use the skipper used) and which is not an analyzer component that matches any input on its own. Instead, you need to specify your specific skipper type (in your case boost::spirit::ascii:space_type ).

In addition, in order for your rules to return a parsed attribute, you need to specify the type of expected attribute when defining your rule. This leaves you:

 qi::rule<string::iterator, std::string(), ascii:space_type> stringrule = *(char_ - ','); qi::rule<string::iterator, MyStruct(), ascii:space_type> myrule = repeat(3)[*(char_ - ',') >> ','] >> (double_ % ','); 

which should do exactly what you expect.

+6
source

Source: https://habr.com/ru/post/1304454/


All Articles