/*============================================================================= Copyright (c) 2001-2011 Joel de Guzman http://spirit.sourceforge.net/ 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) =============================================================================*/ //[reference_includes #include #include #include #include #include #include #include #include #include #include //] //[reference_test template void test_parser( char const* input, P const& p, bool full_match = true) { using boost::spirit::qi::parse; char const* f(input); char const* l(f + strlen(f)); if (parse(f, l, p) && (!full_match || (f == l))) std::cout << "ok" << std::endl; else std::cout << "fail" << std::endl; } template void test_phrase_parser( char const* input, P const& p, bool full_match = true) { using boost::spirit::qi::phrase_parse; using boost::spirit::qi::ascii::space; char const* f(input); char const* l(f + strlen(f)); if (phrase_parse(f, l, p, space) && (!full_match || (f == l))) std::cout << "ok" << std::endl; else std::cout << "fail" << std::endl; } //] //[reference_test_attr template void test_parser_attr( char const* input, P const& p, T& attr, bool full_match = true) { using boost::spirit::qi::parse; char const* f(input); char const* l(f + strlen(f)); if (parse(f, l, p, attr) && (!full_match || (f == l))) std::cout << "ok" << std::endl; else std::cout << "fail" << std::endl; } template void test_phrase_parser_attr( char const* input, P const& p, T& attr, bool full_match = true) { using boost::spirit::qi::phrase_parse; using boost::spirit::qi::ascii::space; char const* f(input); char const* l(f + strlen(f)); if (phrase_parse(f, l, p, space, attr) && (!full_match || (f == l))) std::cout << "ok" << std::endl; else std::cout << "fail" << std::endl; } //] //[reference_keyword_list_test_data_structure // Data structure definitions to test the kwd directive // and the keywords list operator struct person { std::string name; int age; double size; std::vector favorite_colors; }; std::ostream &operator<<(std::ostream &os, const person &p) { os<<"Person : "<(os,"\n")); return os; } BOOST_FUSION_ADAPT_STRUCT( person, (std::string, name) (int, age) (double, size) (std::vector, favorite_colors) ) //] int main() { // keyword_list { //[reference_using_declarations_keyword_list using boost::spirit::repository::qi::kwd; using boost::spirit::qi::inf; using boost::spirit::ascii::space_type; using boost::spirit::ascii::char_; using boost::spirit::qi::double_; using boost::spirit::qi::int_; using boost::spirit::qi::rule; //] //[reference_keyword_list_rule_declarations rule parse_string; rule no_constraint_person_rule, constraint_person_rule; parse_string %= '"'> *(char_-'"') > '"'; //] //[reference_keyword_list_no_constraint_rule no_constraint_person_rule %= kwd("name")['=' > parse_string ] / kwd("age") ['=' > int_] / kwd("size") ['=' > double_ > 'm'] ; //] //[reference_keyword_list //`Parsing a keyword list: // Let's declare a small list of people for which we want to collect information. person John,Mary,Mike,Hellen,Johny; test_phrase_parser_attr( "name = \"John\" \n age = 10 \n size = 1.69m " ,no_constraint_person_rule ,John); // full in orginal order std::cout< int_] / kwd("size" ,1) ['=' > double_ > 'm'] / kwd("favorite color",0,inf) [ '=' > parse_string ] ; //] //[reference_keyword_list_constraints // Here all the give constraint are resepected : parsing will succeed. test_phrase_parser_attr( "name = \"Hellen\" \n age = 10 \n size = 1.80m \n favorite color = \"blue\" \n favorite color = \"green\" " ,constraint_person_rule ,Hellen); std::cout<