Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r66196 - in trunk/boost/proto: . transform
From: eric_at_[hidden]
Date: 2010-10-25 19:55:25


Author: eric_niebler
Date: 2010-10-25 19:55:22 EDT (Mon, 25 Oct 2010)
New Revision: 66196
URL: http://svn.boost.org/trac/boost/changeset/66196

Log:
add when<T, external> to allow transforms to be specified with the Data parameter
Text files modified:
   trunk/boost/proto/proto_fwd.hpp | 2
   trunk/boost/proto/transform/when.hpp | 87 ++++++++++++++++++++++++++++++++++++++++
   2 files changed, 89 insertions(+), 0 deletions(-)

Modified: trunk/boost/proto/proto_fwd.hpp
==============================================================================
--- trunk/boost/proto/proto_fwd.hpp (original)
+++ trunk/boost/proto/proto_fwd.hpp 2010-10-25 19:55:22 EDT (Mon, 25 Oct 2010)
@@ -653,6 +653,8 @@
         BOOST_PROTO_CALLABLE()
     };
 
+ struct external;
+
     template<typename PrimitiveTransform, typename X = void>
     struct transform;
 

Modified: trunk/boost/proto/transform/when.hpp
==============================================================================
--- trunk/boost/proto/transform/when.hpp (original)
+++ trunk/boost/proto/transform/when.hpp 2010-10-25 19:55:22 EDT (Mon, 25 Oct 2010)
@@ -14,7 +14,9 @@
     #include <boost/preprocessor/repetition/enum_params.hpp>
     #include <boost/preprocessor/repetition/enum_trailing_params.hpp>
     #include <boost/preprocessor/iteration/iterate.hpp>
+ #include <boost/mpl/at.hpp>
     #include <boost/mpl/if.hpp>
+ #include <boost/mpl/map.hpp>
     #include <boost/proto/proto_fwd.hpp>
     #include <boost/proto/traits.hpp>
     #include <boost/proto/transform/call.hpp>
@@ -55,6 +57,8 @@
         struct when
           : PrimitiveTransform
         {
+ typedef Grammar first;
+ typedef PrimitiveTransform second;
             typedef typename Grammar::proto_grammar proto_grammar;
         };
 
@@ -97,6 +101,87 @@
           : when<_, Fun>
         {};
 
+ /// \brief This specialization uses the Data parameter as a collection
+ /// of actions that can be indexed by the specified rule.
+ ///
+ /// Use <tt>when\<T, external\></tt> in your code when you would like
+ /// to define a grammar once and use it to evaluate expressions with
+ /// many different sets of transforms. The transforms are found by
+ /// using the Data parameter as a map from rules to transforms.
+ ///
+ /// See \c action_map for an example.
+ template<typename Grammar>
+ struct when<Grammar, external>
+ {
+ typedef Grammar first;
+ typedef external second;
+ typedef typename Grammar::proto_grammar proto_grammar;
+
+ template<typename Expr, typename State, typename Data>
+ struct impl
+ : Data::template when<Grammar>::template impl<Expr, State, Data>
+ {};
+
+ template<typename Expr, typename State, typename Data>
+ struct impl<Expr, State, Data &>
+ : Data::template when<Grammar>::template impl<Expr, State, Data &>
+ {};
+ };
+
+ /// \brief For defining a map of Rule/Transform pairs for use with
+ /// <tt>when\<T, external\></tt> to make transforms external to the grammar
+ ///
+ /// The following code defines a grammar with a couple of external transforms.
+ /// It also defines an action_map that maps from rules to transforms. It then
+ /// passes that action_map at the Data parameter to the grammar. In this way,
+ /// the behavior of the grammar can be modified post-hoc by passing a different
+ /// action_map.
+ ///
+ /// \code
+ /// struct int_terminal
+ /// : proto::terminal<int>
+ /// {};
+ ///
+ /// struct char_terminal
+ /// : proto::terminal<char>
+ /// {};
+ ///
+ /// struct my_grammar
+ /// : proto::or_<
+ /// proto::when< int_terminal, proto::external >
+ /// , proto::when< char_terminal, proto::external >
+ /// , proto::when<
+ /// proto::plus< my_grammar, my_grammar >
+ /// , proto::fold< _, int(), my_grammar >
+ /// >
+ /// >
+ /// {};
+ ///
+ /// struct my_actions
+ /// : proto::action_map<
+ /// proto::when<int_terminal, print(proto::_value)>
+ /// , proto::when<char_terminal, print(proto::_value)>
+ /// >
+ /// {};
+ ///
+ /// proto::literal<int> i(1);
+ /// proto::literal<char> c('a');
+ /// my_actions actions;
+ ///
+ /// // Evaluate "i+c" using my_grammar with the specified actions:
+ /// my_grammar()(i + c, 0, actions);
+ /// \endcode
+ template<BOOST_PP_ENUM_PARAMS_WITH_A_DEFAULT(BOOST_MPL_LIMIT_MAP_SIZE, typename T, mpl::na)>
+ struct action_map
+ {
+ typedef mpl::map<BOOST_PP_ENUM_PARAMS(BOOST_MPL_LIMIT_MAP_SIZE, T)> map_type;
+
+ template<typename Rule>
+ struct when
+ : proto::when<_, typename mpl::at<map_type, Rule>::type>
+ {};
+ };
+
         #define BOOST_PP_ITERATION_PARAMS_1 (3, (0, BOOST_PROTO_MAX_ARITY, <boost/proto/transform/when.hpp>))
         #include BOOST_PP_ITERATE()
 
@@ -138,6 +223,8 @@
         struct when<Grammar, R(BOOST_PP_ENUM_PARAMS(N, A))>
           : transform<when<Grammar, R(BOOST_PP_ENUM_PARAMS(N, A))> >
         {
+ typedef Grammar first;
+ typedef R second(BOOST_PP_ENUM_PARAMS(N, A));
             typedef typename Grammar::proto_grammar proto_grammar;
 
             // Note: do not evaluate is_callable<R> in this scope.


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk