Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:53:39

0001 /*=============================================================================
0002     Boost.Wave: A Standard compliant C++ preprocessor library
0003 
0004     Grammar for universal character validation (see C++ standard: Annex E)
0005 
0006     http://www.boost.org/
0007 
0008     Copyright (c) 2001-2012 Hartmut Kaiser. Distributed under the Boost
0009     Software License, Version 1.0. (See accompanying file
0010     LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0011 =============================================================================*/
0012 #if !defined(BOOST_VALIDATE_UNIVERSAL_CHAR_HPP_55F1B811_CD76_4C72_8344_CBC69CF3B339_INCLUDED)
0013 #define BOOST_VALIDATE_UNIVERSAL_CHAR_HPP_55F1B811_CD76_4C72_8344_CBC69CF3B339_INCLUDED
0014 
0015 #include <boost/assert.hpp>
0016 
0017 #include <boost/wave/wave_config.hpp>
0018 #include <boost/wave/util/file_position.hpp>
0019 #include <boost/wave/cpplexer/cpplexer_exceptions.hpp>
0020 
0021 // this must occur after all of the includes and before any code appears
0022 #ifdef BOOST_HAS_ABI_HEADERS
0023 #include BOOST_ABI_PREFIX
0024 #endif
0025 
0026 ///////////////////////////////////////////////////////////////////////////////
0027 namespace boost {
0028 namespace wave {
0029 namespace cpplexer {
0030 namespace impl {
0031 
0032 enum universal_char_type {
0033     universal_char_type_valid = 0,
0034     universal_char_type_invalid = 1,
0035     universal_char_type_base_charset = 2,
0036     universal_char_type_not_allowed_for_identifiers = 3
0037 };
0038 
0039 ///////////////////////////////////////////////////////////////////////////
0040 //
0041 //  is_range is a helper function for the classification by brute force
0042 //  below
0043 //
0044 ///////////////////////////////////////////////////////////////////////////
0045 inline bool
0046 in_range(unsigned long ch, unsigned long l, unsigned long u)
0047 {
0048     return (l <= ch && ch <= u);
0049 }
0050 
0051 ///////////////////////////////////////////////////////////////////////////////
0052 //
0053 //  classify_universal_char
0054 //
0055 //      This function classifies an universal character value into 4 subranges:
0056 //      universal_char_type_valid
0057 //          the universal character value is valid for identifiers
0058 //      universal_char_type_invalid
0059 //          the universal character value is not valid for its usage inside
0060 //          identifiers (see C++ Standard: 2.2.2 [lex.charset])
0061 //      universal_char_type_base_charset
0062 //          the universal character value designates a character from the base
0063 //          character set
0064 //      universal_char_type_not_allowed_for_identifiers
0065 //          the universal character value is not allowed in an identifier
0066 //
0067 //  Implementation note:
0068 //      This classification isn't implemented very effectively here. This
0069 //      function should be rewritten with some range run matching algorithm.
0070 //
0071 ///////////////////////////////////////////////////////////////////////////////
0072 inline universal_char_type
0073 classify_universal_char (unsigned long ch)
0074 {
0075     // test for invalid characters
0076     if (ch <= 0x0020 || in_range(ch, 0x007f, 0x009f))
0077         return universal_char_type_invalid;
0078 
0079     // test for characters in the range of the base character set
0080     if (in_range(ch, 0x0021, 0x005f) || in_range(ch, 0x0061, 0x007e))
0081         return universal_char_type_base_charset;
0082 
0083     // test for additional valid character values (see C++ Standard: Annex E)
0084     if (in_range(ch, 0x00c0, 0x00d6) || in_range(ch, 0x00d8, 0x00f6) ||
0085         in_range(ch, 0x00f8, 0x01f5) || in_range(ch, 0x01fa, 0x0217) ||
0086         in_range(ch, 0x0250, 0x02a8) || in_range(ch, 0x1e00, 0x1e9a) ||
0087         in_range(ch, 0x1ea0, 0x1ef9))
0088     {
0089         return universal_char_type_valid;   // Latin
0090     }
0091 
0092     if (0x0384 == ch || in_range(ch, 0x0388, 0x038a) ||
0093         0x038c == ch || in_range(ch, 0x038e, 0x03a1) ||
0094         in_range(ch, 0x03a3, 0x03ce) || in_range(ch, 0x03d0, 0x03d6) ||
0095         0x03da == ch || 0x03dc == ch || 0x03de == ch || 0x03e0 == ch ||
0096         in_range(ch, 0x03e2, 0x03f3) || in_range(ch, 0x1f00, 0x1f15) ||
0097         in_range(ch, 0x1f18, 0x1f1d) || in_range(ch, 0x1f20, 0x1f45) ||
0098         in_range(ch, 0x1f48, 0x1f4d) || in_range(ch, 0x1f50, 0x1f57) ||
0099         0x1f59 == ch || 0x1f5b == ch || 0x1f5d == ch ||
0100         in_range(ch, 0x1f5f, 0x1f7d) || in_range(ch, 0x1f80, 0x1fb4) ||
0101         in_range(ch, 0x1fb6, 0x1fbc) || in_range(ch, 0x1fc2, 0x1fc4) ||
0102         in_range(ch, 0x1fc6, 0x1fcc) || in_range(ch, 0x1fd0, 0x1fd3) ||
0103         in_range(ch, 0x1fd6, 0x1fdb) || in_range(ch, 0x1fe0, 0x1fec) ||
0104         in_range(ch, 0x1ff2, 0x1ff4) || in_range(ch, 0x1ff6, 0x1ffc))
0105     {
0106         return universal_char_type_valid;   // Greek
0107     }
0108 
0109     if (in_range(ch, 0x0401, 0x040d) || in_range(ch, 0x040f, 0x044f) ||
0110         in_range(ch, 0x0451, 0x045c) || in_range(ch, 0x045e, 0x0481) ||
0111         in_range(ch, 0x0490, 0x04c4) || in_range(ch, 0x04c7, 0x04c8) ||
0112         in_range(ch, 0x04cb, 0x04cc) || in_range(ch, 0x04d0, 0x04eb) ||
0113         in_range(ch, 0x04ee, 0x04f5) || in_range(ch, 0x04f8, 0x04f9))
0114     {
0115         return universal_char_type_valid;   // Cyrillic
0116     }
0117 
0118     if (in_range(ch, 0x0531, 0x0556) || in_range(ch, 0x0561, 0x0587))
0119         return universal_char_type_valid;   // Armenian
0120 
0121     if (in_range(ch, 0x05d0, 0x05ea) || in_range(ch, 0x05f0, 0x05f4))
0122         return universal_char_type_valid;   // Hebrew
0123 
0124     if (in_range(ch, 0x0621, 0x063a) || in_range(ch, 0x0640, 0x0652) ||
0125         in_range(ch, 0x0670, 0x06b7) || in_range(ch, 0x06ba, 0x06be) ||
0126         in_range(ch, 0x06c0, 0x06ce) || in_range(ch, 0x06e5, 0x06e7))
0127     {
0128         return universal_char_type_valid;   // Arabic
0129     }
0130 
0131     if (in_range(ch, 0x0905, 0x0939) || in_range(ch, 0x0958, 0x0962))
0132         return universal_char_type_valid;   // Devanagari
0133 
0134     if (in_range(ch, 0x0985, 0x098c) || in_range(ch, 0x098f, 0x0990) ||
0135         in_range(ch, 0x0993, 0x09a8) || in_range(ch, 0x09aa, 0x09b0) ||
0136         0x09b2 == ch || in_range(ch, 0x09b6, 0x09b9) ||
0137         in_range(ch, 0x09dc, 0x09dd) || in_range(ch, 0x09df, 0x09e1) ||
0138         in_range(ch, 0x09f0, 0x09f1))
0139     {
0140         return universal_char_type_valid;   // Bengali
0141     }
0142 
0143     if (in_range(ch, 0x0a05, 0x0a0a) || in_range(ch, 0x0a0f, 0x0a10) ||
0144         in_range(ch, 0x0a13, 0x0a28) || in_range(ch, 0x0a2a, 0x0a30) ||
0145         in_range(ch, 0x0a32, 0x0a33) || in_range(ch, 0x0a35, 0x0a36) ||
0146         in_range(ch, 0x0a38, 0x0a39) || in_range(ch, 0x0a59, 0x0a5c) ||
0147         0x0a5e == ch)
0148     {
0149         return universal_char_type_valid;   // Gurmukhi
0150     }
0151 
0152     if (in_range(ch, 0x0a85, 0x0a8b) || 0x0a8d == ch ||
0153         in_range(ch, 0x0a8f, 0x0a91) || in_range(ch, 0x0a93, 0x0aa8) ||
0154         in_range(ch, 0x0aaa, 0x0ab0) || in_range(ch, 0x0ab2, 0x0ab3) ||
0155         in_range(ch, 0x0ab5, 0x0ab9) || 0x0ae0 == ch)
0156     {
0157         return universal_char_type_valid;   // Gujarati
0158     }
0159 
0160     if (in_range(ch, 0x0b05, 0x0b0c) || in_range(ch, 0x0b0f, 0x0b10) ||
0161         in_range(ch, 0x0b13, 0x0b28) || in_range(ch, 0x0b2a, 0x0b30) ||
0162         in_range(ch, 0x0b32, 0x0b33) || in_range(ch, 0x0b36, 0x0b39) ||
0163         in_range(ch, 0x0b5c, 0x0b5d) || in_range(ch, 0x0b5f, 0x0b61))
0164     {
0165         return universal_char_type_valid;   // Oriya
0166     }
0167 
0168     if (in_range(ch, 0x0b85, 0x0b8a) || in_range(ch, 0x0b8e, 0x0b90) ||
0169         in_range(ch, 0x0b92, 0x0b95) || in_range(ch, 0x0b99, 0x0b9a) ||
0170         0x0b9c == ch || in_range(ch, 0x0b9e, 0x0b9f) ||
0171         in_range(ch, 0x0ba3, 0x0ba4) || in_range(ch, 0x0ba8, 0x0baa) ||
0172         in_range(ch, 0x0bae, 0x0bb5) || in_range(ch, 0x0bb7, 0x0bb9))
0173     {
0174         return universal_char_type_valid;   // Tamil
0175     }
0176 
0177     if (in_range(ch, 0x0c05, 0x0c0c) || in_range(ch, 0x0c0e, 0x0c10) ||
0178         in_range(ch, 0x0c12, 0x0c28) || in_range(ch, 0x0c2a, 0x0c33) ||
0179         in_range(ch, 0x0c35, 0x0c39) || in_range(ch, 0x0c60, 0x0c61))
0180     {
0181         return universal_char_type_valid;   // Telugu
0182     }
0183 
0184     if (in_range(ch, 0x0c85, 0x0c8c) || in_range(ch, 0x0c8e, 0x0c90) ||
0185         in_range(ch, 0x0c92, 0x0ca8) || in_range(ch, 0x0caa, 0x0cb3) ||
0186         in_range(ch, 0x0cb5, 0x0cb9) || in_range(ch, 0x0ce0, 0x0ce1))
0187     {
0188         return universal_char_type_valid;   // Kannada
0189     }
0190 
0191     if (in_range(ch, 0x0d05, 0x0d0c) || in_range(ch, 0x0d0e, 0x0d10) ||
0192         in_range(ch, 0x0d12, 0x0d28) || in_range(ch, 0x0d2a, 0x0d39) ||
0193         in_range(ch, 0x0d60, 0x0d61))
0194     {
0195         return universal_char_type_valid;   // Malayalam
0196     }
0197 
0198     if (in_range(ch, 0x0e01, 0x0e30) || in_range(ch, 0x0e32, 0x0e33) ||
0199         in_range(ch, 0x0e40, 0x0e46) || in_range(ch, 0x0e4f, 0x0e5b))
0200     {
0201         return universal_char_type_valid;   // Thai
0202     }
0203 
0204     return universal_char_type_not_allowed_for_identifiers;
0205 }
0206 
0207 ///////////////////////////////////////////////////////////////////////////////
0208 //
0209 //  validate_identifier_name
0210 //
0211 //      The validate_identifier_name function tests a given identifier name for
0212 //      its validity with regard to eventually contained universal characters.
0213 //      These should be in valid ranges (see the function
0214 //      classify_universal_char above).
0215 //
0216 //      If the identifier name contains invalid or not allowed universal
0217 //      characters a corresponding lexing_exception is thrown.
0218 //
0219 ///////////////////////////////////////////////////////////////////////////////
0220 template <typename StringT>
0221 inline void
0222 validate_identifier_name (StringT const &name, std::size_t line,
0223     std::size_t column, StringT const &file_name)
0224 {
0225     using namespace std;    // some systems have strtoul in namespace std::
0226 
0227 typename StringT::size_type pos = name.find_first_of('\\');
0228 
0229     while (StringT::npos != pos) {
0230         // the identifier name contains a backslash (must be universal char)
0231         BOOST_ASSERT('u' == name[pos+1] || 'U' == name[pos+1]);
0232 
0233         StringT uchar_val(name.substr(pos + 2, ('u' == name[pos + 1]) ? 4 : 8));
0234         universal_char_type type =
0235             classify_universal_char(strtoul(uchar_val.c_str(), 0, 16));
0236 
0237         if (universal_char_type_valid != type) {
0238             // an invalid char was found, so throw an exception
0239             StringT error_uchar(name.substr(pos, ('u' == name[pos + 1]) ? 6 : 10));
0240 
0241             if (universal_char_type_invalid == type) {
0242                 BOOST_WAVE_LEXER_THROW(lexing_exception, universal_char_invalid,
0243                     error_uchar, line, column, file_name.c_str());
0244             }
0245             else if (universal_char_type_base_charset == type) {
0246                 BOOST_WAVE_LEXER_THROW(lexing_exception, universal_char_base_charset,
0247                     error_uchar, line, column, file_name.c_str());
0248             }
0249             else {
0250                 BOOST_WAVE_LEXER_THROW(lexing_exception, universal_char_not_allowed,
0251                     error_uchar, line, column, file_name.c_str());
0252             }
0253         }
0254 
0255         // find next universal char (if appropriate)
0256         pos = name.find_first_of('\\', pos+2);
0257     }
0258 }
0259 
0260 ///////////////////////////////////////////////////////////////////////////////
0261 //
0262 //  validate_literal
0263 //
0264 //      The validate_literal function tests a given string or character literal
0265 //      for its validity with regard to eventually contained universal
0266 //      characters. These should be in valid ranges (see the function
0267 //      classify_universal_char above).
0268 //
0269 //      If the string or character literal contains invalid or not allowed
0270 //      universal characters a corresponding lexing_exception is thrown.
0271 //
0272 ///////////////////////////////////////////////////////////////////////////////
0273 template <typename StringT>
0274 inline void
0275 validate_literal (StringT const &name, std::size_t line, std::size_t column,
0276     StringT const &file_name)
0277 {
0278     using namespace std;    // some systems have strtoul in namespace std::
0279 
0280     typename StringT::size_type pos = name.find_first_of('\\');
0281 
0282     while (StringT::npos != pos) {
0283         // the literal contains a backslash (may be universal char)
0284         if ('u' == name[pos+1] || 'U' == name[pos+1]) {
0285             StringT uchar_val(name.substr(pos + 2, ('u' == name[pos + 1]) ? 4 : 8));
0286             universal_char_type type =
0287                 classify_universal_char(strtoul(uchar_val.c_str(), 0, 16));
0288 
0289             if (universal_char_type_valid != type &&
0290                 universal_char_type_not_allowed_for_identifiers != type)
0291             {
0292                 // an invalid char was found, so throw an exception
0293                 StringT error_uchar(name.substr(pos, ('u' == name[pos + 1]) ? 6 : 10));
0294 
0295                 if (universal_char_type_invalid == type) {
0296                     BOOST_WAVE_LEXER_THROW(lexing_exception, universal_char_invalid,
0297                         error_uchar, line, column, file_name.c_str());
0298                 }
0299                 else {
0300                     BOOST_WAVE_LEXER_THROW(lexing_exception, universal_char_base_charset,
0301                         error_uchar, line, column, file_name.c_str());
0302                 }
0303             }
0304         }
0305 
0306         // find next universal char (if appropriate)
0307         pos = name.find_first_of('\\', pos+2);
0308     }
0309 }
0310 
0311 ///////////////////////////////////////////////////////////////////////////////
0312 }   // namespace impl
0313 }   // namespace cpplexer
0314 }   // namespace wave
0315 }   // namespace boost
0316 
0317 // the suffix header occurs after all of the code
0318 #ifdef BOOST_HAS_ABI_HEADERS
0319 #include BOOST_ABI_SUFFIX
0320 #endif
0321 
0322 #endif // !defined(BOOST_VALIDATE_UNIVERSAL_CHAR_HPP_55F1B811_CD76_4C72_8344_CBC69CF3B339_INCLUDED)