summaryrefslogtreecommitdiff
path: root/boost/xpressive/detail/dynamic/parser.hpp
blob: 00d4bbf37570903ba007dbcc5a1bafaa3231e702 (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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
///////////////////////////////////////////////////////////////////////////////
/// \file parser.hpp
/// Contains the definition of regex_compiler, a factory for building regex objects
/// from strings.
//
//  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_DYNAMIC_PARSER_HPP_EAN_10_04_2005
#define BOOST_XPRESSIVE_DETAIL_DYNAMIC_PARSER_HPP_EAN_10_04_2005

// MS compatible compilers support #pragma once
#if defined(_MSC_VER)
# pragma once
# pragma warning(push)
# pragma warning(disable : 4127) // conditional expression is constant
#endif

#include <boost/assert.hpp>
#include <boost/xpressive/regex_constants.hpp>
#include <boost/xpressive/detail/detail_fwd.hpp>
#include <boost/xpressive/detail/core/matchers.hpp>
#include <boost/xpressive/detail/utility/ignore_unused.hpp>
#include <boost/xpressive/detail/dynamic/dynamic.hpp>

// The Regular Expression grammar, in pseudo BNF:
//
// expression   = alternates ;
//
// alternates   = sequence, *('|', sequence) ;
//
// sequence     = quant, *(quant) ;
//
// quant        = atom, [*+?] ;
//
// atom         = literal             |
//                '.'                 |
//                '\' any             |
//                '(' expression ')' ;
//
// literal      = not a meta-character ;
//

namespace boost { namespace xpressive { namespace detail
{

///////////////////////////////////////////////////////////////////////////////
// make_char_xpression
//
template<typename BidiIter, typename Char, typename Traits>
inline sequence<BidiIter> make_char_xpression
(
    Char ch
  , regex_constants::syntax_option_type flags
  , Traits const &tr
)
{
    if(0 != (regex_constants::icase_ & flags))
    {
        literal_matcher<Traits, mpl::true_, mpl::false_> matcher(ch, tr);
        return make_dynamic<BidiIter>(matcher);
    }
    else
    {
        literal_matcher<Traits, mpl::false_, mpl::false_> matcher(ch, tr);
        return make_dynamic<BidiIter>(matcher);
    }
}

///////////////////////////////////////////////////////////////////////////////
// make_any_xpression
//
template<typename BidiIter, typename Traits>
inline sequence<BidiIter> make_any_xpression
(
    regex_constants::syntax_option_type flags
  , Traits const &tr
)
{
    using namespace regex_constants;
    typedef typename iterator_value<BidiIter>::type char_type;
    typedef detail::set_matcher<Traits, mpl::int_<2> > set_matcher;
    typedef literal_matcher<Traits, mpl::false_, mpl::true_> literal_matcher;

    char_type const newline = tr.widen('\n');
    set_matcher s;
    s.set_[0] = newline;
    s.set_[1] = 0;
    s.inverse();

    switch(((int)not_dot_newline | not_dot_null) & flags)
    {
    case not_dot_null:
        return make_dynamic<BidiIter>(literal_matcher(char_type(0), tr));

    case not_dot_newline:
        return make_dynamic<BidiIter>(literal_matcher(newline, tr));

    case (int)not_dot_newline | not_dot_null:
        return make_dynamic<BidiIter>(s);

    default:
        return make_dynamic<BidiIter>(any_matcher());
    }
}

///////////////////////////////////////////////////////////////////////////////
// make_literal_xpression
//
template<typename BidiIter, typename Traits>
inline sequence<BidiIter> make_literal_xpression
(
    typename Traits::string_type const &literal
  , regex_constants::syntax_option_type flags
  , Traits const &tr
)
{
    BOOST_ASSERT(0 != literal.size());
    if(1 == literal.size())
    {
        return make_char_xpression<BidiIter>(literal[0], flags, tr);
    }

    if(0 != (regex_constants::icase_ & flags))
    {
        string_matcher<Traits, mpl::true_> matcher(literal, tr);
        return make_dynamic<BidiIter>(matcher);
    }
    else
    {
        string_matcher<Traits, mpl::false_> matcher(literal, tr);
        return make_dynamic<BidiIter>(matcher);
    }
}

///////////////////////////////////////////////////////////////////////////////
// make_backref_xpression
//
template<typename BidiIter, typename Traits>
inline sequence<BidiIter> make_backref_xpression
(
    int mark_nbr
  , regex_constants::syntax_option_type flags
  , Traits const &tr
)
{
    if(0 != (regex_constants::icase_ & flags))
    {
        return make_dynamic<BidiIter>
        (
            mark_matcher<Traits, mpl::true_>(mark_nbr, tr)
        );
    }
    else
    {
        return make_dynamic<BidiIter>
        (
            mark_matcher<Traits, mpl::false_>(mark_nbr, tr)
        );
    }
}

///////////////////////////////////////////////////////////////////////////////
// merge_charset
//
template<typename Char, typename Traits>
inline void merge_charset
(
    basic_chset<Char> &basic
  , compound_charset<Traits> const &compound
  , Traits const &tr
)
{
    detail::ignore_unused(tr);
    if(0 != compound.posix_yes())
    {
        typename Traits::char_class_type mask = compound.posix_yes();
        for(int i = 0; i <= static_cast<int>(UCHAR_MAX); ++i)
        {
            if(tr.isctype((Char)i, mask))
            {
                basic.set((Char)i);
            }
        }
    }

    if(!compound.posix_no().empty())
    {
        for(std::size_t j = 0; j < compound.posix_no().size(); ++j)
        {
            typename Traits::char_class_type mask = compound.posix_no()[j];
            for(int i = 0; i <= static_cast<int>(UCHAR_MAX); ++i)
            {
                if(!tr.isctype((Char)i, mask))
                {
                    basic.set((Char)i);
                }
            }
        }
    }

    if(compound.is_inverted())
    {
        basic.inverse();
    }
}

///////////////////////////////////////////////////////////////////////////////
// make_charset_xpression
//
template<typename BidiIter, typename Traits>
inline sequence<BidiIter> make_charset_xpression
(
    compound_charset<Traits> &chset
  , Traits const &tr
  , regex_constants::syntax_option_type flags
)
{
    typedef typename Traits::char_type char_type;
    bool const icase = (0 != (regex_constants::icase_ & flags));
    bool const optimize = is_narrow_char<char_type>::value && 0 != (regex_constants::optimize & flags);

    // don't care about compile speed -- fold eveything into a bitset<256>
    if(optimize)
    {
        typedef basic_chset<char_type> charset_type;
        charset_type charset(chset.base());
        if(icase)
        {
            charset_matcher<Traits, mpl::true_, charset_type> matcher(charset);
            merge_charset(matcher.charset_, chset, tr);
            return make_dynamic<BidiIter>(matcher);
        }
        else
        {
            charset_matcher<Traits, mpl::false_, charset_type> matcher(charset);
            merge_charset(matcher.charset_, chset, tr);
            return make_dynamic<BidiIter>(matcher);
        }
    }

    // special case to make [[:digit:]] fast
    else if(chset.base().empty() && chset.posix_no().empty())
    {
        BOOST_ASSERT(0 != chset.posix_yes());
        posix_charset_matcher<Traits> matcher(chset.posix_yes(), chset.is_inverted());
        return make_dynamic<BidiIter>(matcher);
    }

    // default, slow
    else
    {
        if(icase)
        {
            charset_matcher<Traits, mpl::true_> matcher(chset);
            return make_dynamic<BidiIter>(matcher);
        }
        else
        {
            charset_matcher<Traits, mpl::false_> matcher(chset);
            return make_dynamic<BidiIter>(matcher);
        }
    }
}

///////////////////////////////////////////////////////////////////////////////
// make_posix_charset_xpression
//
template<typename BidiIter, typename Traits>
inline sequence<BidiIter> make_posix_charset_xpression
(
    typename Traits::char_class_type m
  , bool no
  , regex_constants::syntax_option_type //flags
  , Traits const & //traits
)
{
    posix_charset_matcher<Traits> charset(m, no);
    return make_dynamic<BidiIter>(charset);
}

///////////////////////////////////////////////////////////////////////////////
// make_assert_begin_line
//
template<typename BidiIter, typename Traits>
inline sequence<BidiIter> make_assert_begin_line
(
    regex_constants::syntax_option_type flags
  , Traits const &tr
)
{
    if(0 != (regex_constants::single_line & flags))
    {
        return detail::make_dynamic<BidiIter>(detail::assert_bos_matcher());
    }
    else
    {
        detail::assert_bol_matcher<Traits> matcher(tr);
        return detail::make_dynamic<BidiIter>(matcher);
    }
}

///////////////////////////////////////////////////////////////////////////////
// make_assert_end_line
//
template<typename BidiIter, typename Traits>
inline sequence<BidiIter> make_assert_end_line
(
    regex_constants::syntax_option_type flags
  , Traits const &tr
)
{
    if(0 != (regex_constants::single_line & flags))
    {
        return detail::make_dynamic<BidiIter>(detail::assert_eos_matcher());
    }
    else
    {
        detail::assert_eol_matcher<Traits> matcher(tr);
        return detail::make_dynamic<BidiIter>(matcher);
    }
}

///////////////////////////////////////////////////////////////////////////////
// make_assert_word
//
template<typename BidiIter, typename Cond, typename Traits>
inline sequence<BidiIter> make_assert_word(Cond, Traits const &tr)
{
    return detail::make_dynamic<BidiIter>
    (
        detail::assert_word_matcher<Cond, Traits>(tr)
    );
}

///////////////////////////////////////////////////////////////////////////////
// make_independent_end_xpression
//
template<typename BidiIter>
inline sequence<BidiIter> make_independent_end_xpression(bool pure)
{
    if(pure)
    {
        return detail::make_dynamic<BidiIter>(detail::true_matcher());
    }
    else
    {
        return detail::make_dynamic<BidiIter>(detail::independent_end_matcher());
    }
}

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

#if defined(_MSC_VER)
# pragma warning(pop)
#endif

#endif