1 ///////////////////////////////////////////////////////////////////////////////
2 // optional_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_OPTIONAL_MATCHER_HPP_EAN_10_04_2005
9 #define BOOST_XPRESSIVE_DETAIL_CORE_MATCHER_OPTIONAL_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 ///////////////////////////////////////////////////////////////////////////////
26 template<typename Xpr, typename Greedy>
27 struct optional_matcher
28 : quant_style<quant_variable_width, unknown_width::value, Xpr::pure>
32 explicit optional_matcher(Xpr const &xpr)
37 template<typename BidiIter, typename Next>
38 bool match(match_state<BidiIter> &state, Next const &next) const
40 return this->match_(state, next, Greedy());
44 template<typename BidiIter, typename Next>
45 bool match_(match_state<BidiIter> &state, Next const &next, mpl::true_) const // Greedy
47 return this->xpr_.BOOST_NESTED_TEMPLATE push_match<Next>(state)
51 template<typename BidiIter, typename Next>
52 bool match_(match_state<BidiIter> &state, Next const &next, mpl::false_) const // Non-greedy
54 return next.match(state)
55 || this->xpr_.BOOST_NESTED_TEMPLATE push_match<Next>(state);
58 optional_matcher &operator =(optional_matcher const &);
61 ///////////////////////////////////////////////////////////////////////////////
62 // optional_mark_matcher
63 template<typename BidiIter, typename Next>
64 inline bool match_next(match_state<BidiIter> &state, Next const &next, int mark_number)
66 sub_match_impl<BidiIter> &br = state.sub_match(mark_number);
68 bool old_matched = br.matched;
76 br.matched = old_matched;
80 ///////////////////////////////////////////////////////////////////////////////
81 // optional_mark_matcher
82 template<typename Xpr, typename Greedy>
83 struct optional_mark_matcher
84 : quant_style<quant_variable_width, unknown_width::value, Xpr::pure>
89 explicit optional_mark_matcher(Xpr const &xpr, int mark_number)
91 , mark_number_(mark_number)
95 template<typename BidiIter, typename Next>
96 bool match(match_state<BidiIter> &state, Next const &next) const
98 return this->match_(state, next, Greedy());
102 template<typename BidiIter, typename Next>
103 bool match_(match_state<BidiIter> &state, Next const &next, mpl::true_) const // Greedy
105 return this->xpr_.BOOST_NESTED_TEMPLATE push_match<Next>(state)
106 || match_next(state, next, this->mark_number_);
109 template<typename BidiIter, typename Next>
110 bool match_(match_state<BidiIter> &state, Next const &next, mpl::false_) const // Non-greedy
112 return match_next(state, next, this->mark_number_)
113 || this->xpr_.BOOST_NESTED_TEMPLATE push_match<Next>(state);
116 optional_mark_matcher &operator =(optional_mark_matcher const &);