1 /*=============================================================================
2 Boost.Wave: A Standard compliant C++ preprocessor library
6 Copyright (c) 2001-2012 Hartmut Kaiser. Distributed under the Boost
7 Software License, Version 1.0. (See accompanying file
8 LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 =============================================================================*/
11 #if !defined(CPPLEXER_EXCEPTIONS_HPP_1A09DE1A_6D1F_4091_AF7F_5F13AB0D31AB_INCLUDED)
12 #define CPPLEXER_EXCEPTIONS_HPP_1A09DE1A_6D1F_4091_AF7F_5F13AB0D31AB_INCLUDED
17 #include <boost/assert.hpp>
18 #include <boost/config.hpp>
19 #include <boost/throw_exception.hpp>
21 #include <boost/wave/wave_config.hpp>
23 // this must occur after all of the includes and before any code appears
24 #ifdef BOOST_HAS_ABI_HEADERS
25 #include BOOST_ABI_PREFIX
28 ///////////////////////////////////////////////////////////////////////////////
29 // helper macro for throwing exceptions
30 #if !defined(BOOST_WAVE_LEXER_THROW)
31 #ifdef BOOST_NO_STRINGSTREAM
33 #define BOOST_WAVE_LEXER_THROW(cls, code, msg, line, column, name) \
35 using namespace boost::wave; \
36 std::strstream stream; \
37 stream << cls::severity_text(cls::code) << ": " \
38 << cls::error_text(cls::code); \
39 if ((msg)[0] != 0) stream << ": " << (msg); \
40 stream << std::ends; \
41 std::string throwmsg = stream.str(); stream.freeze(false); \
42 boost::throw_exception(cls(throwmsg.c_str(), cls::code, line, column, \
48 #define BOOST_WAVE_LEXER_THROW(cls, code, msg, line, column, name) \
50 using namespace boost::wave; \
51 std::stringstream stream; \
52 stream << cls::severity_text(cls::code) << ": " \
53 << cls::error_text(cls::code); \
54 if ((msg)[0] != 0) stream << ": " << (msg); \
55 stream << std::ends; \
56 boost::throw_exception(cls(stream.str().c_str(), cls::code, line, column, \
60 #endif // BOOST_NO_STRINGSTREAM
61 #endif // BOOST_WAVE_LEXER_THROW
63 #if !defined(BOOST_WAVE_LEXER_THROW_VAR)
64 #ifdef BOOST_NO_STRINGSTREAM
66 #define BOOST_WAVE_LEXER_THROW_VAR(cls, codearg, msg, line, column, name) \
68 using namespace boost::wave; \
69 cls::error_code code = static_cast<cls::error_code>(codearg); \
70 std::strstream stream; \
71 stream << cls::severity_text(code) << ": " \
72 << cls::error_text(code); \
73 if ((msg)[0] != 0) stream << ": " << (msg); \
74 stream << std::ends; \
75 std::string throwmsg = stream.str(); stream.freeze(false); \
76 boost::throw_exception(cls(throwmsg.c_str(), code, line, column, \
82 #define BOOST_WAVE_LEXER_THROW_VAR(cls, codearg, msg, line, column, name) \
84 using namespace boost::wave; \
85 cls::error_code code = static_cast<cls::error_code>(codearg); \
86 std::stringstream stream; \
87 stream << cls::severity_text(code) << ": " \
88 << cls::error_text(code); \
89 if ((msg)[0] != 0) stream << ": " << (msg); \
90 stream << std::ends; \
91 boost::throw_exception(cls(stream.str().c_str(), code, line, column, \
95 #endif // BOOST_NO_STRINGSTREAM
96 #endif // BOOST_WAVE_LEXER_THROW
98 ///////////////////////////////////////////////////////////////////////////////
103 ///////////////////////////////////////////////////////////////////////////////
104 // exception severity
115 get_severity(severity level)
117 static char const *severity_text[] =
119 "remark", // severity_remark
120 "warning", // severity_warning
121 "error", // severity_error
122 "fatal error" // severity_fatal
124 BOOST_ASSERT(severity_remark <= level && level <= severity_fatal);
125 return severity_text[level];
129 ///////////////////////////////////////////////////////////////////////////////
130 // cpplexer_exception, the base class for all specific C++ lexer exceptions
131 class BOOST_SYMBOL_VISIBLE cpplexer_exception
132 : public std::exception
135 cpplexer_exception(std::size_t line_, std::size_t column_, char const *filename_) throw()
136 : line(line_), column(column_)
138 unsigned int off = 0;
139 while (off < sizeof(filename)-1 && *filename_)
140 filename[off++] = *filename_++;
143 ~cpplexer_exception() throw() {}
145 virtual char const *what() const throw() = 0; // to be overloaded
146 virtual char const *description() const throw() = 0;
147 virtual int get_errorcode() const throw() = 0;
148 virtual int get_severity() const throw() = 0;
149 virtual bool is_recoverable() const throw() = 0;
151 std::size_t line_no() const throw() { return line; }
152 std::size_t column_no() const throw() { return column; }
153 char const *file_name() const throw() { return filename; }
161 ///////////////////////////////////////////////////////////////////////////////
162 // lexing_exception error
163 class BOOST_SYMBOL_VISIBLE lexing_exception :
164 public cpplexer_exception
168 unexpected_error = 0,
169 universal_char_invalid = 1,
170 universal_char_base_charset = 2,
171 universal_char_not_allowed = 3,
172 invalid_long_long_literal = 4,
173 generic_lexing_error = 5,
174 generic_lexing_warning = 6
177 lexing_exception(char const *what_, error_code code, std::size_t line_,
178 std::size_t column_, char const *filename_) throw()
179 : cpplexer_exception(line_, column_, filename_),
180 level(severity_level(code)), code(code)
182 unsigned int off = 0;
183 while (off < sizeof(buffer)-1 && *what_)
184 buffer[off++] = *what_++;
187 ~lexing_exception() throw() {}
189 virtual char const *what() const throw()
191 return "boost::wave::lexing_exception";
193 virtual char const *description() const throw()
197 virtual int get_severity() const throw()
201 virtual int get_errorcode() const throw()
205 virtual bool is_recoverable() const throw()
207 switch (get_errorcode()) {
208 case lexing_exception::universal_char_invalid:
209 case lexing_exception::universal_char_base_charset:
210 case lexing_exception::universal_char_not_allowed:
211 case lexing_exception::invalid_long_long_literal:
212 case lexing_exception::generic_lexing_warning:
213 case lexing_exception::generic_lexing_error:
214 return true; // for now allow all exceptions to be recoverable
216 case lexing_exception::unexpected_error:
223 static char const *error_text(int code)
225 // error texts in this array must appear in the same order as the items in
226 // the error enum above
227 static char const *preprocess_exception_errors[] = {
228 "unexpected error (should not happen)", // unexpected_error
229 "universal character name specifies an invalid character", // universal_char_invalid
230 "a universal character name cannot designate a character in the "
231 "basic character set", // universal_char_base_charset
232 "this universal character is not allowed in an identifier", // universal_char_not_allowed
233 "long long suffixes are not allowed in pure C++ mode, "
234 "enable long_long mode to allow these", // invalid_long_long_literal
235 "generic lexer error", // generic_lexing_error
236 "generic lexer warning" // generic_lexing_warning
238 return preprocess_exception_errors[code];
241 static util::severity severity_level(int code)
243 static util::severity preprocess_exception_severity[] = {
244 util::severity_fatal, // unexpected_error
245 util::severity_error, // universal_char_invalid
246 util::severity_error, // universal_char_base_charset
247 util::severity_error, // universal_char_not_allowed
248 util::severity_warning, // invalid_long_long_literal
249 util::severity_error, // generic_lexing_error
250 util::severity_warning // invalid_long_long_literal
252 return preprocess_exception_severity[code];
254 static char const *severity_text(int code)
256 return util::get_severity(severity_level(code));
261 util::severity level;
265 ///////////////////////////////////////////////////////////////////////////////
267 // The is_recoverable() function allows to decide, whether it is possible
268 // simply to continue after a given exception was thrown by Wave.
270 // This is kind of a hack to allow to recover from certain errors as long as
271 // Wave doesn't provide better means of error recovery.
273 ///////////////////////////////////////////////////////////////////////////////
275 is_recoverable(lexing_exception const& e)
277 return e.is_recoverable();
280 ///////////////////////////////////////////////////////////////////////////////
281 } // namespace cpplexer
285 // the suffix header occurs after all of the code
286 #ifdef BOOST_HAS_ABI_HEADERS
287 #include BOOST_ABI_SUFFIX
290 #endif // !defined(CPPLEXER_EXCEPTIONS_HPP_1A09DE1A_6D1F_4091_AF7F_5F13AB0D31AB_INCLUDED)