1 ///////////////////////////////////////////////////////////////////////////////
2 // repeat_end_matcher.hpp
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)
8 #ifndef BOOST_XPRESSIVE_DETAIL_CORE_MATCHER_REPEAT_END_MATCHER_HPP_EAN_10_04_2005
9 #define BOOST_XPRESSIVE_DETAIL_CORE_MATCHER_REPEAT_END_MATCHER_HPP_EAN_10_04_2005
11 // MS compatible compilers support #pragma once
16 #include <boost/mpl/bool.hpp>
17 #include <boost/xpressive/detail/detail_fwd.hpp>
18 #include <boost/xpressive/detail/core/quant_style.hpp>
19 #include <boost/xpressive/detail/core/state.hpp>
21 namespace boost { namespace xpressive { namespace detail
24 ///////////////////////////////////////////////////////////////////////////////
27 template<typename Greedy>
28 struct repeat_end_matcher
29 : quant_style<quant_none, 0, false>
31 typedef Greedy greedy_type;
33 unsigned int min_, max_;
34 mutable void const *back_;
36 repeat_end_matcher(int mark_nbr, unsigned int min, unsigned int max)
37 : mark_number_(mark_nbr)
44 template<typename BidiIter, typename Next>
45 bool match(match_state<BidiIter> &state, Next const &next) const
47 // prevent repeated zero-width sub-matches from causing infinite recursion
48 sub_match_impl<BidiIter> &br = state.sub_match(this->mark_number_);
50 if(br.zero_width_ && br.begin_ == state.cur_)
52 return next.skip_match(state);
55 bool old_zero_width = br.zero_width_;
56 br.zero_width_ = (br.begin_ == state.cur_);
58 if(this->match_(state, next, greedy_type()))
63 br.zero_width_ = old_zero_width;
67 // greedy, variable-width quantifier
68 template<typename BidiIter, typename Next>
69 bool match_(match_state<BidiIter> &state, Next const &next, mpl::true_) const
71 sub_match_impl<BidiIter> &br = state.sub_match(this->mark_number_);
73 if(this->max_ > br.repeat_count_)
76 // loop back to the expression "pushed" in repeat_begin_matcher::match
77 if(next.top_match(state, this->back_))
81 else if(--br.repeat_count_ < this->min_)
87 // looping finished, continue matching the rest of the pattern
88 return next.skip_match(state);
91 // non-greedy, variable-width quantifier
92 template<typename BidiIter, typename Next>
93 bool match_(match_state<BidiIter> &state, Next const &next, mpl::false_) const
95 sub_match_impl<BidiIter> &br = state.sub_match(this->mark_number_);
97 if(this->min_ <= br.repeat_count_)
99 if(next.skip_match(state))
105 if(this->max_ > br.repeat_count_)
108 if(next.top_match(state, this->back_))