]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/test/lex/semantic_actions.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / spirit / test / lex / semantic_actions.cpp
1 // Copyright (c) 2001-2011 Hartmut Kaiser
2 //
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5
6 #include <boost/detail/lightweight_test.hpp>
7 #include <boost/config/warning_disable.hpp>
8
9 #include <boost/spirit/include/lex_lexertl.hpp>
10
11 namespace lex = boost::spirit::lex;
12
13 typedef lex::lexertl::token<std::string::iterator> token_type;
14 typedef lex::lexertl::actor_lexer<token_type> lexer_type;
15
16 ///////////////////////////////////////////////////////////////////////////////
17 static bool found_identifier_flag = false;
18
19 ///////////////////////////////////////////////////////////////////////////////
20 void found_identifier_sa0()
21 {
22 found_identifier_flag = true;
23 }
24
25 template <typename Lexer>
26 struct lexer_sa0 : lex::lexer<Lexer>
27 {
28 lexer_sa0()
29 {
30 identifier = "[a-zA-Z][_a-zA-Z0-9]*";
31 this->self += identifier [&found_identifier_sa0];
32 }
33 lex::token_def<> identifier;
34 };
35
36 ///////////////////////////////////////////////////////////////////////////////
37 static std::string found_identifier_str;
38
39 void found_identifier_sa2(std::string::iterator& start
40 , std::string::iterator& end)
41 {
42 found_identifier_flag = true;
43 found_identifier_str = std::string(start, end);
44 }
45
46 template <typename Lexer>
47 struct lexer_sa2 : lex::lexer<Lexer>
48 {
49 lexer_sa2()
50 {
51 identifier = "[a-zA-Z][_a-zA-Z0-9]*";
52 this->self += identifier [&found_identifier_sa2];
53 }
54 lex::token_def<> identifier;
55 };
56
57 ///////////////////////////////////////////////////////////////////////////////
58 void found_identifier_sa3_normal(std::string::iterator& start
59 , std::string::iterator& end, BOOST_SCOPED_ENUM(lex::pass_flags)& pass)
60 {
61 BOOST_TEST(pass == lex::pass_flags::pass_normal);
62
63 found_identifier_flag = true;
64 found_identifier_str = std::string(start, end);
65 }
66
67 template <typename Lexer>
68 struct lexer_sa3_normal : lex::lexer<Lexer>
69 {
70 lexer_sa3_normal()
71 {
72 identifier = "[a-zA-Z][_a-zA-Z0-9]*";
73 this->self += identifier [&found_identifier_sa3_normal];
74 }
75 lex::token_def<> identifier;
76 };
77
78 void found_identifier_sa3_fail(std::string::iterator&, std::string::iterator&
79 , BOOST_SCOPED_ENUM(lex::pass_flags)& pass)
80 {
81 pass = lex::pass_flags::pass_fail;
82 }
83
84 template <typename Lexer>
85 struct lexer_sa3_fail : lex::lexer<Lexer>
86 {
87 lexer_sa3_fail()
88 {
89 identifier = "[a-zA-Z][_a-zA-Z0-9]*";
90 this->self += identifier [&found_identifier_sa3_fail];
91 }
92 lex::token_def<> identifier;
93 };
94
95 void found_identifier_sa3_ignore(std::string::iterator&, std::string::iterator&
96 , BOOST_SCOPED_ENUM(lex::pass_flags)& pass)
97 {
98 pass = lex::pass_flags::pass_ignore;
99 }
100
101 template <typename Lexer>
102 struct lexer_sa3_ignore : lex::lexer<Lexer>
103 {
104 lexer_sa3_ignore()
105 {
106 identifier = "[a-zA-Z][_a-zA-Z0-9]*";
107 this->self += identifier [&found_identifier_sa3_ignore];
108 }
109 lex::token_def<> identifier;
110 };
111
112 ///////////////////////////////////////////////////////////////////////////////
113 static std::size_t found_identifier_id = 0;
114
115 void found_identifier_sa4(std::string::iterator& start
116 , std::string::iterator& end, BOOST_SCOPED_ENUM(lex::pass_flags)& pass
117 , std::size_t id)
118 {
119 BOOST_TEST(pass == lex::pass_flags::pass_normal);
120
121 found_identifier_flag = true;
122 found_identifier_str = std::string(start, end);
123 found_identifier_id = id;
124 }
125
126 template <typename Lexer>
127 struct lexer_sa4 : lex::lexer<Lexer>
128 {
129 lexer_sa4()
130 {
131 identifier = "[a-zA-Z][_a-zA-Z0-9]*";
132 this->self += identifier [&found_identifier_sa4];
133 }
134 lex::token_def<> identifier;
135 };
136
137 void found_identifier_sa4_id(std::string::iterator& start
138 , std::string::iterator& end, BOOST_SCOPED_ENUM(lex::pass_flags)& pass
139 , std::size_t& id)
140 {
141 BOOST_TEST(pass == lex::pass_flags::pass_normal);
142
143 found_identifier_flag = true;
144 found_identifier_str = std::string(start, end);
145 found_identifier_id = id;
146 id = 1;
147 }
148
149 template <typename Lexer>
150 struct lexer_sa4_id : lex::lexer<Lexer>
151 {
152 lexer_sa4_id()
153 {
154 identifier = "[a-zA-Z][_a-zA-Z0-9]*";
155 this->self += identifier [&found_identifier_sa4_id];
156 }
157 lex::token_def<> identifier;
158 };
159
160 static std::size_t found_identifier_id2 = 0;
161
162 bool identifier_token(token_type const& t)
163 {
164 found_identifier_id2 = t.id();
165 return true;
166 }
167
168 ///////////////////////////////////////////////////////////////////////////////
169 struct found_identifier_sa5
170 {
171 template <typename Context>
172 void operator()(std::string::iterator& /*start*/
173 , std::string::iterator& /*end*/, BOOST_SCOPED_ENUM(lex::pass_flags)& pass
174 , std::size_t& /*id*/, Context& ctx)
175 {
176 BOOST_TEST(pass == lex::pass_flags::pass_normal);
177
178 found_identifier_flag = true;
179 found_identifier_str = std::string(ctx.get_value().begin(), ctx.get_value().end());
180 }
181 };
182
183 template <typename Lexer>
184 struct lexer_sa5 : lex::lexer<Lexer>
185 {
186 lexer_sa5()
187 {
188 identifier = "[a-zA-Z][_a-zA-Z0-9]*";
189 this->self += identifier [found_identifier_sa5()];
190 }
191 lex::token_def<> identifier;
192 };
193
194 ///////////////////////////////////////////////////////////////////////////////
195 int main()
196 {
197 std::string identifier ("id_1234");
198 std::string::iterator first = identifier.begin();
199 std::string::iterator last = identifier.end();
200
201 // test semantic action taking no arguments
202 found_identifier_flag = false;
203 {
204 lexer_sa0<lexer_type> sa0;
205 BOOST_TEST(lex::tokenize(first, last, sa0));
206 BOOST_TEST(first == last);
207 BOOST_TEST(found_identifier_flag);
208 }
209
210 // test semantic action taking two arguments (iterator pair for matched
211 // sequence)
212 found_identifier_flag = false;
213 found_identifier_str.clear();
214 first = identifier.begin();
215 {
216 lexer_sa2<lexer_type> sa2;
217 BOOST_TEST(lex::tokenize(first, last, sa2));
218 BOOST_TEST(first == last);
219 BOOST_TEST(found_identifier_flag);
220 BOOST_TEST(found_identifier_str == identifier);
221 }
222
223 // test semantic action taking three arguments (iterator pair for matched
224 // sequence and pass_flags) - pass_flags::pass_normal
225 found_identifier_flag = false;
226 found_identifier_str.clear();
227 first = identifier.begin();
228 {
229 lexer_sa3_normal<lexer_type> sa3;
230 BOOST_TEST(lex::tokenize(first, last, sa3));
231 BOOST_TEST(first == last);
232 BOOST_TEST(found_identifier_flag);
233 BOOST_TEST(found_identifier_str == identifier);
234 }
235
236 // test semantic action taking three arguments (iterator pair for matched
237 // sequence and pass_flags) - pass_flags::pass_fail
238 first = identifier.begin();
239 {
240 lexer_sa3_fail<lexer_type> sa3;
241 BOOST_TEST(!lex::tokenize(first, last, sa3));
242 BOOST_TEST(first != last);
243 }
244
245 // test semantic action taking three arguments (iterator pair for matched
246 // sequence and pass_flags) - pass_flags::pass_ignore
247 first = identifier.begin();
248 {
249 lexer_sa3_ignore<lexer_type> sa3;
250 BOOST_TEST(lex::tokenize(first, last, sa3));
251 BOOST_TEST(first == last);
252 }
253
254 // test semantic action taking four arguments (iterator pair for matched
255 // sequence and pass_flags, and token id)
256 found_identifier_flag = false;
257 found_identifier_str.clear();
258 first = identifier.begin();
259 found_identifier_id = 0;
260 {
261 lexer_sa4<lexer_type> sa4;
262 BOOST_TEST(lex::tokenize(first, last, sa4));
263 BOOST_TEST(first == last);
264 BOOST_TEST(found_identifier_flag);
265 BOOST_TEST(found_identifier_str == identifier);
266 BOOST_TEST(found_identifier_id == lex::min_token_id);
267 }
268
269 found_identifier_flag = false;
270 found_identifier_str.clear();
271 first = identifier.begin();
272 found_identifier_id = 0;
273 found_identifier_id2 = 0;
274 {
275 lexer_sa4_id<lexer_type> sa4;
276 BOOST_TEST(lex::tokenize(first, last, sa4, identifier_token));
277 BOOST_TEST(first == last);
278 BOOST_TEST(found_identifier_flag);
279 BOOST_TEST(found_identifier_str == identifier);
280 BOOST_TEST(found_identifier_id == lex::min_token_id);
281 BOOST_TEST(found_identifier_id2 == 1);
282 }
283
284 // test semantic action taking four arguments (iterator pair for matched
285 // sequence and pass_flags, token id, and context)
286 found_identifier_flag = false;
287 found_identifier_str.clear();
288 first = identifier.begin();
289 found_identifier_id = 0;
290 found_identifier_id2 = 0;
291 {
292 lexer_sa5<lexer_type> sa5;
293 BOOST_TEST(lex::tokenize(first, last, sa5));
294 BOOST_TEST(first == last);
295 BOOST_TEST(found_identifier_flag);
296 BOOST_TEST(found_identifier_str == identifier);
297 }
298
299 return boost::report_errors();
300 }