Release 1.39.0
[boost.git] / Boost_1_39_0 / libs / spirit / example / qi / num_list2.cpp
blob0a8abc348c842b428d21930e47e6ccbdc479ae7b
1 /*=============================================================================
2 Copyright (c) 2002-2007 Joel de Guzman
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.
12 // [ JDG May 10, 2002 ] spirit1
13 // [ JDG March 24, 2007 ] spirit2
15 ///////////////////////////////////////////////////////////////////////////////
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>
23 #include <iostream>
24 #include <string>
25 #include <vector>
27 using namespace boost::phoenix;
28 using namespace boost::spirit;
29 using namespace boost::spirit::qi;
30 using namespace boost::spirit::ascii;
31 using namespace boost::spirit::arg_names;
33 ///////////////////////////////////////////////////////////////////////////////
34 // Our number list compiler
35 ///////////////////////////////////////////////////////////////////////////////
36 //[tutorial_numlist2
37 template <typename Iterator>
38 bool parse_numbers(Iterator first, Iterator last, std::vector<double>& v)
40 bool r = phrase_parse(first, last,
42 // Begin grammar
44 double_[push_back(ref(v), _1)]
45 >> *(',' >> double_[push_back(ref(v), _1)])
48 // End grammar
50 space);
52 if (first != last) // fail if we did not get a full match
53 return false;
54 return r;
56 //]
58 ////////////////////////////////////////////////////////////////////////////
59 // Main program
60 ////////////////////////////////////////////////////////////////////////////
61 int
62 main()
64 std::cout << "/////////////////////////////////////////////////////////\n\n";
65 std::cout << "\t\tA comma separated list parser for Spirit...\n\n";
66 std::cout << "/////////////////////////////////////////////////////////\n\n";
68 std::cout << "Give me a comma separated list of numbers.\n";
69 std::cout << "The numbers will be inserted in a vector of numbers\n";
70 std::cout << "Type [q or Q] to quit\n\n";
72 std::string str;
73 while (getline(std::cin, str))
75 if (str.empty() || str[0] == 'q' || str[0] == 'Q')
76 break;
78 std::vector<double> v;
79 if (parse_numbers(str.begin(), str.end(), v))
81 std::cout << "-------------------------\n";
82 std::cout << "Parsing succeeded\n";
83 std::cout << str << " Parses OK: " << std::endl;
85 for (std::vector<double>::size_type i = 0; i < v.size(); ++i)
86 std::cout << i << ": " << v[i] << std::endl;
88 std::cout << "\n-------------------------\n";
90 else
92 std::cout << "-------------------------\n";
93 std::cout << "Parsing failed\n";
94 std::cout << "-------------------------\n";
98 std::cout << "Bye... :-) \n\n";
99 return 0;