| 1 | /*============================================================================= |
| 2 | Copyright (c) 2003 Sam Nabialek |
| 3 | Copyright (c) 2001-2010 Joel de Guzman |
| 4 | |
| 5 | Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 6 | file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 7 | =============================================================================*/ |
| 8 | /////////////////////////////////////////////////////////////////////////////// |
| 9 | // |
| 10 | // The Nabialek trick. |
| 11 | // |
| 12 | // [ Sam Nabialek; Somewhere, sometime in 2003... ] spirit1 |
| 13 | // [ JDG November 17, 2009 ] spirit2 |
| 14 | // [ JDG January 10, 2010 ] Updated to use rule pointers |
| 15 | // for efficiency. |
| 16 | // |
| 17 | /////////////////////////////////////////////////////////////////////////////// |
| 18 | |
| 19 | #include <boost/spirit/include/qi.hpp> |
| 20 | #include <boost/phoenix/operator.hpp> |
| 21 | #include <iostream> |
| 22 | #include <string> |
| 23 | |
| 24 | namespace client |
| 25 | { |
| 26 | namespace qi = boost::spirit::qi; |
| 27 | namespace ascii = boost::spirit::ascii; |
| 28 | |
| 29 | /////////////////////////////////////////////////////////////////////////////// |
| 30 | // Our nabialek_trick grammar |
| 31 | /////////////////////////////////////////////////////////////////////////////// |
| 32 | template <typename Iterator> |
| 33 | struct nabialek_trick : qi::grammar< |
| 34 | Iterator, ascii::space_type, qi::locals<qi::rule<Iterator, ascii::space_type>*> > |
| 35 | { |
| 36 | nabialek_trick() : nabialek_trick::base_type(start) |
| 37 | { |
| 38 | using ascii::alnum; |
| 39 | using qi::lexeme; |
| 40 | using qi::lazy; |
| 41 | using qi::_a; |
| 42 | using qi::_1; |
| 43 | |
| 44 | id = lexeme[*(ascii::alnum | '_')]; |
| 45 | one = id; |
| 46 | two = id >> ',' >> id; |
| 47 | |
| 48 | keyword.add |
| 49 | ("one" , &one) |
| 50 | ("two" , &two) |
| 51 | ; |
| 52 | |
| 53 | start = *(keyword[_a = _1] >> lazy(*_a)); |
| 54 | } |
| 55 | |
| 56 | qi::rule<Iterator, ascii::space_type> id, one, two; |
| 57 | qi::rule<Iterator, ascii::space_type, qi::locals<qi::rule<Iterator, ascii::space_type>*> > start; |
| 58 | qi::symbols<char, qi::rule<Iterator, ascii::space_type>*> keyword; |
| 59 | }; |
| 60 | } |
| 61 | |
| 62 | /////////////////////////////////////////////////////////////////////////////// |
| 63 | // Main program |
| 64 | /////////////////////////////////////////////////////////////////////////////// |
| 65 | int |
| 66 | main() |
| 67 | { |
| 68 | using boost::spirit::ascii::space; |
| 69 | typedef std::string::const_iterator iterator_type; |
| 70 | typedef client::nabialek_trick<iterator_type> nabialek_trick; |
| 71 | |
| 72 | nabialek_trick g; // Our grammar |
| 73 | |
| 74 | std::string str = "one only\none again\ntwo first,second" ; |
| 75 | std::string::const_iterator iter = str.begin(); |
| 76 | std::string::const_iterator end = str.end(); |
| 77 | bool r = phrase_parse(first&: iter, last: end, expr&: g, skipper: space); |
| 78 | |
| 79 | if (r && iter == end) |
| 80 | { |
| 81 | std::cout << "-------------------------\n" ; |
| 82 | std::cout << "Parsing succeeded\n" ; |
| 83 | std::cout << "-------------------------\n" ; |
| 84 | } |
| 85 | else |
| 86 | { |
| 87 | std::string rest(iter, end); |
| 88 | std::cout << "-------------------------\n" ; |
| 89 | std::cout << "Parsing failed\n" ; |
| 90 | std::cout << "stopped at: \": " << rest << "\"\n" ; |
| 91 | std::cout << "-------------------------\n" ; |
| 92 | } |
| 93 | |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | |
| 98 | |