cpplexer_exceptions.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*=============================================================================
  2. Boost.Wave: A Standard compliant C++ preprocessor library
  3. http://www.boost.org/
  4. Copyright (c) 2001-2012 Hartmut Kaiser. 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)
  7. =============================================================================*/
  8. #if !defined(BOOST_CPPLEXER_EXCEPTIONS_HPP_1A09DE1A_6D1F_4091_AF7F_5F13AB0D31AB_INCLUDED)
  9. #define BOOST_CPPLEXER_EXCEPTIONS_HPP_1A09DE1A_6D1F_4091_AF7F_5F13AB0D31AB_INCLUDED
  10. #include <exception>
  11. #include <string>
  12. #include <boost/assert.hpp>
  13. #include <boost/config.hpp>
  14. #include <boost/throw_exception.hpp>
  15. #include <boost/wave/wave_config.hpp>
  16. // this must occur after all of the includes and before any code appears
  17. #ifdef BOOST_HAS_ABI_HEADERS
  18. #include BOOST_ABI_PREFIX
  19. #endif
  20. ///////////////////////////////////////////////////////////////////////////////
  21. // helper macro for throwing exceptions
  22. #if !defined(BOOST_WAVE_LEXER_THROW)
  23. #ifdef BOOST_NO_STRINGSTREAM
  24. #include <strstream>
  25. #define BOOST_WAVE_LEXER_THROW(cls, code, msg, line, column, name) \
  26. { \
  27. using namespace boost::wave; \
  28. std::strstream stream; \
  29. stream << cls::severity_text(cls::code) << ": " \
  30. << cls::error_text(cls::code); \
  31. if ((msg)[0] != 0) stream << ": " << (msg); \
  32. stream << std::ends; \
  33. std::string throwmsg = stream.str(); stream.freeze(false); \
  34. boost::throw_exception(cls(throwmsg.c_str(), cls::code, line, column, \
  35. name)); \
  36. } \
  37. /**/
  38. #else
  39. #include <sstream>
  40. #define BOOST_WAVE_LEXER_THROW(cls, code, msg, line, column, name) \
  41. { \
  42. using namespace boost::wave; \
  43. std::stringstream stream; \
  44. stream << cls::severity_text(cls::code) << ": " \
  45. << cls::error_text(cls::code); \
  46. if ((msg)[0] != 0) stream << ": " << (msg); \
  47. stream << std::ends; \
  48. boost::throw_exception(cls(stream.str().c_str(), cls::code, line, column, \
  49. name)); \
  50. } \
  51. /**/
  52. #endif // BOOST_NO_STRINGSTREAM
  53. #endif // BOOST_WAVE_LEXER_THROW
  54. #if !defined(BOOST_WAVE_LEXER_THROW_VAR)
  55. #ifdef BOOST_NO_STRINGSTREAM
  56. #include <strstream>
  57. #define BOOST_WAVE_LEXER_THROW_VAR(cls, codearg, msg, line, column, name) \
  58. { \
  59. using namespace boost::wave; \
  60. cls::error_code code = static_cast<cls::error_code>(codearg); \
  61. std::strstream stream; \
  62. stream << cls::severity_text(code) << ": " \
  63. << cls::error_text(code); \
  64. if ((msg)[0] != 0) stream << ": " << (msg); \
  65. stream << std::ends; \
  66. std::string throwmsg = stream.str(); stream.freeze(false); \
  67. boost::throw_exception(cls(throwmsg.c_str(), code, line, column, \
  68. name)); \
  69. } \
  70. /**/
  71. #else
  72. #include <sstream>
  73. #define BOOST_WAVE_LEXER_THROW_VAR(cls, codearg, msg, line, column, name) \
  74. { \
  75. using namespace boost::wave; \
  76. cls::error_code code = static_cast<cls::error_code>(codearg); \
  77. std::stringstream stream; \
  78. stream << cls::severity_text(code) << ": " \
  79. << cls::error_text(code); \
  80. if ((msg)[0] != 0) stream << ": " << (msg); \
  81. stream << std::ends; \
  82. boost::throw_exception(cls(stream.str().c_str(), code, line, column, \
  83. name)); \
  84. } \
  85. /**/
  86. #endif // BOOST_NO_STRINGSTREAM
  87. #endif // BOOST_WAVE_LEXER_THROW
  88. ///////////////////////////////////////////////////////////////////////////////
  89. namespace boost {
  90. namespace wave {
  91. namespace cpplexer {
  92. ///////////////////////////////////////////////////////////////////////////////
  93. // exception severity
  94. namespace util {
  95. enum severity {
  96. severity_remark = 0,
  97. severity_warning,
  98. severity_error,
  99. severity_fatal
  100. };
  101. inline char const *
  102. get_severity(severity level)
  103. {
  104. static char const *severity_text[] =
  105. {
  106. "remark", // severity_remark
  107. "warning", // severity_warning
  108. "error", // severity_error
  109. "fatal error" // severity_fatal
  110. };
  111. BOOST_ASSERT(severity_remark <= level && level <= severity_fatal);
  112. return severity_text[level];
  113. }
  114. }
  115. ///////////////////////////////////////////////////////////////////////////////
  116. // cpplexer_exception, the base class for all specific C++ lexer exceptions
  117. class BOOST_SYMBOL_VISIBLE cpplexer_exception
  118. : public std::exception
  119. {
  120. public:
  121. cpplexer_exception(std::size_t line_, std::size_t column_, char const *filename_) throw()
  122. : line(line_), column(column_)
  123. {
  124. unsigned int off = 0;
  125. while (off < sizeof(filename)-1 && *filename_)
  126. filename[off++] = *filename_++;
  127. filename[off] = 0;
  128. }
  129. ~cpplexer_exception() throw() {}
  130. char const *what() const throw() BOOST_OVERRIDE = 0; // to be overloaded
  131. virtual char const *description() const throw() = 0;
  132. virtual int get_errorcode() const throw() = 0;
  133. virtual int get_severity() const throw() = 0;
  134. virtual bool is_recoverable() const throw() = 0;
  135. std::size_t line_no() const throw() { return line; }
  136. std::size_t column_no() const throw() { return column; }
  137. char const *file_name() const throw() { return filename; }
  138. protected:
  139. char filename[512];
  140. std::size_t line;
  141. std::size_t column;
  142. };
  143. ///////////////////////////////////////////////////////////////////////////////
  144. // lexing_exception error
  145. class BOOST_SYMBOL_VISIBLE lexing_exception :
  146. public cpplexer_exception
  147. {
  148. public:
  149. enum error_code {
  150. unexpected_error = 0,
  151. universal_char_invalid = 1,
  152. universal_char_base_charset = 2,
  153. universal_char_not_allowed = 3,
  154. invalid_long_long_literal = 4,
  155. generic_lexing_error = 5,
  156. generic_lexing_warning = 6
  157. };
  158. lexing_exception(char const *what_, error_code code, std::size_t line_,
  159. std::size_t column_, char const *filename_) throw()
  160. : cpplexer_exception(line_, column_, filename_),
  161. level(severity_level(code)), code(code)
  162. {
  163. unsigned int off = 0;
  164. while (off < sizeof(buffer)-1 && *what_)
  165. buffer[off++] = *what_++;
  166. buffer[off] = 0;
  167. }
  168. ~lexing_exception() throw() {}
  169. char const *what() const throw() BOOST_OVERRIDE
  170. {
  171. return "boost::wave::lexing_exception";
  172. }
  173. char const *description() const throw() BOOST_OVERRIDE
  174. {
  175. return buffer;
  176. }
  177. int get_severity() const throw() BOOST_OVERRIDE
  178. {
  179. return level;
  180. }
  181. int get_errorcode() const throw() BOOST_OVERRIDE
  182. {
  183. return code;
  184. }
  185. bool is_recoverable() const throw() BOOST_OVERRIDE
  186. {
  187. switch (get_errorcode()) {
  188. case lexing_exception::universal_char_invalid:
  189. case lexing_exception::universal_char_base_charset:
  190. case lexing_exception::universal_char_not_allowed:
  191. case lexing_exception::invalid_long_long_literal:
  192. case lexing_exception::generic_lexing_warning:
  193. case lexing_exception::generic_lexing_error:
  194. return true; // for now allow all exceptions to be recoverable
  195. case lexing_exception::unexpected_error:
  196. default:
  197. break;
  198. }
  199. return false;
  200. }
  201. static char const *error_text(int code)
  202. {
  203. // error texts in this array must appear in the same order as the items in
  204. // the error enum above
  205. static char const *preprocess_exception_errors[] = {
  206. "unexpected error (should not happen)", // unexpected_error
  207. "universal character name specifies an invalid character", // universal_char_invalid
  208. "a universal character name cannot designate a character in the "
  209. "basic character set", // universal_char_base_charset
  210. "this universal character is not allowed in an identifier", // universal_char_not_allowed
  211. "long long suffixes are not allowed in pure C++ mode, "
  212. "enable long_long mode to allow these", // invalid_long_long_literal
  213. "generic lexer error", // generic_lexing_error
  214. "generic lexer warning" // generic_lexing_warning
  215. };
  216. return preprocess_exception_errors[code];
  217. }
  218. static util::severity severity_level(int code)
  219. {
  220. static util::severity preprocess_exception_severity[] = {
  221. util::severity_fatal, // unexpected_error
  222. util::severity_error, // universal_char_invalid
  223. util::severity_error, // universal_char_base_charset
  224. util::severity_error, // universal_char_not_allowed
  225. util::severity_warning, // invalid_long_long_literal
  226. util::severity_error, // generic_lexing_error
  227. util::severity_warning // invalid_long_long_literal
  228. };
  229. return preprocess_exception_severity[code];
  230. }
  231. static char const *severity_text(int code)
  232. {
  233. return util::get_severity(severity_level(code));
  234. }
  235. private:
  236. char buffer[512];
  237. util::severity level;
  238. error_code code;
  239. };
  240. ///////////////////////////////////////////////////////////////////////////////
  241. //
  242. // The is_recoverable() function allows to decide, whether it is possible
  243. // simply to continue after a given exception was thrown by Wave.
  244. //
  245. // This is kind of a hack to allow to recover from certain errors as long as
  246. // Wave doesn't provide better means of error recovery.
  247. //
  248. ///////////////////////////////////////////////////////////////////////////////
  249. inline bool
  250. is_recoverable(lexing_exception const& e)
  251. {
  252. return e.is_recoverable();
  253. }
  254. ///////////////////////////////////////////////////////////////////////////////
  255. } // namespace cpplexer
  256. } // namespace wave
  257. } // namespace boost
  258. // the suffix header occurs after all of the code
  259. #ifdef BOOST_HAS_ABI_HEADERS
  260. #include BOOST_ABI_SUFFIX
  261. #endif
  262. #endif // !defined(BOOST_CPPLEXER_EXCEPTIONS_HPP_1A09DE1A_6D1F_4091_AF7F_5F13AB0D31AB_INCLUDED)