1 ///////////////////////////////////////////////////////////////////////////////
3 /// Contains definition of matches\<\> metafunction for determining if
4 /// a given expression matches a given pattern.
6 // Copyright 2008 Eric Niebler. Distributed under the Boost
7 // Software License, Version 1.0. (See accompanying file
8 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
10 #ifndef BOOST_PROTO_MATCHES_HPP_EAN_11_03_2006
11 #define BOOST_PROTO_MATCHES_HPP_EAN_11_03_2006
13 #include <boost/config.hpp>
14 #include <boost/detail/workaround.hpp>
15 #include <boost/preprocessor/cat.hpp>
16 #include <boost/preprocessor/arithmetic/dec.hpp>
17 #include <boost/preprocessor/arithmetic/sub.hpp>
18 #include <boost/preprocessor/iteration/iterate.hpp>
19 #include <boost/preprocessor/facilities/intercept.hpp>
20 #include <boost/preprocessor/punctuation/comma_if.hpp>
21 #include <boost/preprocessor/repetition/enum.hpp>
22 #include <boost/preprocessor/repetition/enum_params.hpp>
23 #include <boost/preprocessor/repetition/enum_shifted.hpp>
24 #include <boost/preprocessor/repetition/enum_binary_params.hpp>
25 #include <boost/preprocessor/repetition/enum_shifted_params.hpp>
26 #include <boost/preprocessor/repetition/enum_trailing_params.hpp>
27 #include <boost/preprocessor/repetition/enum_params_with_a_default.hpp>
28 #include <boost/preprocessor/repetition/repeat.hpp>
29 #include <boost/config.hpp>
30 #include <boost/mpl/logical.hpp>
31 #include <boost/mpl/eval_if.hpp>
32 #include <boost/proto/detail/template_arity.hpp>
33 #include <boost/utility/enable_if.hpp>
34 #if BOOST_WORKAROUND(BOOST_MSVC, == 1310)
35 #include <boost/type_traits/is_array.hpp>
37 #include <boost/type_traits/is_const.hpp>
38 #include <boost/type_traits/is_convertible.hpp>
39 #include <boost/type_traits/is_reference.hpp>
40 #include <boost/type_traits/is_pointer.hpp>
41 #include <boost/proto/proto_fwd.hpp>
42 #include <boost/proto/traits.hpp>
43 #include <boost/proto/transform/when.hpp>
44 #include <boost/proto/transform/impl.hpp>
47 # pragma warning(push)
48 # pragma warning(disable:4305) // 'specialization' : truncation from 'const int' to 'bool'
51 #define BOOST_PROTO_LOGICAL_typename_G BOOST_PP_ENUM_PARAMS(BOOST_PROTO_MAX_LOGICAL_ARITY, typename G)
52 #define BOOST_PROTO_LOGICAL_G BOOST_PP_ENUM_PARAMS(BOOST_PROTO_MAX_LOGICAL_ARITY, G)
54 namespace boost { namespace proto
59 template<typename Expr, typename BasicExpr, typename Grammar>
62 template<bool B, typename Pred>
65 template<typename And, typename Expr, typename State, typename Data>
68 template<typename T, typename U>
73 template<typename T, std::size_t M>
74 struct array_matches<T[M], T *>
78 template<typename T, std::size_t M>
79 struct array_matches<T[M], T const *>
83 template<typename T, std::size_t M>
84 struct array_matches<T[M], T[proto::N]>
88 template<typename T, typename U
89 BOOST_PROTO_TEMPLATE_ARITY_PARAM(long Arity = detail::template_arity<U>::value)
96 struct lambda_matches<T, proto::_ BOOST_PROTO_TEMPLATE_ARITY_PARAM(-1)>
101 struct lambda_matches<T, T BOOST_PROTO_TEMPLATE_ARITY_PARAM(-1)>
105 template<typename T, std::size_t M, typename U>
106 struct lambda_matches<T[M], U BOOST_PROTO_TEMPLATE_ARITY_PARAM(-1)>
107 : array_matches<T[M], U>
110 template<typename T, std::size_t M>
111 struct lambda_matches<T[M], _ BOOST_PROTO_TEMPLATE_ARITY_PARAM(-1)>
115 template<typename T, std::size_t M>
116 struct lambda_matches<T[M], T[M] BOOST_PROTO_TEMPLATE_ARITY_PARAM(-1)>
120 template<template<typename> class T, typename Expr0, typename Grammar0>
121 struct lambda_matches<T<Expr0>, T<Grammar0> BOOST_PROTO_TEMPLATE_ARITY_PARAM(1) >
122 : lambda_matches<Expr0, Grammar0>
125 // vararg_matches_impl
126 template<typename Args1, typename Back, long From, long To>
127 struct vararg_matches_impl;
130 template<typename Expr, typename Args1, typename Args2, typename Back, bool Can, bool Zero, typename Void = void>
131 struct vararg_matches
135 template<typename Expr, typename Args1, typename Args2, typename Back>
136 struct vararg_matches<Expr, Args1, Args2, Back, true, true, typename Back::proto_is_vararg_>
139 , proto::basic_expr<ignore, Args1, Args1::arity>
140 , proto::basic_expr<ignore, Args2, Args1::arity>
144 template<typename Expr, typename Args1, typename Args2, typename Back>
145 struct vararg_matches<Expr, Args1, Args2, Back, true, false, typename Back::proto_is_vararg_>
149 , proto::basic_expr<ignore, Args1, Args2::arity>
150 , proto::basic_expr<ignore, Args2, Args2::arity>
152 , vararg_matches_impl<Args1, typename Back::proto_grammar, Args2::arity + 1, Args1::arity>
156 // How terminal_matches<> handles references and cv-qualifiers.
157 // The cv and ref matter *only* if the grammar has a top-level ref.
159 // Expr | Grammar | Matches?
160 // -------------------------------------
169 // T const & T const & yes
171 template<typename T, typename U>
172 struct is_cv_ref_compatible
176 template<typename T, typename U>
177 struct is_cv_ref_compatible<T, U &>
181 template<typename T, typename U>
182 struct is_cv_ref_compatible<T &, U &>
183 : mpl::bool_<is_const<T>::value == is_const<U>::value>
186 #if BOOST_WORKAROUND(BOOST_MSVC, == 1310)
187 // MSVC-7.1 has lots of problems with array types that have been
188 // deduced. Partially specializing terminal_matches<> on array types
189 // doesn't seem to work.
193 , bool B = is_array<BOOST_PROTO_UNCVREF(T)>::value
195 struct terminal_array_matches
199 template<typename T, typename U, std::size_t M>
200 struct terminal_array_matches<T, U(&)[M], true>
201 : is_convertible<T, U(&)[M]>
204 template<typename T, typename U>
205 struct terminal_array_matches<T, U(&)[proto::N], true>
206 : is_convertible<T, U *>
209 template<typename T, typename U>
210 struct terminal_array_matches<T, U *, true>
211 : is_convertible<T, U *>
215 template<typename T, typename U>
216 struct terminal_matches
219 is_cv_ref_compatible<T, U>
221 BOOST_PROTO_UNCVREF(T)
222 , BOOST_PROTO_UNCVREF(U)
225 , terminal_array_matches<T, U>
230 template<typename T, typename U>
231 struct terminal_matches
233 is_cv_ref_compatible<T, U>
235 BOOST_PROTO_UNCVREF(T)
236 , BOOST_PROTO_UNCVREF(U)
241 template<typename T, std::size_t M>
242 struct terminal_matches<T(&)[M], T(&)[proto::N]>
246 template<typename T, std::size_t M>
247 struct terminal_matches<T(&)[M], T *>
251 // Avoid ambiguity errors on MSVC
252 #if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1500))
253 template<typename T, std::size_t M>
254 struct terminal_matches<T const (&)[M], T const[M]>
261 struct terminal_matches<T, T>
266 struct terminal_matches<T &, T>
271 struct terminal_matches<T const &, T>
276 struct terminal_matches<T, proto::_>
281 struct terminal_matches<T, exact<T> >
285 template<typename T, typename U>
286 struct terminal_matches<T, proto::convertible_to<U> >
287 : is_convertible<T, U>
291 template<typename Expr, typename BasicExpr, typename Grammar>
296 template<typename Expr, typename BasicExpr>
297 struct matches_< Expr, BasicExpr, proto::_ >
301 template<typename Expr, typename Tag, typename Args1, long N1, typename Args2, long N2>
302 struct matches_< Expr, proto::basic_expr<Tag, Args1, N1>, proto::basic_expr<Tag, Args2, N2> >
303 : vararg_matches< Expr, Args1, Args2, typename Args2::back_, (N1+2 > N2), (N2 > N1) >
306 template<typename Expr, typename Tag, typename Args1, long N1, typename Args2, long N2>
307 struct matches_< Expr, proto::basic_expr<Tag, Args1, N1>, proto::basic_expr<proto::_, Args2, N2> >
308 : vararg_matches< Expr, Args1, Args2, typename Args2::back_, (N1+2 > N2), (N2 > N1) >
311 template<typename Expr, typename Tag, typename Args1, typename Args2>
312 struct matches_< Expr, proto::basic_expr<Tag, Args1, 0>, proto::basic_expr<Tag, Args2, 0> >
313 : terminal_matches<typename Args1::child0, typename Args2::child0>
316 template<typename Expr, typename Tag, typename Args1, typename Args2, long N2>
317 struct matches_< Expr, proto::basic_expr<Tag, Args1, 0>, proto::basic_expr<proto::_, Args2, N2> >
321 template<typename Expr, typename Tag, typename Args1, typename Args2>
322 struct matches_< Expr, proto::basic_expr<Tag, Args1, 0>, proto::basic_expr<proto::_, Args2, 0> >
323 : terminal_matches<typename Args1::child0, typename Args2::child0>
326 template<typename Expr, typename Tag, typename Args1, typename Args2>
327 struct matches_< Expr, proto::basic_expr<Tag, Args1, 1>, proto::basic_expr<Tag, Args2, 1> >
329 typename detail::expr_traits<typename Args1::child0>::value_type::proto_derived_expr
330 , typename detail::expr_traits<typename Args1::child0>::value_type::proto_grammar
331 , typename Args2::child0::proto_grammar
335 template<typename Expr, typename Tag, typename Args1, typename Args2>
336 struct matches_< Expr, proto::basic_expr<Tag, Args1, 1>, proto::basic_expr<proto::_, Args2, 1> >
338 typename detail::expr_traits<typename Args1::child0>::value_type::proto_derived_expr
339 , typename detail::expr_traits<typename Args1::child0>::value_type::proto_grammar
340 , typename Args2::child0::proto_grammar
344 #include <boost/proto/detail/and_n.hpp>
345 #include <boost/proto/detail/or_n.hpp>
346 #include <boost/proto/detail/matches_.hpp>
347 #include <boost/proto/detail/vararg_matches_impl.hpp>
348 #include <boost/proto/detail/lambda_matches.hpp>
351 template<typename Expr, typename Tag, typename Args, long Arity, typename If, typename Then, typename Else>
352 struct matches_<Expr, proto::basic_expr<Tag, Args, Arity>, proto::if_<If, Then, Else> >
356 typename when<_, If>::template impl<Expr, int, int>::result_type
359 , matches_<Expr, proto::basic_expr<Tag, Args, Arity>, typename Then::proto_grammar>
360 , matches_<Expr, proto::basic_expr<Tag, Args, Arity>, typename Else::proto_grammar>
367 typename when<_, If>::template impl<Expr, int, int>::result_type
376 // handle degenerate cases of proto::or_
377 template<typename Expr, typename BasicExpr>
378 struct matches_<Expr, BasicExpr, or_<> >
381 typedef not_<_> which;
384 template<typename Expr, typename BasicExpr, typename G0>
385 struct matches_<Expr, BasicExpr, or_<G0> >
386 : matches_<Expr, BasicExpr, typename G0::proto_grammar>
391 // handle degenerate cases of proto::and_
392 template<typename Expr, typename BasicExpr>
393 struct matches_<Expr, BasicExpr, and_<> >
397 template<typename Expr, typename BasicExpr, typename G0>
398 struct matches_<Expr, BasicExpr, and_<G0> >
399 : matches_<Expr, BasicExpr, typename G0::proto_grammar>
402 // handle proto::not_
403 template<typename Expr, typename BasicExpr, typename Grammar>
404 struct matches_<Expr, BasicExpr, not_<Grammar> >
405 : mpl::not_<matches_<Expr, BasicExpr, typename Grammar::proto_grammar> >
408 // handle proto::switch_
409 template<typename Expr, typename Tag, typename Args, long Arity, typename Cases, typename Transform>
410 struct matches_<Expr, proto::basic_expr<Tag, Args, Arity>, switch_<Cases, Transform> >
413 , proto::basic_expr<Tag, Args, Arity>
414 , typename Cases::template case_<
415 typename when<_,Transform>::template impl<Expr,int,int>::result_type
420 typename Cases::template case_<
421 typename when<_, Transform>::template impl<Expr, int, int>::result_type
426 // handle proto::switch_ with the default Transform for specially for better compile times
427 template<typename Expr, typename Tag, typename Args, long Arity, typename Cases>
428 struct matches_<Expr, proto::basic_expr<Tag, Args, Arity>, switch_<Cases> >
431 , proto::basic_expr<Tag, Args, Arity>
432 , typename Cases::template case_<Tag>::proto_grammar
435 typedef typename Cases::template case_<Tag> which;
439 /// \brief A Boolean metafunction that evaluates whether a given
440 /// expression type matches a grammar.
442 /// <tt>matches\<Expr,Grammar\></tt> inherits (indirectly) from
443 /// \c mpl::true_ if <tt>Expr::proto_grammar</tt> matches
444 /// <tt>Grammar::proto_grammar</tt>, and from \c mpl::false_
447 /// Non-terminal expressions are matched against a grammar
448 /// according to the following rules:
450 /// \li The wildcard pattern, \c _, matches any expression.
451 /// \li An expression <tt>expr\<AT, listN\<A0,A1,...An\> \></tt>
452 /// matches a grammar <tt>expr\<BT, listN\<B0,B1,...Bn\> \></tt>
453 /// if \c BT is \c _ or \c AT, and if \c Ax matches \c Bx for
454 /// each \c x in <tt>[0,n)</tt>.
455 /// \li An expression <tt>expr\<AT, listN\<A0,...An,U0,...Um\> \></tt>
456 /// matches a grammar <tt>expr\<BT, listM\<B0,...Bn,vararg\<V\> \> \></tt>
457 /// if \c BT is \c _ or \c AT, and if \c Ax matches \c Bx
458 /// for each \c x in <tt>[0,n)</tt> and if \c Ux matches \c V
459 /// for each \c x in <tt>[0,m)</tt>.
460 /// \li An expression \c E matches <tt>or_\<B0,B1,...Bn\></tt> if \c E
461 /// matches some \c Bx for \c x in <tt>[0,n)</tt>.
462 /// \li An expression \c E matches <tt>and_\<B0,B1,...Bn\></tt> if \c E
463 /// matches all \c Bx for \c x in <tt>[0,n)</tt>.
464 /// \li An expression \c E matches <tt>if_\<T,U,V\></tt> if
465 /// <tt>boost::result_of\<when\<_,T\>(E,int,int)\>::type::value</tt>
466 /// is \c true and \c E matches \c U; or, if
467 /// <tt>boost::result_of\<when\<_,T\>(E,int,int)\>::type::value</tt>
468 /// is \c false and \c E matches \c V. (Note: \c U defaults to \c _
469 /// and \c V defaults to \c not_\<_\>.)
470 /// \li An expression \c E matches <tt>not_\<T\></tt> if \c E does
472 /// \li An expression \c E matches <tt>switch_\<C,T\></tt> if
473 /// \c E matches <tt>C::case_\<boost::result_of\<T(E)\>::type\></tt>.
474 /// (Note: T defaults to <tt>tag_of\<_\>()</tt>.)
476 /// A terminal expression <tt>expr\<AT,term\<A\> \></tt> matches
477 /// a grammar <tt>expr\<BT,term\<B\> \></tt> if \c BT is \c AT or
478 /// \c proto::_ and if one of the following is true:
480 /// \li \c B is the wildcard pattern, \c _
482 /// \li \c A is <tt>B &</tt>
483 /// \li \c A is <tt>B const &</tt>
484 /// \li \c B is <tt>exact\<A\></tt>
485 /// \li \c B is <tt>convertible_to\<X\></tt> and
486 /// <tt>is_convertible\<A,X\>::value</tt> is \c true.
487 /// \li \c A is <tt>X[M]</tt> or <tt>X(&)[M]</tt> and
488 /// \c B is <tt>X[proto::N]</tt>.
489 /// \li \c A is <tt>X(&)[M]</tt> and \c B is <tt>X(&)[proto::N]</tt>.
490 /// \li \c A is <tt>X[M]</tt> or <tt>X(&)[M]</tt> and
491 /// \c B is <tt>X*</tt>.
492 /// \li \c B lambda-matches \c A (see below).
494 /// A type \c B lambda-matches \c A if one of the following is true:
497 /// \li \c B is the wildcard pattern, \c _
498 /// \li \c B is <tt>T\<B0,B1,...Bn\></tt> and \c A is
499 /// <tt>T\<A0,A1,...An\></tt> and for each \c x in
500 /// <tt>[0,n)</tt>, \c Ax and \c Bx are types
501 /// such that \c Ax lambda-matches \c Bx
502 template<typename Expr, typename Grammar>
505 typename Expr::proto_derived_expr
506 , typename Expr::proto_grammar
507 , typename Grammar::proto_grammar
513 template<typename Expr, typename Grammar>
514 struct matches<Expr &, Grammar>
516 typename Expr::proto_derived_expr
517 , typename Expr::proto_grammar
518 , typename Grammar::proto_grammar
522 /// \brief A wildcard grammar element that matches any expression,
523 /// and a transform that returns the current expression unchanged.
525 /// The wildcard type, \c _, is a grammar element such that
526 /// <tt>matches\<E,_\>::value</tt> is \c true for any expression
529 /// The wildcard can also be used as a stand-in for a template
530 /// argument when matching terminals. For instance, the following
531 /// is a grammar that will match any <tt>std::complex\<\></tt>
535 /// BOOST_MPL_ASSERT((
537 /// terminal<std::complex<double> >::type
538 /// , terminal<std::complex< _ > >
543 /// When used as a transform, \c _ returns the current expression
544 /// unchanged. For instance, in the following, \c _ is used with
545 /// the \c fold\<\> transform to fold the children of a node:
548 /// struct CountChildren
550 /// // Terminals have no children
551 /// when<terminal<_>, mpl::int_<0>()>
552 /// // Use fold<> to count the children of non-terminals
555 /// _ // <-- fold the current expression
557 /// , mpl::plus<_state, mpl::int_<1> >()
563 struct _ : transform<_>
565 typedef _ proto_grammar;
567 template<typename Expr, typename State, typename Data>
568 struct impl : transform_impl<Expr, State, Data>
570 typedef Expr result_type;
572 /// \param expr An expression
574 BOOST_PROTO_RETURN_TYPE_STRICT_LOOSE(result_type, typename impl::expr_param)
576 typename impl::expr_param e
577 , typename impl::state_param
578 , typename impl::data_param
588 template<typename Expr, typename State, typename Data>
589 struct _and_impl<proto::and_<>, Expr, State, Data>
590 : proto::_::impl<Expr, State, Data>
593 template<typename G0, typename Expr, typename State, typename Data>
594 struct _and_impl<proto::and_<G0>, Expr, State, Data>
595 : proto::when<proto::_, G0>::template impl<Expr, State, Data>
599 /// \brief Inverts the set of expressions matched by a grammar. When
600 /// used as a transform, \c not_\<\> returns the current expression
603 /// If an expression type \c E does not match a grammar \c G, then
604 /// \c E \e does match <tt>not_\<G\></tt>. For example,
605 /// <tt>not_\<terminal\<_\> \></tt> will match any non-terminal.
606 template<typename Grammar>
607 struct not_ : transform<not_<Grammar> >
609 typedef not_ proto_grammar;
611 template<typename Expr, typename State, typename Data>
612 struct impl : transform_impl<Expr, State, Data>
614 typedef Expr result_type;
616 /// \param e An expression
617 /// \pre <tt>matches\<Expr,not_\>::value</tt> is \c true.
619 BOOST_PROTO_RETURN_TYPE_STRICT_LOOSE(result_type, typename impl::expr_param)
621 typename impl::expr_param e
622 , typename impl::state_param
623 , typename impl::data_param
631 /// \brief Used to select one grammar or another based on the result
632 /// of a compile-time Boolean. When used as a transform, \c if_\<\>
633 /// selects between two transforms based on a compile-time Boolean.
635 /// When <tt>if_\<If,Then,Else\></tt> is used as a grammar, \c If
636 /// must be a Proto transform and \c Then and \c Else must be grammars.
637 /// An expression type \c E matches <tt>if_\<If,Then,Else\></tt> if
638 /// <tt>boost::result_of\<when\<_,If\>(E,int,int)\>::type::value</tt>
639 /// is \c true and \c E matches \c U; or, if
640 /// <tt>boost::result_of\<when\<_,If\>(E,int,int)\>::type::value</tt>
641 /// is \c false and \c E matches \c V.
643 /// The template parameter \c Then defaults to \c _
644 /// and \c Else defaults to \c not\<_\>, so an expression type \c E
645 /// will match <tt>if_\<If\></tt> if and only if
646 /// <tt>boost::result_of\<when\<_,If\>(E,int,int)\>::type::value</tt>
650 /// // A grammar that only matches integral terminals,
651 /// // using is_integral<> from Boost.Type_traits.
652 /// struct IsIntegral
655 /// , if_< is_integral<_value>() >
660 /// When <tt>if_\<If,Then,Else\></tt> is used as a transform, \c If,
661 /// \c Then and \c Else must be Proto transforms. When applying
662 /// the transform to an expression \c E, state \c S and data \c V,
663 /// if <tt>boost::result_of\<when\<_,If\>(E,S,V)\>::type::value</tt>
664 /// is \c true then the \c Then transform is applied; otherwise
665 /// the \c Else transform is applied.
668 /// // Match a terminal. If the terminal is integral, return
669 /// // mpl::true_; otherwise, return mpl::false_.
670 /// struct IsIntegral2
674 /// is_integral<_value>()
683 , typename Then // = _
684 , typename Else // = not_<_>
686 struct if_ : transform<if_<If, Then, Else> >
688 typedef if_ proto_grammar;
690 template<typename Expr, typename State, typename Data>
691 struct impl : transform_impl<Expr, State, Data>
694 typename when<_, If>::template impl<Expr, State, Data>::result_type
699 static_cast<bool>(remove_reference<condition>::type::value)
705 typedef typename which::template impl<Expr, State, Data>::result_type result_type;
707 /// \param e An expression
708 /// \param s The current state
709 /// \param d A data of arbitrary type
710 /// \return <tt>which::impl<Expr, State, Data>()(e, s, d)</tt>
711 result_type operator ()(
712 typename impl::expr_param e
713 , typename impl::state_param s
714 , typename impl::data_param d
717 return typename which::template impl<Expr, State, Data>()(e, s, d);
722 /// \brief For matching one of a set of alternate grammars. Alternates
723 /// tried in order to avoid ambiguity. When used as a transform, \c or_\<\>
724 /// applies the transform associated with the first grammar that matches
727 /// An expression type \c E matches <tt>or_\<B0,B1,...Bn\></tt> if \c E
728 /// matches any \c Bx for \c x in <tt>[0,n)</tt>.
730 /// When applying <tt>or_\<B0,B1,...Bn\></tt> as a transform with an
731 /// expression \c e of type \c E, state \c s and data \c d, it is
732 /// equivalent to <tt>Bx()(e, s, d)</tt>, where \c x is the lowest
733 /// number such that <tt>matches\<E,Bx\>::value</tt> is \c true.
734 template<BOOST_PROTO_LOGICAL_typename_G>
735 struct or_ : transform<or_<BOOST_PROTO_LOGICAL_G> >
737 typedef or_ proto_grammar;
739 /// \param e An expression
740 /// \param s The current state
741 /// \param d A data of arbitrary type
742 /// \pre <tt>matches\<Expr,or_\>::value</tt> is \c true.
743 /// \return <tt>which()(e, s, d)</tt>, where <tt>which</tt> is the
744 /// sub-grammar that matched <tt>Expr</tt>.
746 template<typename Expr, typename State, typename Data>
749 typename Expr::proto_derived_expr
750 , typename Expr::proto_grammar
752 >::which::template impl<Expr, State, Data>
755 template<typename Expr, typename State, typename Data>
756 struct impl<Expr &, State, Data>
758 typename Expr::proto_derived_expr
759 , typename Expr::proto_grammar
761 >::which::template impl<Expr &, State, Data>
765 /// \brief For matching all of a set of grammars. When used as a
766 /// transform, \c and_\<\> applies the transforms associated with
767 /// the each grammar in the set, and returns the result of the last.
769 /// An expression type \c E matches <tt>and_\<B0,B1,...Bn\></tt> if \c E
770 /// matches all \c Bx for \c x in <tt>[0,n)</tt>.
772 /// When applying <tt>and_\<B0,B1,...Bn\></tt> as a transform with an
773 /// expression \c e, state \c s and data \c d, it is
774 /// equivalent to <tt>(B0()(e, s, d),B1()(e, s, d),...Bn()(e, s, d))</tt>.
775 template<BOOST_PROTO_LOGICAL_typename_G>
776 struct and_ : transform<and_<BOOST_PROTO_LOGICAL_G> >
778 typedef and_ proto_grammar;
780 template<typename Expr, typename State, typename Data>
782 : detail::_and_impl<and_, Expr, State, Data>
786 /// \brief For matching one of a set of alternate grammars, which
787 /// are looked up based on some property of an expression. The
788 /// property on which to dispatch is specified by the \c Transform
789 /// template parameter, which defaults to <tt>tag_of\<_\>()</tt>.
790 /// That is, when the \c Trannsform is not specified, the alternate
791 /// grammar is looked up using the tag type of the current expression.
793 /// When used as a transform, \c switch_\<\> applies the transform
794 /// associated with the grammar that matches the expression.
796 /// \note \c switch_\<\> is functionally identical to \c or_\<\> but
797 /// is often more efficient. It does a fast, O(1) lookup using the
798 /// result of the specified transform to find a sub-grammar that may
799 /// potentially match the expression.
801 /// An expression type \c E matches <tt>switch_\<C,T\></tt> if \c E
802 /// matches <tt>C::case_\<boost::result_of\<T(E)\>::type\></tt>.
804 /// When applying <tt>switch_\<C,T\></tt> as a transform with an
805 /// expression \c e of type \c E, state \c s of type \S and data
806 /// \c d of type \c D, it is equivalent to
807 /// <tt>C::case_\<boost::result_of\<T(E,S,D)\>::type\>()(e, s, d)</tt>.
808 template<typename Cases, typename Transform>
809 struct switch_ : transform<switch_<Cases, Transform> >
811 typedef switch_ proto_grammar;
813 template<typename Expr, typename State, typename Data>
815 : Cases::template case_<
816 typename when<_, Transform>::template impl<Expr, State, Data>::result_type
817 >::template impl<Expr, State, Data>
821 /// INTERNAL ONLY (This is merely a compile-time optimization for the common case)
823 template<typename Cases>
824 struct switch_<Cases> : transform<switch_<Cases> >
826 typedef switch_ proto_grammar;
828 template<typename Expr, typename State, typename Data>
830 : Cases::template case_<typename Expr::proto_tag>::template impl<Expr, State, Data>
833 template<typename Expr, typename State, typename Data>
834 struct impl<Expr &, State, Data>
835 : Cases::template case_<typename Expr::proto_tag>::template impl<Expr &, State, Data>
839 /// \brief For forcing exact matches of terminal types.
841 /// By default, matching terminals ignores references and
842 /// cv-qualifiers. For instance, a terminal expression of
843 /// type <tt>terminal\<int const &\>::type</tt> will match
844 /// the grammar <tt>terminal\<int\></tt>. If that is not
845 /// desired, you can force an exact match with
846 /// <tt>terminal\<exact\<int\> \></tt>. This will only
847 /// match integer terminals where the terminal is held by
853 /// \brief For matching terminals that are convertible to
856 /// Use \c convertible_to\<\> to match a terminal that is
857 /// convertible to some type. For example, the grammar
858 /// <tt>terminal\<convertible_to\<int\> \></tt> will match
859 /// any terminal whose argument is convertible to an integer.
861 /// \note The trait \c is_convertible\<\> from Boost.Type_traits
862 /// is used to determinal convertibility.
864 struct convertible_to
867 /// \brief For matching a Grammar to a variable number of
870 /// An expression type <tt>expr\<AT, listN\<A0,...An,U0,...Um\> \></tt>
871 /// matches a grammar <tt>expr\<BT, listM\<B0,...Bn,vararg\<V\> \> \></tt>
872 /// if \c BT is \c _ or \c AT, and if \c Ax matches \c Bx
873 /// for each \c x in <tt>[0,n)</tt> and if \c Ux matches \c V
874 /// for each \c x in <tt>[0,m)</tt>.
879 /// // Match any function call expression, irregardless
880 /// // of the number of function arguments:
882 /// : function< vararg<_> >
886 /// When used as a transform, <tt>vararg\<G\></tt> applies
887 /// <tt>G</tt>'s transform.
888 template<typename Grammar>
893 typedef void proto_is_vararg_;
898 template<BOOST_PROTO_LOGICAL_typename_G>
899 struct is_callable<or_<BOOST_PROTO_LOGICAL_G> >
905 template<BOOST_PROTO_LOGICAL_typename_G>
906 struct is_callable<and_<BOOST_PROTO_LOGICAL_G> >
912 template<typename Grammar>
913 struct is_callable<not_<Grammar> >
919 template<typename If, typename Then, typename Else>
920 struct is_callable<if_<If, Then, Else> >
926 template<typename Grammar>
927 struct is_callable<vararg<Grammar> >
933 template<typename Cases, typename Transform>
934 struct is_callable<switch_<Cases, Transform> >
940 #undef BOOST_PROTO_LOGICAL_typename_G
941 #undef BOOST_PROTO_LOGICAL_G
943 #if defined(_MSC_VER)
944 # pragma warning(pop)