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