]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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 | // No actions. | |
11 | // | |
12 | // [ JDG May 10, 2002 ] spirit1 | |
13 | // [ JDG March 24, 2007 ] spirit2 | |
14 | // | |
15 | /////////////////////////////////////////////////////////////////////////////// | |
16 | ||
7c673cae FG |
17 | #include <boost/spirit/include/qi.hpp> |
18 | ||
19 | #include <iostream> | |
20 | #include <string> | |
21 | #include <vector> | |
22 | ||
23 | namespace client | |
24 | { | |
25 | namespace qi = boost::spirit::qi; | |
26 | namespace ascii = boost::spirit::ascii; | |
27 | ||
28 | /////////////////////////////////////////////////////////////////////////// | |
29 | // Our number list parser | |
30 | /////////////////////////////////////////////////////////////////////////// | |
31 | //[tutorial_numlist1 | |
32 | template <typename Iterator> | |
33 | bool parse_numbers(Iterator first, Iterator last) | |
34 | { | |
35 | using qi::double_; | |
36 | using qi::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 | //////////////////////////////////////////////////////////////////////////// | |
53 | // Main program | |
54 | //////////////////////////////////////////////////////////////////////////// | |
55 | int | |
56 | main() | |
57 | { | |
58 | std::cout << "/////////////////////////////////////////////////////////\n\n"; | |
59 | std::cout << "\t\tA comma separated list parser for Spirit...\n\n"; | |
60 | std::cout << "/////////////////////////////////////////////////////////\n\n"; | |
61 | ||
62 | std::cout << "Give me a comma separated list of numbers.\n"; | |
63 | std::cout << "Type [q or Q] to quit\n\n"; | |
64 | ||
65 | std::string str; | |
66 | while (getline(std::cin, str)) | |
67 | { | |
68 | if (str.empty() || str[0] == 'q' || str[0] == 'Q') | |
69 | break; | |
70 | ||
71 | if (client::parse_numbers(str.begin(), str.end())) | |
72 | { | |
73 | std::cout << "-------------------------\n"; | |
74 | std::cout << "Parsing succeeded\n"; | |
75 | std::cout << str << " Parses OK: " << std::endl; | |
76 | } | |
77 | else | |
78 | { | |
79 | std::cout << "-------------------------\n"; | |
80 | std::cout << "Parsing failed\n"; | |
81 | std::cout << "-------------------------\n"; | |
82 | } | |
83 | } | |
84 | ||
85 | std::cout << "Bye... :-) \n\n"; | |
86 | return 0; | |
87 | } | |
88 | ||
89 |