Wednesday, 15 July 2015

c++ - Why can't I parse this double_? -



c++ - Why can't I parse this double_? -

when parse input std::string, string, when parse double_, fusion struct contains little number rather expected.

#include <boost/spirit/include/qi.hpp> #include <boost/fusion/include/adapt_struct.hpp> #include <string> // http://www.boost.org/doc/libs/1_57_0/libs/spirit/example/qi/employee.cpp namespace formatconverter { namespace qi = boost::spirit::qi; namespace ascii = boost::spirit::ascii; // todo: should have initializer? struct asc { double timestamp; }; } boost_fusion_adapt_struct( formatconverter::asc, (double, timestamp) ) namespace formatconverter { template <typename iterator> struct asc_parser : qi::grammar< iterator, asc(), ascii::space_type > { asc_parser() : asc_parser::base_type(start) { timestamp %= qi::double_ ; start %= timestamp ; ; } qi::rule< iterator, double, ascii::space_type > timestamp; qi::rule< iterator, asc(), ascii::space_type > start; }; }

and testing with:

#define boost_test_module parser #include <boost/test/included/unit_test.hpp> #include "../formatconverter/formatconverter.h" #include <string> boost_auto_test_suite( testsuite1 ) boost_auto_test_case( timestamp ) { namespace qi = boost::spirit::qi; namespace ascii = boost::spirit::ascii; using iterator_type = std::string::iterator; using parser_type = formatconverter::asc_parser<iterator_type>; parser_type grammar; formatconverter::asc record; std::string str("161.096841 "); auto beg = str.begin(); auto end = str.end(); auto success = qi::phrase_parse(beg, end, grammar, ascii::space, record); boost_require(success); boost_require(beg==end); std::cout << "timestamp: " << boost::fusion::as_vector(record) << std::endl; } boost_auto_test_suite_end()

you missed () in attribute:

qi::rule< iterator, double, ascii::space_type > timestamp;

should be

qi::rule< iterator, double(), ascii::space_type > timestamp;

because params order in qi::rule can arbitrary (except iterator), internally lib uses traits recognize attr, skipper, ...etc. attr field has in form of function-sig, i.e. synthesized(inherited), if write double, won't recognized attr, timestamp rule have unused_type instead of double attr, means, record.timestamp won't filled parser, it's uninitialized.

c++ boost-spirit boost-spirit-qi

No comments:

Post a Comment