validate_universal_char.hpp 13 KB

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