]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/test/x3/rule4.cpp
1 /*=============================================================================
2 Copyright (c) 2001-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 =============================================================================*/
8 // this file deliberately contains non-ascii characters
9 // boostinspect:noascii
11 #include <boost/detail/lightweight_test.hpp>
12 #include <boost/spirit/home/x3.hpp>
13 #include <boost/fusion/include/vector.hpp>
14 #include <boost/fusion/include/at.hpp>
21 namespace x3
= boost::spirit::x3
;
27 template <typename Iterator
, typename Exception
, typename Context
>
28 x3::error_handler_result
29 on_error(Iterator
&, Iterator
const& last
, Exception
const& x
, Context
const& context
)
32 << "Error! Expecting: "
35 << std::string(x
.where(), last
)
39 return x3::error_handler_result::fail
;
42 template <typename Iterator
, typename Attribute
, typename Context
>
44 on_success(Iterator
const&, Iterator
const&, Attribute
&, Context
const&)
53 using spirit_test::test_attr
;
54 using spirit_test::test
;
56 using namespace boost::spirit::x3::ascii
;
57 using boost::spirit::x3::rule
;
58 using boost::spirit::x3::int_
;
59 using boost::spirit::x3::lit
;
61 { // show that ra = rb and ra %= rb works as expected
62 rule
<class a
, int> ra
;
63 rule
<class b
, int> rb
;
66 auto ra_def
= (ra
%= int_
);
67 BOOST_TEST(test_attr("123", ra_def
, attr
));
68 BOOST_TEST(attr
== 123);
70 auto rb_def
= (rb
%= ra_def
);
71 BOOST_TEST(test_attr("123", rb_def
, attr
));
72 BOOST_TEST(attr
== 123);
74 auto rb_def2
= (rb
= ra_def
);
75 BOOST_TEST(test_attr("123", rb_def2
, attr
));
76 BOOST_TEST(attr
== 123);
79 { // show that ra %= rb works as expected with semantic actions
80 rule
<class a
, int> ra
;
81 rule
<class b
, int> rb
;
84 auto f
= [](auto c
){};
85 auto ra_def
= (ra
%= int_
[f
]);
86 BOOST_TEST(test_attr("123", ra_def
, attr
));
87 BOOST_TEST(attr
== 123);
89 auto ra_def2
= (rb
= (ra
%= int_
[f
]));
90 BOOST_TEST(test_attr("123", ra_def2
, attr
));
91 BOOST_TEST(attr
== 123);
95 { // std::string as container attribute with auto rules
99 // test deduced auto rule behavior
101 auto text
= rule
<class text
, std::string
>()
102 = +(!char_(')') >> !char_('>') >> char_
);
105 BOOST_TEST(test_attr("x", text
, attr
));
106 BOOST_TEST(attr
== "x");
111 auto r
= rule
<my_rule_class
, char const*>()
112 = '(' > int_
> ',' > int_
> ')';
114 BOOST_TEST(test("(123,456)", r
));
115 BOOST_TEST(!test("(abc,def)", r
));
116 BOOST_TEST(!test("(123,456]", r
));
117 BOOST_TEST(!test("(123;456)", r
));
118 BOOST_TEST(!test("[123,456]", r
));
120 BOOST_TEST(got_it
== 1);
124 typedef boost::variant
<double, int> v_type
;
125 auto r1
= rule
<class r1
, v_type
>()
128 BOOST_TEST(test_attr("1", r1
, v
) && v
.which() == 1 &&
129 boost::get
<int>(v
) == 1);
131 typedef boost::optional
<int> ov_type
;
132 auto r2
= rule
<class r2
, ov_type
>()
135 BOOST_TEST(test_attr("1", r2
, ov
) && ov
&& boost::get
<int>(ov
) == 1);
138 // test handling of single element fusion sequences
140 using boost::fusion::vector
;
141 using boost::fusion::at_c
;
142 auto r
= rule
<class r
, vector
<int>>()
146 BOOST_TEST(test_attr("1", r
, v
) && at_c
<0>(v
) == 1);
149 { // attribute compatibility test
150 using boost::spirit::x3::rule
;
151 using boost::spirit::x3::int_
;
153 auto const expr
= int_
;
156 BOOST_TEST(test_attr("1", expr
, i
) && i
== 1); // ok
158 const rule
< class int_rule
, int > int_rule( "int_rule" );
159 auto const int_rule_def
= int_
;
160 auto const start
= int_rule
= int_rule_def
;
163 BOOST_TEST(test_attr("1", start
, j
) && j
== 1); // error
166 return boost::report_errors();