]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/example/x3/num_list/num_list4.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 // This sample demontrates a parser for a comma separated list of numbers.
10 // This time, the numbers are automatically collected into the attribute by
11 // the parser itself using the full power of attribute grammars.
13 // [ JDG May 10, 2002 ] spirit1
14 // [ JDG March 24, 2007 ] spirit2
15 // [ JDG May 12, 2015 ] spirit X3
17 ///////////////////////////////////////////////////////////////////////////////
19 #include <boost/config/warning_disable.hpp>
20 #include <boost/spirit/home/x3.hpp>
28 namespace x3
= boost::spirit::x3
;
29 namespace ascii
= boost::spirit::x3::ascii
;
31 ///////////////////////////////////////////////////////////////////////////
32 // Our number list compiler
33 ///////////////////////////////////////////////////////////////////////////
35 template <typename Iterator
>
36 bool parse_numbers(Iterator first
, Iterator last
, std::vector
<double>& v
)
39 using x3::phrase_parse
;
43 bool r
= phrase_parse(first
, last
,
54 if (first
!= last
) // fail if we did not get a full match
61 ////////////////////////////////////////////////////////////////////////////
63 ////////////////////////////////////////////////////////////////////////////
67 std::cout
<< "/////////////////////////////////////////////////////////\n\n";
68 std::cout
<< "\t\tA comma separated list parser for Spirit...\n\n";
69 std::cout
<< "/////////////////////////////////////////////////////////\n\n";
71 std::cout
<< "Give me a comma separated list of numbers.\n";
72 std::cout
<< "The numbers will be inserted in a vector of numbers\n";
73 std::cout
<< "Type [q or Q] to quit\n\n";
76 while (getline(std::cin
, str
))
78 if (str
.empty() || str
[0] == 'q' || str
[0] == 'Q')
81 std::vector
<double> v
;
82 if (client::parse_numbers(str
.begin(), str
.end(), v
))
84 std::cout
<< "-------------------------\n";
85 std::cout
<< "Parsing succeeded\n";
86 std::cout
<< str
<< " Parses OK: " << std::endl
;
88 for (std::vector
<double>::size_type i
= 0; i
< v
.size(); ++i
)
89 std::cout
<< i
<< ": " << v
[i
] << std::endl
;
91 std::cout
<< "\n-------------------------\n";
95 std::cout
<< "-------------------------\n";
96 std::cout
<< "Parsing failed\n";
97 std::cout
<< "-------------------------\n";
101 std::cout
<< "Bye... :-) \n\n";