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