]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/example/qi/sum.cpp
1 /*=============================================================================
2 Copyright (c) 2002-2010 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
14 ///////////////////////////////////////////////////////////////////////////////
16 #include <boost/config/warning_disable.hpp>
17 //[tutorial_adder_includes
18 #include <boost/spirit/include/qi.hpp>
19 #include <boost/spirit/include/phoenix_core.hpp>
20 #include <boost/spirit/include/phoenix_operator.hpp>
27 //[tutorial_adder_using
28 namespace qi
= boost::spirit::qi
;
29 namespace ascii
= boost::spirit::ascii
;
30 namespace phoenix
= boost::phoenix
;
38 ///////////////////////////////////////////////////////////////////////////
40 ///////////////////////////////////////////////////////////////////////////
43 template <typename Iterator
>
44 bool adder(Iterator first
, Iterator last
, double& n
)
46 bool r
= qi::phrase_parse(first
, last
,
50 double_
[ref(n
) = _1
] >> *(',' >> double_
[ref(n
) += _1
])
57 if (first
!= last
) // fail if we did not get a full match
64 ////////////////////////////////////////////////////////////////////////////
66 ////////////////////////////////////////////////////////////////////////////
70 std::cout
<< "/////////////////////////////////////////////////////////\n\n";
71 std::cout
<< "\t\tA parser for summing a list of numbers...\n\n";
72 std::cout
<< "/////////////////////////////////////////////////////////\n\n";
74 std::cout
<< "Give me a comma separated list of numbers.\n";
75 std::cout
<< "The numbers are added using Phoenix.\n";
76 std::cout
<< "Type [q or Q] to quit\n\n";
79 while (getline(std::cin
, str
))
81 if (str
.empty() || str
[0] == 'q' || str
[0] == 'Q')
85 if (client::adder(str
.begin(), str
.end(), n
))
87 std::cout
<< "-------------------------\n";
88 std::cout
<< "Parsing succeeded\n";
89 std::cout
<< str
<< " Parses OK: " << std::endl
;
91 std::cout
<< "sum = " << n
;
92 std::cout
<< "\n-------------------------\n";
96 std::cout
<< "-------------------------\n";
97 std::cout
<< "Parsing failed\n";
98 std::cout
<< "-------------------------\n";
102 std::cout
<< "Bye... :-) \n\n";