I created a test application to illustrate my problem. It parses a list of integers preceded by "a =" or "b =" and separates "\ r \ n". The list contains several occurrences of these fields in any order.
typedef std::vector<unsigned int> uint_vector_t;
std::ostream& operator<<(std::ostream& out, const uint_vector_t &data)
{
for (unsigned int i(0); i < data.size(); i++)
{
out << data[i] << '\n';
}
return out;
}
struct MyStruct
{
uint_vector_t m_aList;
uint_vector_t m_bList;
};
BOOST_FUSION_ADAPT_STRUCT
(
MyStruct,
(uint_vector_t, m_aList)
(uint_vector_t, m_bList)
)
;
template<typename Iterator>
struct MyParser : public boost::spirit::qi::grammar<Iterator,
MyStruct()>
{
MyParser() :
MyParser::base_type(Parser, "Parser")
{
using boost::spirit::qi::uint_;
using boost::spirit::qi::_val;
using boost::spirit::qi::_1;
using boost::phoenix::at_c;
using boost::phoenix::push_back;
Parser =
*(
aParser [push_back(at_c<0>(_val), _1)]
|
bParser [push_back(at_c<1>(_val), _1)]
);
aParser = "a=" >> uint_ >> "\r\n";
bParser = "b=" >> uint_ >> "\r\n";
}
boost::spirit::qi::rule<Iterator, MyStruct()> Parser;
boost::spirit::qi::rule<Iterator, unsigned int()> aParser, bParser;
};
int main()
{
using boost::spirit::qi::phrase_parse;
std::string input("a=0\r\nb=7531\r\na=2\r\na=3\r\nb=246\r\n");
std::string::const_iterator begin = input.begin();
std::string::const_iterator end = input.end();
MyParser<std::string::const_iterator> parser;
MyStruct result;
bool succes = phrase_parse(begin, end, parser, "", result);
assert(succes);
std::cout << "===A===\n" <<result.m_aList << "===B===\n" << result.m_bList << std::endl;
}
In practice, there are more fields with different types that need to be analyzed. My objection to this approach is the following expression: [push_back (at_c <0> (_ val), _1)] Here is a “hidden relationship” between the destination and the first MyStruct element. This makes the code fragile for change. If the structure is changed, it can still compile, but there will no longer be what is expected.
, : [push_back (at_c < 0 > bind (& MyStruct:: aList, arg1) (_ val), _1)]
. this. .
- ? ?