| 1 | /*============================================================================= |
| 2 | Copyright (c) 2002-2015 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 | // No semantic actions. |
| 11 | // |
| 12 | // [ JDG May 10, 2002 ] spirit1 |
| 13 | // [ JDG March 24, 2007 ] spirit2 |
| 14 | // [ JDG May 12, 2015 ] spirit X3 |
| 15 | // |
| 16 | /////////////////////////////////////////////////////////////////////////////// |
| 17 | |
| 18 | #include <boost/spirit/home/x3.hpp> |
| 19 | |
| 20 | #include <iostream> |
| 21 | #include <string> |
| 22 | #include <vector> |
| 23 | |
| 24 | namespace client |
| 25 | { |
| 26 | namespace x3 = boost::spirit::x3; |
| 27 | namespace ascii = boost::spirit::x3::ascii; |
| 28 | |
| 29 | /////////////////////////////////////////////////////////////////////////// |
| 30 | // Our number list parser |
| 31 | /////////////////////////////////////////////////////////////////////////// |
| 32 | template <typename Iterator> |
| 33 | bool parse_numbers(Iterator first, Iterator last) |
| 34 | { |
| 35 | using x3::double_; |
| 36 | using x3::phrase_parse; |
| 37 | using ascii::space; |
| 38 | |
| 39 | bool r = phrase_parse( |
| 40 | first, // Start Iterator |
| 41 | last, // End Iterator |
| 42 | double_ >> *(',' >> double_), // The Parser |
| 43 | space // The Skip-Parser |
| 44 | ); |
| 45 | if (first != last) // fail if we did not get a full match |
| 46 | return false; |
| 47 | return r; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | //////////////////////////////////////////////////////////////////////////// |
| 52 | // Main program |
| 53 | //////////////////////////////////////////////////////////////////////////// |
| 54 | int |
| 55 | main() |
| 56 | { |
| 57 | std::cout << "/////////////////////////////////////////////////////////\n\n" ; |
| 58 | std::cout << "\t\tA comma separated list parser for Spirit...\n\n" ; |
| 59 | std::cout << "/////////////////////////////////////////////////////////\n\n" ; |
| 60 | |
| 61 | std::cout << "Give me a comma separated list of numbers.\n" ; |
| 62 | std::cout << "Type [q or Q] to quit\n\n" ; |
| 63 | |
| 64 | std::string str; |
| 65 | while (getline(is&: std::cin, str&: str)) |
| 66 | { |
| 67 | if (str.empty() || str[0] == 'q' || str[0] == 'Q') |
| 68 | break; |
| 69 | |
| 70 | if (client::parse_numbers(first: str.begin(), last: str.end())) |
| 71 | { |
| 72 | std::cout << "-------------------------\n" ; |
| 73 | std::cout << "Parsing succeeded\n" ; |
| 74 | std::cout << str << " Parses OK: " << std::endl; |
| 75 | } |
| 76 | else |
| 77 | { |
| 78 | std::cout << "-------------------------\n" ; |
| 79 | std::cout << "Parsing failed\n" ; |
| 80 | std::cout << "-------------------------\n" ; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | std::cout << "Bye... :-) \n\n" ; |
| 85 | return 0; |
| 86 | } |
| 87 | |