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