1 ///////////////////////////////////////////////////////////////////////////////
4 // Copyright 2008 Eric Niebler.
5 // Copyright 2008 David Jenkins.
7 // Distributed under the Boost Software License, Version 1.0. (See
8 // accompanying file LICENSE_1_0.txt or copy at
9 // http://www.boost.org/LICENSE_1_0.txt)
11 #ifndef BOOST_XPRESSIVE_DETAIL_CORE_MATCHER_ACTION_MATCHER_HPP_EAN_10_04_2005
12 #define BOOST_XPRESSIVE_DETAIL_CORE_MATCHER_ACTION_MATCHER_HPP_EAN_10_04_2005
14 // MS compatible compilers support #pragma once
15 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
19 #include <boost/config.hpp>
20 #include <boost/version.hpp>
21 #include <boost/ref.hpp>
22 #include <boost/assert.hpp>
23 #include <boost/mpl/if.hpp>
24 #include <boost/throw_exception.hpp>
25 #include <boost/utility/result_of.hpp>
26 #include <boost/type_traits/is_const.hpp>
27 #include <boost/type_traits/remove_reference.hpp>
28 #include <boost/xpressive/detail/detail_fwd.hpp>
29 #include <boost/xpressive/detail/core/quant_style.hpp>
30 #include <boost/xpressive/detail/core/action.hpp>
31 #include <boost/xpressive/detail/core/state.hpp>
32 #include <boost/proto/core.hpp>
33 #include <boost/proto/context.hpp>
34 #include <boost/xpressive/match_results.hpp> // for type_info_less
35 #include <boost/xpressive/detail/static/transforms/as_action.hpp> // for 'read_attr'
36 #if BOOST_VERSION >= 103500
37 # include <boost/proto/fusion.hpp>
38 # include <boost/fusion/include/transform_view.hpp>
39 # include <boost/fusion/include/invoke.hpp>
40 # include <boost/fusion/include/push_front.hpp>
41 # include <boost/fusion/include/pop_front.hpp>
46 #pragma warning(disable : 4510) // default constructor could not be generated
47 #pragma warning(disable : 4512) // assignment operator could not be generated
48 #pragma warning(disable : 4610) // can never be instantiated - user defined constructor required
51 namespace boost { namespace xpressive { namespace detail
54 #if BOOST_VERSION >= 103500
56 : proto::mem_ptr<proto::_, proto::terminal<proto::_> >
59 template<typename Expr, long N>
62 typename proto::result_of::child_c<Expr &, N>::type
66 ///////////////////////////////////////////////////////////////////////////////
68 // Rewrites expressions of the form x->*foo(a) into foo(x, a) and then
70 template<typename Expr, typename Context, bool IsDataMember = proto::matches<Expr, DataMember>::value>
73 typedef typename child_<Expr, 0>::type left_type;
74 typedef typename child_<Expr, 1>::type right_type;
77 typename proto::result_of::value<
78 typename proto::result_of::child_c<right_type, 0>::type
83 fusion::transform_view<
84 typename fusion::result_of::push_front<
85 typename fusion::result_of::pop_front<right_type>::type const
86 , reference_wrapper<left_type>
88 , proto::eval_fun<Context>
93 typename fusion::result_of::invoke<function_type, evaluated_args>::type
96 result_type operator()(Expr &expr, Context &ctx) const
98 return fusion::invoke<function_type>(
99 proto::value(proto::child_c<0>(proto::right(expr)))
101 fusion::push_front(fusion::pop_front(proto::right(expr)), boost::ref(proto::left(expr)))
102 , proto::eval_fun<Context>(ctx)
108 ///////////////////////////////////////////////////////////////////////////////
110 // Rewrites expressions of the form x->*foo into foo(x) and then
112 template<typename Expr, typename Context>
113 struct mem_ptr_eval<Expr, Context, true>
115 typedef typename child_<Expr, 0>::type left_type;
116 typedef typename child_<Expr, 1>::type right_type;
119 typename proto::result_of::value<right_type>::type
122 typedef typename boost::result_of<
123 function_type(typename proto::result_of::eval<left_type, Context>::type)
126 result_type operator()(Expr &expr, Context &ctx) const
128 return proto::value(proto::right(expr))(
129 proto::eval(proto::left(expr), ctx)
135 struct attr_with_default_tag
141 ///////////////////////////////////////////////////////////////////////////////
144 struct action_context
146 explicit action_context(action_args_type *action_args)
147 : action_args_(action_args)
150 action_args_type const &args() const
152 return *this->action_args_;
156 template<typename Expr, typename Arg>
158 : proto::default_eval<Expr, action_context const>
161 template<typename Expr, typename Arg>
162 struct eval_terminal<Expr, reference_wrapper<Arg> >
164 typedef Arg &result_type;
165 result_type operator()(Expr &expr, action_context const &) const
167 return proto::value(expr).get();
171 template<typename Expr, typename Arg>
172 struct eval_terminal<Expr, opt<Arg> >
174 typedef Arg const &result_type;
175 result_type operator()(Expr &expr, action_context const &) const
177 return proto::value(expr);
181 template<typename Expr, typename Type, typename Int>
182 struct eval_terminal<Expr, action_arg<Type, Int> >
184 typedef typename action_arg<Type, Int>::reference result_type;
185 result_type operator()(Expr &expr, action_context const &ctx) const
187 action_args_type::const_iterator where_ = ctx.args().find(&typeid(proto::value(expr)));
188 if(where_ == ctx.args().end())
190 BOOST_THROW_EXCEPTION(
192 regex_constants::error_badarg
193 , "An argument to an action was unspecified"
197 return proto::value(expr).cast(where_->second);
202 template<typename Expr, typename Tag = typename Expr::proto_tag>
204 : proto::default_eval<Expr, action_context const>
207 template<typename Expr>
208 struct eval<Expr, proto::tag::terminal>
209 : eval_terminal<Expr, typename proto::result_of::value<Expr>::type>
212 // Evaluate attributes like a1|42
213 template<typename Expr>
214 struct eval<Expr, attr_with_default_tag>
217 typename proto::result_of::value<
218 typename proto::result_of::left<
219 typename proto::result_of::child<
226 typedef typename temp_type::type result_type;
228 result_type operator ()(Expr const &expr, action_context const &ctx) const
230 return proto::value(proto::left(proto::child(expr))).t_
231 ? *proto::value(proto::left(proto::child(expr))).t_
232 : proto::eval(proto::right(proto::child(expr)), ctx);
236 #if BOOST_VERSION >= 103500
237 template<typename Expr>
238 struct eval<Expr, proto::tag::mem_ptr>
239 : mem_ptr_eval<Expr, action_context const>
244 action_args_type *action_args_;
247 ///////////////////////////////////////////////////////////////////////////////
250 template<typename Actor>
254 action(Actor const &actor)
260 virtual void execute(action_args_type *action_args) const
262 action_context const ctx(action_args);
263 proto::eval(this->actor_, ctx);
270 ///////////////////////////////////////////////////////////////////////////////
273 struct subreg_transform : proto::transform<subreg_transform>
275 template<typename Expr, typename State, typename Data>
276 struct impl : proto::transform_impl<Expr, State, Data>
278 typedef typename impl::state state_type;
281 typename proto::terminal<sub_match<typename state_type::iterator> >::type
284 result_type operator ()(
285 typename impl::expr_param
286 , typename impl::state_param state
287 , typename impl::data_param data
290 return result_type::make(state.sub_matches_[ data ]);
295 ///////////////////////////////////////////////////////////////////////////////
298 struct mark_transform : proto::transform<mark_transform>
300 template<typename Expr, typename State, typename Data>
301 struct impl : proto::transform_impl<Expr, State, Data>
303 typedef typename impl::state state_type;
305 typename proto::terminal<sub_match<typename state_type::iterator> >::type
308 result_type operator ()(
309 typename impl::expr_param expr
310 , typename impl::state_param state
311 , typename impl::data_param
314 return result_type::make(state.sub_matches_[ proto::value(expr).mark_number_ ]);
319 ///////////////////////////////////////////////////////////////////////////////
326 typedef T const &reference;
332 operator reference() const
334 BOOST_XPR_ENSURE_(0 != this->t_, regex_constants::error_badattr, "Use of uninitialized regex attribute");
341 ///////////////////////////////////////////////////////////////////////////////
344 struct attr_transform : proto::transform<attr_transform>
346 template<typename Expr, typename State, typename Data>
347 struct impl : proto::transform_impl<Expr, State, Data>
349 typedef typename impl::expr expr_type;
352 typename expr_type::proto_child0::matcher_type::value_type::second_type
356 typename proto::terminal<opt<attr_type> >::type
359 result_type operator ()(
360 typename impl::expr_param
361 , typename impl::state_param state
362 , typename impl::data_param
365 int slot = typename expr_type::proto_child0::nbr_type();
366 attr_type const *attr = static_cast<attr_type const *>(state.attr_context_.attr_slots_[slot-1]);
367 return result_type::make(opt<attr_type>(attr));
372 ///////////////////////////////////////////////////////////////////////////////
373 // attr_with_default_transform
375 template<typename Grammar, typename Callable = proto::callable>
376 struct attr_with_default_transform : proto::transform<attr_with_default_transform<Grammar, Callable> >
378 template<typename Expr, typename State, typename Data>
379 struct impl : proto::transform_impl<Expr, State, Data>
382 typename proto::unary_expr<
383 attr_with_default_tag
384 , typename Grammar::template impl<Expr, State, Data>::result_type
388 result_type operator ()(
389 typename impl::expr_param expr
390 , typename impl::state_param state
391 , typename impl::data_param data
395 typename Grammar::template impl<Expr, State, Data>()(expr, state, data)
402 ///////////////////////////////////////////////////////////////////////////////
405 struct by_ref_transform : proto::transform<by_ref_transform>
407 template<typename Expr, typename State, typename Data>
408 struct impl : proto::transform_impl<Expr, State, Data>
411 typename proto::result_of::value<typename impl::expr_param>::type
415 typename proto::terminal<reference>::type
418 result_type operator ()(
419 typename impl::expr_param expr
420 , typename impl::state_param
421 , typename impl::data_param
424 return result_type::make(proto::value(expr));
429 ///////////////////////////////////////////////////////////////////////////////
432 struct BindActionArgs
434 proto::when<proto::terminal<any_matcher>, subreg_transform>
435 , proto::when<proto::terminal<mark_placeholder>, mark_transform>
436 , proto::when<proto::terminal<read_attr<proto::_, proto::_> >, attr_transform>
437 , proto::when<proto::terminal<proto::_>, by_ref_transform>
439 proto::bitwise_or<proto::terminal<read_attr<proto::_, proto::_> >, BindActionArgs>
440 , attr_with_default_transform<proto::bitwise_or<attr_transform, BindActionArgs> >
442 , proto::otherwise<proto::nary_expr<proto::_, proto::vararg<BindActionArgs> > >
446 ///////////////////////////////////////////////////////////////////////////////
449 template<typename Actor>
450 struct action_matcher
451 : quant_style<quant_none, 0, false>
456 action_matcher(Actor const &actor, int sub)
462 template<typename BidiIter, typename Next>
463 bool match(match_state<BidiIter> &state, Next const &next) const
465 // Bind the arguments
467 typename boost::result_of<BindActionArgs(
469 , match_state<BidiIter> &
474 action<action_type> actor(BindActionArgs()(this->actor_, state, this->sub_));
476 // Put the action in the action list
477 actionable const **action_list_tail = state.action_list_tail_;
478 *state.action_list_tail_ = &actor;
479 state.action_list_tail_ = &actor.next;
481 // Match the rest of the pattern
482 if(next.match(state))
487 BOOST_ASSERT(0 == actor.next);
488 // remove action from list
489 *action_list_tail = 0;
490 state.action_list_tail_ = action_list_tail;