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/config/warning_disable.hpp>
18 #include <boost/spirit/include/qi.hpp>
19 #include <boost/spirit/include/phoenix_core.hpp>
20 #include <boost/spirit/include/phoenix_operator.hpp>
21 #include <boost/spirit/include/phoenix_stl.hpp>
22
23 #include <iostream>
24 #include <string>
25 #include <vector>
26
27 namespace client
28 {
29 namespace qi = boost::spirit::qi;
30 namespace ascii = boost::spirit::ascii;
31 namespace phoenix = boost::phoenix;
32
33 ///////////////////////////////////////////////////////////////////////////
34 // Our number list compiler
35 ///////////////////////////////////////////////////////////////////////////
36 //[tutorial_numlist2
37 template <typename Iterator>
parse_numbers(Iterator first,Iterator last,std::vector<double> & v)38 bool parse_numbers(Iterator first, Iterator last, std::vector<double>& v)
39 {
40 using qi::double_;
41 using qi::phrase_parse;
42 using qi::_1;
43 using ascii::space;
44 using phoenix::push_back;
45
46 bool r = phrase_parse(first, last,
47
48 // Begin grammar
49 (
50 double_[push_back(phoenix::ref(v), _1)]
51 >> *(',' >> double_[push_back(phoenix::ref(v), _1)])
52 )
53 ,
54 // End grammar
55
56 space);
57
58 if (first != last) // fail if we did not get a full match
59 return false;
60 return r;
61 }
62 //]
63 }
64
65 ////////////////////////////////////////////////////////////////////////////
66 // Main program
67 ////////////////////////////////////////////////////////////////////////////
68 int
main()69 main()
70 {
71 std::cout << "/////////////////////////////////////////////////////////\n\n";
72 std::cout << "\t\tA comma separated list parser for Spirit...\n\n";
73 std::cout << "/////////////////////////////////////////////////////////\n\n";
74
75 std::cout << "Give me a comma separated list of numbers.\n";
76 std::cout << "The numbers will be inserted in a vector of numbers\n";
77 std::cout << "Type [q or Q] to quit\n\n";
78
79 std::string str;
80 while (getline(std::cin, str))
81 {
82 if (str.empty() || str[0] == 'q' || str[0] == 'Q')
83 break;
84
85 std::vector<double> v;
86 if (client::parse_numbers(str.begin(), str.end(), v))
87 {
88 std::cout << "-------------------------\n";
89 std::cout << "Parsing succeeded\n";
90 std::cout << str << " Parses OK: " << std::endl;
91
92 for (std::vector<double>::size_type i = 0; i < v.size(); ++i)
93 std::cout << i << ": " << v[i] << std::endl;
94
95 std::cout << "\n-------------------------\n";
96 }
97 else
98 {
99 std::cout << "-------------------------\n";
100 std::cout << "Parsing failed\n";
101 std::cout << "-------------------------\n";
102 }
103 }
104
105 std::cout << "Bye... :-) \n\n";
106 return 0;
107 }
108
109
110