]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/example/x3/sum.cpp
1 /*=============================================================================
2 Copyright (c) 2002-2015 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 ///////////////////////////////////////////////////////////////////////////////
9 // A parser for summing a comma-separated list of numbers using phoenix.
11 // [ JDG June 28, 2002 ] spirit1
12 // [ JDG March 24, 2007 ] spirit2
13 // [ JDG May 12, 2015 ] spirit X3
15 ///////////////////////////////////////////////////////////////////////////////
17 #include <boost/config/warning_disable.hpp>
18 #include <boost/spirit/home/x3.hpp>
24 namespace x3
= boost::spirit::x3
;
25 namespace ascii
= boost::spirit::x3::ascii
;
31 ///////////////////////////////////////////////////////////////////////////
33 ///////////////////////////////////////////////////////////////////////////
35 template <typename Iterator
>
36 bool adder(Iterator first
, Iterator last
, double& n
)
38 auto assign
= [&](auto& ctx
){ n
= _attr(ctx
); };
39 auto add
= [&](auto& ctx
){ n
+= _attr(ctx
); };
41 bool r
= x3::phrase_parse(first
, last
,
45 double_
[assign
] >> *(',' >> double_
[add
])
52 if (first
!= last
) // fail if we did not get a full match
58 ////////////////////////////////////////////////////////////////////////////
60 ////////////////////////////////////////////////////////////////////////////
64 std::cout
<< "/////////////////////////////////////////////////////////\n\n";
65 std::cout
<< "\t\tA parser for summing a list of numbers...\n\n";
66 std::cout
<< "/////////////////////////////////////////////////////////\n\n";
68 std::cout
<< "Give me a comma separated list of numbers.\n";
69 std::cout
<< "The numbers are added using Phoenix.\n";
70 std::cout
<< "Type [q or Q] to quit\n\n";
73 while (getline(std::cin
, str
))
75 if (str
.empty() || str
[0] == 'q' || str
[0] == 'Q')
79 if (client::adder(str
.begin(), str
.end(), n
))
81 std::cout
<< "-------------------------\n";
82 std::cout
<< "Parsing succeeded\n";
83 std::cout
<< str
<< " Parses OK: " << std::endl
;
85 std::cout
<< "sum = " << n
;
86 std::cout
<< "\n-------------------------\n";
90 std::cout
<< "-------------------------\n";
91 std::cout
<< "Parsing failed\n";
92 std::cout
<< "-------------------------\n";
96 std::cout
<< "Bye... :-) \n\n";