summaryrefslogtreecommitdiff
path: root/boost/xpressive/detail/core/linker.hpp
blob: e87e26d125f8177b6e5a107e227d73697a3d1d42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
///////////////////////////////////////////////////////////////////////////////
// linker.hpp
//
//  Copyright 2008 Eric Niebler. Distributed under the Boost
//  Software License, Version 1.0. (See accompanying file
//  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#ifndef BOOST_XPRESSIVE_DETAIL_CORE_LINKER_HPP_EAN_10_04_2005
#define BOOST_XPRESSIVE_DETAIL_CORE_LINKER_HPP_EAN_10_04_2005

// MS compatible compilers support #pragma once
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# pragma once
#endif

#include <boost/config.hpp>
#ifndef BOOST_NO_STD_LOCALE
# include <locale>
#endif
#include <stack>
#include <limits>
#include <typeinfo>
#include <boost/shared_ptr.hpp>
#include <boost/type_traits/is_same.hpp>
#include <boost/version.hpp>

#if BOOST_VERSION >= 103500
# include <boost/fusion/include/for_each.hpp>
#else
# include <boost/spirit/fusion/algorithm/for_each.hpp>
#endif

#include <boost/xpressive/detail/detail_fwd.hpp>
#include <boost/xpressive/detail/dynamic/matchable.hpp>
#include <boost/xpressive/detail/core/matchers.hpp>
#include <boost/xpressive/detail/core/peeker.hpp>
#include <boost/xpressive/detail/utility/never_true.hpp>

namespace boost { namespace xpressive { namespace detail
{

///////////////////////////////////////////////////////////////////////////////
// icase_modifier
//
//   wrapped by the modifier<> template and inserted into the xpression
//   template with the icase() helper function. icase_modifier morphs
//   a case-sensitive visitor into a case-insensitive visitor, which
//   causes all matchers visited to become case-insensitive.
//
struct icase_modifier
{
    template<typename Visitor>
    struct apply {};

    template<typename BidiIter, typename ICase, typename Traits>
    struct apply<xpression_visitor<BidiIter, ICase, Traits> >
    {
        typedef xpression_visitor<BidiIter, mpl::true_, Traits> type;
    };

    template<typename Visitor>
    static typename apply<Visitor>::type
    call(Visitor &visitor)
    {
        return typename apply<Visitor>::type(visitor.traits(), visitor.self());
    }
};

///////////////////////////////////////////////////////////////////////////////
// regex_traits_type : wrap a locale in the appropriate regex_traits
//
template<typename Locale, typename BidiIter>
struct regex_traits_type
{
#ifndef BOOST_NO_STD_LOCALE

    typedef typename iterator_value<BidiIter>::type char_type;

    // if Locale is std::locale, wrap it in a cpp_regex_traits<Char>
    typedef typename mpl::if_c
    <
        is_same<Locale, std::locale>::value
      , cpp_regex_traits<char_type>
      , Locale
    >::type type;

#else

    typedef Locale type;

#endif
};

///////////////////////////////////////////////////////////////////////////////
// locale_modifier
//
//   wrapped by the modifier<> template and inserted into the xpression
//   template with the imbue() helper function. Causes a sub-xpression to
//   use the specified Locale
//
template<typename Locale>
struct locale_modifier
{
    typedef Locale locale_type;

    locale_modifier(Locale const &loc)
      : loc_(loc)
    {
    }

    template<typename Visitor>
    struct apply {};

    template<typename BidiIter, typename ICase, typename OtherTraits>
    struct apply<xpression_visitor<BidiIter, ICase, OtherTraits> >
    {
        typedef typename regex_traits_type<Locale, BidiIter>::type traits_type;
        typedef xpression_visitor<BidiIter, ICase, traits_type> type;
    };

    template<typename Visitor>
    typename apply<Visitor>::type
    call(Visitor &visitor) const
    {
        return typename apply<Visitor>::type(this->loc_, visitor.self());
    }

    Locale getloc() const
    {
        return this->loc_;
    }

private:
    Locale loc_;
};

///////////////////////////////////////////////////////////////////////////////
// xpression_linker
//
template<typename Char>
struct xpression_linker
{
    template<typename Traits>
    explicit xpression_linker(Traits const &tr)
      : back_stack_()
      , traits_(&tr)
      , traits_type_(&typeid(Traits))
      , has_backrefs_(false)
    {
    }

    template<typename Matcher>
    void accept(Matcher const &, void const *)
    {
        // no-op
    }

    template<typename Traits, typename ICase>
    void accept(mark_matcher<Traits, ICase> const &, void const *)
    {
        this->has_backrefs_ = true;
    }

    template<typename Action>
    void accept(action_matcher<Action> const &, void const *)
    {
        this->has_backrefs_ = true;
    }

    template<typename Predicate>
    void accept(predicate_matcher<Predicate> const &, void const *)
    {
        this->has_backrefs_ = true;
    }

    void accept(repeat_begin_matcher const &, void const *next)
    {
        this->back_stack_.push(next);
    }

    template<typename Greedy>
    void accept(repeat_end_matcher<Greedy> const &matcher, void const *)
    {
        matcher.back_ = this->back_stack_.top();
        this->back_stack_.pop();
    }

    template<typename Alternates, typename Traits>
    void accept(alternate_matcher<Alternates, Traits> const &matcher, void const *next)
    {
        xpression_peeker<Char> peeker(matcher.bset_, this->get_traits<Traits>());
        this->alt_link(matcher.alternates_, next, &peeker);
    }

    void accept(alternate_end_matcher const &matcher, void const *)
    {
        matcher.back_ = this->back_stack_.top();
        this->back_stack_.pop();
    }

    template<typename Xpr, typename Greedy>
    void accept(optional_matcher<Xpr, Greedy> const &matcher, void const *next)
    {
        this->back_stack_.push(next);
        matcher.xpr_.link(*this);
    }

    template<typename Xpr, typename Greedy>
    void accept(optional_mark_matcher<Xpr, Greedy> const &matcher, void const *next)
    {
        this->back_stack_.push(next);
        matcher.xpr_.link(*this);
    }

    template<typename Xpr>
    void accept(keeper_matcher<Xpr> const &matcher, void const *)
    {
        matcher.xpr_.link(*this);
    }

    template<typename Xpr>
    void accept(lookahead_matcher<Xpr> const &matcher, void const *)
    {
        matcher.xpr_.link(*this);
    }

    template<typename Xpr>
    void accept(lookbehind_matcher<Xpr> const &matcher, void const *)
    {
        matcher.xpr_.link(*this);
    }

    template<typename Xpr, typename Greedy>
    void accept(simple_repeat_matcher<Xpr, Greedy> const &matcher, void const *)
    {
        matcher.xpr_.link(*this);
    }

    // accessors
    bool has_backrefs() const
    {
        return this->has_backrefs_;
    }

    // for use by alt_link_pred below
    template<typename Xpr>
    void alt_branch_link(Xpr const &xpr, void const *next, xpression_peeker<Char> *peeker)
    {
        this->back_stack_.push(next);
        xpr.link(*this);
        xpr.peek(*peeker);
    }

private:

    ///////////////////////////////////////////////////////////////////////////////
    // alt_link_pred
    //
    struct alt_link_pred
    {
        xpression_linker<Char> *linker_;
        xpression_peeker<Char> *peeker_;
        void const *next_;

        alt_link_pred
        (
            xpression_linker<Char> *linker
          , xpression_peeker<Char> *peeker
          , void const *next
        )
          : linker_(linker)
          , peeker_(peeker)
          , next_(next)
        {
        }

        template<typename Xpr>
        void operator ()(Xpr const &xpr) const
        {
            this->linker_->alt_branch_link(xpr, this->next_, this->peeker_);
        }
    };

    template<typename BidiIter>
    void alt_link
    (
        alternates_vector<BidiIter> const &alternates
      , void const *next
      , xpression_peeker<Char> *peeker
    )
    {
        std::for_each(alternates.begin(), alternates.end(), alt_link_pred(this, peeker, next));
    }

    template<typename Alternates>
    void alt_link
    (
        fusion::sequence_base<Alternates> const &alternates
      , void const *next
      , xpression_peeker<Char> *peeker
    )
    {
#if BOOST_VERSION >= 103500
        fusion::for_each(alternates.derived(), alt_link_pred(this, peeker, next));
#else
        fusion::for_each(alternates.cast(), alt_link_pred(this, peeker, next));
#endif
    }

    template<typename Traits>
    Traits const &get_traits() const
    {
        BOOST_ASSERT(*this->traits_type_ == typeid(Traits));
        return *static_cast<Traits const *>(this->traits_);
    }

    std::stack<void const *> back_stack_;
    void const *traits_;
    std::type_info const *traits_type_;
    bool has_backrefs_;
};

}}} // namespace boost::xpressive::detail

#endif