]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/test/qi/utree4.cpp
1 // Copyright (c) 2001-2011 Hartmut Kaiser
2 // Copyright (c) 2001-2011 Joel de Guzman
3 // Copyright (c) 2010 Bryce Lelbach
5 // Distributed under the Boost Software License, Version 1.0. (See accompanying
6 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8 #include <boost/spirit/include/qi.hpp>
9 #include <boost/spirit/include/support_utree.hpp>
15 inline bool check(boost::spirit::utree
const& val
, std::string expected
)
19 if (s
.str() == expected
+ " ")
22 std::cerr
<< "got result: " << s
.str()
23 << ", expected: " << expected
<< std::endl
;
29 using spirit_test::test_attr
;
30 using boost::spirit::utree
;
31 using boost::spirit::utree_type
;
32 using boost::spirit::utf8_string_range_type
;
33 using boost::spirit::utf8_symbol_type
;
34 using boost::spirit::utf8_string_type
;
36 using boost::spirit::qi::real_parser
;
37 using boost::spirit::qi::strict_real_policies
;
38 using boost::spirit::qi::digit
;
39 using boost::spirit::qi::char_
;
40 using boost::spirit::qi::string
;
41 using boost::spirit::qi::int_
;
42 using boost::spirit::qi::double_
;
43 using boost::spirit::qi::space
;
44 using boost::spirit::qi::space_type
;
45 using boost::spirit::qi::rule
;
46 using boost::spirit::qi::as
;
47 using boost::spirit::qi::lexeme
;
51 typedef as
<std::string
> as_string_type
;
52 as_string_type
const as_string
= as_string_type();
54 typedef as
<utf8_symbol_type
> as_symbol_type
;
55 as_symbol_type
const as_symbol
= as_symbol_type();
58 BOOST_TEST(test_attr("xy", as_string
[char_
>> char_
], ut
) &&
59 ut
.which() == utree_type::string_type
&& check(ut
, "\"xy\""));
62 BOOST_TEST(test_attr("ab1.2", as_string
[*~digit
] >> double_
, ut
) &&
63 ut
.which() == utree_type::list_type
&& check(ut
, "( \"ab\" 1.2 )"));
66 BOOST_TEST(test_attr("xy", as_string
[*char_
], ut
) &&
67 ut
.which() == utree_type::string_type
&& check(ut
, "\"xy\""));
70 BOOST_TEST(test_attr("x,y", as_string
[char_
>> ',' >> char_
], ut
) &&
71 ut
.which() == utree_type::string_type
&& check(ut
, "\"xy\""));
74 BOOST_TEST(test_attr("x,y", char_
>> ',' >> char_
, ut
) &&
75 ut
.which() == utree_type::list_type
&& check(ut
, "( \"x\" \"y\" )"));
78 BOOST_TEST(test_attr("a,b1.2", as_string
[~digit
% ','] >> double_
, ut
) &&
79 ut
.which() == utree_type::list_type
&& check(ut
, "( \"ab\" 1.2 )"));
82 BOOST_TEST(test_attr("a,b1.2", ~digit
% ',' >> double_
, ut
) &&
83 ut
.which() == utree_type::list_type
&& check(ut
, "( \"a\" \"b\" 1.2 )"));
86 BOOST_TEST(test_attr("xy", as_symbol
[char_
>> char_
], ut
) &&
87 ut
.which() == utree_type::symbol_type
&& check(ut
, "xy"));
90 BOOST_TEST(test_attr("ab1.2", as_symbol
[*~digit
] >> double_
, ut
) &&
91 ut
.which() == utree_type::list_type
&& check(ut
, "( ab 1.2 )"));
94 BOOST_TEST(test_attr("xy", as_symbol
[*char_
], ut
) &&
95 ut
.which() == utree_type::symbol_type
&& check(ut
, "xy"));
98 BOOST_TEST(test_attr("x,y", as_symbol
[char_
>> ',' >> char_
], ut
) &&
99 ut
.which() == utree_type::symbol_type
&& check(ut
, "xy"));
101 BOOST_TEST(test_attr("a,b1.2", as_symbol
[~digit
% ','] >> double_
, ut
) &&
102 ut
.which() == utree_type::list_type
&& check(ut
, "( ab 1.2 )"));
108 // -(+int_) is forcing a subtree
110 BOOST_TEST(test_attr("1 2", int_
>> ' ' >> -(+int_
), ut
) &&
111 ut
.which() == utree_type::list_type
&& check(ut
, "( 1 2 )"));
114 BOOST_TEST(test_attr("1 2", int_
>> ' ' >> *int_
, ut
) &&
115 ut
.which() == utree_type::list_type
&& check(ut
, "( 1 2 )"));
118 rule
<char const*, std::vector
<int>()> r1
= int_
% ',';
119 BOOST_TEST(test_attr("1 2,3", int_
>> ' ' >> r1
, ut
) &&
120 ut
.which() == utree_type::list_type
&& check(ut
, "( 1 2 3 )"));
123 BOOST_TEST(test_attr("1,2 2,3", r1
>> ' ' >> r1
, ut
) &&
124 ut
.which() == utree_type::list_type
&& check(ut
, "( 1 2 2 3 )"));
127 rule
<char const*, utree()> r2
= int_
% ',';
128 BOOST_TEST(test_attr("1 2,3", int_
>> ' ' >> r2
, ut
) &&
129 ut
.which() == utree_type::list_type
&& check(ut
, "( 1 2 3 )"));
132 BOOST_TEST(test_attr("1,2 2,3", r2
>> ' ' >> r2
, ut
) &&
133 ut
.which() == utree_type::list_type
&& check(ut
, "( 1 2 2 3 )"));
136 rule
<char const*, utree::list_type()> r3
= int_
% ',';
137 BOOST_TEST(test_attr("1 2,3", int_
>> ' ' >> r3
, ut
) &&
138 ut
.which() == utree_type::list_type
&& check(ut
, "( 1 ( 2 3 ) )"));
141 BOOST_TEST(test_attr("1,2 2,3", r3
>> ' ' >> r3
, ut
) &&
142 ut
.which() == utree_type::list_type
&& check(ut
, "( ( 1 2 ) ( 2 3 ) )"));
145 rule
<char const*, utree()> r4
= int_
;
146 BOOST_TEST(test_attr("1 1", int_
>> ' ' >> -r4
, ut
) &&
147 ut
.which() == utree_type::list_type
&& check(ut
, "( 1 1 )"));
150 BOOST_TEST(test_attr("1 ", int_
>> ' ' >> -r4
, ut
) &&
151 ut
.which() == utree_type::list_type
&& check(ut
, "( 1 )"));
154 rule
<char const*, utree::list_type()> r5
= -r4
;
155 BOOST_TEST(test_attr("1", r5
, ut
) &&
156 ut
.which() == utree_type::list_type
&& check(ut
, "( 1 )"));
159 BOOST_TEST(test_attr("", r5
, ut
) &&
160 ut
.which() == utree_type::list_type
&& check(ut
, "( )"));
163 BOOST_TEST(test_attr("1 1", r5
>> ' ' >> r5
, ut
) &&
164 ut
.which() == utree_type::list_type
&& check(ut
, "( ( 1 ) ( 1 ) )"));
167 rule
<char const*, utree::list_type()> r6
= int_
;
168 rule
<char const*, utree()> r7
= -r6
;
169 BOOST_TEST(test_attr("1", r7
, ut
) &&
170 ut
.which() == utree_type::list_type
&& check(ut
, "( 1 )"));
173 rule
<char const*, utree::list_type()> r8
= r6
>> ' ' >> r6
;
174 BOOST_TEST(test_attr("1 1", r8
, ut
) &&
175 ut
.which() == utree_type::list_type
&& check(ut
, "( ( 1 ) ( 1 ) )"));
179 return boost::report_errors();