// Copyright (c) 2001-2010 Hartmut Kaiser // // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // This example demonstrates a trick allowing to adapt a template data // structure as a Fusion sequence in order to use is for direct attribute // propagation. For more information see // http://boost-spirit.com/home/2010/02/08/how-to-adapt-templates-as-a-fusion-sequence #include namespace qi = boost::spirit::qi; namespace fusion = boost::fusion; namespace client { template struct data { A a; B b; }; template struct data_grammar : qi::grammar()> { data_grammar() : data_grammar::base_type(start) { start = real_start; real_start = qi::auto_ >> ',' >> qi::auto_; } qi::rule()> start; qi::rule()> real_start; }; } namespace boost { namespace spirit { namespace traits { template struct transform_attribute, fusion::vector, qi::domain> { typedef fusion::vector type; static type pre(client::data& val) { return type(val.a, val.b); } static void post(client::data&, fusion::vector const&) {} static void fail(client::data&) {} }; }}} /////////////////////////////////////////////////////////////////////////////// int main() { std::cout << "/////////////////////////////////////////////////////////\n\n"; std::cout << "\t\tA parser for Spirit utilizing an adapted template ...\n\n"; std::cout << "/////////////////////////////////////////////////////////\n\n"; std::cout << "Give me two comma separated integers:\n"; std::cout << "Type [q or Q] to quit\n\n"; std::string str; client::data_grammar g; // Our grammar while (getline(std::cin, str)) { if (str.empty() || str[0] == 'q' || str[0] == 'Q') break; client::data d; std::string::const_iterator iter = str.begin(); std::string::const_iterator end = str.end(); bool r = phrase_parse(iter, end, g, qi::space, d); if (r && iter == end) { std::cout << "-------------------------\n"; std::cout << "Parsing succeeded\n"; std::cout << "got: " << d.a << "," << d.b << std::endl; std::cout << "\n-------------------------\n"; } else { std::cout << "-------------------------\n"; std::cout << "Parsing failed\n"; std::cout << "-------------------------\n"; } } return 0; }