Chris@16
|
1 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
2 /// \file matches.hpp
|
Chris@16
|
3 /// Contains definition of matches\<\> metafunction for determining if
|
Chris@16
|
4 /// a given expression matches a given pattern.
|
Chris@16
|
5 //
|
Chris@16
|
6 // Copyright 2008 Eric Niebler. Distributed under the Boost
|
Chris@16
|
7 // Software License, Version 1.0. (See accompanying file
|
Chris@16
|
8 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
9
|
Chris@16
|
10 #ifndef BOOST_PROTO_MATCHES_HPP_EAN_11_03_2006
|
Chris@16
|
11 #define BOOST_PROTO_MATCHES_HPP_EAN_11_03_2006
|
Chris@16
|
12
|
Chris@16
|
13 #include <boost/config.hpp>
|
Chris@16
|
14 #include <boost/detail/workaround.hpp>
|
Chris@16
|
15 #include <boost/preprocessor/cat.hpp>
|
Chris@16
|
16 #include <boost/preprocessor/arithmetic/dec.hpp>
|
Chris@16
|
17 #include <boost/preprocessor/arithmetic/sub.hpp>
|
Chris@16
|
18 #include <boost/preprocessor/iteration/iterate.hpp>
|
Chris@16
|
19 #include <boost/preprocessor/facilities/intercept.hpp>
|
Chris@16
|
20 #include <boost/preprocessor/punctuation/comma_if.hpp>
|
Chris@16
|
21 #include <boost/preprocessor/repetition/enum.hpp>
|
Chris@16
|
22 #include <boost/preprocessor/repetition/enum_params.hpp>
|
Chris@16
|
23 #include <boost/preprocessor/repetition/enum_shifted.hpp>
|
Chris@16
|
24 #include <boost/preprocessor/repetition/enum_binary_params.hpp>
|
Chris@16
|
25 #include <boost/preprocessor/repetition/enum_shifted_params.hpp>
|
Chris@16
|
26 #include <boost/preprocessor/repetition/enum_trailing_params.hpp>
|
Chris@16
|
27 #include <boost/preprocessor/repetition/enum_params_with_a_default.hpp>
|
Chris@16
|
28 #include <boost/preprocessor/repetition/repeat.hpp>
|
Chris@16
|
29 #include <boost/config.hpp>
|
Chris@16
|
30 #include <boost/mpl/logical.hpp>
|
Chris@16
|
31 #include <boost/mpl/eval_if.hpp>
|
Chris@16
|
32 #include <boost/proto/detail/template_arity.hpp>
|
Chris@16
|
33 #include <boost/utility/enable_if.hpp>
|
Chris@16
|
34 #if BOOST_WORKAROUND(BOOST_MSVC, == 1310)
|
Chris@16
|
35 #include <boost/type_traits/is_array.hpp>
|
Chris@16
|
36 #endif
|
Chris@16
|
37 #include <boost/type_traits/is_const.hpp>
|
Chris@16
|
38 #include <boost/type_traits/is_convertible.hpp>
|
Chris@16
|
39 #include <boost/type_traits/is_reference.hpp>
|
Chris@16
|
40 #include <boost/type_traits/is_pointer.hpp>
|
Chris@16
|
41 #include <boost/proto/proto_fwd.hpp>
|
Chris@16
|
42 #include <boost/proto/traits.hpp>
|
Chris@16
|
43 #include <boost/proto/transform/when.hpp>
|
Chris@16
|
44 #include <boost/proto/transform/impl.hpp>
|
Chris@16
|
45
|
Chris@101
|
46 #if defined(_MSC_VER)
|
Chris@16
|
47 # pragma warning(push)
|
Chris@16
|
48 # pragma warning(disable:4305) // 'specialization' : truncation from 'const int' to 'bool'
|
Chris@16
|
49 #endif
|
Chris@16
|
50
|
Chris@16
|
51 #define BOOST_PROTO_LOGICAL_typename_G BOOST_PP_ENUM_PARAMS(BOOST_PROTO_MAX_LOGICAL_ARITY, typename G)
|
Chris@16
|
52 #define BOOST_PROTO_LOGICAL_G BOOST_PP_ENUM_PARAMS(BOOST_PROTO_MAX_LOGICAL_ARITY, G)
|
Chris@16
|
53
|
Chris@16
|
54 namespace boost { namespace proto
|
Chris@16
|
55 {
|
Chris@16
|
56
|
Chris@16
|
57 namespace detail
|
Chris@16
|
58 {
|
Chris@16
|
59 template<typename Expr, typename BasicExpr, typename Grammar>
|
Chris@16
|
60 struct matches_;
|
Chris@16
|
61
|
Chris@16
|
62 template<bool B, typename Pred>
|
Chris@16
|
63 struct and_2;
|
Chris@16
|
64
|
Chris@16
|
65 template<typename And, typename Expr, typename State, typename Data>
|
Chris@16
|
66 struct _and_impl;
|
Chris@16
|
67
|
Chris@16
|
68 template<typename T, typename U>
|
Chris@16
|
69 struct array_matches
|
Chris@16
|
70 : mpl::false_
|
Chris@16
|
71 {};
|
Chris@16
|
72
|
Chris@16
|
73 template<typename T, std::size_t M>
|
Chris@16
|
74 struct array_matches<T[M], T *>
|
Chris@16
|
75 : mpl::true_
|
Chris@16
|
76 {};
|
Chris@16
|
77
|
Chris@16
|
78 template<typename T, std::size_t M>
|
Chris@16
|
79 struct array_matches<T[M], T const *>
|
Chris@16
|
80 : mpl::true_
|
Chris@16
|
81 {};
|
Chris@16
|
82
|
Chris@16
|
83 template<typename T, std::size_t M>
|
Chris@16
|
84 struct array_matches<T[M], T[proto::N]>
|
Chris@16
|
85 : mpl::true_
|
Chris@16
|
86 {};
|
Chris@16
|
87
|
Chris@16
|
88 template<typename T, typename U
|
Chris@16
|
89 BOOST_PROTO_TEMPLATE_ARITY_PARAM(long Arity = detail::template_arity<U>::value)
|
Chris@16
|
90 >
|
Chris@16
|
91 struct lambda_matches
|
Chris@16
|
92 : mpl::false_
|
Chris@16
|
93 {};
|
Chris@16
|
94
|
Chris@16
|
95 template<typename T>
|
Chris@16
|
96 struct lambda_matches<T, proto::_ BOOST_PROTO_TEMPLATE_ARITY_PARAM(-1)>
|
Chris@16
|
97 : mpl::true_
|
Chris@16
|
98 {};
|
Chris@16
|
99
|
Chris@16
|
100 template<typename T>
|
Chris@16
|
101 struct lambda_matches<T, T BOOST_PROTO_TEMPLATE_ARITY_PARAM(-1)>
|
Chris@16
|
102 : mpl::true_
|
Chris@16
|
103 {};
|
Chris@16
|
104
|
Chris@16
|
105 template<typename T, std::size_t M, typename U>
|
Chris@16
|
106 struct lambda_matches<T[M], U BOOST_PROTO_TEMPLATE_ARITY_PARAM(-1)>
|
Chris@16
|
107 : array_matches<T[M], U>
|
Chris@16
|
108 {};
|
Chris@16
|
109
|
Chris@16
|
110 template<typename T, std::size_t M>
|
Chris@16
|
111 struct lambda_matches<T[M], _ BOOST_PROTO_TEMPLATE_ARITY_PARAM(-1)>
|
Chris@16
|
112 : mpl::true_
|
Chris@16
|
113 {};
|
Chris@16
|
114
|
Chris@16
|
115 template<typename T, std::size_t M>
|
Chris@16
|
116 struct lambda_matches<T[M], T[M] BOOST_PROTO_TEMPLATE_ARITY_PARAM(-1)>
|
Chris@16
|
117 : mpl::true_
|
Chris@16
|
118 {};
|
Chris@16
|
119
|
Chris@16
|
120 template<template<typename> class T, typename Expr0, typename Grammar0>
|
Chris@16
|
121 struct lambda_matches<T<Expr0>, T<Grammar0> BOOST_PROTO_TEMPLATE_ARITY_PARAM(1) >
|
Chris@16
|
122 : lambda_matches<Expr0, Grammar0>
|
Chris@16
|
123 {};
|
Chris@16
|
124
|
Chris@16
|
125 // vararg_matches_impl
|
Chris@16
|
126 template<typename Args1, typename Back, long From, long To>
|
Chris@16
|
127 struct vararg_matches_impl;
|
Chris@16
|
128
|
Chris@16
|
129 // vararg_matches
|
Chris@16
|
130 template<typename Expr, typename Args1, typename Args2, typename Back, bool Can, bool Zero, typename Void = void>
|
Chris@16
|
131 struct vararg_matches
|
Chris@16
|
132 : mpl::false_
|
Chris@16
|
133 {};
|
Chris@16
|
134
|
Chris@16
|
135 template<typename Expr, typename Args1, typename Args2, typename Back>
|
Chris@16
|
136 struct vararg_matches<Expr, Args1, Args2, Back, true, true, typename Back::proto_is_vararg_>
|
Chris@16
|
137 : matches_<
|
Chris@16
|
138 Expr
|
Chris@16
|
139 , proto::basic_expr<ignore, Args1, Args1::arity>
|
Chris@16
|
140 , proto::basic_expr<ignore, Args2, Args1::arity>
|
Chris@16
|
141 >
|
Chris@16
|
142 {};
|
Chris@16
|
143
|
Chris@16
|
144 template<typename Expr, typename Args1, typename Args2, typename Back>
|
Chris@16
|
145 struct vararg_matches<Expr, Args1, Args2, Back, true, false, typename Back::proto_is_vararg_>
|
Chris@16
|
146 : and_2<
|
Chris@16
|
147 matches_<
|
Chris@16
|
148 Expr
|
Chris@16
|
149 , proto::basic_expr<ignore, Args1, Args2::arity>
|
Chris@16
|
150 , proto::basic_expr<ignore, Args2, Args2::arity>
|
Chris@16
|
151 >::value
|
Chris@16
|
152 , vararg_matches_impl<Args1, typename Back::proto_grammar, Args2::arity + 1, Args1::arity>
|
Chris@16
|
153 >
|
Chris@16
|
154 {};
|
Chris@16
|
155
|
Chris@16
|
156 // How terminal_matches<> handles references and cv-qualifiers.
|
Chris@16
|
157 // The cv and ref matter *only* if the grammar has a top-level ref.
|
Chris@16
|
158 //
|
Chris@16
|
159 // Expr | Grammar | Matches?
|
Chris@16
|
160 // -------------------------------------
|
Chris@16
|
161 // T T yes
|
Chris@16
|
162 // T & T yes
|
Chris@16
|
163 // T const & T yes
|
Chris@16
|
164 // T T & no
|
Chris@16
|
165 // T & T & yes
|
Chris@16
|
166 // T const & T & no
|
Chris@16
|
167 // T T const & no
|
Chris@16
|
168 // T & T const & no
|
Chris@16
|
169 // T const & T const & yes
|
Chris@16
|
170
|
Chris@16
|
171 template<typename T, typename U>
|
Chris@16
|
172 struct is_cv_ref_compatible
|
Chris@16
|
173 : mpl::true_
|
Chris@16
|
174 {};
|
Chris@16
|
175
|
Chris@16
|
176 template<typename T, typename U>
|
Chris@16
|
177 struct is_cv_ref_compatible<T, U &>
|
Chris@16
|
178 : mpl::false_
|
Chris@16
|
179 {};
|
Chris@16
|
180
|
Chris@16
|
181 template<typename T, typename U>
|
Chris@16
|
182 struct is_cv_ref_compatible<T &, U &>
|
Chris@16
|
183 : mpl::bool_<is_const<T>::value == is_const<U>::value>
|
Chris@16
|
184 {};
|
Chris@16
|
185
|
Chris@16
|
186 #if BOOST_WORKAROUND(BOOST_MSVC, == 1310)
|
Chris@16
|
187 // MSVC-7.1 has lots of problems with array types that have been
|
Chris@16
|
188 // deduced. Partially specializing terminal_matches<> on array types
|
Chris@16
|
189 // doesn't seem to work.
|
Chris@16
|
190 template<
|
Chris@16
|
191 typename T
|
Chris@16
|
192 , typename U
|
Chris@16
|
193 , bool B = is_array<BOOST_PROTO_UNCVREF(T)>::value
|
Chris@16
|
194 >
|
Chris@16
|
195 struct terminal_array_matches
|
Chris@16
|
196 : mpl::false_
|
Chris@16
|
197 {};
|
Chris@16
|
198
|
Chris@16
|
199 template<typename T, typename U, std::size_t M>
|
Chris@16
|
200 struct terminal_array_matches<T, U(&)[M], true>
|
Chris@16
|
201 : is_convertible<T, U(&)[M]>
|
Chris@16
|
202 {};
|
Chris@16
|
203
|
Chris@16
|
204 template<typename T, typename U>
|
Chris@16
|
205 struct terminal_array_matches<T, U(&)[proto::N], true>
|
Chris@16
|
206 : is_convertible<T, U *>
|
Chris@16
|
207 {};
|
Chris@16
|
208
|
Chris@16
|
209 template<typename T, typename U>
|
Chris@16
|
210 struct terminal_array_matches<T, U *, true>
|
Chris@16
|
211 : is_convertible<T, U *>
|
Chris@16
|
212 {};
|
Chris@16
|
213
|
Chris@16
|
214 // terminal_matches
|
Chris@16
|
215 template<typename T, typename U>
|
Chris@16
|
216 struct terminal_matches
|
Chris@16
|
217 : mpl::or_<
|
Chris@16
|
218 mpl::and_<
|
Chris@16
|
219 is_cv_ref_compatible<T, U>
|
Chris@16
|
220 , lambda_matches<
|
Chris@16
|
221 BOOST_PROTO_UNCVREF(T)
|
Chris@16
|
222 , BOOST_PROTO_UNCVREF(U)
|
Chris@16
|
223 >
|
Chris@16
|
224 >
|
Chris@16
|
225 , terminal_array_matches<T, U>
|
Chris@16
|
226 >
|
Chris@16
|
227 {};
|
Chris@16
|
228 #else
|
Chris@16
|
229 // terminal_matches
|
Chris@16
|
230 template<typename T, typename U>
|
Chris@16
|
231 struct terminal_matches
|
Chris@16
|
232 : mpl::and_<
|
Chris@16
|
233 is_cv_ref_compatible<T, U>
|
Chris@16
|
234 , lambda_matches<
|
Chris@16
|
235 BOOST_PROTO_UNCVREF(T)
|
Chris@16
|
236 , BOOST_PROTO_UNCVREF(U)
|
Chris@16
|
237 >
|
Chris@16
|
238 >
|
Chris@16
|
239 {};
|
Chris@16
|
240
|
Chris@16
|
241 template<typename T, std::size_t M>
|
Chris@16
|
242 struct terminal_matches<T(&)[M], T(&)[proto::N]>
|
Chris@16
|
243 : mpl::true_
|
Chris@16
|
244 {};
|
Chris@16
|
245
|
Chris@16
|
246 template<typename T, std::size_t M>
|
Chris@16
|
247 struct terminal_matches<T(&)[M], T *>
|
Chris@16
|
248 : mpl::true_
|
Chris@16
|
249 {};
|
Chris@16
|
250
|
Chris@16
|
251 // Avoid ambiguity errors on MSVC
|
Chris@16
|
252 #if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1500))
|
Chris@16
|
253 template<typename T, std::size_t M>
|
Chris@16
|
254 struct terminal_matches<T const (&)[M], T const[M]>
|
Chris@16
|
255 : mpl::true_
|
Chris@16
|
256 {};
|
Chris@16
|
257 #endif
|
Chris@16
|
258 #endif
|
Chris@16
|
259
|
Chris@16
|
260 template<typename T>
|
Chris@16
|
261 struct terminal_matches<T, T>
|
Chris@16
|
262 : mpl::true_
|
Chris@16
|
263 {};
|
Chris@16
|
264
|
Chris@16
|
265 template<typename T>
|
Chris@16
|
266 struct terminal_matches<T &, T>
|
Chris@16
|
267 : mpl::true_
|
Chris@16
|
268 {};
|
Chris@16
|
269
|
Chris@16
|
270 template<typename T>
|
Chris@16
|
271 struct terminal_matches<T const &, T>
|
Chris@16
|
272 : mpl::true_
|
Chris@16
|
273 {};
|
Chris@16
|
274
|
Chris@16
|
275 template<typename T>
|
Chris@16
|
276 struct terminal_matches<T, proto::_>
|
Chris@16
|
277 : mpl::true_
|
Chris@16
|
278 {};
|
Chris@16
|
279
|
Chris@16
|
280 template<typename T>
|
Chris@16
|
281 struct terminal_matches<T, exact<T> >
|
Chris@16
|
282 : mpl::true_
|
Chris@16
|
283 {};
|
Chris@16
|
284
|
Chris@16
|
285 template<typename T, typename U>
|
Chris@16
|
286 struct terminal_matches<T, proto::convertible_to<U> >
|
Chris@16
|
287 : is_convertible<T, U>
|
Chris@16
|
288 {};
|
Chris@16
|
289
|
Chris@16
|
290 // matches_
|
Chris@16
|
291 template<typename Expr, typename BasicExpr, typename Grammar>
|
Chris@16
|
292 struct matches_
|
Chris@16
|
293 : mpl::false_
|
Chris@16
|
294 {};
|
Chris@16
|
295
|
Chris@16
|
296 template<typename Expr, typename BasicExpr>
|
Chris@16
|
297 struct matches_< Expr, BasicExpr, proto::_ >
|
Chris@16
|
298 : mpl::true_
|
Chris@16
|
299 {};
|
Chris@16
|
300
|
Chris@16
|
301 template<typename Expr, typename Tag, typename Args1, long N1, typename Args2, long N2>
|
Chris@16
|
302 struct matches_< Expr, proto::basic_expr<Tag, Args1, N1>, proto::basic_expr<Tag, Args2, N2> >
|
Chris@16
|
303 : vararg_matches< Expr, Args1, Args2, typename Args2::back_, (N1+2 > N2), (N2 > N1) >
|
Chris@16
|
304 {};
|
Chris@16
|
305
|
Chris@16
|
306 template<typename Expr, typename Tag, typename Args1, long N1, typename Args2, long N2>
|
Chris@16
|
307 struct matches_< Expr, proto::basic_expr<Tag, Args1, N1>, proto::basic_expr<proto::_, Args2, N2> >
|
Chris@16
|
308 : vararg_matches< Expr, Args1, Args2, typename Args2::back_, (N1+2 > N2), (N2 > N1) >
|
Chris@16
|
309 {};
|
Chris@16
|
310
|
Chris@16
|
311 template<typename Expr, typename Tag, typename Args1, typename Args2>
|
Chris@16
|
312 struct matches_< Expr, proto::basic_expr<Tag, Args1, 0>, proto::basic_expr<Tag, Args2, 0> >
|
Chris@16
|
313 : terminal_matches<typename Args1::child0, typename Args2::child0>
|
Chris@16
|
314 {};
|
Chris@16
|
315
|
Chris@16
|
316 template<typename Expr, typename Tag, typename Args1, typename Args2, long N2>
|
Chris@16
|
317 struct matches_< Expr, proto::basic_expr<Tag, Args1, 0>, proto::basic_expr<proto::_, Args2, N2> >
|
Chris@16
|
318 : mpl::false_
|
Chris@16
|
319 {};
|
Chris@16
|
320
|
Chris@16
|
321 template<typename Expr, typename Tag, typename Args1, typename Args2>
|
Chris@16
|
322 struct matches_< Expr, proto::basic_expr<Tag, Args1, 0>, proto::basic_expr<proto::_, Args2, 0> >
|
Chris@16
|
323 : terminal_matches<typename Args1::child0, typename Args2::child0>
|
Chris@16
|
324 {};
|
Chris@16
|
325
|
Chris@16
|
326 template<typename Expr, typename Tag, typename Args1, typename Args2>
|
Chris@16
|
327 struct matches_< Expr, proto::basic_expr<Tag, Args1, 1>, proto::basic_expr<Tag, Args2, 1> >
|
Chris@16
|
328 : matches_<
|
Chris@16
|
329 typename detail::expr_traits<typename Args1::child0>::value_type::proto_derived_expr
|
Chris@16
|
330 , typename detail::expr_traits<typename Args1::child0>::value_type::proto_grammar
|
Chris@16
|
331 , typename Args2::child0::proto_grammar
|
Chris@16
|
332 >
|
Chris@16
|
333 {};
|
Chris@16
|
334
|
Chris@16
|
335 template<typename Expr, typename Tag, typename Args1, typename Args2>
|
Chris@16
|
336 struct matches_< Expr, proto::basic_expr<Tag, Args1, 1>, proto::basic_expr<proto::_, Args2, 1> >
|
Chris@16
|
337 : matches_<
|
Chris@16
|
338 typename detail::expr_traits<typename Args1::child0>::value_type::proto_derived_expr
|
Chris@16
|
339 , typename detail::expr_traits<typename Args1::child0>::value_type::proto_grammar
|
Chris@16
|
340 , typename Args2::child0::proto_grammar
|
Chris@16
|
341 >
|
Chris@16
|
342 {};
|
Chris@16
|
343
|
Chris@16
|
344 #include <boost/proto/detail/and_n.hpp>
|
Chris@16
|
345 #include <boost/proto/detail/or_n.hpp>
|
Chris@16
|
346 #include <boost/proto/detail/matches_.hpp>
|
Chris@16
|
347 #include <boost/proto/detail/vararg_matches_impl.hpp>
|
Chris@16
|
348 #include <boost/proto/detail/lambda_matches.hpp>
|
Chris@16
|
349
|
Chris@16
|
350 // handle proto::if_
|
Chris@16
|
351 template<typename Expr, typename Tag, typename Args, long Arity, typename If, typename Then, typename Else>
|
Chris@16
|
352 struct matches_<Expr, proto::basic_expr<Tag, Args, Arity>, proto::if_<If, Then, Else> >
|
Chris@16
|
353 : mpl::eval_if_c<
|
Chris@16
|
354 static_cast<bool>(
|
Chris@16
|
355 remove_reference<
|
Chris@16
|
356 typename when<_, If>::template impl<Expr, int, int>::result_type
|
Chris@16
|
357 >::type::value
|
Chris@16
|
358 )
|
Chris@16
|
359 , matches_<Expr, proto::basic_expr<Tag, Args, Arity>, typename Then::proto_grammar>
|
Chris@16
|
360 , matches_<Expr, proto::basic_expr<Tag, Args, Arity>, typename Else::proto_grammar>
|
Chris@16
|
361 >::type
|
Chris@16
|
362 {
|
Chris@16
|
363 typedef
|
Chris@16
|
364 typename mpl::if_c<
|
Chris@16
|
365 static_cast<bool>(
|
Chris@16
|
366 remove_reference<
|
Chris@16
|
367 typename when<_, If>::template impl<Expr, int, int>::result_type
|
Chris@16
|
368 >::type::value
|
Chris@16
|
369 )
|
Chris@16
|
370 , Then
|
Chris@16
|
371 , Else
|
Chris@16
|
372 >::type
|
Chris@16
|
373 which;
|
Chris@16
|
374 };
|
Chris@16
|
375
|
Chris@16
|
376 // handle degenerate cases of proto::or_
|
Chris@16
|
377 template<typename Expr, typename BasicExpr>
|
Chris@16
|
378 struct matches_<Expr, BasicExpr, or_<> >
|
Chris@16
|
379 : mpl::false_
|
Chris@16
|
380 {
|
Chris@16
|
381 typedef not_<_> which;
|
Chris@16
|
382 };
|
Chris@16
|
383
|
Chris@16
|
384 template<typename Expr, typename BasicExpr, typename G0>
|
Chris@16
|
385 struct matches_<Expr, BasicExpr, or_<G0> >
|
Chris@16
|
386 : matches_<Expr, BasicExpr, typename G0::proto_grammar>
|
Chris@16
|
387 {
|
Chris@16
|
388 typedef G0 which;
|
Chris@16
|
389 };
|
Chris@16
|
390
|
Chris@16
|
391 // handle degenerate cases of proto::and_
|
Chris@16
|
392 template<typename Expr, typename BasicExpr>
|
Chris@16
|
393 struct matches_<Expr, BasicExpr, and_<> >
|
Chris@16
|
394 : mpl::true_
|
Chris@16
|
395 {};
|
Chris@16
|
396
|
Chris@16
|
397 template<typename Expr, typename BasicExpr, typename G0>
|
Chris@16
|
398 struct matches_<Expr, BasicExpr, and_<G0> >
|
Chris@16
|
399 : matches_<Expr, BasicExpr, typename G0::proto_grammar>
|
Chris@16
|
400 {};
|
Chris@16
|
401
|
Chris@16
|
402 // handle proto::not_
|
Chris@16
|
403 template<typename Expr, typename BasicExpr, typename Grammar>
|
Chris@16
|
404 struct matches_<Expr, BasicExpr, not_<Grammar> >
|
Chris@16
|
405 : mpl::not_<matches_<Expr, BasicExpr, typename Grammar::proto_grammar> >
|
Chris@16
|
406 {};
|
Chris@16
|
407
|
Chris@16
|
408 // handle proto::switch_
|
Chris@16
|
409 template<typename Expr, typename Tag, typename Args, long Arity, typename Cases, typename Transform>
|
Chris@16
|
410 struct matches_<Expr, proto::basic_expr<Tag, Args, Arity>, switch_<Cases, Transform> >
|
Chris@16
|
411 : matches_<
|
Chris@16
|
412 Expr
|
Chris@16
|
413 , proto::basic_expr<Tag, Args, Arity>
|
Chris@16
|
414 , typename Cases::template case_<
|
Chris@16
|
415 typename when<_,Transform>::template impl<Expr,int,int>::result_type
|
Chris@16
|
416 >::proto_grammar
|
Chris@16
|
417 >
|
Chris@16
|
418 {
|
Chris@16
|
419 typedef
|
Chris@16
|
420 typename Cases::template case_<
|
Chris@16
|
421 typename when<_, Transform>::template impl<Expr, int, int>::result_type
|
Chris@16
|
422 >
|
Chris@16
|
423 which;
|
Chris@16
|
424 };
|
Chris@16
|
425
|
Chris@16
|
426 // handle proto::switch_ with the default Transform for specially for better compile times
|
Chris@16
|
427 template<typename Expr, typename Tag, typename Args, long Arity, typename Cases>
|
Chris@16
|
428 struct matches_<Expr, proto::basic_expr<Tag, Args, Arity>, switch_<Cases> >
|
Chris@16
|
429 : matches_<
|
Chris@16
|
430 Expr
|
Chris@16
|
431 , proto::basic_expr<Tag, Args, Arity>
|
Chris@16
|
432 , typename Cases::template case_<Tag>::proto_grammar
|
Chris@16
|
433 >
|
Chris@16
|
434 {
|
Chris@16
|
435 typedef typename Cases::template case_<Tag> which;
|
Chris@16
|
436 };
|
Chris@16
|
437 }
|
Chris@16
|
438
|
Chris@16
|
439 /// \brief A Boolean metafunction that evaluates whether a given
|
Chris@16
|
440 /// expression type matches a grammar.
|
Chris@16
|
441 ///
|
Chris@16
|
442 /// <tt>matches\<Expr,Grammar\></tt> inherits (indirectly) from
|
Chris@16
|
443 /// \c mpl::true_ if <tt>Expr::proto_grammar</tt> matches
|
Chris@16
|
444 /// <tt>Grammar::proto_grammar</tt>, and from \c mpl::false_
|
Chris@16
|
445 /// otherwise.
|
Chris@16
|
446 ///
|
Chris@16
|
447 /// Non-terminal expressions are matched against a grammar
|
Chris@16
|
448 /// according to the following rules:
|
Chris@16
|
449 ///
|
Chris@16
|
450 /// \li The wildcard pattern, \c _, matches any expression.
|
Chris@16
|
451 /// \li An expression <tt>expr\<AT, listN\<A0,A1,...An\> \></tt>
|
Chris@16
|
452 /// matches a grammar <tt>expr\<BT, listN\<B0,B1,...Bn\> \></tt>
|
Chris@16
|
453 /// if \c BT is \c _ or \c AT, and if \c Ax matches \c Bx for
|
Chris@16
|
454 /// each \c x in <tt>[0,n)</tt>.
|
Chris@16
|
455 /// \li An expression <tt>expr\<AT, listN\<A0,...An,U0,...Um\> \></tt>
|
Chris@16
|
456 /// matches a grammar <tt>expr\<BT, listM\<B0,...Bn,vararg\<V\> \> \></tt>
|
Chris@16
|
457 /// if \c BT is \c _ or \c AT, and if \c Ax matches \c Bx
|
Chris@16
|
458 /// for each \c x in <tt>[0,n)</tt> and if \c Ux matches \c V
|
Chris@16
|
459 /// for each \c x in <tt>[0,m)</tt>.
|
Chris@16
|
460 /// \li An expression \c E matches <tt>or_\<B0,B1,...Bn\></tt> if \c E
|
Chris@16
|
461 /// matches some \c Bx for \c x in <tt>[0,n)</tt>.
|
Chris@16
|
462 /// \li An expression \c E matches <tt>and_\<B0,B1,...Bn\></tt> if \c E
|
Chris@16
|
463 /// matches all \c Bx for \c x in <tt>[0,n)</tt>.
|
Chris@16
|
464 /// \li An expression \c E matches <tt>if_\<T,U,V\></tt> if
|
Chris@16
|
465 /// <tt>boost::result_of\<when\<_,T\>(E,int,int)\>::type::value</tt>
|
Chris@16
|
466 /// is \c true and \c E matches \c U; or, if
|
Chris@16
|
467 /// <tt>boost::result_of\<when\<_,T\>(E,int,int)\>::type::value</tt>
|
Chris@16
|
468 /// is \c false and \c E matches \c V. (Note: \c U defaults to \c _
|
Chris@16
|
469 /// and \c V defaults to \c not_\<_\>.)
|
Chris@16
|
470 /// \li An expression \c E matches <tt>not_\<T\></tt> if \c E does
|
Chris@16
|
471 /// not match \c T.
|
Chris@16
|
472 /// \li An expression \c E matches <tt>switch_\<C,T\></tt> if
|
Chris@16
|
473 /// \c E matches <tt>C::case_\<boost::result_of\<T(E)\>::type\></tt>.
|
Chris@16
|
474 /// (Note: T defaults to <tt>tag_of\<_\>()</tt>.)
|
Chris@16
|
475 ///
|
Chris@16
|
476 /// A terminal expression <tt>expr\<AT,term\<A\> \></tt> matches
|
Chris@16
|
477 /// a grammar <tt>expr\<BT,term\<B\> \></tt> if \c BT is \c AT or
|
Chris@16
|
478 /// \c proto::_ and if one of the following is true:
|
Chris@16
|
479 ///
|
Chris@16
|
480 /// \li \c B is the wildcard pattern, \c _
|
Chris@16
|
481 /// \li \c A is \c B
|
Chris@16
|
482 /// \li \c A is <tt>B &</tt>
|
Chris@16
|
483 /// \li \c A is <tt>B const &</tt>
|
Chris@16
|
484 /// \li \c B is <tt>exact\<A\></tt>
|
Chris@16
|
485 /// \li \c B is <tt>convertible_to\<X\></tt> and
|
Chris@16
|
486 /// <tt>is_convertible\<A,X\>::value</tt> is \c true.
|
Chris@16
|
487 /// \li \c A is <tt>X[M]</tt> or <tt>X(&)[M]</tt> and
|
Chris@16
|
488 /// \c B is <tt>X[proto::N]</tt>.
|
Chris@16
|
489 /// \li \c A is <tt>X(&)[M]</tt> and \c B is <tt>X(&)[proto::N]</tt>.
|
Chris@16
|
490 /// \li \c A is <tt>X[M]</tt> or <tt>X(&)[M]</tt> and
|
Chris@16
|
491 /// \c B is <tt>X*</tt>.
|
Chris@16
|
492 /// \li \c B lambda-matches \c A (see below).
|
Chris@16
|
493 ///
|
Chris@16
|
494 /// A type \c B lambda-matches \c A if one of the following is true:
|
Chris@16
|
495 ///
|
Chris@16
|
496 /// \li \c B is \c A
|
Chris@16
|
497 /// \li \c B is the wildcard pattern, \c _
|
Chris@16
|
498 /// \li \c B is <tt>T\<B0,B1,...Bn\></tt> and \c A is
|
Chris@16
|
499 /// <tt>T\<A0,A1,...An\></tt> and for each \c x in
|
Chris@16
|
500 /// <tt>[0,n)</tt>, \c Ax and \c Bx are types
|
Chris@16
|
501 /// such that \c Ax lambda-matches \c Bx
|
Chris@16
|
502 template<typename Expr, typename Grammar>
|
Chris@16
|
503 struct matches
|
Chris@16
|
504 : detail::matches_<
|
Chris@16
|
505 typename Expr::proto_derived_expr
|
Chris@16
|
506 , typename Expr::proto_grammar
|
Chris@16
|
507 , typename Grammar::proto_grammar
|
Chris@16
|
508 >
|
Chris@16
|
509 {};
|
Chris@16
|
510
|
Chris@16
|
511 /// INTERNAL ONLY
|
Chris@16
|
512 ///
|
Chris@16
|
513 template<typename Expr, typename Grammar>
|
Chris@16
|
514 struct matches<Expr &, Grammar>
|
Chris@16
|
515 : detail::matches_<
|
Chris@16
|
516 typename Expr::proto_derived_expr
|
Chris@16
|
517 , typename Expr::proto_grammar
|
Chris@16
|
518 , typename Grammar::proto_grammar
|
Chris@16
|
519 >
|
Chris@16
|
520 {};
|
Chris@16
|
521
|
Chris@16
|
522 /// \brief A wildcard grammar element that matches any expression,
|
Chris@16
|
523 /// and a transform that returns the current expression unchanged.
|
Chris@16
|
524 ///
|
Chris@16
|
525 /// The wildcard type, \c _, is a grammar element such that
|
Chris@16
|
526 /// <tt>matches\<E,_\>::value</tt> is \c true for any expression
|
Chris@16
|
527 /// type \c E.
|
Chris@16
|
528 ///
|
Chris@16
|
529 /// The wildcard can also be used as a stand-in for a template
|
Chris@16
|
530 /// argument when matching terminals. For instance, the following
|
Chris@16
|
531 /// is a grammar that will match any <tt>std::complex\<\></tt>
|
Chris@16
|
532 /// terminal:
|
Chris@16
|
533 ///
|
Chris@16
|
534 /// \code
|
Chris@16
|
535 /// BOOST_MPL_ASSERT((
|
Chris@16
|
536 /// matches<
|
Chris@16
|
537 /// terminal<std::complex<double> >::type
|
Chris@16
|
538 /// , terminal<std::complex< _ > >
|
Chris@16
|
539 /// >
|
Chris@16
|
540 /// ));
|
Chris@16
|
541 /// \endcode
|
Chris@16
|
542 ///
|
Chris@16
|
543 /// When used as a transform, \c _ returns the current expression
|
Chris@16
|
544 /// unchanged. For instance, in the following, \c _ is used with
|
Chris@16
|
545 /// the \c fold\<\> transform to fold the children of a node:
|
Chris@16
|
546 ///
|
Chris@16
|
547 /// \code
|
Chris@16
|
548 /// struct CountChildren
|
Chris@16
|
549 /// : or_<
|
Chris@16
|
550 /// // Terminals have no children
|
Chris@16
|
551 /// when<terminal<_>, mpl::int_<0>()>
|
Chris@16
|
552 /// // Use fold<> to count the children of non-terminals
|
Chris@16
|
553 /// , otherwise<
|
Chris@16
|
554 /// fold<
|
Chris@16
|
555 /// _ // <-- fold the current expression
|
Chris@16
|
556 /// , mpl::int_<0>()
|
Chris@16
|
557 /// , mpl::plus<_state, mpl::int_<1> >()
|
Chris@16
|
558 /// >
|
Chris@16
|
559 /// >
|
Chris@16
|
560 /// >
|
Chris@16
|
561 /// {};
|
Chris@16
|
562 /// \endcode
|
Chris@16
|
563 struct _ : transform<_>
|
Chris@16
|
564 {
|
Chris@16
|
565 typedef _ proto_grammar;
|
Chris@16
|
566
|
Chris@16
|
567 template<typename Expr, typename State, typename Data>
|
Chris@16
|
568 struct impl : transform_impl<Expr, State, Data>
|
Chris@16
|
569 {
|
Chris@16
|
570 typedef Expr result_type;
|
Chris@16
|
571
|
Chris@16
|
572 /// \param expr An expression
|
Chris@16
|
573 /// \return \c e
|
Chris@16
|
574 BOOST_PROTO_RETURN_TYPE_STRICT_LOOSE(result_type, typename impl::expr_param)
|
Chris@16
|
575 operator()(
|
Chris@16
|
576 typename impl::expr_param e
|
Chris@16
|
577 , typename impl::state_param
|
Chris@16
|
578 , typename impl::data_param
|
Chris@16
|
579 ) const
|
Chris@16
|
580 {
|
Chris@16
|
581 return e;
|
Chris@16
|
582 }
|
Chris@16
|
583 };
|
Chris@16
|
584 };
|
Chris@16
|
585
|
Chris@16
|
586 namespace detail
|
Chris@16
|
587 {
|
Chris@16
|
588 template<typename Expr, typename State, typename Data>
|
Chris@16
|
589 struct _and_impl<proto::and_<>, Expr, State, Data>
|
Chris@16
|
590 : proto::_::impl<Expr, State, Data>
|
Chris@16
|
591 {};
|
Chris@16
|
592
|
Chris@16
|
593 template<typename G0, typename Expr, typename State, typename Data>
|
Chris@16
|
594 struct _and_impl<proto::and_<G0>, Expr, State, Data>
|
Chris@16
|
595 : proto::when<proto::_, G0>::template impl<Expr, State, Data>
|
Chris@16
|
596 {};
|
Chris@16
|
597 }
|
Chris@16
|
598
|
Chris@16
|
599 /// \brief Inverts the set of expressions matched by a grammar. When
|
Chris@16
|
600 /// used as a transform, \c not_\<\> returns the current expression
|
Chris@16
|
601 /// unchanged.
|
Chris@16
|
602 ///
|
Chris@16
|
603 /// If an expression type \c E does not match a grammar \c G, then
|
Chris@16
|
604 /// \c E \e does match <tt>not_\<G\></tt>. For example,
|
Chris@16
|
605 /// <tt>not_\<terminal\<_\> \></tt> will match any non-terminal.
|
Chris@16
|
606 template<typename Grammar>
|
Chris@16
|
607 struct not_ : transform<not_<Grammar> >
|
Chris@16
|
608 {
|
Chris@16
|
609 typedef not_ proto_grammar;
|
Chris@16
|
610
|
Chris@16
|
611 template<typename Expr, typename State, typename Data>
|
Chris@16
|
612 struct impl : transform_impl<Expr, State, Data>
|
Chris@16
|
613 {
|
Chris@16
|
614 typedef Expr result_type;
|
Chris@16
|
615
|
Chris@16
|
616 /// \param e An expression
|
Chris@16
|
617 /// \pre <tt>matches\<Expr,not_\>::value</tt> is \c true.
|
Chris@16
|
618 /// \return \c e
|
Chris@16
|
619 BOOST_PROTO_RETURN_TYPE_STRICT_LOOSE(result_type, typename impl::expr_param)
|
Chris@16
|
620 operator()(
|
Chris@16
|
621 typename impl::expr_param e
|
Chris@16
|
622 , typename impl::state_param
|
Chris@16
|
623 , typename impl::data_param
|
Chris@16
|
624 ) const
|
Chris@16
|
625 {
|
Chris@16
|
626 return e;
|
Chris@16
|
627 }
|
Chris@16
|
628 };
|
Chris@16
|
629 };
|
Chris@16
|
630
|
Chris@16
|
631 /// \brief Used to select one grammar or another based on the result
|
Chris@16
|
632 /// of a compile-time Boolean. When used as a transform, \c if_\<\>
|
Chris@16
|
633 /// selects between two transforms based on a compile-time Boolean.
|
Chris@16
|
634 ///
|
Chris@16
|
635 /// When <tt>if_\<If,Then,Else\></tt> is used as a grammar, \c If
|
Chris@16
|
636 /// must be a Proto transform and \c Then and \c Else must be grammars.
|
Chris@16
|
637 /// An expression type \c E matches <tt>if_\<If,Then,Else\></tt> if
|
Chris@16
|
638 /// <tt>boost::result_of\<when\<_,If\>(E,int,int)\>::type::value</tt>
|
Chris@16
|
639 /// is \c true and \c E matches \c U; or, if
|
Chris@16
|
640 /// <tt>boost::result_of\<when\<_,If\>(E,int,int)\>::type::value</tt>
|
Chris@16
|
641 /// is \c false and \c E matches \c V.
|
Chris@16
|
642 ///
|
Chris@16
|
643 /// The template parameter \c Then defaults to \c _
|
Chris@16
|
644 /// and \c Else defaults to \c not\<_\>, so an expression type \c E
|
Chris@16
|
645 /// will match <tt>if_\<If\></tt> if and only if
|
Chris@16
|
646 /// <tt>boost::result_of\<when\<_,If\>(E,int,int)\>::type::value</tt>
|
Chris@16
|
647 /// is \c true.
|
Chris@16
|
648 ///
|
Chris@16
|
649 /// \code
|
Chris@16
|
650 /// // A grammar that only matches integral terminals,
|
Chris@16
|
651 /// // using is_integral<> from Boost.Type_traits.
|
Chris@16
|
652 /// struct IsIntegral
|
Chris@16
|
653 /// : and_<
|
Chris@16
|
654 /// terminal<_>
|
Chris@16
|
655 /// , if_< is_integral<_value>() >
|
Chris@16
|
656 /// >
|
Chris@16
|
657 /// {};
|
Chris@16
|
658 /// \endcode
|
Chris@16
|
659 ///
|
Chris@16
|
660 /// When <tt>if_\<If,Then,Else\></tt> is used as a transform, \c If,
|
Chris@16
|
661 /// \c Then and \c Else must be Proto transforms. When applying
|
Chris@16
|
662 /// the transform to an expression \c E, state \c S and data \c V,
|
Chris@16
|
663 /// if <tt>boost::result_of\<when\<_,If\>(E,S,V)\>::type::value</tt>
|
Chris@16
|
664 /// is \c true then the \c Then transform is applied; otherwise
|
Chris@16
|
665 /// the \c Else transform is applied.
|
Chris@16
|
666 ///
|
Chris@16
|
667 /// \code
|
Chris@16
|
668 /// // Match a terminal. If the terminal is integral, return
|
Chris@16
|
669 /// // mpl::true_; otherwise, return mpl::false_.
|
Chris@16
|
670 /// struct IsIntegral2
|
Chris@16
|
671 /// : when<
|
Chris@16
|
672 /// terminal<_>
|
Chris@16
|
673 /// , if_<
|
Chris@16
|
674 /// is_integral<_value>()
|
Chris@16
|
675 /// , mpl::true_()
|
Chris@16
|
676 /// , mpl::false_()
|
Chris@16
|
677 /// >
|
Chris@16
|
678 /// >
|
Chris@16
|
679 /// {};
|
Chris@16
|
680 /// \endcode
|
Chris@16
|
681 template<
|
Chris@16
|
682 typename If
|
Chris@16
|
683 , typename Then // = _
|
Chris@16
|
684 , typename Else // = not_<_>
|
Chris@16
|
685 >
|
Chris@16
|
686 struct if_ : transform<if_<If, Then, Else> >
|
Chris@16
|
687 {
|
Chris@16
|
688 typedef if_ proto_grammar;
|
Chris@16
|
689
|
Chris@16
|
690 template<typename Expr, typename State, typename Data>
|
Chris@16
|
691 struct impl : transform_impl<Expr, State, Data>
|
Chris@16
|
692 {
|
Chris@16
|
693 typedef
|
Chris@16
|
694 typename when<_, If>::template impl<Expr, State, Data>::result_type
|
Chris@16
|
695 condition;
|
Chris@16
|
696
|
Chris@16
|
697 typedef
|
Chris@16
|
698 typename mpl::if_c<
|
Chris@16
|
699 static_cast<bool>(remove_reference<condition>::type::value)
|
Chris@16
|
700 , when<_, Then>
|
Chris@16
|
701 , when<_, Else>
|
Chris@16
|
702 >::type
|
Chris@16
|
703 which;
|
Chris@16
|
704
|
Chris@16
|
705 typedef typename which::template impl<Expr, State, Data>::result_type result_type;
|
Chris@16
|
706
|
Chris@16
|
707 /// \param e An expression
|
Chris@16
|
708 /// \param s The current state
|
Chris@16
|
709 /// \param d A data of arbitrary type
|
Chris@16
|
710 /// \return <tt>which::impl<Expr, State, Data>()(e, s, d)</tt>
|
Chris@16
|
711 result_type operator ()(
|
Chris@16
|
712 typename impl::expr_param e
|
Chris@16
|
713 , typename impl::state_param s
|
Chris@16
|
714 , typename impl::data_param d
|
Chris@16
|
715 ) const
|
Chris@16
|
716 {
|
Chris@16
|
717 return typename which::template impl<Expr, State, Data>()(e, s, d);
|
Chris@16
|
718 }
|
Chris@16
|
719 };
|
Chris@16
|
720 };
|
Chris@16
|
721
|
Chris@16
|
722 /// \brief For matching one of a set of alternate grammars. Alternates
|
Chris@16
|
723 /// tried in order to avoid ambiguity. When used as a transform, \c or_\<\>
|
Chris@16
|
724 /// applies the transform associated with the first grammar that matches
|
Chris@16
|
725 /// the expression.
|
Chris@16
|
726 ///
|
Chris@16
|
727 /// An expression type \c E matches <tt>or_\<B0,B1,...Bn\></tt> if \c E
|
Chris@16
|
728 /// matches any \c Bx for \c x in <tt>[0,n)</tt>.
|
Chris@16
|
729 ///
|
Chris@16
|
730 /// When applying <tt>or_\<B0,B1,...Bn\></tt> as a transform with an
|
Chris@16
|
731 /// expression \c e of type \c E, state \c s and data \c d, it is
|
Chris@16
|
732 /// equivalent to <tt>Bx()(e, s, d)</tt>, where \c x is the lowest
|
Chris@16
|
733 /// number such that <tt>matches\<E,Bx\>::value</tt> is \c true.
|
Chris@16
|
734 template<BOOST_PROTO_LOGICAL_typename_G>
|
Chris@16
|
735 struct or_ : transform<or_<BOOST_PROTO_LOGICAL_G> >
|
Chris@16
|
736 {
|
Chris@16
|
737 typedef or_ proto_grammar;
|
Chris@16
|
738
|
Chris@16
|
739 /// \param e An expression
|
Chris@16
|
740 /// \param s The current state
|
Chris@16
|
741 /// \param d A data of arbitrary type
|
Chris@16
|
742 /// \pre <tt>matches\<Expr,or_\>::value</tt> is \c true.
|
Chris@16
|
743 /// \return <tt>which()(e, s, d)</tt>, where <tt>which</tt> is the
|
Chris@16
|
744 /// sub-grammar that matched <tt>Expr</tt>.
|
Chris@16
|
745
|
Chris@16
|
746 template<typename Expr, typename State, typename Data>
|
Chris@16
|
747 struct impl
|
Chris@16
|
748 : detail::matches_<
|
Chris@16
|
749 typename Expr::proto_derived_expr
|
Chris@16
|
750 , typename Expr::proto_grammar
|
Chris@16
|
751 , or_
|
Chris@16
|
752 >::which::template impl<Expr, State, Data>
|
Chris@16
|
753 {};
|
Chris@16
|
754
|
Chris@16
|
755 template<typename Expr, typename State, typename Data>
|
Chris@16
|
756 struct impl<Expr &, State, Data>
|
Chris@16
|
757 : detail::matches_<
|
Chris@16
|
758 typename Expr::proto_derived_expr
|
Chris@16
|
759 , typename Expr::proto_grammar
|
Chris@16
|
760 , or_
|
Chris@16
|
761 >::which::template impl<Expr &, State, Data>
|
Chris@16
|
762 {};
|
Chris@16
|
763 };
|
Chris@16
|
764
|
Chris@16
|
765 /// \brief For matching all of a set of grammars. When used as a
|
Chris@16
|
766 /// transform, \c and_\<\> applies the transforms associated with
|
Chris@16
|
767 /// the each grammar in the set, and returns the result of the last.
|
Chris@16
|
768 ///
|
Chris@16
|
769 /// An expression type \c E matches <tt>and_\<B0,B1,...Bn\></tt> if \c E
|
Chris@16
|
770 /// matches all \c Bx for \c x in <tt>[0,n)</tt>.
|
Chris@16
|
771 ///
|
Chris@16
|
772 /// When applying <tt>and_\<B0,B1,...Bn\></tt> as a transform with an
|
Chris@16
|
773 /// expression \c e, state \c s and data \c d, it is
|
Chris@16
|
774 /// equivalent to <tt>(B0()(e, s, d),B1()(e, s, d),...Bn()(e, s, d))</tt>.
|
Chris@16
|
775 template<BOOST_PROTO_LOGICAL_typename_G>
|
Chris@16
|
776 struct and_ : transform<and_<BOOST_PROTO_LOGICAL_G> >
|
Chris@16
|
777 {
|
Chris@16
|
778 typedef and_ proto_grammar;
|
Chris@16
|
779
|
Chris@16
|
780 template<typename Expr, typename State, typename Data>
|
Chris@16
|
781 struct impl
|
Chris@16
|
782 : detail::_and_impl<and_, Expr, State, Data>
|
Chris@16
|
783 {};
|
Chris@16
|
784 };
|
Chris@16
|
785
|
Chris@16
|
786 /// \brief For matching one of a set of alternate grammars, which
|
Chris@16
|
787 /// are looked up based on some property of an expression. The
|
Chris@16
|
788 /// property on which to dispatch is specified by the \c Transform
|
Chris@16
|
789 /// template parameter, which defaults to <tt>tag_of\<_\>()</tt>.
|
Chris@16
|
790 /// That is, when the \c Trannsform is not specified, the alternate
|
Chris@16
|
791 /// grammar is looked up using the tag type of the current expression.
|
Chris@16
|
792 ///
|
Chris@16
|
793 /// When used as a transform, \c switch_\<\> applies the transform
|
Chris@16
|
794 /// associated with the grammar that matches the expression.
|
Chris@16
|
795 ///
|
Chris@16
|
796 /// \note \c switch_\<\> is functionally identical to \c or_\<\> but
|
Chris@16
|
797 /// is often more efficient. It does a fast, O(1) lookup using the
|
Chris@16
|
798 /// result of the specified transform to find a sub-grammar that may
|
Chris@16
|
799 /// potentially match the expression.
|
Chris@16
|
800 ///
|
Chris@16
|
801 /// An expression type \c E matches <tt>switch_\<C,T\></tt> if \c E
|
Chris@16
|
802 /// matches <tt>C::case_\<boost::result_of\<T(E)\>::type\></tt>.
|
Chris@16
|
803 ///
|
Chris@16
|
804 /// When applying <tt>switch_\<C,T\></tt> as a transform with an
|
Chris@16
|
805 /// expression \c e of type \c E, state \c s of type \S and data
|
Chris@16
|
806 /// \c d of type \c D, it is equivalent to
|
Chris@16
|
807 /// <tt>C::case_\<boost::result_of\<T(E,S,D)\>::type\>()(e, s, d)</tt>.
|
Chris@16
|
808 template<typename Cases, typename Transform>
|
Chris@16
|
809 struct switch_ : transform<switch_<Cases, Transform> >
|
Chris@16
|
810 {
|
Chris@16
|
811 typedef switch_ proto_grammar;
|
Chris@16
|
812
|
Chris@16
|
813 template<typename Expr, typename State, typename Data>
|
Chris@16
|
814 struct impl
|
Chris@16
|
815 : Cases::template case_<
|
Chris@16
|
816 typename when<_, Transform>::template impl<Expr, State, Data>::result_type
|
Chris@16
|
817 >::template impl<Expr, State, Data>
|
Chris@16
|
818 {};
|
Chris@16
|
819 };
|
Chris@16
|
820
|
Chris@16
|
821 /// INTERNAL ONLY (This is merely a compile-time optimization for the common case)
|
Chris@16
|
822 ///
|
Chris@16
|
823 template<typename Cases>
|
Chris@16
|
824 struct switch_<Cases> : transform<switch_<Cases> >
|
Chris@16
|
825 {
|
Chris@16
|
826 typedef switch_ proto_grammar;
|
Chris@16
|
827
|
Chris@16
|
828 template<typename Expr, typename State, typename Data>
|
Chris@16
|
829 struct impl
|
Chris@16
|
830 : Cases::template case_<typename Expr::proto_tag>::template impl<Expr, State, Data>
|
Chris@16
|
831 {};
|
Chris@16
|
832
|
Chris@16
|
833 template<typename Expr, typename State, typename Data>
|
Chris@16
|
834 struct impl<Expr &, State, Data>
|
Chris@16
|
835 : Cases::template case_<typename Expr::proto_tag>::template impl<Expr &, State, Data>
|
Chris@16
|
836 {};
|
Chris@16
|
837 };
|
Chris@16
|
838
|
Chris@16
|
839 /// \brief For forcing exact matches of terminal types.
|
Chris@16
|
840 ///
|
Chris@16
|
841 /// By default, matching terminals ignores references and
|
Chris@16
|
842 /// cv-qualifiers. For instance, a terminal expression of
|
Chris@16
|
843 /// type <tt>terminal\<int const &\>::type</tt> will match
|
Chris@16
|
844 /// the grammar <tt>terminal\<int\></tt>. If that is not
|
Chris@16
|
845 /// desired, you can force an exact match with
|
Chris@16
|
846 /// <tt>terminal\<exact\<int\> \></tt>. This will only
|
Chris@16
|
847 /// match integer terminals where the terminal is held by
|
Chris@16
|
848 /// value.
|
Chris@16
|
849 template<typename T>
|
Chris@16
|
850 struct exact
|
Chris@16
|
851 {};
|
Chris@16
|
852
|
Chris@16
|
853 /// \brief For matching terminals that are convertible to
|
Chris@16
|
854 /// a type.
|
Chris@16
|
855 ///
|
Chris@16
|
856 /// Use \c convertible_to\<\> to match a terminal that is
|
Chris@16
|
857 /// convertible to some type. For example, the grammar
|
Chris@16
|
858 /// <tt>terminal\<convertible_to\<int\> \></tt> will match
|
Chris@16
|
859 /// any terminal whose argument is convertible to an integer.
|
Chris@16
|
860 ///
|
Chris@16
|
861 /// \note The trait \c is_convertible\<\> from Boost.Type_traits
|
Chris@16
|
862 /// is used to determinal convertibility.
|
Chris@16
|
863 template<typename T>
|
Chris@16
|
864 struct convertible_to
|
Chris@16
|
865 {};
|
Chris@16
|
866
|
Chris@16
|
867 /// \brief For matching a Grammar to a variable number of
|
Chris@16
|
868 /// sub-expressions.
|
Chris@16
|
869 ///
|
Chris@16
|
870 /// An expression type <tt>expr\<AT, listN\<A0,...An,U0,...Um\> \></tt>
|
Chris@16
|
871 /// matches a grammar <tt>expr\<BT, listM\<B0,...Bn,vararg\<V\> \> \></tt>
|
Chris@16
|
872 /// if \c BT is \c _ or \c AT, and if \c Ax matches \c Bx
|
Chris@16
|
873 /// for each \c x in <tt>[0,n)</tt> and if \c Ux matches \c V
|
Chris@16
|
874 /// for each \c x in <tt>[0,m)</tt>.
|
Chris@16
|
875 ///
|
Chris@16
|
876 /// For example:
|
Chris@16
|
877 ///
|
Chris@16
|
878 /// \code
|
Chris@16
|
879 /// // Match any function call expression, irregardless
|
Chris@16
|
880 /// // of the number of function arguments:
|
Chris@16
|
881 /// struct Function
|
Chris@16
|
882 /// : function< vararg<_> >
|
Chris@16
|
883 /// {};
|
Chris@16
|
884 /// \endcode
|
Chris@16
|
885 ///
|
Chris@16
|
886 /// When used as a transform, <tt>vararg\<G\></tt> applies
|
Chris@16
|
887 /// <tt>G</tt>'s transform.
|
Chris@16
|
888 template<typename Grammar>
|
Chris@16
|
889 struct vararg
|
Chris@16
|
890 : Grammar
|
Chris@16
|
891 {
|
Chris@16
|
892 /// INTERNAL ONLY
|
Chris@16
|
893 typedef void proto_is_vararg_;
|
Chris@16
|
894 };
|
Chris@16
|
895
|
Chris@16
|
896 /// INTERNAL ONLY
|
Chris@16
|
897 ///
|
Chris@16
|
898 template<BOOST_PROTO_LOGICAL_typename_G>
|
Chris@16
|
899 struct is_callable<or_<BOOST_PROTO_LOGICAL_G> >
|
Chris@16
|
900 : mpl::true_
|
Chris@16
|
901 {};
|
Chris@16
|
902
|
Chris@16
|
903 /// INTERNAL ONLY
|
Chris@16
|
904 ///
|
Chris@16
|
905 template<BOOST_PROTO_LOGICAL_typename_G>
|
Chris@16
|
906 struct is_callable<and_<BOOST_PROTO_LOGICAL_G> >
|
Chris@16
|
907 : mpl::true_
|
Chris@16
|
908 {};
|
Chris@16
|
909
|
Chris@16
|
910 /// INTERNAL ONLY
|
Chris@16
|
911 ///
|
Chris@16
|
912 template<typename Grammar>
|
Chris@16
|
913 struct is_callable<not_<Grammar> >
|
Chris@16
|
914 : mpl::true_
|
Chris@16
|
915 {};
|
Chris@16
|
916
|
Chris@16
|
917 /// INTERNAL ONLY
|
Chris@16
|
918 ///
|
Chris@16
|
919 template<typename If, typename Then, typename Else>
|
Chris@16
|
920 struct is_callable<if_<If, Then, Else> >
|
Chris@16
|
921 : mpl::true_
|
Chris@16
|
922 {};
|
Chris@16
|
923
|
Chris@16
|
924 /// INTERNAL ONLY
|
Chris@16
|
925 ///
|
Chris@16
|
926 template<typename Grammar>
|
Chris@16
|
927 struct is_callable<vararg<Grammar> >
|
Chris@16
|
928 : mpl::true_
|
Chris@16
|
929 {};
|
Chris@16
|
930
|
Chris@16
|
931 /// INTERNAL ONLY
|
Chris@16
|
932 ///
|
Chris@16
|
933 template<typename Cases, typename Transform>
|
Chris@16
|
934 struct is_callable<switch_<Cases, Transform> >
|
Chris@16
|
935 : mpl::true_
|
Chris@16
|
936 {};
|
Chris@16
|
937
|
Chris@16
|
938 }}
|
Chris@16
|
939
|
Chris@16
|
940 #undef BOOST_PROTO_LOGICAL_typename_G
|
Chris@16
|
941 #undef BOOST_PROTO_LOGICAL_G
|
Chris@16
|
942
|
Chris@101
|
943 #if defined(_MSC_VER)
|
Chris@16
|
944 # pragma warning(pop)
|
Chris@16
|
945 #endif
|
Chris@16
|
946
|
Chris@16
|
947 #endif
|