predicate_matcher.hpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // predicate_matcher.hpp
  3. //
  4. // Copyright 2008 Eric Niebler. Distributed under the Boost
  5. // Software License, Version 1.0. (See accompanying file
  6. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. #ifndef BOOST_XPRESSIVE_DETAIL_CORE_MATCHER_PREDICATE_MATCHER_HPP_EAN_03_22_2007
  8. #define BOOST_XPRESSIVE_DETAIL_CORE_MATCHER_PREDICATE_MATCHER_HPP_EAN_03_22_2007
  9. // MS compatible compilers support #pragma once
  10. #if defined(_MSC_VER)
  11. # pragma once
  12. #endif
  13. #include <boost/mpl/not.hpp>
  14. #include <boost/mpl/placeholders.hpp>
  15. #include <boost/xpressive/detail/detail_fwd.hpp>
  16. #include <boost/xpressive/detail/core/quant_style.hpp>
  17. #include <boost/xpressive/detail/core/matcher/action_matcher.hpp>
  18. #include <boost/xpressive/detail/core/state.hpp>
  19. #include <boost/proto/core.hpp>
  20. namespace boost { namespace xpressive { namespace detail
  21. {
  22. ///////////////////////////////////////////////////////////////////////////////
  23. // predicate_context
  24. //
  25. template<typename BidiIter>
  26. struct predicate_context
  27. {
  28. explicit predicate_context(int sub, sub_match_impl<BidiIter> const *sub_matches, action_args_type *action_args)
  29. : sub_(sub)
  30. , sub_matches_(sub_matches)
  31. , action_args_(action_args)
  32. {}
  33. action_args_type const &args() const
  34. {
  35. return *this->action_args_;
  36. }
  37. // eval_terminal
  38. template<typename Expr, typename Arg>
  39. struct eval_terminal
  40. : proto::default_eval<Expr, predicate_context const>
  41. {};
  42. template<typename Expr, typename Arg>
  43. struct eval_terminal<Expr, reference_wrapper<Arg> >
  44. {
  45. typedef Arg &result_type;
  46. result_type operator()(Expr &expr, predicate_context const &) const
  47. {
  48. return proto::value(expr).get();
  49. }
  50. };
  51. template<typename Expr>
  52. struct eval_terminal<Expr, any_matcher>
  53. {
  54. typedef sub_match<BidiIter> const &result_type;
  55. result_type operator()(Expr &, predicate_context const &ctx) const
  56. {
  57. return ctx.sub_matches_[ctx.sub_];
  58. }
  59. };
  60. template<typename Expr>
  61. struct eval_terminal<Expr, mark_placeholder>
  62. {
  63. typedef sub_match<BidiIter> const &result_type;
  64. result_type operator()(Expr &expr, predicate_context const &ctx) const
  65. {
  66. return ctx.sub_matches_[proto::value(expr).mark_number_];
  67. }
  68. };
  69. template<typename Expr, typename Type, typename Int>
  70. struct eval_terminal<Expr, action_arg<Type, Int> >
  71. {
  72. typedef typename action_arg<Type, Int>::reference result_type;
  73. result_type operator()(Expr &expr, predicate_context const &ctx) const
  74. {
  75. action_args_type::const_iterator where_ = ctx.args().find(&typeid(proto::value(expr)));
  76. if(where_ == ctx.args().end())
  77. {
  78. BOOST_THROW_EXCEPTION(
  79. regex_error(
  80. regex_constants::error_badarg
  81. , "An argument to an action was unspecified"
  82. )
  83. );
  84. }
  85. return proto::value(expr).cast(where_->second);
  86. }
  87. };
  88. // eval
  89. template<typename Expr, typename Tag = typename Expr::proto_tag>
  90. struct eval
  91. : proto::default_eval<Expr, predicate_context const>
  92. {};
  93. template<typename Expr>
  94. struct eval<Expr, proto::tag::terminal>
  95. : eval_terminal<Expr, typename proto::result_of::value<Expr>::type>
  96. {};
  97. #if BOOST_VERSION >= 103500
  98. template<typename Expr>
  99. struct eval<Expr, proto::tag::mem_ptr>
  100. : mem_ptr_eval<Expr, predicate_context const>
  101. {};
  102. #endif
  103. int sub_;
  104. sub_match_impl<BidiIter> const *sub_matches_;
  105. action_args_type *action_args_;
  106. };
  107. ///////////////////////////////////////////////////////////////////////////////
  108. // AssertionFunctor
  109. //
  110. struct AssertionFunctor
  111. : proto::function<
  112. proto::terminal<check_tag>
  113. , proto::terminal<proto::_>
  114. >
  115. {};
  116. ///////////////////////////////////////////////////////////////////////////////
  117. // predicate_matcher
  118. //
  119. template<typename Predicate>
  120. struct predicate_matcher
  121. : quant_style_assertion
  122. {
  123. int sub_;
  124. Predicate predicate_;
  125. predicate_matcher(Predicate const &pred, int sub)
  126. : sub_(sub)
  127. , predicate_(pred)
  128. {
  129. }
  130. template<typename BidiIter, typename Next>
  131. bool match(match_state<BidiIter> &state, Next const &next) const
  132. {
  133. // Predicate is check(assertion), where assertion can be
  134. // a lambda or a function object.
  135. return this->match_(state, next, proto::matches<Predicate, AssertionFunctor>());
  136. }
  137. private:
  138. template<typename BidiIter, typename Next>
  139. bool match_(match_state<BidiIter> &state, Next const &next, mpl::true_) const
  140. {
  141. sub_match<BidiIter> const &sub = state.sub_match(this->sub_);
  142. return proto::value(proto::child_c<1>(this->predicate_))(sub) && next.match(state);
  143. }
  144. template<typename BidiIter, typename Next>
  145. bool match_(match_state<BidiIter> &state, Next const &next, mpl::false_) const
  146. {
  147. predicate_context<BidiIter> ctx(this->sub_, state.sub_matches_, state.action_args_);
  148. return proto::eval(proto::child_c<1>(this->predicate_), ctx) && next.match(state);
  149. }
  150. };
  151. }}}
  152. #endif // BOOST_XPRESSIVE_DETAIL_CORE_MATCHER_PREDICATE_MATCHER_HPP_EAN_03_22_2007