]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/example/x3/num_list/num_list3.cpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / libs / spirit / example / x3 / num_list / num_list3.cpp
1 /*=============================================================================
2 Copyright (c) 2002-2015 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 //
11 // [ JDG May 10, 2002 ] spirit1
12 // [ JDG March 24, 2007 ] spirit2
13 // [ JDG May 12, 2015 ] spirit X3
14 //
15 ///////////////////////////////////////////////////////////////////////////////
16
17 #include <boost/spirit/home/x3.hpp>
18
19 #include <iostream>
20 #include <string>
21 #include <vector>
22
23 namespace client
24 {
25 namespace x3 = boost::spirit::x3;
26 namespace ascii = boost::spirit::x3::ascii;
27
28 ///////////////////////////////////////////////////////////////////////////
29 // Our number list compiler
30 ///////////////////////////////////////////////////////////////////////////
31 template <typename Iterator>
32 bool parse_numbers(Iterator first, Iterator last, std::vector<double>& v)
33 {
34 using x3::double_;
35 using x3::phrase_parse;
36 using x3::_attr;
37 using ascii::space;
38
39 auto push_back = [&](auto& ctx){ v.push_back(_attr(ctx)); };
40
41 bool r = phrase_parse(first, last,
42
43 // Begin grammar
44 (
45 double_[push_back] % ','
46 )
47 ,
48 // End grammar
49
50 space);
51
52 if (first != last) // fail if we did not get a full match
53 return false;
54 return r;
55 }
56 }
57
58 ////////////////////////////////////////////////////////////////////////////
59 // Main program
60 ////////////////////////////////////////////////////////////////////////////
61 int
62 main()
63 {
64 std::cout << "/////////////////////////////////////////////////////////\n\n";
65 std::cout << "\t\tA comma separated list parser for Spirit...\n\n";
66 std::cout << "/////////////////////////////////////////////////////////\n\n";
67
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";
71
72 std::string str;
73 while (getline(std::cin, str))
74 {
75 if (str.empty() || str[0] == 'q' || str[0] == 'Q')
76 break;
77
78 std::vector<double> v;
79 if (client::parse_numbers(str.begin(), str.end(), v))
80 {
81 std::cout << "-------------------------\n";
82 std::cout << "Parsing succeeded\n";
83 std::cout << str << " Parses OK: " << std::endl;
84
85 for (std::vector<double>::size_type i = 0; i < v.size(); ++i)
86 std::cout << i << ": " << v[i] << std::endl;
87
88 std::cout << "\n-------------------------\n";
89 }
90 else
91 {
92 std::cout << "-------------------------\n";
93 std::cout << "Parsing failed\n";
94 std::cout << "-------------------------\n";
95 }
96 }
97
98 std::cout << "Bye... :-) \n\n";
99 return 0;
100 }