1 // Copyright (c) 2001-2011 Hartmut Kaiser
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)
6 #include <boost/detail/lightweight_test.hpp>
7 #include <boost/config/warning_disable.hpp>
9 #include <boost/spirit/include/lex_lexertl.hpp>
11 namespace lex
= boost::spirit::lex
;
13 typedef lex::lexertl::token
<std::string::iterator
> token_type
;
14 typedef lex::lexertl::actor_lexer
<token_type
> lexer_type
;
16 ///////////////////////////////////////////////////////////////////////////////
17 static bool found_identifier_flag
= false;
19 ///////////////////////////////////////////////////////////////////////////////
20 void found_identifier_sa0()
22 found_identifier_flag
= true;
25 template <typename Lexer
>
26 struct lexer_sa0
: lex::lexer
<Lexer
>
30 identifier
= "[a-zA-Z][_a-zA-Z0-9]*";
31 this->self
+= identifier
[&found_identifier_sa0
];
33 lex::token_def
<> identifier
;
36 ///////////////////////////////////////////////////////////////////////////////
37 static std::string found_identifier_str
;
39 void found_identifier_sa2(std::string::iterator
& start
40 , std::string::iterator
& end
)
42 found_identifier_flag
= true;
43 found_identifier_str
= std::string(start
, end
);
46 template <typename Lexer
>
47 struct lexer_sa2
: lex::lexer
<Lexer
>
51 identifier
= "[a-zA-Z][_a-zA-Z0-9]*";
52 this->self
+= identifier
[&found_identifier_sa2
];
54 lex::token_def
<> identifier
;
57 ///////////////////////////////////////////////////////////////////////////////
58 void found_identifier_sa3_normal(std::string::iterator
& start
59 , std::string::iterator
& end
, BOOST_SCOPED_ENUM(lex::pass_flags
)& pass
)
61 BOOST_TEST(pass
== lex::pass_flags::pass_normal
);
63 found_identifier_flag
= true;
64 found_identifier_str
= std::string(start
, end
);
67 template <typename Lexer
>
68 struct lexer_sa3_normal
: lex::lexer
<Lexer
>
72 identifier
= "[a-zA-Z][_a-zA-Z0-9]*";
73 this->self
+= identifier
[&found_identifier_sa3_normal
];
75 lex::token_def
<> identifier
;
78 void found_identifier_sa3_fail(std::string::iterator
&, std::string::iterator
&
79 , BOOST_SCOPED_ENUM(lex::pass_flags
)& pass
)
81 pass
= lex::pass_flags::pass_fail
;
84 template <typename Lexer
>
85 struct lexer_sa3_fail
: lex::lexer
<Lexer
>
89 identifier
= "[a-zA-Z][_a-zA-Z0-9]*";
90 this->self
+= identifier
[&found_identifier_sa3_fail
];
92 lex::token_def
<> identifier
;
95 void found_identifier_sa3_ignore(std::string::iterator
&, std::string::iterator
&
96 , BOOST_SCOPED_ENUM(lex::pass_flags
)& pass
)
98 pass
= lex::pass_flags::pass_ignore
;
101 template <typename Lexer
>
102 struct lexer_sa3_ignore
: lex::lexer
<Lexer
>
106 identifier
= "[a-zA-Z][_a-zA-Z0-9]*";
107 this->self
+= identifier
[&found_identifier_sa3_ignore
];
109 lex::token_def
<> identifier
;
112 ///////////////////////////////////////////////////////////////////////////////
113 static std::size_t found_identifier_id
= 0;
115 void found_identifier_sa4(std::string::iterator
& start
116 , std::string::iterator
& end
, BOOST_SCOPED_ENUM(lex::pass_flags
)& pass
119 BOOST_TEST(pass
== lex::pass_flags::pass_normal
);
121 found_identifier_flag
= true;
122 found_identifier_str
= std::string(start
, end
);
123 found_identifier_id
= id
;
126 template <typename Lexer
>
127 struct lexer_sa4
: lex::lexer
<Lexer
>
131 identifier
= "[a-zA-Z][_a-zA-Z0-9]*";
132 this->self
+= identifier
[&found_identifier_sa4
];
134 lex::token_def
<> identifier
;
137 void found_identifier_sa4_id(std::string::iterator
& start
138 , std::string::iterator
& end
, BOOST_SCOPED_ENUM(lex::pass_flags
)& pass
141 BOOST_TEST(pass
== lex::pass_flags::pass_normal
);
143 found_identifier_flag
= true;
144 found_identifier_str
= std::string(start
, end
);
145 found_identifier_id
= id
;
149 template <typename Lexer
>
150 struct lexer_sa4_id
: lex::lexer
<Lexer
>
154 identifier
= "[a-zA-Z][_a-zA-Z0-9]*";
155 this->self
+= identifier
[&found_identifier_sa4_id
];
157 lex::token_def
<> identifier
;
160 static std::size_t found_identifier_id2
= 0;
162 bool identifier_token(token_type
const& t
)
164 found_identifier_id2
= t
.id();
168 ///////////////////////////////////////////////////////////////////////////////
169 struct found_identifier_sa5
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
)
176 BOOST_TEST(pass
== lex::pass_flags::pass_normal
);
178 found_identifier_flag
= true;
179 found_identifier_str
= std::string(ctx
.get_value().begin(), ctx
.get_value().end());
183 template <typename Lexer
>
184 struct lexer_sa5
: lex::lexer
<Lexer
>
188 identifier
= "[a-zA-Z][_a-zA-Z0-9]*";
189 this->self
+= identifier
[found_identifier_sa5()];
191 lex::token_def
<> identifier
;
194 ///////////////////////////////////////////////////////////////////////////////
197 std::string
identifier ("id_1234");
198 std::string::iterator first
= identifier
.begin();
199 std::string::iterator last
= identifier
.end();
201 // test semantic action taking no arguments
202 found_identifier_flag
= false;
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
);
210 // test semantic action taking two arguments (iterator pair for matched
212 found_identifier_flag
= false;
213 found_identifier_str
.clear();
214 first
= identifier
.begin();
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
);
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();
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
);
236 // test semantic action taking three arguments (iterator pair for matched
237 // sequence and pass_flags) - pass_flags::pass_fail
238 first
= identifier
.begin();
240 lexer_sa3_fail
<lexer_type
> sa3
;
241 BOOST_TEST(!lex::tokenize(first
, last
, sa3
));
242 BOOST_TEST(first
!= last
);
245 // test semantic action taking three arguments (iterator pair for matched
246 // sequence and pass_flags) - pass_flags::pass_ignore
247 first
= identifier
.begin();
249 lexer_sa3_ignore
<lexer_type
> sa3
;
250 BOOST_TEST(lex::tokenize(first
, last
, sa3
));
251 BOOST_TEST(first
== last
);
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;
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
);
269 found_identifier_flag
= false;
270 found_identifier_str
.clear();
271 first
= identifier
.begin();
272 found_identifier_id
= 0;
273 found_identifier_id2
= 0;
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);
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;
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
);
299 return boost::report_errors();