1 ///////////////////////////////////////////////////////////////////////////////
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_DYNAMIC_DYNAMIC_HPP_EAN_10_04_2005
9 #define BOOST_XPRESSIVE_DETAIL_DYNAMIC_DYNAMIC_HPP_EAN_10_04_2005
11 // MS compatible compilers support #pragma once
19 #include <boost/assert.hpp>
20 #include <boost/mpl/int.hpp>
21 #include <boost/mpl/assert.hpp>
22 #include <boost/throw_exception.hpp>
23 #include <boost/type_traits/is_same.hpp>
24 #include <boost/xpressive/detail/detail_fwd.hpp>
25 #include <boost/xpressive/detail/core/quant_style.hpp>
26 #include <boost/xpressive/detail/dynamic/matchable.hpp>
27 #include <boost/xpressive/detail/dynamic/sequence.hpp>
28 #include <boost/xpressive/detail/core/icase.hpp>
30 namespace boost { namespace xpressive { namespace detail
33 ///////////////////////////////////////////////////////////////////////////////
35 template<typename BidiIter>
36 struct invalid_xpression
37 : matchable_ex<BidiIter>
40 : matchable_ex<BidiIter>()
42 intrusive_ptr_add_ref(this); // keep alive forever
45 bool match(match_state<BidiIter> &) const
52 ///////////////////////////////////////////////////////////////////////////////
53 // get_invalid_xpression
54 template<typename BidiIter>
55 inline shared_matchable<BidiIter> const &get_invalid_xpression()
57 static invalid_xpression<BidiIter> const invalid_xpr;
58 static intrusive_ptr<matchable_ex<BidiIter> const> const invalid_ptr(&invalid_xpr);
59 static shared_matchable<BidiIter> const invalid_matchable(invalid_ptr);
60 return invalid_matchable;
63 ///////////////////////////////////////////////////////////////////////////////
65 template<typename Matcher, typename BidiIter>
66 struct dynamic_xpression
68 , matchable_ex<BidiIter>
70 typedef typename iterator_value<BidiIter>::type char_type;
72 dynamic_xpression(Matcher const &matcher = Matcher())
74 , next_(get_invalid_xpression<BidiIter>())
78 virtual bool match(match_state<BidiIter> &state) const
80 return this->Matcher::match(state, *this->next_.matchable());
83 virtual void link(xpression_linker<char_type> &linker) const
85 linker.accept(*static_cast<Matcher const *>(this), this->next_.matchable().get());
86 this->next_.link(linker);
89 virtual void peek(xpression_peeker<char_type> &peeker) const
91 this->peek_next_(peeker.accept(*static_cast<Matcher const *>(this)), peeker);
94 virtual void repeat(quant_spec const &spec, sequence<BidiIter> &seq) const
96 this->repeat_(spec, seq, quant_type<Matcher>(), is_same<Matcher, mark_begin_matcher>());
100 friend struct sequence<BidiIter>;
102 void peek_next_(mpl::true_, xpression_peeker<char_type> &peeker) const
104 this->next_.peek(peeker);
107 void peek_next_(mpl::false_, xpression_peeker<char_type> &) const
112 void repeat_(quant_spec const &spec, sequence<BidiIter> &seq, mpl::int_<quant_none>, mpl::false_) const
114 if(quant_none == seq.quant())
116 BOOST_THROW_EXCEPTION(
117 regex_error(regex_constants::error_badrepeat, "expression cannot be quantified")
122 this->repeat_(spec, seq, mpl::int_<quant_variable_width>(), mpl::false_());
126 void repeat_(quant_spec const &spec, sequence<BidiIter> &seq, mpl::int_<quant_fixed_width>, mpl::false_) const
128 if(this->next_ == get_invalid_xpression<BidiIter>())
130 make_simple_repeat(spec, seq, matcher_wrapper<Matcher>(*this));
134 this->repeat_(spec, seq, mpl::int_<quant_variable_width>(), mpl::false_());
138 void repeat_(quant_spec const &spec, sequence<BidiIter> &seq, mpl::int_<quant_variable_width>, mpl::false_) const
140 if(!is_unknown(seq.width()) && seq.pure())
142 make_simple_repeat(spec, seq);
146 make_repeat(spec, seq);
150 void repeat_(quant_spec const &spec, sequence<BidiIter> &seq, mpl::int_<quant_fixed_width>, mpl::true_) const
152 make_repeat(spec, seq, this->mark_number_);
155 shared_matchable<BidiIter> next_;
158 ///////////////////////////////////////////////////////////////////////////////
160 template<typename BidiIter, typename Matcher>
161 inline sequence<BidiIter> make_dynamic(Matcher const &matcher)
163 typedef dynamic_xpression<Matcher, BidiIter> xpression_type;
164 intrusive_ptr<xpression_type> xpr(new xpression_type(matcher));
165 return sequence<BidiIter>(xpr);
168 ///////////////////////////////////////////////////////////////////////////////
170 template<typename BidiIter>
171 struct alternates_vector
172 : std::vector<shared_matchable<BidiIter> >
174 BOOST_STATIC_CONSTANT(std::size_t, width = unknown_width::value);
175 BOOST_STATIC_CONSTANT(bool, pure = false);
178 ///////////////////////////////////////////////////////////////////////////////
180 template<typename Matcher>
181 struct matcher_wrapper
184 matcher_wrapper(Matcher const &matcher = Matcher())
189 template<typename BidiIter>
190 bool match(match_state<BidiIter> &state) const
192 return this->Matcher::match(state, matcher_wrapper<true_matcher>());
195 template<typename Char>
196 void link(xpression_linker<Char> &linker) const
198 linker.accept(*static_cast<Matcher const *>(this), 0);
201 template<typename Char>
202 void peek(xpression_peeker<Char> &peeker) const
204 peeker.accept(*static_cast<Matcher const *>(this));
208 //////////////////////////////////////////////////////////////////////////
209 // make_simple_repeat
210 template<typename BidiIter, typename Xpr>
212 make_simple_repeat(quant_spec const &spec, sequence<BidiIter> &seq, Xpr const &xpr)
216 simple_repeat_matcher<Xpr, mpl::true_> quant(xpr, spec.min_, spec.max_, seq.width().value());
217 seq = make_dynamic<BidiIter>(quant);
221 simple_repeat_matcher<Xpr, mpl::false_> quant(xpr, spec.min_, spec.max_, seq.width().value());
222 seq = make_dynamic<BidiIter>(quant);
226 //////////////////////////////////////////////////////////////////////////
227 // make_simple_repeat
228 template<typename BidiIter>
230 make_simple_repeat(quant_spec const &spec, sequence<BidiIter> &seq)
232 seq += make_dynamic<BidiIter>(true_matcher());
233 make_simple_repeat(spec, seq, seq.xpr());
236 //////////////////////////////////////////////////////////////////////////
238 template<typename BidiIter>
240 make_optional(quant_spec const &spec, sequence<BidiIter> &seq)
242 typedef shared_matchable<BidiIter> xpr_type;
243 seq += make_dynamic<BidiIter>(alternate_end_matcher());
246 optional_matcher<xpr_type, mpl::true_> opt(seq.xpr());
247 seq = make_dynamic<BidiIter>(opt);
251 optional_matcher<xpr_type, mpl::false_> opt(seq.xpr());
252 seq = make_dynamic<BidiIter>(opt);
256 //////////////////////////////////////////////////////////////////////////
258 template<typename BidiIter>
260 make_optional(quant_spec const &spec, sequence<BidiIter> &seq, int mark_nbr)
262 typedef shared_matchable<BidiIter> xpr_type;
263 seq += make_dynamic<BidiIter>(alternate_end_matcher());
266 optional_mark_matcher<xpr_type, mpl::true_> opt(seq.xpr(), mark_nbr);
267 seq = make_dynamic<BidiIter>(opt);
271 optional_mark_matcher<xpr_type, mpl::false_> opt(seq.xpr(), mark_nbr);
272 seq = make_dynamic<BidiIter>(opt);
276 //////////////////////////////////////////////////////////////////////////
278 template<typename BidiIter>
280 make_repeat(quant_spec const &spec, sequence<BidiIter> &seq)
282 // only bother creating a repeater if max is greater than one
285 // create a hidden mark so this expression can be quantified
286 int mark_nbr = -static_cast<int>(++*spec.hidden_mark_count_);
287 seq = make_dynamic<BidiIter>(mark_begin_matcher(mark_nbr)) + seq
288 + make_dynamic<BidiIter>(mark_end_matcher(mark_nbr));
289 make_repeat(spec, seq, mark_nbr);
293 // if min is 0, the repeat must be made optional
296 make_optional(spec, seq);
300 //////////////////////////////////////////////////////////////////////////
302 template<typename BidiIter>
304 make_repeat(quant_spec const &spec, sequence<BidiIter> &seq, int mark_nbr)
306 BOOST_ASSERT(spec.max_); // we should never get here if max is 0
308 // only bother creating a repeater if max is greater than one
311 // TODO: statically bind the repeat matchers to the mark matchers for better perf
312 unsigned int min = spec.min_ ? spec.min_ : 1U;
313 repeat_begin_matcher repeat_begin(mark_nbr);
316 repeat_end_matcher<mpl::true_> repeat_end(mark_nbr, min, spec.max_);
317 seq = make_dynamic<BidiIter>(repeat_begin) + seq
318 + make_dynamic<BidiIter>(repeat_end);
322 repeat_end_matcher<mpl::false_> repeat_end(mark_nbr, min, spec.max_);
323 seq = make_dynamic<BidiIter>(repeat_begin) + seq
324 + make_dynamic<BidiIter>(repeat_end);
328 // if min is 0, the repeat must be made optional
331 make_optional(spec, seq, mark_nbr);
335 }}} // namespace boost::xpressive::detail