| 1 | /*============================================================================= |
| 2 | Copyright (c) 2002-2010 Joel de Guzman |
| 3 | |
| 4 | Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 5 | file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 | =============================================================================*/ |
| 7 | /////////////////////////////////////////////////////////////////////////////// |
| 8 | // |
| 9 | // This sample demontrates a parser for a comma separated list of numbers. |
| 10 | // The numbers are inserted in a vector using phoenix. |
| 11 | // |
| 12 | // [ JDG May 10, 2002 ] spirit1 |
| 13 | // [ JDG March 24, 2007 ] spirit2 |
| 14 | // |
| 15 | /////////////////////////////////////////////////////////////////////////////// |
| 16 | |
| 17 | #include <boost/spirit/include/qi.hpp> |
| 18 | #include <boost/phoenix/core.hpp> |
| 19 | #include <boost/phoenix/operator.hpp> |
| 20 | #include <boost/phoenix/stl.hpp> |
| 21 | |
| 22 | #include <iostream> |
| 23 | #include <string> |
| 24 | #include <vector> |
| 25 | |
| 26 | namespace client |
| 27 | { |
| 28 | namespace qi = boost::spirit::qi; |
| 29 | namespace ascii = boost::spirit::ascii; |
| 30 | |
| 31 | /////////////////////////////////////////////////////////////////////////// |
| 32 | // Our number list compiler |
| 33 | /////////////////////////////////////////////////////////////////////////// |
| 34 | //[tutorial_numlist4 |
| 35 | template <typename Iterator> |
| 36 | bool parse_numbers(Iterator first, Iterator last, std::vector<double>& v) |
| 37 | { |
| 38 | using qi::double_; |
| 39 | using qi::phrase_parse; |
| 40 | using qi::_1; |
| 41 | using ascii::space; |
| 42 | |
| 43 | bool r = phrase_parse(first, last, |
| 44 | |
| 45 | // Begin grammar |
| 46 | ( |
| 47 | double_ % ',' |
| 48 | ) |
| 49 | , |
| 50 | // End grammar |
| 51 | |
| 52 | space, v); |
| 53 | |
| 54 | if (first != last) // fail if we did not get a full match |
| 55 | return false; |
| 56 | return r; |
| 57 | } |
| 58 | //] |
| 59 | } |
| 60 | |
| 61 | //////////////////////////////////////////////////////////////////////////// |
| 62 | // Main program |
| 63 | //////////////////////////////////////////////////////////////////////////// |
| 64 | int |
| 65 | main() |
| 66 | { |
| 67 | std::cout << "/////////////////////////////////////////////////////////\n\n" ; |
| 68 | std::cout << "\t\tA comma separated list parser for Spirit...\n\n" ; |
| 69 | std::cout << "/////////////////////////////////////////////////////////\n\n" ; |
| 70 | |
| 71 | std::cout << "Give me a comma separated list of numbers.\n" ; |
| 72 | std::cout << "The numbers will be inserted in a vector of numbers\n" ; |
| 73 | std::cout << "Type [q or Q] to quit\n\n" ; |
| 74 | |
| 75 | std::string str; |
| 76 | while (getline(is&: std::cin, str&: str)) |
| 77 | { |
| 78 | if (str.empty() || str[0] == 'q' || str[0] == 'Q') |
| 79 | break; |
| 80 | |
| 81 | std::vector<double> v; |
| 82 | if (client::parse_numbers(first: str.begin(), last: str.end(), v)) |
| 83 | { |
| 84 | std::cout << "-------------------------\n" ; |
| 85 | std::cout << "Parsing succeeded\n" ; |
| 86 | std::cout << str << " Parses OK: " << std::endl; |
| 87 | |
| 88 | for (std::vector<double>::size_type i = 0; i < v.size(); ++i) |
| 89 | std::cout << i << ": " << v[i] << std::endl; |
| 90 | |
| 91 | std::cout << "\n-------------------------\n" ; |
| 92 | } |
| 93 | else |
| 94 | { |
| 95 | std::cout << "-------------------------\n" ; |
| 96 | std::cout << "Parsing failed\n" ; |
| 97 | std::cout << "-------------------------\n" ; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | std::cout << "Bye... :-) \n\n" ; |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | |
| 106 | |