repeat_end_matcher.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // repeat_end_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_REPEAT_END_MATCHER_HPP_EAN_10_04_2005
  8. #define BOOST_XPRESSIVE_DETAIL_CORE_MATCHER_REPEAT_END_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. // repeat_end_matcher
  21. //
  22. template<typename Greedy>
  23. struct repeat_end_matcher
  24. : quant_style<quant_none, 0, false>
  25. {
  26. typedef Greedy greedy_type;
  27. int mark_number_;
  28. unsigned int min_, max_;
  29. mutable void const *back_;
  30. repeat_end_matcher(int mark_nbr, unsigned int min, unsigned int max)
  31. : mark_number_(mark_nbr)
  32. , min_(min)
  33. , max_(max)
  34. , back_(0)
  35. {
  36. }
  37. template<typename BidiIter, typename Next>
  38. bool match(match_state<BidiIter> &state, Next const &next) const
  39. {
  40. // prevent repeated zero-width sub-matches from causing infinite recursion
  41. sub_match_impl<BidiIter> &br = state.sub_match(this->mark_number_);
  42. if(br.zero_width_ && br.begin_ == state.cur_)
  43. {
  44. return next.skip_match(state);
  45. }
  46. bool old_zero_width = br.zero_width_;
  47. br.zero_width_ = (br.begin_ == state.cur_);
  48. if(this->match_(state, next, greedy_type()))
  49. {
  50. return true;
  51. }
  52. br.zero_width_ = old_zero_width;
  53. return false;
  54. }
  55. // greedy, variable-width quantifier
  56. template<typename BidiIter, typename Next>
  57. bool match_(match_state<BidiIter> &state, Next const &next, mpl::true_) const
  58. {
  59. sub_match_impl<BidiIter> &br = state.sub_match(this->mark_number_);
  60. if(this->max_ > br.repeat_count_)
  61. {
  62. ++br.repeat_count_;
  63. // loop back to the expression "pushed" in repeat_begin_matcher::match
  64. if(next.top_match(state, this->back_))
  65. {
  66. return true;
  67. }
  68. else if(--br.repeat_count_ < this->min_)
  69. {
  70. return false;
  71. }
  72. }
  73. // looping finished, continue matching the rest of the pattern
  74. return next.skip_match(state);
  75. }
  76. // non-greedy, variable-width quantifier
  77. template<typename BidiIter, typename Next>
  78. bool match_(match_state<BidiIter> &state, Next const &next, mpl::false_) const
  79. {
  80. sub_match_impl<BidiIter> &br = state.sub_match(this->mark_number_);
  81. if(this->min_ <= br.repeat_count_)
  82. {
  83. if(next.skip_match(state))
  84. {
  85. return true;
  86. }
  87. }
  88. if(this->max_ > br.repeat_count_)
  89. {
  90. ++br.repeat_count_;
  91. if(next.top_match(state, this->back_))
  92. {
  93. return true;
  94. }
  95. --br.repeat_count_;
  96. }
  97. return false;
  98. }
  99. };
  100. }}}
  101. #endif