~ai.tron/armagetronad/0.4-winlibs-updated

« back to all changes in this revision

Viewing changes to boost/includes/boost/xpressive/detail/dynamic/parse_charset.hpp

  • Committer: Manuel Moos
  • Date: 2013-08-10 16:21:05 UTC
  • mfrom: (118.1.64 winlibs-refactor)
  • Revision ID: z-man@users.sf.net-20130810162105-e6v55tas5si5gb3e
Updating boost to 1.53

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
///////////////////////////////////////////////////////////////////////////////
 
2
// parse_charset.hpp
 
3
//
 
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)
 
7
 
 
8
#ifndef BOOST_XPRESSIVE_DETAIL_DYNAMIC_PARSE_CHARSET_HPP_EAN_10_04_2005
 
9
#define BOOST_XPRESSIVE_DETAIL_DYNAMIC_PARSE_CHARSET_HPP_EAN_10_04_2005
 
10
 
 
11
// MS compatible compilers support #pragma once
 
12
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
 
13
# pragma once
 
14
#endif
 
15
 
 
16
#include <boost/integer.hpp>
 
17
#include <boost/mpl/bool.hpp>
 
18
#include <boost/throw_exception.hpp>
 
19
#include <boost/numeric/conversion/converter.hpp>
 
20
#include <boost/xpressive/detail/detail_fwd.hpp>
 
21
#include <boost/xpressive/detail/dynamic/parser_enum.hpp>
 
22
#include <boost/xpressive/detail/utility/literals.hpp>
 
23
#include <boost/xpressive/detail/utility/chset/chset.hpp>
 
24
#include <boost/xpressive/regex_constants.hpp>
 
25
 
 
26
namespace boost { namespace xpressive { namespace detail
 
27
{
 
28
 
 
29
enum escape_type
 
30
{
 
31
    escape_char
 
32
  , escape_mark
 
33
  , escape_class
 
34
};
 
35
 
 
36
///////////////////////////////////////////////////////////////////////////////
 
37
// escape_value
 
38
//
 
39
template<typename Char, typename Class>
 
40
struct escape_value
 
41
{
 
42
    Char ch_;
 
43
    int mark_nbr_;
 
44
    Class class_;
 
45
    escape_type type_;
 
46
};
 
47
 
 
48
///////////////////////////////////////////////////////////////////////////////
 
49
// char_overflow_handler
 
50
//
 
51
struct char_overflow_handler
 
52
{
 
53
    void operator ()(numeric::range_check_result result) const // throw(regex_error)
 
54
    {
 
55
        if(numeric::cInRange != result)
 
56
        {
 
57
            BOOST_THROW_EXCEPTION(
 
58
                regex_error(
 
59
                    regex_constants::error_escape
 
60
                  , "character escape too large to fit in target character type"
 
61
                )
 
62
            );
 
63
        }
 
64
    }
 
65
};
 
66
 
 
67
///////////////////////////////////////////////////////////////////////////////
 
68
// parse_escape
 
69
//
 
70
template<typename FwdIter, typename CompilerTraits>
 
71
escape_value<typename iterator_value<FwdIter>::type, typename CompilerTraits::regex_traits::char_class_type>
 
72
parse_escape(FwdIter &begin, FwdIter end, CompilerTraits &tr)
 
73
{
 
74
    using namespace regex_constants;
 
75
    typedef typename iterator_value<FwdIter>::type char_type;
 
76
    typedef typename CompilerTraits::regex_traits regex_traits;
 
77
    typedef typename regex_traits::char_class_type char_class_type;
 
78
 
 
79
    // define an unsigned type the same size as char_type
 
80
    typedef typename boost::uint_t<CHAR_BIT * sizeof(char_type)>::least uchar_t;
 
81
    BOOST_MPL_ASSERT_RELATION(sizeof(uchar_t), ==, sizeof(char_type));
 
82
    typedef numeric::conversion_traits<uchar_t, int> converstion_traits;
 
83
 
 
84
    BOOST_XPR_ENSURE_(begin != end, error_escape, "unexpected end of pattern found");
 
85
    numeric::converter<int, uchar_t, converstion_traits, char_overflow_handler> converter;
 
86
    escape_value<char_type,char_class_type> esc = { 0, 0, 0, escape_char };
 
87
    bool const icase = (0 != (regex_constants::icase_ & tr.flags()));
 
88
    regex_traits const &rxtraits = tr.traits();
 
89
    FwdIter tmp;
 
90
 
 
91
    esc.class_ = rxtraits.lookup_classname(begin, begin + 1, icase);
 
92
    if(0 != esc.class_)
 
93
    {
 
94
        esc.type_ = escape_class;
 
95
        return esc;
 
96
    }
 
97
 
 
98
    if(-1 != rxtraits.value(*begin, 8))
 
99
    {
 
100
        esc.ch_ = converter(toi(begin, end, rxtraits, 8, 0777));
 
101
        return esc;
 
102
    }
 
103
 
 
104
    switch(*begin)
 
105
    {
 
106
    // bell character
 
107
    case BOOST_XPR_CHAR_(char_type, 'a'):
 
108
        esc.ch_ = BOOST_XPR_CHAR_(char_type, '\a');
 
109
        ++begin;
 
110
        break;
 
111
    // escape character
 
112
    case BOOST_XPR_CHAR_(char_type, 'e'):
 
113
        esc.ch_ = converter(27);
 
114
        ++begin;
 
115
        break;
 
116
    // control character
 
117
    case BOOST_XPR_CHAR_(char_type, 'c'):
 
118
        BOOST_XPR_ENSURE_(++begin != end, error_escape, "unexpected end of pattern found");
 
119
        BOOST_XPR_ENSURE_
 
120
        (
 
121
            rxtraits.in_range(BOOST_XPR_CHAR_(char_type, 'a'), BOOST_XPR_CHAR_(char_type, 'z'), *begin)
 
122
         || rxtraits.in_range(BOOST_XPR_CHAR_(char_type, 'A'), BOOST_XPR_CHAR_(char_type, 'Z'), *begin)
 
123
          , error_escape
 
124
          , "invalid escape control letter; must be one of a-z or A-Z"
 
125
        );
 
126
        // Convert to character according to ECMA-262, section 15.10.2.10:
 
127
        esc.ch_ = converter(*begin % 32);
 
128
        ++begin;
 
129
        break;
 
130
    // formfeed character
 
131
    case BOOST_XPR_CHAR_(char_type, 'f'):
 
132
        esc.ch_ = BOOST_XPR_CHAR_(char_type, '\f');
 
133
        ++begin;
 
134
        break;
 
135
    // newline
 
136
    case BOOST_XPR_CHAR_(char_type, 'n'):
 
137
        esc.ch_ = BOOST_XPR_CHAR_(char_type, '\n');
 
138
        ++begin;
 
139
        break;
 
140
    // return
 
141
    case BOOST_XPR_CHAR_(char_type, 'r'):
 
142
        esc.ch_ = BOOST_XPR_CHAR_(char_type, '\r');
 
143
        ++begin;
 
144
        break;
 
145
    // horizontal tab
 
146
    case BOOST_XPR_CHAR_(char_type, 't'):
 
147
        esc.ch_ = BOOST_XPR_CHAR_(char_type, '\t');
 
148
        ++begin;
 
149
        break;
 
150
    // vertical tab
 
151
    case BOOST_XPR_CHAR_(char_type, 'v'):
 
152
        esc.ch_ = BOOST_XPR_CHAR_(char_type, '\v');
 
153
        ++begin;
 
154
        break;
 
155
    // hex escape sequence
 
156
    case BOOST_XPR_CHAR_(char_type, 'x'):
 
157
        BOOST_XPR_ENSURE_(++begin != end, error_escape, "unexpected end of pattern found");
 
158
        tmp = begin;
 
159
        esc.ch_ = converter(toi(begin, end, rxtraits, 16, 0xff));
 
160
        BOOST_XPR_ENSURE_(2 == std::distance(tmp, begin), error_escape, "invalid hex escape : "
 
161
            "must be \\x HexDigit HexDigit");
 
162
        break;
 
163
    // Unicode escape sequence
 
164
    case BOOST_XPR_CHAR_(char_type, 'u'):
 
165
        BOOST_XPR_ENSURE_(++begin != end, error_escape, "unexpected end of pattern found");
 
166
        tmp = begin;
 
167
        esc.ch_ = converter(toi(begin, end, rxtraits, 16, 0xffff));
 
168
        BOOST_XPR_ENSURE_(4 == std::distance(tmp, begin), error_escape, "invalid Unicode escape : "
 
169
            "must be \\u HexDigit HexDigit HexDigit HexDigit");
 
170
        break;
 
171
    // backslash
 
172
    case BOOST_XPR_CHAR_(char_type, '\\'):
 
173
        //esc.ch_ = BOOST_XPR_CHAR_(char_type, '\\');
 
174
        //++begin;
 
175
        //break;
 
176
    // all other escaped characters represent themselves
 
177
    default:
 
178
        esc.ch_ = *begin;
 
179
        ++begin;
 
180
        break;
 
181
    }
 
182
 
 
183
    return esc;
 
184
}
 
185
 
 
186
//////////////////////////////////////////////////////////////////////////
 
187
// parse_charset
 
188
//
 
189
template<typename FwdIter, typename RegexTraits, typename CompilerTraits>
 
190
inline void parse_charset
 
191
(
 
192
    FwdIter &begin
 
193
  , FwdIter end
 
194
  , compound_charset<RegexTraits> &chset
 
195
  , CompilerTraits &tr
 
196
)
 
197
{
 
198
    using namespace regex_constants;
 
199
    typedef typename RegexTraits::char_type char_type;
 
200
    typedef typename RegexTraits::char_class_type char_class_type;
 
201
    BOOST_ASSERT(begin != end);
 
202
    RegexTraits const &rxtraits = tr.traits();
 
203
    bool const icase = (0 != (regex_constants::icase_ & tr.flags()));
 
204
    FwdIter iprev = FwdIter();
 
205
    escape_value<char_type, char_class_type> esc = {0, 0, 0, escape_char};
 
206
    bool invert = false;
 
207
 
 
208
    // check to see if we have an inverse charset
 
209
    if(begin != end && token_charset_invert == tr.get_charset_token(iprev = begin, end))
 
210
    {
 
211
        begin = iprev;
 
212
        invert = true;
 
213
    }
 
214
 
 
215
    // skip the end token if-and-only-if it is the first token in the charset
 
216
    if(begin != end && token_charset_end == tr.get_charset_token(iprev = begin, end))
 
217
    {
 
218
        for(; begin != iprev; ++begin)
 
219
        {
 
220
            chset.set_char(*begin, rxtraits, icase);
 
221
        }
 
222
    }
 
223
 
 
224
    compiler_token_type tok;
 
225
    char_type ch_prev = char_type(), ch_next = char_type();
 
226
    bool have_prev = false;
 
227
 
 
228
    BOOST_XPR_ENSURE_(begin != end, error_brack, "unexpected end of pattern found");
 
229
 
 
230
    // remember the current position and grab the next token
 
231
    iprev = begin;
 
232
    tok = tr.get_charset_token(begin, end);
 
233
    do
 
234
    {
 
235
        BOOST_XPR_ENSURE_(begin != end, error_brack, "unexpected end of pattern found");
 
236
 
 
237
        if(token_charset_hyphen == tok && have_prev)
 
238
        {
 
239
            // remember the current position
 
240
            FwdIter iprev2 = begin;
 
241
            have_prev = false;
 
242
 
 
243
            // ch_prev is lower bound of a range
 
244
            switch(tr.get_charset_token(begin, end))
 
245
            {
 
246
            case token_charset_hyphen:
 
247
            case token_charset_invert:
 
248
                begin = iprev2; // un-get these tokens and fall through
 
249
            case token_literal:
 
250
                ch_next = *begin++;
 
251
                BOOST_XPR_ENSURE_(ch_prev <= ch_next, error_range, "invalid charset range");
 
252
                chset.set_range(ch_prev, ch_next, rxtraits, icase);
 
253
                continue;
 
254
            case token_charset_backspace:
 
255
                ch_next = char_type(8); // backspace
 
256
                BOOST_XPR_ENSURE_(ch_prev <= ch_next, error_range, "invalid charset range");
 
257
                chset.set_range(ch_prev, ch_next, rxtraits, icase);
 
258
                continue;
 
259
            case token_escape:
 
260
                esc = parse_escape(begin, end, tr);
 
261
                if(escape_char == esc.type_)
 
262
                {
 
263
                    BOOST_XPR_ENSURE_(ch_prev <= esc.ch_, error_range, "invalid charset range");
 
264
                    chset.set_range(ch_prev, esc.ch_, rxtraits, icase);
 
265
                    continue;
 
266
                }
 
267
            case token_charset_end: // fall through
 
268
            default:                // not a range.
 
269
                begin = iprev;      // backup to hyphen token
 
270
                chset.set_char(ch_prev, rxtraits, icase);
 
271
                chset.set_char(*begin++, rxtraits, icase);
 
272
                continue;
 
273
            }
 
274
        }
 
275
 
 
276
        if(have_prev)
 
277
        {
 
278
            chset.set_char(ch_prev, rxtraits, icase);
 
279
            have_prev = false;
 
280
        }
 
281
 
 
282
        switch(tok)
 
283
        {
 
284
        case token_charset_hyphen:
 
285
        case token_charset_invert:
 
286
        case token_charset_end:
 
287
        case token_posix_charset_end:
 
288
            begin = iprev; // un-get these tokens
 
289
            ch_prev = *begin++;
 
290
            have_prev = true;
 
291
            continue;
 
292
 
 
293
        case token_charset_backspace:
 
294
            ch_prev = char_type(8); // backspace
 
295
            have_prev = true;
 
296
            continue;
 
297
 
 
298
        case token_posix_charset_begin:
 
299
            {
 
300
                FwdIter tmp = begin, start = begin;
 
301
                bool invert = (token_charset_invert == tr.get_charset_token(tmp, end));
 
302
                if(invert)
 
303
                {
 
304
                    begin = start = tmp;
 
305
                }
 
306
                while(token_literal == (tok = tr.get_charset_token(begin, end)))
 
307
                {
 
308
                    tmp = ++begin;
 
309
                    BOOST_XPR_ENSURE_(begin != end, error_brack, "unexpected end of pattern found");
 
310
                }
 
311
                if(token_posix_charset_end == tok)
 
312
                {
 
313
                    char_class_type chclass = rxtraits.lookup_classname(start, tmp, icase);
 
314
                    BOOST_XPR_ENSURE_(0 != chclass, error_ctype, "unknown class name");
 
315
                    chset.set_class(chclass, invert);
 
316
                    continue;
 
317
                }
 
318
                begin = iprev; // un-get this token
 
319
                ch_prev = *begin++;
 
320
                have_prev = true;
 
321
            }
 
322
            continue;
 
323
 
 
324
        case token_escape:
 
325
            esc = parse_escape(begin, end, tr);
 
326
            if(escape_char == esc.type_)
 
327
            {
 
328
                ch_prev = esc.ch_;
 
329
                have_prev = true;
 
330
            }
 
331
            else if(escape_class == esc.type_)
 
332
            {
 
333
                char_class_type upper_ = lookup_classname(rxtraits, "upper");
 
334
                BOOST_ASSERT(0 != upper_);
 
335
                chset.set_class(esc.class_, rxtraits.isctype(*begin++, upper_));
 
336
            }
 
337
            else
 
338
            {
 
339
                BOOST_ASSERT(false);
 
340
            }
 
341
            continue;
 
342
 
 
343
        default:
 
344
            ch_prev = *begin++;
 
345
            have_prev = true;
 
346
            continue;
 
347
        }
 
348
    }
 
349
    while(BOOST_XPR_ENSURE_((iprev = begin) != end, error_brack, "unexpected end of pattern found"),
 
350
          token_charset_end != (tok = tr.get_charset_token(begin, end)));
 
351
 
 
352
    if(have_prev)
 
353
    {
 
354
        chset.set_char(ch_prev, rxtraits, icase);
 
355
    }
 
356
 
 
357
    if(invert)
 
358
    {
 
359
        chset.inverse();
 
360
    }
 
361
}
 
362
 
 
363
}}} // namespace boost::xpressive::detail
 
364
 
 
365
#endif