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_PARSE_CHARSET_HPP_EAN_10_04_2005
9
#define BOOST_XPRESSIVE_DETAIL_DYNAMIC_PARSE_CHARSET_HPP_EAN_10_04_2005
11
// MS compatible compilers support #pragma once
12
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
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>
26
namespace boost { namespace xpressive { namespace detail
36
///////////////////////////////////////////////////////////////////////////////
39
template<typename Char, typename Class>
48
///////////////////////////////////////////////////////////////////////////////
49
// char_overflow_handler
51
struct char_overflow_handler
53
void operator ()(numeric::range_check_result result) const // throw(regex_error)
55
if(numeric::cInRange != result)
57
BOOST_THROW_EXCEPTION(
59
regex_constants::error_escape
60
, "character escape too large to fit in target character type"
67
///////////////////////////////////////////////////////////////////////////////
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)
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;
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;
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();
91
esc.class_ = rxtraits.lookup_classname(begin, begin + 1, icase);
94
esc.type_ = escape_class;
98
if(-1 != rxtraits.value(*begin, 8))
100
esc.ch_ = converter(toi(begin, end, rxtraits, 8, 0777));
107
case BOOST_XPR_CHAR_(char_type, 'a'):
108
esc.ch_ = BOOST_XPR_CHAR_(char_type, '\a');
112
case BOOST_XPR_CHAR_(char_type, 'e'):
113
esc.ch_ = converter(27);
117
case BOOST_XPR_CHAR_(char_type, 'c'):
118
BOOST_XPR_ENSURE_(++begin != end, error_escape, "unexpected end of pattern found");
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)
124
, "invalid escape control letter; must be one of a-z or A-Z"
126
// Convert to character according to ECMA-262, section 15.10.2.10:
127
esc.ch_ = converter(*begin % 32);
130
// formfeed character
131
case BOOST_XPR_CHAR_(char_type, 'f'):
132
esc.ch_ = BOOST_XPR_CHAR_(char_type, '\f');
136
case BOOST_XPR_CHAR_(char_type, 'n'):
137
esc.ch_ = BOOST_XPR_CHAR_(char_type, '\n');
141
case BOOST_XPR_CHAR_(char_type, 'r'):
142
esc.ch_ = BOOST_XPR_CHAR_(char_type, '\r');
146
case BOOST_XPR_CHAR_(char_type, 't'):
147
esc.ch_ = BOOST_XPR_CHAR_(char_type, '\t');
151
case BOOST_XPR_CHAR_(char_type, 'v'):
152
esc.ch_ = BOOST_XPR_CHAR_(char_type, '\v');
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");
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");
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");
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");
172
case BOOST_XPR_CHAR_(char_type, '\\'):
173
//esc.ch_ = BOOST_XPR_CHAR_(char_type, '\\');
176
// all other escaped characters represent themselves
186
//////////////////////////////////////////////////////////////////////////
189
template<typename FwdIter, typename RegexTraits, typename CompilerTraits>
190
inline void parse_charset
194
, compound_charset<RegexTraits> &chset
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};
208
// check to see if we have an inverse charset
209
if(begin != end && token_charset_invert == tr.get_charset_token(iprev = begin, end))
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))
218
for(; begin != iprev; ++begin)
220
chset.set_char(*begin, rxtraits, icase);
224
compiler_token_type tok;
225
char_type ch_prev = char_type(), ch_next = char_type();
226
bool have_prev = false;
228
BOOST_XPR_ENSURE_(begin != end, error_brack, "unexpected end of pattern found");
230
// remember the current position and grab the next token
232
tok = tr.get_charset_token(begin, end);
235
BOOST_XPR_ENSURE_(begin != end, error_brack, "unexpected end of pattern found");
237
if(token_charset_hyphen == tok && have_prev)
239
// remember the current position
240
FwdIter iprev2 = begin;
243
// ch_prev is lower bound of a range
244
switch(tr.get_charset_token(begin, end))
246
case token_charset_hyphen:
247
case token_charset_invert:
248
begin = iprev2; // un-get these tokens and fall through
251
BOOST_XPR_ENSURE_(ch_prev <= ch_next, error_range, "invalid charset range");
252
chset.set_range(ch_prev, ch_next, rxtraits, icase);
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);
260
esc = parse_escape(begin, end, tr);
261
if(escape_char == esc.type_)
263
BOOST_XPR_ENSURE_(ch_prev <= esc.ch_, error_range, "invalid charset range");
264
chset.set_range(ch_prev, esc.ch_, rxtraits, icase);
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);
278
chset.set_char(ch_prev, rxtraits, icase);
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
293
case token_charset_backspace:
294
ch_prev = char_type(8); // backspace
298
case token_posix_charset_begin:
300
FwdIter tmp = begin, start = begin;
301
bool invert = (token_charset_invert == tr.get_charset_token(tmp, end));
306
while(token_literal == (tok = tr.get_charset_token(begin, end)))
309
BOOST_XPR_ENSURE_(begin != end, error_brack, "unexpected end of pattern found");
311
if(token_posix_charset_end == tok)
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);
318
begin = iprev; // un-get this token
325
esc = parse_escape(begin, end, tr);
326
if(escape_char == esc.type_)
331
else if(escape_class == esc.type_)
333
char_class_type upper_ = lookup_classname(rxtraits, "upper");
334
BOOST_ASSERT(0 != upper_);
335
chset.set_class(esc.class_, rxtraits.isctype(*begin++, upper_));
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)));
354
chset.set_char(ch_prev, rxtraits, icase);
363
}}} // namespace boost::xpressive::detail