Chris@16
|
1 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
2 // matchable.hpp
|
Chris@16
|
3 //
|
Chris@16
|
4 // Copyright 2008 Eric Niebler. Distributed under the Boost
|
Chris@16
|
5 // Software License, Version 1.0. (See accompanying file
|
Chris@16
|
6 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
7
|
Chris@16
|
8 #ifndef BOOST_XPRESSIVE_DETAIL_DYNAMIC_MATCHABLE_HPP_EAN_10_04_2005
|
Chris@16
|
9 #define BOOST_XPRESSIVE_DETAIL_DYNAMIC_MATCHABLE_HPP_EAN_10_04_2005
|
Chris@16
|
10
|
Chris@16
|
11 // MS compatible compilers support #pragma once
|
Chris@101
|
12 #if defined(_MSC_VER)
|
Chris@16
|
13 # pragma once
|
Chris@16
|
14 #endif
|
Chris@16
|
15
|
Chris@16
|
16 #include <boost/assert.hpp>
|
Chris@16
|
17 #include <boost/mpl/assert.hpp>
|
Chris@16
|
18 #include <boost/intrusive_ptr.hpp>
|
Chris@16
|
19 #include <boost/throw_exception.hpp>
|
Chris@16
|
20 #include <boost/type_traits/is_same.hpp>
|
Chris@16
|
21 #include <boost/xpressive/detail/core/quant_style.hpp>
|
Chris@16
|
22 #include <boost/xpressive/detail/utility/counted_base.hpp>
|
Chris@16
|
23 #include <boost/xpressive/detail/detail_fwd.hpp>
|
Chris@16
|
24 #include <boost/xpressive/detail/dynamic/sequence.hpp>
|
Chris@16
|
25 #include <boost/xpressive/regex_error.hpp>
|
Chris@16
|
26
|
Chris@16
|
27 namespace boost { namespace xpressive { namespace detail
|
Chris@16
|
28 {
|
Chris@16
|
29
|
Chris@16
|
30 //////////////////////////////////////////////////////////////////////////
|
Chris@16
|
31 // quant_spec
|
Chris@16
|
32 struct quant_spec
|
Chris@16
|
33 {
|
Chris@16
|
34 unsigned int min_;
|
Chris@16
|
35 unsigned int max_;
|
Chris@16
|
36 bool greedy_;
|
Chris@16
|
37 std::size_t *hidden_mark_count_;
|
Chris@16
|
38 };
|
Chris@16
|
39
|
Chris@16
|
40 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
41 // matchable
|
Chris@16
|
42 template<typename BidiIter>
|
Chris@16
|
43 struct matchable
|
Chris@16
|
44 {
|
Chris@16
|
45 typedef BidiIter iterator_type;
|
Chris@16
|
46 typedef typename iterator_value<iterator_type>::type char_type;
|
Chris@16
|
47 virtual ~matchable() {}
|
Chris@16
|
48 virtual bool match(match_state<BidiIter> &state) const = 0;
|
Chris@16
|
49 };
|
Chris@16
|
50
|
Chris@16
|
51 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
52 // matchable_ex
|
Chris@16
|
53 template<typename BidiIter>
|
Chris@16
|
54 struct matchable_ex
|
Chris@16
|
55 : matchable<BidiIter>
|
Chris@16
|
56 , counted_base<matchable_ex<BidiIter> >
|
Chris@16
|
57 {
|
Chris@16
|
58 typedef BidiIter iterator_type;
|
Chris@16
|
59 typedef typename iterator_value<iterator_type>::type char_type;
|
Chris@16
|
60
|
Chris@16
|
61 virtual void link(xpression_linker<char_type> &) const
|
Chris@16
|
62 {
|
Chris@16
|
63 }
|
Chris@16
|
64
|
Chris@16
|
65 virtual void peek(xpression_peeker<char_type> &peeker) const
|
Chris@16
|
66 {
|
Chris@16
|
67 peeker.fail();
|
Chris@16
|
68 }
|
Chris@16
|
69
|
Chris@16
|
70 virtual void repeat(quant_spec const &, sequence<BidiIter> &) const
|
Chris@16
|
71 {
|
Chris@16
|
72 BOOST_THROW_EXCEPTION(
|
Chris@16
|
73 regex_error(regex_constants::error_badrepeat, "expression cannot be quantified")
|
Chris@16
|
74 );
|
Chris@16
|
75 }
|
Chris@16
|
76
|
Chris@16
|
77 ///////////////////////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
78 // The following 4 functions (push_match, top_match, pop_match and skip_match) are
|
Chris@16
|
79 // used to implement looping and branching across the matchers. Call push_match to record
|
Chris@16
|
80 // a position. Then, another matcher further down the xpression chain has the
|
Chris@16
|
81 // option to call either top_match, pop_match or skip_match. top_match and pop_match will
|
Chris@16
|
82 // jump back to the place recorded by push_match, whereas skip_match will skip the jump and
|
Chris@16
|
83 // pass execution down the xpression chain. top_match will leave the xpression on top of the
|
Chris@16
|
84 // stack, whereas pop_match will remove it. Each function comes in 2 flavors: one for
|
Chris@16
|
85 // statically bound xpressions and one for dynamically bound xpressions.
|
Chris@16
|
86 //
|
Chris@16
|
87
|
Chris@16
|
88 template<typename Top>
|
Chris@16
|
89 bool push_match(match_state<BidiIter> &state) const
|
Chris@16
|
90 {
|
Chris@16
|
91 BOOST_MPL_ASSERT((is_same<Top, matchable_ex<BidiIter> >));
|
Chris@16
|
92 return this->match(state);
|
Chris@16
|
93 }
|
Chris@16
|
94
|
Chris@16
|
95 static bool top_match(match_state<BidiIter> &state, void const *top)
|
Chris@16
|
96 {
|
Chris@16
|
97 return static_cast<matchable_ex<BidiIter> const *>(top)->match(state);
|
Chris@16
|
98 }
|
Chris@16
|
99
|
Chris@16
|
100 static bool pop_match(match_state<BidiIter> &state, void const *top)
|
Chris@16
|
101 {
|
Chris@16
|
102 return static_cast<matchable_ex<BidiIter> const *>(top)->match(state);
|
Chris@16
|
103 }
|
Chris@16
|
104
|
Chris@16
|
105 bool skip_match(match_state<BidiIter> &state) const
|
Chris@16
|
106 {
|
Chris@16
|
107 return this->match(state);
|
Chris@16
|
108 }
|
Chris@16
|
109 };
|
Chris@16
|
110
|
Chris@16
|
111 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
112 // shared_matchable
|
Chris@16
|
113 template<typename BidiIter>
|
Chris@16
|
114 struct shared_matchable
|
Chris@16
|
115 {
|
Chris@16
|
116 typedef BidiIter iterator_type;
|
Chris@16
|
117 typedef typename iterator_value<BidiIter>::type char_type;
|
Chris@16
|
118 typedef intrusive_ptr<matchable_ex<BidiIter> const> matchable_ptr;
|
Chris@16
|
119
|
Chris@16
|
120 BOOST_STATIC_CONSTANT(std::size_t, width = unknown_width::value);
|
Chris@16
|
121 BOOST_STATIC_CONSTANT(bool, pure = false);
|
Chris@16
|
122
|
Chris@16
|
123 shared_matchable(matchable_ptr const &xpr = matchable_ptr())
|
Chris@16
|
124 : xpr_(xpr)
|
Chris@16
|
125 {
|
Chris@16
|
126 }
|
Chris@16
|
127
|
Chris@16
|
128 bool operator !() const
|
Chris@16
|
129 {
|
Chris@16
|
130 return !this->xpr_;
|
Chris@16
|
131 }
|
Chris@16
|
132
|
Chris@16
|
133 friend bool operator ==(shared_matchable<BidiIter> const &left, shared_matchable<BidiIter> const &right)
|
Chris@16
|
134 {
|
Chris@16
|
135 return left.xpr_ == right.xpr_;
|
Chris@16
|
136 }
|
Chris@16
|
137
|
Chris@16
|
138 friend bool operator !=(shared_matchable<BidiIter> const &left, shared_matchable<BidiIter> const &right)
|
Chris@16
|
139 {
|
Chris@16
|
140 return left.xpr_ != right.xpr_;
|
Chris@16
|
141 }
|
Chris@16
|
142
|
Chris@16
|
143 matchable_ptr const &matchable() const
|
Chris@16
|
144 {
|
Chris@16
|
145 return this->xpr_;
|
Chris@16
|
146 }
|
Chris@16
|
147
|
Chris@16
|
148 bool match(match_state<BidiIter> &state) const
|
Chris@16
|
149 {
|
Chris@16
|
150 return this->xpr_->match(state);
|
Chris@16
|
151 }
|
Chris@16
|
152
|
Chris@16
|
153 void link(xpression_linker<char_type> &linker) const
|
Chris@16
|
154 {
|
Chris@16
|
155 this->xpr_->link(linker);
|
Chris@16
|
156 }
|
Chris@16
|
157
|
Chris@16
|
158 void peek(xpression_peeker<char_type> &peeker) const
|
Chris@16
|
159 {
|
Chris@16
|
160 this->xpr_->peek(peeker);
|
Chris@16
|
161 }
|
Chris@16
|
162
|
Chris@16
|
163 // BUGBUG yuk!
|
Chris@16
|
164 template<typename Top>
|
Chris@16
|
165 bool push_match(match_state<BidiIter> &state) const
|
Chris@16
|
166 {
|
Chris@16
|
167 BOOST_MPL_ASSERT((is_same<Top, matchable_ex<BidiIter> >));
|
Chris@16
|
168 return this->match(state);
|
Chris@16
|
169 }
|
Chris@16
|
170
|
Chris@16
|
171 private:
|
Chris@16
|
172 matchable_ptr xpr_;
|
Chris@16
|
173 };
|
Chris@16
|
174
|
Chris@16
|
175 }}} // namespace boost::xpressive::detail
|
Chris@16
|
176
|
Chris@16
|
177 #endif
|