Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:53:49

0001 ///////////////////////////////////////////////////////////////////////////////
0002 // repeat_end_matcher.hpp
0003 //
0004 //  Copyright 2008 Eric Niebler. Distributed under the Boost
0005 //  Software License, Version 1.0. (See accompanying file
0006 //  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0007 
0008 #ifndef BOOST_XPRESSIVE_DETAIL_CORE_MATCHER_REPEAT_END_MATCHER_HPP_EAN_10_04_2005
0009 #define BOOST_XPRESSIVE_DETAIL_CORE_MATCHER_REPEAT_END_MATCHER_HPP_EAN_10_04_2005
0010 
0011 // MS compatible compilers support #pragma once
0012 #if defined(_MSC_VER)
0013 # pragma once
0014 #endif
0015 
0016 #include <boost/mpl/bool.hpp>
0017 #include <boost/xpressive/detail/detail_fwd.hpp>
0018 #include <boost/xpressive/detail/core/quant_style.hpp>
0019 #include <boost/xpressive/detail/core/state.hpp>
0020 
0021 namespace boost { namespace xpressive { namespace detail
0022 {
0023 
0024     ///////////////////////////////////////////////////////////////////////////////
0025     // repeat_end_matcher
0026     //
0027     template<typename Greedy>
0028     struct repeat_end_matcher
0029       : quant_style<quant_none, 0, false>
0030     {
0031         typedef Greedy greedy_type;
0032         int mark_number_;
0033         unsigned int min_, max_;
0034         mutable void const *back_;
0035 
0036         repeat_end_matcher(int mark_nbr, unsigned int min, unsigned int max)
0037           : mark_number_(mark_nbr)
0038           , min_(min)
0039           , max_(max)
0040           , back_(0)
0041         {
0042         }
0043 
0044         template<typename BidiIter, typename Next>
0045         bool match(match_state<BidiIter> &state, Next const &next) const
0046         {
0047             // prevent repeated zero-width sub-matches from causing infinite recursion
0048             sub_match_impl<BidiIter> &br = state.sub_match(this->mark_number_);
0049 
0050             if(br.zero_width_ && br.begin_ == state.cur_)
0051             {
0052                 return next.skip_match(state);
0053             }
0054 
0055             bool old_zero_width = br.zero_width_;
0056             br.zero_width_ = (br.begin_ == state.cur_);
0057 
0058             if(this->match_(state, next, greedy_type()))
0059             {
0060                 return true;
0061             }
0062 
0063             br.zero_width_ = old_zero_width;
0064             return false;
0065         }
0066 
0067         // greedy, variable-width quantifier
0068         template<typename BidiIter, typename Next>
0069         bool match_(match_state<BidiIter> &state, Next const &next, mpl::true_) const
0070         {
0071             sub_match_impl<BidiIter> &br = state.sub_match(this->mark_number_);
0072 
0073             if(this->max_ > br.repeat_count_)
0074             {
0075                 ++br.repeat_count_;
0076                 // loop back to the expression "pushed" in repeat_begin_matcher::match
0077                 if(next.top_match(state, this->back_))
0078                 {
0079                     return true;
0080                 }
0081                 else if(--br.repeat_count_ < this->min_)
0082                 {
0083                     return false;
0084                 }
0085             }
0086 
0087             // looping finished, continue matching the rest of the pattern
0088             return next.skip_match(state);
0089         }
0090 
0091         // non-greedy, variable-width quantifier
0092         template<typename BidiIter, typename Next>
0093         bool match_(match_state<BidiIter> &state, Next const &next, mpl::false_) const
0094         {
0095             sub_match_impl<BidiIter> &br = state.sub_match(this->mark_number_);
0096 
0097             if(this->min_ <= br.repeat_count_)
0098             {
0099                 if(next.skip_match(state))
0100                 {
0101                     return true;
0102                 }
0103             }
0104 
0105             if(this->max_ > br.repeat_count_)
0106             {
0107                 ++br.repeat_count_;
0108                 if(next.top_match(state, this->back_))
0109                 {
0110                     return true;
0111                 }
0112                 --br.repeat_count_;
0113             }
0114 
0115             return false;
0116         }
0117     };
0118 
0119 }}}
0120 
0121 #endif