1 ///////////////////////////////////////////////////////////////////////////////
2 // alternate_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_ALTERNATE_MATCHER_HPP_EAN_10_04_2005
9 #define BOOST_XPRESSIVE_DETAIL_CORE_MATCHER_ALTERNATE_MATCHER_HPP_EAN_10_04_2005
11 // MS compatible compilers support #pragma once
16 #include <boost/version.hpp>
17 #if BOOST_VERSION <= 103200
18 // WORKAROUND for Fusion bug in Boost 1.32
19 namespace boost { namespace fusion
21 namespace detail { struct iterator_root; }
22 using detail::iterator_root;
26 #include <boost/xpressive/detail/detail_fwd.hpp>
27 #include <boost/xpressive/detail/core/quant_style.hpp>
28 #include <boost/xpressive/detail/core/state.hpp>
29 #include <boost/xpressive/detail/dynamic/matchable.hpp>
30 #include <boost/xpressive/detail/utility/hash_peek_bitset.hpp>
31 #include <boost/xpressive/detail/utility/algorithm.hpp>
32 #include <boost/xpressive/detail/utility/any.hpp>
34 namespace boost { namespace xpressive { namespace detail
37 ///////////////////////////////////////////////////////////////////////////////
40 template<typename BidiIter, typename Next>
43 alt_match_pred(match_state<BidiIter> &state)
48 template<typename Xpr>
49 bool operator ()(Xpr const &xpr) const
51 return xpr.BOOST_NESTED_TEMPLATE push_match<Next>(*this->state_);
55 match_state<BidiIter> *state_;
58 ///////////////////////////////////////////////////////////////////////////////
61 template<typename BidiIter, typename Next>
64 alternates_vector<BidiIter> const &alts, match_state<BidiIter> &state, Next const &
67 return detail::any(alts.begin(), alts.end(), alt_match_pred<BidiIter, Next>(state));
70 template<typename Head, typename Tail, typename BidiIter, typename Next>
73 alternates_list<Head, Tail> const &alts, match_state<BidiIter> &state, Next const &
76 return fusion::any(alts, alt_match_pred<BidiIter, Next>(state));
79 ///////////////////////////////////////////////////////////////////////////////
81 template<typename Alternates, typename Traits>
82 struct alternate_matcher
84 Alternates::width != unknown_width::value && Alternates::pure ? quant_fixed_width : quant_variable_width
89 typedef Alternates alternates_type;
90 typedef typename Traits::char_type char_type;
92 Alternates alternates_;
93 mutable hash_peek_bitset<char_type> bset_;
95 explicit alternate_matcher(Alternates const &alternates = Alternates())
96 : alternates_(alternates)
101 template<typename BidiIter, typename Next>
102 bool match(match_state<BidiIter> &state, Next const &next) const
104 if(!state.eos() && !this->can_match_(*state.cur_, traits_cast<Traits>(state)))
109 return detail::alt_match(this->alternates_, state, next);
112 detail::width get_width() const
114 // Only called when constructing static regexes, and this is a
115 // set of same-width alternates where the widths are known at compile
116 // time, as in: sregex rx = +(_ | 'a' | _n);
117 BOOST_MPL_ASSERT_RELATION(unknown_width::value, !=, Alternates::width);
118 return Alternates::width;
122 alternate_matcher &operator =(alternate_matcher const &);
124 bool can_match_(char_type ch, Traits const &tr) const
126 return this->bset_.test(ch, tr);