]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/spirit/test/x3/rule3.cpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / libs / spirit / test / x3 / rule3.cpp
CommitLineData
7c673cae
FG
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
7c673cae 8#include <boost/spirit/home/x3.hpp>
20effc67 9#include <boost/fusion/include/adapt_struct.hpp>
7c673cae
FG
10#include <boost/fusion/include/std_pair.hpp>
11
f67539c2 12#include <boost/variant.hpp>
7c673cae 13#include <string>
20effc67 14#include <vector>
7c673cae
FG
15#include <cstring>
16#include <iostream>
17#include "test.hpp"
18
1e59de90
TL
19#ifdef _MSC_VER
20// bogus https://developercommunity.visualstudio.com/t/buggy-warning-c4709/471956
21# pragma warning(disable: 4709) // comma operator within array index expression
22#endif
23
7c673cae 24using boost::spirit::x3::_val;
20effc67 25namespace x3 = boost::spirit::x3;
7c673cae
FG
26
27struct f
28{
29 template <typename Context>
30 void operator()(Context const& ctx) const
31 {
32 _val(ctx) += _attr(ctx);
33 }
34};
35
92f5a8d4
TL
36
37struct stationary : boost::noncopyable
38{
39 explicit stationary(int i) : val{i} {}
40 stationary& operator=(int i) { val = i; return *this; }
41
42 int val;
43};
44
45
46namespace check_stationary {
47
48boost::spirit::x3::rule<class a_r, stationary> const a;
49boost::spirit::x3::rule<class b_r, stationary> const b;
50
51auto const a_def = '{' >> boost::spirit::x3::int_ >> '}';
52auto const b_def = a;
53
54BOOST_SPIRIT_DEFINE(a, b)
55
56}
57
f67539c2
TL
58namespace check_recursive {
59
60using node_t = boost::make_recursive_variant<
61 int,
62 std::vector<boost::recursive_variant_>
63 >::type;
64
65boost::spirit::x3::rule<class grammar_r, node_t> const grammar;
66
67auto const grammar_def = '[' >> grammar % ',' >> ']' | boost::spirit::x3::int_;
68
69BOOST_SPIRIT_DEFINE(grammar)
70
71}
72
1e59de90
TL
73namespace check_recursive_scoped {
74
75using check_recursive::node_t;
76
77x3::rule<class intvec_r, node_t> const intvec;
78auto const grammar = intvec = '[' >> intvec % ',' >> ']' | x3::int_;
79
80}
81
20effc67
TL
82struct recursive_tuple
83{
84 int value;
85 std::vector<recursive_tuple> children;
86};
87BOOST_FUSION_ADAPT_STRUCT(recursive_tuple,
88 value, children)
89
90// regression test for #461
91namespace check_recursive_tuple {
92
93x3::rule<class grammar_r, recursive_tuple> const grammar;
94auto const grammar_def = x3::int_ >> ('{' >> grammar % ',' >> '}' | x3::eps);
95BOOST_SPIRIT_DEFINE(grammar)
96
97BOOST_SPIRIT_INSTANTIATE(decltype(grammar), char const*, x3::unused_type)
98
99}
100
92f5a8d4 101
7c673cae
FG
102int main()
103{
104 using spirit_test::test_attr;
105 using spirit_test::test;
106
107 using namespace boost::spirit::x3::ascii;
108 using boost::spirit::x3::rule;
7c673cae 109 using boost::spirit::x3::lit;
92f5a8d4
TL
110 using boost::spirit::x3::eps;
111 using boost::spirit::x3::unused_type;
7c673cae
FG
112
113
114 { // synth attribute value-init
115
116 std::string s;
117 typedef rule<class r, std::string> rule_type;
118
119 auto rdef = rule_type()
120 = alpha [f()]
121 ;
122
123 BOOST_TEST(test_attr("abcdef", +rdef, s));
124 BOOST_TEST(s == "abcdef");
125 }
126
127 { // synth attribute value-init
128
129 std::string s;
130 typedef rule<class r, std::string> rule_type;
131
132 auto rdef = rule_type() =
133 alpha /
134 [](auto& ctx)
135 {
136 _val(ctx) += _attr(ctx);
137 }
138 ;
139
140 BOOST_TEST(test_attr("abcdef", +rdef, s));
141 BOOST_TEST(s == "abcdef");
142 }
143
92f5a8d4 144 {
1e59de90 145 auto r = rule<class r_id, int>{} = eps[([] (auto& ctx) {
92f5a8d4
TL
146 using boost::spirit::x3::_val;
147 static_assert(std::is_same<std::decay_t<decltype(_val(ctx))>, unused_type>::value,
148 "Attribute must not be synthesized");
149 })];
150 BOOST_TEST(test("", r));
151 }
152
f67539c2 153 { // ensure no unneeded synthesization, copying and moving occurred
92f5a8d4
TL
154 stationary st { 0 };
155 BOOST_TEST(test_attr("{42}", check_stationary::b, st));
156 BOOST_TEST_EQ(st.val, 42);
157 }
158
f67539c2
TL
159 {
160 using namespace check_recursive;
161 node_t v;
162 BOOST_TEST(test_attr("[4,2]", grammar, v));
163 BOOST_TEST((node_t{std::vector<node_t>{{4}, {2}}} == v));
164 }
1e59de90
TL
165 {
166 using namespace check_recursive_scoped;
167 node_t v;
168 BOOST_TEST(test_attr("[4,2]", grammar, v));
169 BOOST_TEST((node_t{std::vector<node_t>{{4}, {2}}} == v));
170 }
f67539c2 171
7c673cae
FG
172 return boost::report_errors();
173}