#ifndef BOOST_PP_IS_ITERATING /////////////////////////////////////////////////////////////////////////////// /// \file matches.hpp /// Contains definition of matches\<\> metafunction for determining if /// a given expression matches a given pattern. // // Copyright 2008 Eric Niebler. Distributed under the Boost // Software License, Version 1.0. (See accompanying file // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) #ifndef BOOST_PROTO_MATCHES_HPP_EAN_11_03_2006 #define BOOST_PROTO_MATCHES_HPP_EAN_11_03_2006 #include // must be first include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include // must be last include // Some compilers (like GCC) need extra help figuring out a template's arity. // I use MPL's BOOST_MPL_AUX_LAMBDA_ARITY_PARAM() macro to disambiguate, which // which is controlled by the BOOST_MPL_LIMIT_METAFUNCTION_ARITY macro. If // You define BOOST_PROTO_MAX_ARITY to be greater than // BOOST_MPL_LIMIT_METAFUNCTION_ARITY on these compilers, things don't work. // You must define BOOST_MPL_LIMIT_METAFUNCTION_ARITY to be greater. #ifdef BOOST_MPL_CFG_EXTENDED_TEMPLATE_PARAMETERS_MATCHING # if BOOST_PROTO_MAX_ARITY > BOOST_MPL_LIMIT_METAFUNCTION_ARITY # error BOOST_MPL_LIMIT_METAFUNCTION_ARITY must be at least as large as BOOST_PROTO_MAX_ARITY # endif #endif #if defined(_MSC_VER) && (_MSC_VER >= 1020) # pragma warning(push) # pragma warning(disable:4305) // 'specialization' : truncation from 'const int' to 'bool' #endif namespace boost { namespace proto { namespace detail { struct ignore; template struct matches_; // and_ and or_ implementation template struct or1 : mpl::bool_ { typedef G0 which; }; template struct and1 : mpl::bool_ {}; template struct and2; template struct last; template struct array_matches : mpl::false_ {}; template struct array_matches : mpl::true_ {}; template struct array_matches : mpl::true_ {}; template struct array_matches : mpl::true_ {}; template::value) > struct lambda_matches : mpl::false_ {}; template struct lambda_matches : mpl::true_ {}; template struct lambda_matches : mpl::true_ {}; template struct lambda_matches : array_matches {}; template struct lambda_matches : mpl::true_ {}; template class T, typename Expr0, typename Grammar0> struct lambda_matches, T BOOST_MPL_AUX_LAMBDA_ARITY_PARAM(1) > : lambda_matches {}; // vararg_matches_impl template struct vararg_matches_impl; // vararg_matches template struct vararg_matches : mpl::false_ {}; template struct vararg_matches : matches_, proto::expr > {}; template struct vararg_matches : and2< matches_, proto::expr >::value , vararg_matches_impl > {}; // How terminal_matches<> handles references and cv-qualifiers. // The cv and ref_ matter *only* if the grammar has a top-level ref_. // // Expr | Grammar | Match // ------------------------------ // T T yes // T & T yes // T const & T yes // T T & no // T & T & yes // T const & T & no // T T const & no // T & T const & no // T const & T const & yes template struct is_cv_ref_compatible : mpl::true_ {}; template struct is_cv_ref_compatible : mpl::false_ {}; template struct is_cv_ref_compatible : mpl::bool_::value == is_const::value> {}; #if BOOST_WORKAROUND(BOOST_MSVC, == 1310) // MSVC-7.1 has lots of problems with array types that have been // deduced. Partially specializing terminal_matches<> on array types // doesn't seem to work. template< typename T , typename U , bool B = is_array::value > struct terminal_array_matches : mpl::false_ {}; template struct terminal_array_matches : is_convertible {}; template struct terminal_array_matches : is_convertible {}; template struct terminal_array_matches : is_convertible {}; // terminal_matches template struct terminal_matches : mpl::or_< mpl::and_< is_cv_ref_compatible , lambda_matches< BOOST_PROTO_UNCVREF(T) , BOOST_PROTO_UNCVREF(U) > > , terminal_array_matches > {}; #else // terminal_matches template struct terminal_matches : mpl::and_< is_cv_ref_compatible , lambda_matches< BOOST_PROTO_UNCVREF(T) , BOOST_PROTO_UNCVREF(U) > > {}; template struct terminal_matches : mpl::true_ {}; template struct terminal_matches : mpl::true_ {}; #endif template struct terminal_matches : mpl::true_ {}; template struct terminal_matches : mpl::true_ {}; template struct terminal_matches : mpl::true_ {}; template struct terminal_matches : mpl::true_ {}; template struct terminal_matches > : mpl::true_ {}; template struct terminal_matches > : is_convertible {}; // matches_ template struct matches_ : mpl::false_ {}; template struct matches_< Expr, proto::_ > : mpl::true_ {}; template struct matches_< proto::expr, proto::expr > : vararg_matches< Args1, Args2, typename Args2::back_, (N1+2 > N2), (N2 > N1) > {}; template struct matches_< proto::expr, proto::expr > : vararg_matches< Args1, Args2, typename Args2::back_, (N1+2 > N2), (N2 > N1) > {}; template struct matches_< proto::expr, proto::expr > : mpl::false_ {}; template struct matches_< proto::expr, proto::expr > : matches_ {}; template struct matches_< proto::expr, proto::expr > : matches_ {}; template struct matches_< proto::expr, proto::expr > : terminal_matches {}; #define BOOST_PROTO_MATCHES_N_FUN(z, n, data)\ matches_<\ typename Args1::BOOST_PP_CAT(arg, n)::proto_base_expr\ , typename Args2::BOOST_PP_CAT(arg, n)::proto_base_expr\ > #define BOOST_PROTO_DEFINE_MATCHES(z, n, data)\ matches_<\ typename Expr::proto_base_expr\ , typename BOOST_PP_CAT(G, n)::proto_base_expr\ > #define BOOST_PROTO_DEFINE_LAMBDA_MATCHES(z, n, data)\ lambda_matches<\ BOOST_PP_CAT(Expr, n)\ , BOOST_PP_CAT(Grammar, n)\ > #if BOOST_PROTO_MAX_LOGICAL_ARITY > BOOST_PROTO_MAX_ARITY #define BOOST_PP_ITERATION_PARAMS_1 (4, (2, BOOST_PROTO_MAX_LOGICAL_ARITY, , 1)) #else #define BOOST_PP_ITERATION_PARAMS_1 (4, (2, BOOST_PROTO_MAX_ARITY, , 1)) #endif #include BOOST_PP_ITERATE() #define BOOST_PP_ITERATION_PARAMS_1 (4, (2, BOOST_PROTO_MAX_ARITY, , 2)) #include BOOST_PP_ITERATE() #undef BOOST_PROTO_MATCHES_N_FUN #undef BOOST_PROTO_DEFINE_MATCHES #undef BOOST_PROTO_DEFINE_LAMBDA_MATCHES // handle proto::if_ template struct matches_ > : mpl::eval_if< typename when<_, If>::template result::type , matches_ , matches_ >::type {}; template struct matches_ > : when<_, If>::template result::type {}; // handle proto::not_ template struct matches_ > : mpl::not_ > {}; // handle proto::switch_ template struct matches_ > : matches_< Expr , typename Cases::template case_::proto_base_expr > {}; } namespace result_of { /// \brief A Boolean metafunction that evaluates whether a given /// expression type matches a grammar. /// /// matches\ inherits (indirectly) from /// \c mpl::true_ if Expr::proto_base_expr matches /// Grammar::proto_base_expr, and from \c mpl::false_ /// otherwise. /// /// Non-terminal expressions are matched against a grammar /// according to the following rules: /// /// \li The wildcard pattern, \c _, matches any expression. /// \li An expression expr\ \> /// matches a grammar expr\ \> /// if \c BT is \c _ or \c AT, and if \c Ax matches \c Bx for /// each \c x in [0,n). /// \li An expression expr\ \> /// matches a grammar expr\ \> \> /// if \c BT is \c _ or \c AT, and if \c Ax matches \c Bx /// for each \c x in [0,n) and if \c Ux matches \c V /// for each \c x in [0,m). /// \li An expression \c E matches or_\ if \c E /// matches some \c Bx for \c x in [0,n). /// \li An expression \c E matches and_\ if \c E /// matches all \c Bx for \c x in [0,n). /// \li An expression \c E matches if_\ if /// when\<_,T\>::::result\::::type::value /// is \c true and \c E matches \c U; or, if /// when\<_,T\>::::result\::::type::value /// is \c false and \c E matches \c V. (Note: \c U defaults to \c _ /// and \c V defaults to \c not_\<_\>.) /// \li An expression \c E matches not_\ if \c E does /// not match \c T. /// \li An expression \c E matches switch_\ if /// \c E matches C::case_\. /// /// A terminal expression expr\ \> matches /// a grammar expr\ \> if \c BT is \c _ or /// \c tag::terminal and one of the following is true: /// /// \li \c B is the wildcard pattern, \c _ /// \li \c A is \c B /// \li \c A is B & /// \li \c A is B const & /// \li \c B is exact\ /// \li \c B is convertible_to\ and /// is_convertible\::::value is \c true. /// \li \c A is X[M] or X(&)[M] and /// \c B is X[proto::N]. /// \li \c A is X(&)[M] and \c B is X(&)[proto::N]. /// \li \c A is X[M] or X(&)[M] and /// \c B is X*. /// \li \c B lambda-matches \c A (see below). /// /// A type \c B lambda-matches \c A if one of the following is true: /// /// \li \c B is \c A /// \li \c B is the wildcard pattern, \c _ /// \li \c B is T\ and \c A is /// T\ and for each \c x in /// [0,n), \c Ax and \c Bx are types /// such that \c Ax lambda-matches \c Bx template struct matches : detail::matches_< typename Expr::proto_base_expr , typename Grammar::proto_base_expr > {}; } namespace wildcardns_ { /// \brief A wildcard grammar element that matches any expression, /// and a transform that returns the current expression unchanged. /// /// The wildcard type, \c _, is a grammar element such that /// matches\::::value is \c true for any expression /// type \c E. /// /// The wildcard can also be used as a stand-in for a template /// argument when matching terminals. For instance, the following /// is a grammar that will match any std::complex\<\> /// terminal: /// /// \code /// BOOST_MPL_ASSERT(( /// matches< /// terminal >::type /// , terminal > /// > /// )); /// \endcode /// /// When used as a transform, \c _ returns the current expression /// unchanged. For instance, in the following, \c _ is used with /// the \c fold\<\> transform to fold the children of a node: /// /// \code /// struct CountChildren /// : or_< /// // Terminals have no children /// when, mpl::int_<0>()> /// // Use fold<> to count the children of non-terminals /// , otherwise< /// fold< /// _ // <-- fold the current expression /// , mpl::int_<0>() /// , mpl::plus<_state, mpl::int_<1> >() /// > /// > /// > /// {}; /// \endcode struct _ : proto::callable { typedef _ proto_base_expr; template struct result; template struct result { typedef Expr type; }; /// \param expr An expression /// \return \c expr template Expr const &operator ()(Expr const &expr, State const &, Visitor &) const { return expr; } }; } namespace control { /// \brief Inverts the set of expressions matched by a grammar. When /// used as a transform, \c not_\<\> returns the current expression /// unchanged. /// /// If an expression type \c E does not match a grammar \c G, then /// \c E \e does match not_\. For example, /// not_\ \> will match any non-terminal. template struct not_ : proto::callable { typedef not_ proto_base_expr; template struct result; template struct result { typedef Expr type; }; /// \param expr An expression /// \pre matches\::::value is \c true. /// \return \c expr template Expr const &operator ()(Expr const &expr, State const &, Visitor &) const { return expr; } }; /// \brief Used to select one grammar or another based on the result /// of a compile-time Boolean. When used as a transform, \c if_\<\> /// selects between two transforms based on a compile-time Boolean. /// /// When if_\ is used as a grammar, \c If /// must be a Proto transform and \c Then and \c Else must be grammars. /// An expression type \c E matches if_\ if /// when\<_,If\>::::result\::::type::value /// is \c true and \c E matches \c U; or, if /// when\<_,If\>::::result\::::type::value /// is \c false and \c E matches \c V. /// /// The template parameter \c Then defaults to \c _ /// and \c Else defaults to \c not\<_\>, so an expression type \c E /// will match if_\ if and only if /// when\<_,If\>::::result\::::type::value /// is \c true. /// /// \code /// // A grammar that only matches integral terminals, /// // using is_integral<> from Boost.Type_traits. /// struct IsIntegral /// : and_< /// terminal<_> /// , if_< is_integral<_arg>() > /// > /// {}; /// \endcode /// /// When if_\ is used as a transform, \c If, /// \c Then and \c Else must be Proto transforms. When applying /// the transform to an expression \c E, state \c S and visitor \c V, /// if when\<_,If\>::::result\::::type::value /// is \c true then the \c Then transform is applied; otherwise /// the \c Else transform is applied. /// /// \code /// // Match a terminal. If the terminal is integral, return /// // mpl::true_; otherwise, return mpl::false_. /// struct IsIntegral2 /// : when< /// terminal<_> /// , if_< /// is_integral<_arg>() /// , mpl::true_() /// , mpl::false_() /// > /// > /// {}; /// \endcode template< typename If , typename Then BOOST_PROTO_FOR_DOXYGEN_ONLY(= _) , typename Else BOOST_PROTO_FOR_DOXYGEN_ONLY(= not_<_>) > struct if_ : proto::callable { typedef if_ proto_base_expr; template struct result; template struct result { typedef typename when<_, If>::template result::type condition; typedef typename mpl::if_< condition , when<_, Then> , when<_, Else> >::type which; typedef typename which::template result::type type; }; /// \param expr An expression /// \param state The current state /// \param visitor A visitor of arbitrary type /// \return result\::::which()(expr, state, visitor) template typename result::type operator ()(Expr const &expr, State const &state, Visitor &visitor) const { typedef typename result::which which; return which()(expr, state, visitor); } }; /// \brief For matching one of a set of alternate grammars. Alternates /// tried in order to avoid ambiguity. When used as a transform, \c or_\<\> /// applies the transform associated with the first grammar that matches /// the expression. /// /// An expression type \c E matches or_\ if \c E /// matches any \c Bx for \c x in [0,n). /// /// When applying or_\ as a transform with an /// expression \c e of type \c E, state \c s and visitor \c v, it is /// equivalent to Bx()(e, s, v), where \c x is the lowest /// number such that matches\::::value is \c true. template struct or_ : proto::callable { typedef or_ proto_base_expr; template struct result; template struct result { typedef typename detail::matches_::which which; typedef typename which::template result::type type; }; /// \param expr An expression /// \param state The current state /// \param visitor A visitor of arbitrary type /// \pre matches\::::value is \c true. /// \return result\::::which()(expr, state, visitor) template typename result::type operator ()(Expr const &expr, State const &state, Visitor &visitor) const { typedef typename detail::matches_::which which; return which()(expr, state, visitor); } }; /// \brief For matching all of a set of grammars. When used as a /// transform, \c and_\<\> applies the transform associated with /// the last grammar in the set. /// /// An expression type \c E matches and_\ if \c E /// matches all \c Bx for \c x in [0,n). /// /// When applying and_\ as a transform with an /// expression \c e, state \c s and visitor \c v, it is /// equivalent to Bn()(e, s, v). template struct and_ : proto::callable { typedef and_ proto_base_expr; template struct result; template struct result { typedef typename detail::last::type which; typedef typename which::template result::type type; }; /// \param expr An expression /// \param state The current state /// \param visitor A visitor of arbitrary type /// \pre matches\::::value is \c true. /// \return result\::::which()(expr, state, visitor) template typename result::type operator ()(Expr const &expr, State const &state, Visitor &visitor) const { typedef typename detail::last::type which; return which()(expr, state, visitor); } }; /// \brief For matching one of a set of alternate grammars, which /// are looked up based on an expression's tag type. When used as a /// transform, \c switch_\<\> applies the transform associated with /// the grammar that matches the expression. /// /// \note \c switch_\<\> is functionally identical to \c or_\<\> but /// is often more efficient. It does a fast, O(1) lookup based on an /// expression's tag type to find a sub-grammar that may potentially /// match the expression. /// /// An expression type \c E matches switch_\ if \c E /// matches C::case_\. /// /// When applying switch_\ as a transform with an /// expression \c e of type \c E, state \c s and visitor \c v, it is /// equivalent to C::case_\()(e, s, v). template struct switch_ : proto::callable { typedef switch_ proto_base_expr; template struct result; template struct result { typedef typename Cases::template case_ which; typedef typename which::template result::type type; }; /// \param expr An expression /// \param state The current state /// \param visitor A visitor of arbitrary type /// \pre matches\::::value is \c true. /// \return result\::::which()(expr, state, visitor) template typename result::type operator ()(Expr const &expr, State const &state, Visitor &visitor) const { typedef typename Cases::template case_ which; return which()(expr, state, visitor); } }; /// \brief For forcing exact matches of terminal types. /// /// By default, matching terminals ignores references and /// cv-qualifiers. For instance, a terminal expression of /// type terminal\::::type will match /// the grammar terminal\. If that is not /// desired, you can force an exact match with /// terminal\ \>. This will only /// match integer terminals where the terminal is held by /// value. template struct exact {}; /// \brief For matching terminals that are convertible to /// a type. /// /// Use \c convertible_to\<\> to match a terminal that is /// convertible to some type. For example, the grammar /// terminal\ \> will match /// any terminal whose argument is convertible to an integer. /// /// \note The trait \c is_convertible\<\> from Boost.Type_traits /// is used to determinal convertibility. template struct convertible_to {}; /// \brief For matching a Grammar to a variable number of /// sub-expressions. /// /// An expression type expr\ \> /// matches a grammar expr\ \> \> /// if \c BT is \c _ or \c AT, and if \c Ax matches \c Bx /// for each \c x in [0,n) and if \c Ux matches \c V /// for each \c x in [0,m). /// /// For example: /// /// \code /// // Match any function call expression, irregardless /// // of the number of function arguments: /// struct Function /// : function< vararg<_> > /// {}; /// \endcode /// /// When used as a transform, vararg\ applies /// G's transform. template struct vararg : Grammar { typedef void proto_is_vararg_; }; } /// INTERNAL ONLY /// template struct is_callable > : mpl::true_ {}; /// INTERNAL ONLY /// template struct is_callable > : mpl::true_ {}; /// INTERNAL ONLY /// template struct is_callable > : mpl::true_ {}; /// INTERNAL ONLY /// template struct is_callable > : mpl::true_ {}; /// INTERNAL ONLY /// template struct is_callable > : mpl::true_ {}; }} #if defined(_MSC_VER) && (_MSC_VER >= 1020) # pragma warning(pop) #endif #endif #elif BOOST_PP_ITERATION_FLAGS() == 1 #define N BOOST_PP_ITERATION() template struct BOOST_PP_CAT(and, N) : BOOST_PP_CAT(and, BOOST_PP_DEC(N))< P0::value BOOST_PP_COMMA_IF(BOOST_PP_SUB(N,2)) BOOST_PP_ENUM_SHIFTED_PARAMS(BOOST_PP_DEC(N), P) > {}; template struct BOOST_PP_CAT(and, N) : mpl::false_ {}; #if N <= BOOST_PROTO_MAX_LOGICAL_ARITY template struct last > { typedef BOOST_PP_CAT(G, BOOST_PP_DEC(N)) type; }; template struct BOOST_PP_CAT(or, N) : BOOST_PP_CAT(or, BOOST_PP_DEC(N))< matches_::value , Expr, BOOST_PP_ENUM_SHIFTED_PARAMS(N, G) > {}; template struct BOOST_PP_CAT(or, N) : mpl::true_ { typedef G0 which; }; // handle proto::or_ template struct matches_ > : BOOST_PP_CAT(or, N)< matches_::value, typename Expr::proto_base_expr, BOOST_PP_ENUM_PARAMS(N, G) > {}; // handle proto::and_ template struct matches_ > : detail::BOOST_PP_CAT(and, N)< BOOST_PROTO_DEFINE_MATCHES(~, 0, ~)::value, BOOST_PP_ENUM_SHIFTED(N, BOOST_PROTO_DEFINE_MATCHES, ~) > {}; #endif #undef N #elif BOOST_PP_ITERATION_FLAGS() == 2 #define N BOOST_PP_ITERATION() template struct vararg_matches_impl : and2< matches_::value , vararg_matches_impl > {}; template struct vararg_matches_impl : matches_ {}; template< template class T BOOST_PP_ENUM_TRAILING_PARAMS(N, typename Expr) BOOST_PP_ENUM_TRAILING_PARAMS(N, typename Grammar) > struct lambda_matches, T BOOST_MPL_AUX_LAMBDA_ARITY_PARAM(N) > : BOOST_PP_CAT(and, N)< BOOST_PROTO_DEFINE_LAMBDA_MATCHES(~, 0, ~)::value, BOOST_PP_ENUM_SHIFTED(N, BOOST_PROTO_DEFINE_LAMBDA_MATCHES, ~) > {}; template struct matches_< proto::expr, proto::expr > : BOOST_PP_CAT(and, N)< BOOST_PROTO_MATCHES_N_FUN(~, 0, ~)::value, BOOST_PP_ENUM_SHIFTED(N, BOOST_PROTO_MATCHES_N_FUN, ~) > {}; template struct matches_< proto::expr, proto::expr > : BOOST_PP_CAT(and, N)< BOOST_PROTO_MATCHES_N_FUN(~, 0, ~)::value, BOOST_PP_ENUM_SHIFTED(N, BOOST_PROTO_MATCHES_N_FUN, ~) > {}; #undef N #endif