keeper_matcher.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // keeper_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_KEEPER_MATCHER_HPP_EAN_10_04_2005
  8. #define BOOST_XPRESSIVE_DETAIL_CORE_MATCHER_KEEPER_MATCHER_HPP_EAN_10_04_2005
  9. // MS compatible compilers support #pragma once
  10. #if defined(_MSC_VER)
  11. # pragma once
  12. #endif
  13. #include <boost/mpl/bool.hpp>
  14. #include <boost/xpressive/detail/detail_fwd.hpp>
  15. #include <boost/xpressive/detail/core/quant_style.hpp>
  16. #include <boost/xpressive/detail/core/state.hpp>
  17. namespace boost { namespace xpressive { namespace detail
  18. {
  19. ///////////////////////////////////////////////////////////////////////////////
  20. // keeper_matcher
  21. // Xpr can be either a static_xpression, or a shared_matchable
  22. template<typename Xpr>
  23. struct keeper_matcher
  24. : quant_style<quant_variable_width, unknown_width::value, Xpr::pure>
  25. {
  26. keeper_matcher(Xpr const &xpr, bool pure = Xpr::pure)
  27. : xpr_(xpr)
  28. , pure_(pure)
  29. {
  30. }
  31. template<typename BidiIter, typename Next>
  32. bool match(match_state<BidiIter> &state, Next const &next) const
  33. {
  34. return Xpr::pure || this->pure_
  35. ? this->match_(state, next, mpl::true_())
  36. : this->match_(state, next, mpl::false_());
  37. }
  38. template<typename BidiIter, typename Next>
  39. bool match_(match_state<BidiIter> &state, Next const &next, mpl::true_) const
  40. {
  41. BidiIter const tmp = state.cur_;
  42. // matching xpr is guaranteed to not produce side-effects, don't bother saving state
  43. if(!this->xpr_.match(state))
  44. {
  45. return false;
  46. }
  47. else if(next.match(state))
  48. {
  49. return true;
  50. }
  51. state.cur_ = tmp;
  52. return false;
  53. }
  54. template<typename BidiIter, typename Next>
  55. bool match_(match_state<BidiIter> &state, Next const &next, mpl::false_) const
  56. {
  57. BidiIter const tmp = state.cur_;
  58. // matching xpr could produce side-effects, save state
  59. memento<BidiIter> mem = save_sub_matches(state);
  60. if(!this->xpr_.match(state))
  61. {
  62. restore_action_queue(mem, state);
  63. reclaim_sub_matches(mem, state, false);
  64. return false;
  65. }
  66. restore_action_queue(mem, state);
  67. if(next.match(state))
  68. {
  69. reclaim_sub_matches(mem, state, true);
  70. return true;
  71. }
  72. restore_sub_matches(mem, state);
  73. state.cur_ = tmp;
  74. return false;
  75. }
  76. Xpr xpr_;
  77. bool pure_; // false if matching xpr_ could modify the sub-matches
  78. };
  79. }}}
  80. #endif