[857] | 1 | /*=============================================================================
|
---|
| 2 | Boost.Wave: A Standard compliant C++ preprocessor library
|
---|
| 3 |
|
---|
| 4 | Grammar for universal character validation (see C++ standard: Annex E)
|
---|
| 5 |
|
---|
| 6 | http://www.boost.org/
|
---|
| 7 |
|
---|
| 8 | Copyright (c) 2001-2005 Hartmut Kaiser. Distributed under the Boost
|
---|
| 9 | Software License, Version 1.0. (See accompanying file
|
---|
| 10 | LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
---|
| 11 | =============================================================================*/
|
---|
| 12 | #if !defined(VALIDATE_UNIVERSAL_CHAR_HPP_55F1B811_CD76_4C72_8344_CBC69CF3B339_INCLUDED)
|
---|
| 13 | #define VALIDATE_UNIVERSAL_CHAR_HPP_55F1B811_CD76_4C72_8344_CBC69CF3B339_INCLUDED
|
---|
| 14 |
|
---|
| 15 | #include <boost/assert.hpp>
|
---|
| 16 |
|
---|
| 17 | #include <boost/wave/util/file_position.hpp>
|
---|
| 18 | #include <boost/wave/cpplexer/cpplexer_exceptions.hpp>
|
---|
| 19 |
|
---|
| 20 | ///////////////////////////////////////////////////////////////////////////////
|
---|
| 21 | namespace boost {
|
---|
| 22 | namespace wave {
|
---|
| 23 | namespace cpplexer {
|
---|
| 24 | namespace impl {
|
---|
| 25 |
|
---|
| 26 | enum universal_char_type {
|
---|
| 27 | universal_char_type_valid = 0,
|
---|
| 28 | universal_char_type_invalid = 1,
|
---|
| 29 | universal_char_type_base_charset = 2,
|
---|
| 30 | universal_char_type_not_allowed_for_identifiers = 3
|
---|
| 31 | };
|
---|
| 32 |
|
---|
| 33 | namespace {
|
---|
| 34 |
|
---|
| 35 | ///////////////////////////////////////////////////////////////////////////
|
---|
| 36 | //
|
---|
| 37 | // is_range is a helper function for the classification by brute force
|
---|
| 38 | // below
|
---|
| 39 | //
|
---|
| 40 | ///////////////////////////////////////////////////////////////////////////
|
---|
| 41 | inline bool
|
---|
| 42 | in_range(unsigned long ch, unsigned long l, unsigned long u)
|
---|
| 43 | {
|
---|
| 44 | return (l <= ch && ch <= u);
|
---|
| 45 | }
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | ///////////////////////////////////////////////////////////////////////////////
|
---|
| 49 | //
|
---|
| 50 | // classify_universal_char
|
---|
| 51 | //
|
---|
| 52 | // This function classifies an universal character value into 4 subranges:
|
---|
| 53 | // universal_char_type_valid
|
---|
| 54 | // the universal character value is valid for identifiers
|
---|
| 55 | // universal_char_type_invalid
|
---|
| 56 | // the universal character value is not valid for its usage inside
|
---|
| 57 | // identifiers (see C++ Standard: 2.2.2 [lex.charset])
|
---|
| 58 | // universal_char_type_base_charset
|
---|
| 59 | // the universal character value designates a character from the base
|
---|
| 60 | // character set
|
---|
| 61 | // universal_char_type_not_allowed_for_identifiers
|
---|
| 62 | // the universal character value is not allowed in an identifier
|
---|
| 63 | //
|
---|
| 64 | // Implementation note:
|
---|
| 65 | // This classification isn't implemented very effectively here. This
|
---|
| 66 | // function should be rewritten with some range run matching algorithm.
|
---|
| 67 | //
|
---|
| 68 | ///////////////////////////////////////////////////////////////////////////////
|
---|
| 69 | inline universal_char_type
|
---|
| 70 | classify_universal_char (unsigned long ch)
|
---|
| 71 | {
|
---|
| 72 | // test for invalid characters
|
---|
| 73 | if (ch <= 0x0020 || in_range(ch, 0x007f, 0x009f))
|
---|
| 74 | return universal_char_type_invalid;
|
---|
| 75 |
|
---|
| 76 | // test for characters in the range of the base character set
|
---|
| 77 | if (in_range(ch, 0x0021, 0x005f) || in_range(ch, 0x0061, 0x007e))
|
---|
| 78 | return universal_char_type_base_charset;
|
---|
| 79 |
|
---|
| 80 | // test for additional valid character values (see C++ Standard: Annex E)
|
---|
| 81 | if (in_range(ch, 0x00c0, 0x00d6) || in_range(ch, 0x00d8, 0x00f6) ||
|
---|
| 82 | in_range(ch, 0x00f8, 0x01f5) || in_range(ch, 0x01fa, 0x0217) ||
|
---|
| 83 | in_range(ch, 0x0250, 0x02a8) || in_range(ch, 0x1e00, 0x1e9a) ||
|
---|
| 84 | in_range(ch, 0x1ea0, 0x1ef9))
|
---|
| 85 | {
|
---|
| 86 | return universal_char_type_valid; // Latin
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | if (0x0384 == ch || in_range(ch, 0x0388, 0x038a) ||
|
---|
| 90 | 0x038c == ch || in_range(ch, 0x038e, 0x03a1) ||
|
---|
| 91 | in_range(ch, 0x03a3, 0x03ce) || in_range(ch, 0x03d0, 0x03d6) ||
|
---|
| 92 | 0x03da == ch || 0x03dc == ch || 0x03de == ch || 0x03e0 == ch ||
|
---|
| 93 | in_range(ch, 0x03e2, 0x03f3) || in_range(ch, 0x1f00, 0x1f15) ||
|
---|
| 94 | in_range(ch, 0x1f18, 0x1f1d) || in_range(ch, 0x1f20, 0x1f45) ||
|
---|
| 95 | in_range(ch, 0x1f48, 0x1f4d) || in_range(ch, 0x1f50, 0x1f57) ||
|
---|
| 96 | 0x1f59 == ch || 0x1f5b == ch || 0x1f5d == ch ||
|
---|
| 97 | in_range(ch, 0x1f5f, 0x1f7d) || in_range(ch, 0x1f80, 0x1fb4) ||
|
---|
| 98 | in_range(ch, 0x1fb6, 0x1fbc) || in_range(ch, 0x1fc2, 0x1fc4) ||
|
---|
| 99 | in_range(ch, 0x1fc6, 0x1fcc) || in_range(ch, 0x1fd0, 0x1fd3) ||
|
---|
| 100 | in_range(ch, 0x1fd6, 0x1fdb) || in_range(ch, 0x1fe0, 0x1fec) ||
|
---|
| 101 | in_range(ch, 0x1ff2, 0x1ff4) || in_range(ch, 0x1ff6, 0x1ffc))
|
---|
| 102 | {
|
---|
| 103 | return universal_char_type_valid; // Greek
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | if (in_range(ch, 0x0401, 0x040d) || in_range(ch, 0x040f, 0x044f) ||
|
---|
| 107 | in_range(ch, 0x0451, 0x045c) || in_range(ch, 0x045e, 0x0481) ||
|
---|
| 108 | in_range(ch, 0x0490, 0x04c4) || in_range(ch, 0x04c7, 0x04c8) ||
|
---|
| 109 | in_range(ch, 0x04cb, 0x04cc) || in_range(ch, 0x04d0, 0x04eb) ||
|
---|
| 110 | in_range(ch, 0x04ee, 0x04f5) || in_range(ch, 0x04f8, 0x04f9))
|
---|
| 111 | {
|
---|
| 112 | return universal_char_type_valid; // Cyrillic
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | if (in_range(ch, 0x0531, 0x0556) || in_range(ch, 0x0561, 0x0587))
|
---|
| 116 | return universal_char_type_valid; // Armenian
|
---|
| 117 |
|
---|
| 118 | if (in_range(ch, 0x05d0, 0x05ea) || in_range(ch, 0x05f0, 0x05f4))
|
---|
| 119 | return universal_char_type_valid; // Hebrew
|
---|
| 120 |
|
---|
| 121 | if (in_range(ch, 0x0621, 0x063a) || in_range(ch, 0x0640, 0x0652) ||
|
---|
| 122 | in_range(ch, 0x0670, 0x06b7) || in_range(ch, 0x06ba, 0x06be) ||
|
---|
| 123 | in_range(ch, 0x06c0, 0x06ce) || in_range(ch, 0x06e5, 0x06e7))
|
---|
| 124 | {
|
---|
| 125 | return universal_char_type_valid; // Arabic
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | if (in_range(ch, 0x0905, 0x0939) || in_range(ch, 0x0958, 0x0962))
|
---|
| 129 | return universal_char_type_valid; // Devanagari
|
---|
| 130 |
|
---|
| 131 | if (in_range(ch, 0x0985, 0x098c) || in_range(ch, 0x098f, 0x0990) ||
|
---|
| 132 | in_range(ch, 0x0993, 0x09a8) || in_range(ch, 0x09aa, 0x09b0) ||
|
---|
| 133 | 0x09b2 == ch || in_range(ch, 0x09b6, 0x09b9) ||
|
---|
| 134 | in_range(ch, 0x09dc, 0x09dd) || in_range(ch, 0x09df, 0x09e1) ||
|
---|
| 135 | in_range(ch, 0x09f0, 0x09f1))
|
---|
| 136 | {
|
---|
| 137 | return universal_char_type_valid; // Bengali
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | if (in_range(ch, 0x0a05, 0x0a0a) || in_range(ch, 0x0a0f, 0x0a10) ||
|
---|
| 141 | in_range(ch, 0x0a13, 0x0a28) || in_range(ch, 0x0a2a, 0x0a30) ||
|
---|
| 142 | in_range(ch, 0x0a32, 0x0a33) || in_range(ch, 0x0a35, 0x0a36) ||
|
---|
| 143 | in_range(ch, 0x0a38, 0x0a39) || in_range(ch, 0x0a59, 0x0a5c) ||
|
---|
| 144 | 0x0a5e == ch)
|
---|
| 145 | {
|
---|
| 146 | return universal_char_type_valid; // Gurmukhi
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | if (in_range(ch, 0x0a85, 0x0a8b) || 0x0a8d == ch ||
|
---|
| 150 | in_range(ch, 0x0a8f, 0x0a91) || in_range(ch, 0x0a93, 0x0aa8) ||
|
---|
| 151 | in_range(ch, 0x0aaa, 0x0ab0) || in_range(ch, 0x0ab2, 0x0ab3) ||
|
---|
| 152 | in_range(ch, 0x0ab5, 0x0ab9) || 0x0ae0 == ch)
|
---|
| 153 | {
|
---|
| 154 | return universal_char_type_valid; // Gujarati
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | if (in_range(ch, 0x0b05, 0x0b0c) || in_range(ch, 0x0b0f, 0x0b10) ||
|
---|
| 158 | in_range(ch, 0x0b13, 0x0b28) || in_range(ch, 0x0b2a, 0x0b30) ||
|
---|
| 159 | in_range(ch, 0x0b32, 0x0b33) || in_range(ch, 0x0b36, 0x0b39) ||
|
---|
| 160 | in_range(ch, 0x0b5c, 0x0b5d) || in_range(ch, 0x0b5f, 0x0b61))
|
---|
| 161 | {
|
---|
| 162 | return universal_char_type_valid; // Oriya
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | if (in_range(ch, 0x0b85, 0x0b8a) || in_range(ch, 0x0b8e, 0x0b90) ||
|
---|
| 166 | in_range(ch, 0x0b92, 0x0b95) || in_range(ch, 0x0b99, 0x0b9a) ||
|
---|
| 167 | 0x0b9c == ch || in_range(ch, 0x0b9e, 0x0b9f) ||
|
---|
| 168 | in_range(ch, 0x0ba3, 0x0ba4) || in_range(ch, 0x0ba8, 0x0baa) ||
|
---|
| 169 | in_range(ch, 0x0bae, 0x0bb5) || in_range(ch, 0x0bb7, 0x0bb9))
|
---|
| 170 | {
|
---|
| 171 | return universal_char_type_valid; // Tamil
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | if (in_range(ch, 0x0c05, 0x0c0c) || in_range(ch, 0x0c0e, 0x0c10) ||
|
---|
| 175 | in_range(ch, 0x0c12, 0x0c28) || in_range(ch, 0x0c2a, 0x0c33) ||
|
---|
| 176 | in_range(ch, 0x0c35, 0x0c39) || in_range(ch, 0x0c60, 0x0c61))
|
---|
| 177 | {
|
---|
| 178 | return universal_char_type_valid; // Telugu
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | if (in_range(ch, 0x0c85, 0x0c8c) || in_range(ch, 0x0c8e, 0x0c90) ||
|
---|
| 182 | in_range(ch, 0x0c92, 0x0ca8) || in_range(ch, 0x0caa, 0x0cb3) ||
|
---|
| 183 | in_range(ch, 0x0cb5, 0x0cb9) || in_range(ch, 0x0ce0, 0x0ce1))
|
---|
| 184 | {
|
---|
| 185 | return universal_char_type_valid; // Kannada
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 | if (in_range(ch, 0x0d05, 0x0d0c) || in_range(ch, 0x0d0e, 0x0d10) ||
|
---|
| 189 | in_range(ch, 0x0d12, 0x0d28) || in_range(ch, 0x0d2a, 0x0d39) ||
|
---|
| 190 | in_range(ch, 0x0d60, 0x0d61))
|
---|
| 191 | {
|
---|
| 192 | return universal_char_type_valid; // Malayalam
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | if (in_range(ch, 0x0e01, 0x0e30) || in_range(ch, 0x0e32, 0x0e33) ||
|
---|
| 196 | in_range(ch, 0x0e40, 0x0e46) || in_range(ch, 0x0e4f, 0x0e5b))
|
---|
| 197 | {
|
---|
| 198 | return universal_char_type_valid; // Thai
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | return universal_char_type_not_allowed_for_identifiers;
|
---|
| 202 | }
|
---|
| 203 |
|
---|
| 204 | ///////////////////////////////////////////////////////////////////////////////
|
---|
| 205 | //
|
---|
| 206 | // validate_identifier_name
|
---|
| 207 | //
|
---|
| 208 | // The validate_identifier_name function tests a given identifier name for
|
---|
| 209 | // its validity with regard to eventually contained universal characters.
|
---|
| 210 | // These should be in valid ranges (see the function
|
---|
| 211 | // classify_universal_char above).
|
---|
| 212 | //
|
---|
| 213 | // If the identifier name contains invalid or not allowed universal
|
---|
| 214 | // characters a corresponding lexing_exception is thrown.
|
---|
| 215 | //
|
---|
| 216 | ///////////////////////////////////////////////////////////////////////////////
|
---|
| 217 | template <typename StringT>
|
---|
| 218 | inline void
|
---|
| 219 | validate_identifier_name (StringT const &name, int line, int column,
|
---|
| 220 | StringT const &file_name)
|
---|
| 221 | {
|
---|
| 222 | using namespace std; // some systems have strtoul in namespace std::
|
---|
| 223 |
|
---|
| 224 | typename StringT::size_type pos = name.find_first_of('\\');
|
---|
| 225 |
|
---|
| 226 | while (StringT::npos != pos) {
|
---|
| 227 | // the identifier name contains a backslash (must be universal char)
|
---|
| 228 | BOOST_ASSERT('u' == name[pos+1] || 'U' == name[pos+1]);
|
---|
| 229 |
|
---|
| 230 | StringT uchar_val(name.substr(pos+2, ('u' == name[pos+1]) ? 4 : 8));
|
---|
| 231 | universal_char_type type =
|
---|
| 232 | classify_universal_char(strtoul(uchar_val.c_str(), 0, 16));
|
---|
| 233 |
|
---|
| 234 | if (universal_char_type_valid != type) {
|
---|
| 235 | // an invalid char was found, so throw an exception
|
---|
| 236 | StringT error_uchar(name.substr(pos, ('u' == name[pos+1]) ? 6 : 10));
|
---|
| 237 |
|
---|
| 238 | if (universal_char_type_invalid == type) {
|
---|
| 239 | BOOST_WAVE_LEXER_THROW(lexing_exception, universal_char_invalid,
|
---|
| 240 | error_uchar, line, column, file_name.c_str());
|
---|
| 241 | }
|
---|
| 242 | else if (universal_char_type_base_charset == type) {
|
---|
| 243 | BOOST_WAVE_LEXER_THROW(lexing_exception, universal_char_base_charset,
|
---|
| 244 | error_uchar, line, column, file_name.c_str());
|
---|
| 245 | }
|
---|
| 246 | else {
|
---|
| 247 | BOOST_WAVE_LEXER_THROW(lexing_exception, universal_char_not_allowed,
|
---|
| 248 | error_uchar, line, column, file_name.c_str());
|
---|
| 249 | }
|
---|
| 250 | }
|
---|
| 251 |
|
---|
| 252 | // find next universal char (if appropriate)
|
---|
| 253 | pos = name.find_first_of('\\', pos+2);
|
---|
| 254 | }
|
---|
| 255 | }
|
---|
| 256 |
|
---|
| 257 | ///////////////////////////////////////////////////////////////////////////////
|
---|
| 258 | //
|
---|
| 259 | // validate_literal
|
---|
| 260 | //
|
---|
| 261 | // The validate_literal function tests a given string or character literal
|
---|
| 262 | // for its validity with regard to eventually contained universal
|
---|
| 263 | // characters. These should be in valid ranges (see the function
|
---|
| 264 | // classify_universal_char above).
|
---|
| 265 | //
|
---|
| 266 | // If the string or character literal contains invalid or not allowed
|
---|
| 267 | // universal characters a corresponding lexing_exception is thrown.
|
---|
| 268 | //
|
---|
| 269 | ///////////////////////////////////////////////////////////////////////////////
|
---|
| 270 | template <typename StringT>
|
---|
| 271 | inline void
|
---|
| 272 | validate_literal (StringT const &name, int line, int column,
|
---|
| 273 | StringT const &file_name)
|
---|
| 274 | {
|
---|
| 275 | using namespace std; // some systems have strtoul in namespace std::
|
---|
| 276 |
|
---|
| 277 | typename StringT::size_type pos = name.find_first_of('\\');
|
---|
| 278 |
|
---|
| 279 | while (StringT::npos != pos) {
|
---|
| 280 | // the literal contains a backslash (may be universal char)
|
---|
| 281 | if ('u' == name[pos+1] || 'U' == name[pos+1]) {
|
---|
| 282 | StringT uchar_val(name.substr(pos+2, ('u' == name[pos+1]) ? 4 : 8));
|
---|
| 283 | universal_char_type type =
|
---|
| 284 | classify_universal_char(strtoul(uchar_val.c_str(), 0, 16));
|
---|
| 285 |
|
---|
| 286 | if (universal_char_type_valid != type &&
|
---|
| 287 | universal_char_type_not_allowed_for_identifiers != type)
|
---|
| 288 | {
|
---|
| 289 | // an invalid char was found, so throw an exception
|
---|
| 290 | StringT error_uchar(name.substr(pos, ('u' == name[pos+1]) ? 6 : 10));
|
---|
| 291 |
|
---|
| 292 | if (universal_char_type_invalid == type) {
|
---|
| 293 | BOOST_WAVE_LEXER_THROW(lexing_exception, universal_char_invalid,
|
---|
| 294 | error_uchar, line, column, file_name.c_str());
|
---|
| 295 | }
|
---|
| 296 | else {
|
---|
| 297 | BOOST_WAVE_LEXER_THROW(lexing_exception, universal_char_base_charset,
|
---|
| 298 | error_uchar, line, column, file_name.c_str());
|
---|
| 299 | }
|
---|
| 300 | }
|
---|
| 301 | }
|
---|
| 302 |
|
---|
| 303 | // find next universal char (if appropriate)
|
---|
| 304 | pos = name.find_first_of('\\', pos+2);
|
---|
| 305 | }
|
---|
| 306 | }
|
---|
| 307 |
|
---|
| 308 | ///////////////////////////////////////////////////////////////////////////////
|
---|
| 309 | } // namespace impl
|
---|
| 310 | } // namespace cpplexer
|
---|
| 311 | } // namespace wave
|
---|
| 312 | } // namespace boost
|
---|
| 313 |
|
---|
| 314 | #endif // !defined(VALIDATE_UNIVERSAL_CHAR_HPP_55F1B811_CD76_4C72_8344_CBC69CF3B339_INCLUDED)
|
---|