]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/example/qi/num_list4.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / spirit / example / qi / num_list4.cpp
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
32 ///////////////////////////////////////////////////////////////////////////
33 // Our number list compiler
34 ///////////////////////////////////////////////////////////////////////////
35 //[tutorial_numlist4
36 template <typename Iterator>
37 bool parse_numbers(Iterator first, Iterator last, std::vector<double>& v)
38 {
39 using qi::double_;
40 using qi::phrase_parse;
41 using qi::_1;
42 using ascii::space;
43
44 bool r = phrase_parse(first, last,
45
46 // Begin grammar
47 (
48 double_ % ','
49 )
50 ,
51 // End grammar
52
53 space, v);
54
55 if (first != last) // fail if we did not get a full match
56 return false;
57 return r;
58 }
59 //]
60 }
61
62 ////////////////////////////////////////////////////////////////////////////
63 // Main program
64 ////////////////////////////////////////////////////////////////////////////
65 int
66 main()
67 {
68 std::cout << "/////////////////////////////////////////////////////////\n\n";
69 std::cout << "\t\tA comma separated list parser for Spirit...\n\n";
70 std::cout << "/////////////////////////////////////////////////////////\n\n";
71
72 std::cout << "Give me a comma separated list of numbers.\n";
73 std::cout << "The numbers will be inserted in a vector of numbers\n";
74 std::cout << "Type [q or Q] to quit\n\n";
75
76 std::string str;
77 while (getline(std::cin, str))
78 {
79 if (str.empty() || str[0] == 'q' || str[0] == 'Q')
80 break;
81
82 std::vector<double> v;
83 if (client::parse_numbers(str.begin(), str.end(), v))
84 {
85 std::cout << "-------------------------\n";
86 std::cout << "Parsing succeeded\n";
87 std::cout << str << " Parses OK: " << std::endl;
88
89 for (std::vector<double>::size_type i = 0; i < v.size(); ++i)
90 std::cout << i << ": " << v[i] << std::endl;
91
92 std::cout << "\n-------------------------\n";
93 }
94 else
95 {
96 std::cout << "-------------------------\n";
97 std::cout << "Parsing failed\n";
98 std::cout << "-------------------------\n";
99 }
100 }
101
102 std::cout << "Bye... :-) \n\n";
103 return 0;
104 }
105
106