]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/test/x3/rule3.cpp
420146c0458ec1ee0a6acae781fd0f9bd9dc804a
[ceph.git] / ceph / src / boost / libs / spirit / test / x3 / rule3.cpp
1 /*=============================================================================
2 Copyright (c) 2001-2012 Joel de Guzman
3
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
8 #include <boost/detail/lightweight_test.hpp>
9 #include <boost/spirit/home/x3.hpp>
10 #include <boost/fusion/include/std_pair.hpp>
11
12 #include <string>
13 #include <cstring>
14 #include <iostream>
15 #include "test.hpp"
16
17 using boost::spirit::x3::_val;
18
19 struct f
20 {
21 template <typename Context>
22 void operator()(Context const& ctx) const
23 {
24 _val(ctx) += _attr(ctx);
25 }
26 };
27
28
29 struct stationary : boost::noncopyable
30 {
31 explicit stationary(int i) : val{i} {}
32 stationary& operator=(int i) { val = i; return *this; }
33
34 int val;
35 };
36
37
38 namespace check_stationary {
39
40 boost::spirit::x3::rule<class a_r, stationary> const a;
41 boost::spirit::x3::rule<class b_r, stationary> const b;
42
43 auto const a_def = '{' >> boost::spirit::x3::int_ >> '}';
44 auto const b_def = a;
45
46 BOOST_SPIRIT_DEFINE(a, b)
47
48 }
49
50
51 int main()
52 {
53 using spirit_test::test_attr;
54 using spirit_test::test;
55
56 using namespace boost::spirit::x3::ascii;
57 using boost::spirit::x3::rule;
58 using boost::spirit::x3::lit;
59 using boost::spirit::x3::eps;
60 using boost::spirit::x3::unused_type;
61
62
63 { // synth attribute value-init
64
65 std::string s;
66 typedef rule<class r, std::string> rule_type;
67
68 auto rdef = rule_type()
69 = alpha [f()]
70 ;
71
72 BOOST_TEST(test_attr("abcdef", +rdef, s));
73 BOOST_TEST(s == "abcdef");
74 }
75
76 { // synth attribute value-init
77
78 std::string s;
79 typedef rule<class r, std::string> rule_type;
80
81 auto rdef = rule_type() =
82 alpha /
83 [](auto& ctx)
84 {
85 _val(ctx) += _attr(ctx);
86 }
87 ;
88
89 BOOST_TEST(test_attr("abcdef", +rdef, s));
90 BOOST_TEST(s == "abcdef");
91 }
92
93 {
94 auto r = rule<class r, int>{} = eps[([] (auto& ctx) {
95 using boost::spirit::x3::_val;
96 static_assert(std::is_same<std::decay_t<decltype(_val(ctx))>, unused_type>::value,
97 "Attribute must not be synthesized");
98 })];
99 BOOST_TEST(test("", r));
100 }
101
102 { // ensure no unneded synthesization, copying and moving occured
103 stationary st { 0 };
104 BOOST_TEST(test_attr("{42}", check_stationary::b, st));
105 BOOST_TEST_EQ(st.val, 42);
106 }
107
108 return boost::report_errors();
109 }