]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/test/x3/rule3.cpp
1 /*=============================================================================
2 Copyright (c) 2001-2012 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 #include <boost/detail/lightweight_test.hpp>
9 #include <boost/spirit/home/x3.hpp>
10 #include <boost/fusion/include/adapt_struct.hpp>
11 #include <boost/fusion/include/std_pair.hpp>
13 #include <boost/variant.hpp>
20 using boost::spirit::x3::_val
;
21 namespace x3
= boost::spirit::x3
;
25 template <typename Context
>
26 void operator()(Context
const& ctx
) const
28 _val(ctx
) += _attr(ctx
);
33 struct stationary
: boost::noncopyable
35 explicit stationary(int i
) : val
{i
} {}
36 stationary
& operator=(int i
) { val
= i
; return *this; }
42 namespace check_stationary
{
44 boost::spirit::x3::rule
<class a_r
, stationary
> const a
;
45 boost::spirit::x3::rule
<class b_r
, stationary
> const b
;
47 auto const a_def
= '{' >> boost::spirit::x3::int_
>> '}';
50 BOOST_SPIRIT_DEFINE(a
, b
)
54 namespace check_recursive
{
56 using node_t
= boost::make_recursive_variant
<
58 std::vector
<boost::recursive_variant_
>
61 boost::spirit::x3::rule
<class grammar_r
, node_t
> const grammar
;
63 auto const grammar_def
= '[' >> grammar
% ',' >> ']' | boost::spirit::x3::int_
;
65 BOOST_SPIRIT_DEFINE(grammar
)
69 struct recursive_tuple
72 std::vector
<recursive_tuple
> children
;
74 BOOST_FUSION_ADAPT_STRUCT(recursive_tuple
,
77 // regression test for #461
78 namespace check_recursive_tuple
{
80 x3::rule
<class grammar_r
, recursive_tuple
> const grammar
;
81 auto const grammar_def
= x3::int_
>> ('{' >> grammar
% ',' >> '}' | x3::eps
);
82 BOOST_SPIRIT_DEFINE(grammar
)
84 BOOST_SPIRIT_INSTANTIATE(decltype(grammar
), char const*, x3::unused_type
)
91 using spirit_test::test_attr
;
92 using spirit_test::test
;
94 using namespace boost::spirit::x3::ascii
;
95 using boost::spirit::x3::rule
;
96 using boost::spirit::x3::lit
;
97 using boost::spirit::x3::eps
;
98 using boost::spirit::x3::unused_type
;
101 { // synth attribute value-init
104 typedef rule
<class r
, std::string
> rule_type
;
106 auto rdef
= rule_type()
110 BOOST_TEST(test_attr("abcdef", +rdef
, s
));
111 BOOST_TEST(s
== "abcdef");
114 { // synth attribute value-init
117 typedef rule
<class r
, std::string
> rule_type
;
119 auto rdef
= rule_type() =
123 _val(ctx
) += _attr(ctx
);
127 BOOST_TEST(test_attr("abcdef", +rdef
, s
));
128 BOOST_TEST(s
== "abcdef");
132 auto r
= rule
<class r
, int>{} = eps
[([] (auto& ctx
) {
133 using boost::spirit::x3::_val
;
134 static_assert(std::is_same
<std::decay_t
<decltype(_val(ctx
))>, unused_type
>::value
,
135 "Attribute must not be synthesized");
137 BOOST_TEST(test("", r
));
140 { // ensure no unneeded synthesization, copying and moving occurred
142 BOOST_TEST(test_attr("{42}", check_stationary::b
, st
));
143 BOOST_TEST_EQ(st
.val
, 42);
147 using namespace check_recursive
;
149 BOOST_TEST(test_attr("[4,2]", grammar
, v
));
150 BOOST_TEST((node_t
{std::vector
<node_t
>{{4}, {2}}} == v
));
153 return boost::report_errors();