]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /*============================================================================= |
2 | Boost.Wave: A Standard compliant C++ preprocessor library | |
3 | ||
4 | Re2C based C++ lexer | |
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 | ||
13 | #if !defined(CPP_RE2C_LEXER_HPP_B81A2629_D5B1_4944_A97D_60254182B9A8_INCLUDED) | |
14 | #define CPP_RE2C_LEXER_HPP_B81A2629_D5B1_4944_A97D_60254182B9A8_INCLUDED | |
15 | ||
16 | #include <string> | |
17 | #include <cstdio> | |
18 | #include <cstdarg> | |
19 | #if defined(BOOST_SPIRIT_DEBUG) | |
20 | #include <iostream> | |
21 | #endif // defined(BOOST_SPIRIT_DEBUG) | |
22 | ||
23 | #include <boost/concept_check.hpp> | |
24 | #include <boost/assert.hpp> | |
25 | #include <boost/spirit/include/classic_core.hpp> | |
26 | ||
27 | #include <boost/wave/wave_config.hpp> | |
28 | #include <boost/wave/language_support.hpp> | |
29 | #include <boost/wave/token_ids.hpp> | |
30 | #include <boost/wave/util/file_position.hpp> | |
31 | #include <boost/wave/cpplexer/validate_universal_char.hpp> | |
32 | #include <boost/wave/cpplexer/cpplexer_exceptions.hpp> | |
33 | #include <boost/wave/cpplexer/token_cache.hpp> | |
34 | #include <boost/wave/cpplexer/convert_trigraphs.hpp> | |
35 | ||
36 | #include <boost/wave/cpplexer/cpp_lex_interface.hpp> | |
37 | #include <boost/wave/cpplexer/re2clex/scanner.hpp> | |
38 | #include <boost/wave/cpplexer/re2clex/cpp_re.hpp> | |
39 | #if BOOST_WAVE_SUPPORT_PRAGMA_ONCE != 0 | |
40 | #include <boost/wave/cpplexer/detect_include_guards.hpp> | |
41 | #endif | |
42 | ||
43 | #include <boost/wave/cpplexer/cpp_lex_interface_generator.hpp> | |
44 | ||
45 | // this must occur after all of the includes and before any code appears | |
46 | #ifdef BOOST_HAS_ABI_HEADERS | |
47 | #include BOOST_ABI_PREFIX | |
48 | #endif | |
49 | ||
50 | /////////////////////////////////////////////////////////////////////////////// | |
51 | namespace boost { | |
52 | namespace wave { | |
53 | namespace cpplexer { | |
54 | namespace re2clex { | |
55 | ||
56 | /////////////////////////////////////////////////////////////////////////////// | |
57 | // | |
58 | // encapsulation of the re2c based cpp lexer | |
59 | // | |
60 | /////////////////////////////////////////////////////////////////////////////// | |
61 | ||
62 | template <typename IteratorT, | |
63 | typename PositionT = boost::wave::util::file_position_type, | |
64 | typename TokenT = lex_token<PositionT> > | |
65 | class lexer | |
66 | { | |
67 | public: | |
68 | typedef TokenT token_type; | |
69 | typedef typename token_type::string_type string_type; | |
70 | ||
71 | lexer(IteratorT const &first, IteratorT const &last, | |
72 | PositionT const &pos, boost::wave::language_support language_); | |
73 | ~lexer(); | |
74 | ||
75 | token_type& get(token_type&); | |
76 | void set_position(PositionT const &pos) | |
77 | { | |
78 | // set position has to change the file name and line number only | |
79 | filename = pos.get_file(); | |
80 | scanner.line = pos.get_line(); | |
81 | // scanner.column = scanner.curr_column = pos.get_column(); | |
82 | scanner.file_name = filename.c_str(); | |
83 | } | |
84 | #if BOOST_WAVE_SUPPORT_PRAGMA_ONCE != 0 | |
85 | bool has_include_guards(std::string& guard_name) const | |
86 | { | |
87 | return guards.detected(guard_name); | |
88 | } | |
89 | #endif | |
90 | ||
91 | // error reporting from the re2c generated lexer | |
92 | static int report_error(Scanner const* s, int code, char const *, ...); | |
93 | ||
94 | private: | |
95 | static char const *tok_names[]; | |
96 | ||
97 | Scanner scanner; | |
98 | string_type filename; | |
99 | string_type value; | |
100 | bool at_eof; | |
101 | boost::wave::language_support language; | |
102 | #if BOOST_WAVE_SUPPORT_PRAGMA_ONCE != 0 | |
103 | include_guards<token_type> guards; | |
104 | #endif | |
105 | ||
106 | #if BOOST_WAVE_SUPPORT_THREADING == 0 | |
107 | static token_cache<string_type> const cache; | |
108 | #else | |
109 | token_cache<string_type> const cache; | |
110 | #endif | |
111 | }; | |
112 | ||
113 | /////////////////////////////////////////////////////////////////////////////// | |
114 | // initialize cpp lexer | |
115 | template <typename IteratorT, typename PositionT, typename TokenT> | |
116 | inline | |
117 | lexer<IteratorT, PositionT, TokenT>::lexer(IteratorT const &first, | |
118 | IteratorT const &last, PositionT const &pos, | |
119 | boost::wave::language_support language_) | |
120 | : filename(pos.get_file()), at_eof(false), language(language_) | |
121 | #if BOOST_WAVE_SUPPORT_THREADING != 0 | |
122 | , cache() | |
123 | #endif | |
124 | { | |
125 | using namespace std; // some systems have memset in std | |
126 | memset(&scanner, '\0', sizeof(Scanner)); | |
127 | scanner.eol_offsets = aq_create(); | |
128 | if (first != last) { | |
129 | scanner.first = scanner.act = (uchar *)&(*first); | |
130 | scanner.last = scanner.first + std::distance(first, last); | |
131 | } | |
132 | scanner.line = pos.get_line(); | |
133 | scanner.column = scanner.curr_column = pos.get_column(); | |
134 | scanner.error_proc = report_error; | |
135 | scanner.file_name = filename.c_str(); | |
136 | ||
137 | #if BOOST_WAVE_SUPPORT_MS_EXTENSIONS != 0 | |
138 | scanner.enable_ms_extensions = true; | |
139 | #else | |
140 | scanner.enable_ms_extensions = false; | |
141 | #endif | |
142 | ||
143 | #if BOOST_WAVE_SUPPORT_VARIADICS_PLACEMARKERS != 0 | |
144 | scanner.act_in_c99_mode = boost::wave::need_c99(language_); | |
145 | #endif | |
146 | ||
147 | #if BOOST_WAVE_SUPPORT_IMPORT_KEYWORD != 0 | |
148 | scanner.enable_import_keyword = !boost::wave::need_c99(language_); | |
149 | #else | |
150 | scanner.enable_import_keyword = false; | |
151 | #endif | |
152 | ||
153 | scanner.detect_pp_numbers = boost::wave::need_prefer_pp_numbers(language_); | |
154 | scanner.single_line_only = boost::wave::need_single_line(language_); | |
155 | ||
156 | #if BOOST_WAVE_SUPPORT_CPP0X != 0 | |
157 | scanner.act_in_cpp0x_mode = boost::wave::need_cpp0x(language_); | |
158 | #else | |
159 | scanner.act_in_cpp0x_mode = false; | |
160 | #endif | |
161 | } | |
162 | ||
163 | template <typename IteratorT, typename PositionT, typename TokenT> | |
164 | inline | |
165 | lexer<IteratorT, PositionT, TokenT>::~lexer() | |
166 | { | |
167 | using namespace std; // some systems have free in std | |
168 | aq_terminate(scanner.eol_offsets); | |
169 | free(scanner.bot); | |
170 | } | |
171 | ||
172 | /////////////////////////////////////////////////////////////////////////////// | |
173 | // get the next token from the input stream | |
174 | template <typename IteratorT, typename PositionT, typename TokenT> | |
175 | inline TokenT& | |
176 | lexer<IteratorT, PositionT, TokenT>::get(TokenT& result) | |
177 | { | |
178 | if (at_eof) | |
179 | return result = token_type(); // return T_EOI | |
180 | ||
181 | std::size_t actline = scanner.line; | |
182 | token_id id = token_id(scan(&scanner)); | |
183 | ||
184 | switch (static_cast<unsigned int>(id)) { | |
185 | case T_IDENTIFIER: | |
186 | // test identifier characters for validity (throws if invalid chars found) | |
187 | value = string_type((char const *)scanner.tok, | |
188 | scanner.cur-scanner.tok); | |
189 | if (!boost::wave::need_no_character_validation(language)) | |
190 | impl::validate_identifier_name(value, actline, scanner.column, filename); | |
191 | break; | |
192 | ||
193 | case T_STRINGLIT: | |
194 | case T_CHARLIT: | |
195 | case T_RAWSTRINGLIT: | |
196 | // test literal characters for validity (throws if invalid chars found) | |
197 | value = string_type((char const *)scanner.tok, | |
198 | scanner.cur-scanner.tok); | |
199 | if (boost::wave::need_convert_trigraphs(language)) | |
200 | value = impl::convert_trigraphs(value); | |
201 | if (!boost::wave::need_no_character_validation(language)) | |
202 | impl::validate_literal(value, actline, scanner.column, filename); | |
203 | break; | |
204 | ||
205 | #if BOOST_WAVE_SUPPORT_INCLUDE_NEXT != 0 | |
206 | case T_PP_HHEADER: | |
207 | case T_PP_QHEADER: | |
208 | case T_PP_INCLUDE: | |
209 | // convert to the corresponding ..._next token, if appropriate | |
210 | { | |
211 | value = string_type((char const *)scanner.tok, | |
212 | scanner.cur-scanner.tok); | |
213 | ||
214 | // Skip '#' and whitespace and see whether we find an 'include_next' here. | |
215 | typename string_type::size_type start = value.find("include"); | |
216 | if (value.compare(start, 12, "include_next", 12) == 0) | |
217 | id = token_id(id | AltTokenType); | |
218 | break; | |
219 | } | |
220 | #endif | |
221 | ||
222 | case T_LONGINTLIT: // supported in C++11, C99 and long_long mode | |
223 | value = string_type((char const *)scanner.tok, | |
224 | scanner.cur-scanner.tok); | |
225 | if (!boost::wave::need_long_long(language)) { | |
226 | // syntax error: not allowed in C++ mode | |
227 | BOOST_WAVE_LEXER_THROW(lexing_exception, invalid_long_long_literal, | |
228 | value.c_str(), actline, scanner.column, filename.c_str()); | |
229 | } | |
230 | break; | |
231 | ||
232 | case T_OCTALINT: | |
233 | case T_DECIMALINT: | |
234 | case T_HEXAINT: | |
235 | case T_INTLIT: | |
236 | case T_FLOATLIT: | |
237 | case T_FIXEDPOINTLIT: | |
238 | case T_CCOMMENT: | |
239 | case T_CPPCOMMENT: | |
240 | case T_SPACE: | |
241 | case T_SPACE2: | |
242 | case T_ANY: | |
243 | case T_PP_NUMBER: | |
244 | value = string_type((char const *)scanner.tok, | |
245 | scanner.cur-scanner.tok); | |
246 | break; | |
247 | ||
248 | case T_EOF: | |
249 | // T_EOF is returned as a valid token, the next call will return T_EOI, | |
250 | // i.e. the actual end of input | |
251 | at_eof = true; | |
252 | value.clear(); | |
253 | break; | |
254 | ||
255 | case T_OR_TRIGRAPH: | |
256 | case T_XOR_TRIGRAPH: | |
257 | case T_LEFTBRACE_TRIGRAPH: | |
258 | case T_RIGHTBRACE_TRIGRAPH: | |
259 | case T_LEFTBRACKET_TRIGRAPH: | |
260 | case T_RIGHTBRACKET_TRIGRAPH: | |
261 | case T_COMPL_TRIGRAPH: | |
262 | case T_POUND_TRIGRAPH: | |
263 | if (boost::wave::need_convert_trigraphs(language)) { | |
264 | value = cache.get_token_value(BASEID_FROM_TOKEN(id)); | |
265 | } | |
266 | else { | |
267 | value = string_type((char const *)scanner.tok, | |
268 | scanner.cur-scanner.tok); | |
269 | } | |
270 | break; | |
271 | ||
272 | case T_ANY_TRIGRAPH: | |
273 | if (boost::wave::need_convert_trigraphs(language)) { | |
274 | value = impl::convert_trigraph( | |
275 | string_type((char const *)scanner.tok)); | |
276 | } | |
277 | else { | |
278 | value = string_type((char const *)scanner.tok, | |
279 | scanner.cur-scanner.tok); | |
280 | } | |
281 | break; | |
282 | ||
283 | default: | |
284 | if (CATEGORY_FROM_TOKEN(id) != EXTCATEGORY_FROM_TOKEN(id) || | |
285 | IS_CATEGORY(id, UnknownTokenType)) | |
286 | { | |
287 | value = string_type((char const *)scanner.tok, | |
288 | scanner.cur-scanner.tok); | |
289 | } | |
290 | else { | |
291 | value = cache.get_token_value(id); | |
292 | } | |
293 | break; | |
294 | } | |
295 | ||
296 | // std::cerr << boost::wave::get_token_name(id) << ": " << value << std::endl; | |
297 | ||
298 | // the re2c lexer reports the new line number for newline tokens | |
299 | result = token_type(id, value, PositionT(filename, actline, scanner.column)); | |
300 | ||
301 | #if BOOST_WAVE_SUPPORT_PRAGMA_ONCE != 0 | |
302 | return guards.detect_guard(result); | |
303 | #else | |
304 | return result; | |
305 | #endif | |
306 | } | |
307 | ||
308 | template <typename IteratorT, typename PositionT, typename TokenT> | |
309 | inline int | |
310 | lexer<IteratorT, PositionT, TokenT>::report_error(Scanner const *s, int errcode, | |
311 | char const *msg, ...) | |
312 | { | |
313 | BOOST_ASSERT(0 != s); | |
314 | BOOST_ASSERT(0 != msg); | |
315 | ||
316 | using namespace std; // some system have vsprintf in namespace std | |
317 | ||
318 | char buffer[200]; // should be large enough | |
319 | va_list params; | |
320 | va_start(params, msg); | |
321 | vsprintf(buffer, msg, params); | |
322 | va_end(params); | |
323 | ||
324 | BOOST_WAVE_LEXER_THROW_VAR(lexing_exception, errcode, buffer, s->line, | |
325 | s->column, s->file_name); | |
326 | // BOOST_UNREACHABLE_RETURN(0); | |
327 | return 0; | |
328 | } | |
329 | ||
330 | /////////////////////////////////////////////////////////////////////////////// | |
331 | // | |
332 | // lex_functor | |
333 | // | |
334 | /////////////////////////////////////////////////////////////////////////////// | |
335 | ||
336 | template <typename IteratorT, | |
337 | typename PositionT = boost::wave::util::file_position_type, | |
338 | typename TokenT = typename lexer<IteratorT, PositionT>::token_type> | |
339 | class lex_functor | |
340 | : public lex_input_interface_generator<TokenT> | |
341 | { | |
342 | public: | |
343 | typedef TokenT token_type; | |
344 | ||
345 | lex_functor(IteratorT const &first, IteratorT const &last, | |
346 | PositionT const &pos, boost::wave::language_support language) | |
347 | : re2c_lexer(first, last, pos, language) | |
348 | {} | |
349 | virtual ~lex_functor() {} | |
350 | ||
351 | // get the next token from the input stream | |
352 | token_type& get(token_type& result) { return re2c_lexer.get(result); } | |
353 | void set_position(PositionT const &pos) { re2c_lexer.set_position(pos); } | |
354 | #if BOOST_WAVE_SUPPORT_PRAGMA_ONCE != 0 | |
355 | bool has_include_guards(std::string& guard_name) const | |
356 | { return re2c_lexer.has_include_guards(guard_name); } | |
357 | #endif | |
358 | ||
359 | private: | |
360 | lexer<IteratorT, PositionT, TokenT> re2c_lexer; | |
361 | }; | |
362 | ||
363 | #if BOOST_WAVE_SUPPORT_THREADING == 0 | |
364 | /////////////////////////////////////////////////////////////////////////////// | |
365 | template <typename IteratorT, typename PositionT, typename TokenT> | |
366 | token_cache<typename lexer<IteratorT, PositionT, TokenT>::string_type> const | |
367 | lexer<IteratorT, PositionT, TokenT>::cache = | |
368 | token_cache<typename lexer<IteratorT, PositionT, TokenT>::string_type>(); | |
369 | #endif | |
370 | ||
371 | } // namespace re2clex | |
372 | ||
373 | /////////////////////////////////////////////////////////////////////////////// | |
374 | // | |
375 | // The new_lexer_gen<>::new_lexer function (declared in cpp_lex_interface.hpp) | |
376 | // should be defined inline, if the lex_functor shouldn't be instantiated | |
377 | // separately from the lex_iterator. | |
378 | // | |
379 | // Separate (explicit) instantiation helps to reduce compilation time. | |
380 | // | |
381 | /////////////////////////////////////////////////////////////////////////////// | |
382 | ||
383 | #if BOOST_WAVE_SEPARATE_LEXER_INSTANTIATION != 0 | |
384 | #define BOOST_WAVE_RE2C_NEW_LEXER_INLINE | |
385 | #else | |
386 | #define BOOST_WAVE_RE2C_NEW_LEXER_INLINE inline | |
387 | #endif | |
388 | ||
389 | /////////////////////////////////////////////////////////////////////////////// | |
390 | // | |
391 | // The 'new_lexer' function allows the opaque generation of a new lexer object. | |
392 | // It is coupled to the iterator type to allow to decouple the lexer/iterator | |
393 | // configurations at compile time. | |
394 | // | |
395 | // This function is declared inside the cpp_lex_token.hpp file, which is | |
396 | // referenced by the source file calling the lexer and the source file, which | |
397 | // instantiates the lex_functor. But it is defined here, so it will be | |
398 | // instantiated only while compiling the source file, which instantiates the | |
399 | // lex_functor. While the cpp_re2c_token.hpp file may be included everywhere, | |
400 | // this file (cpp_re2c_lexer.hpp) should be included only once. This allows | |
401 | // to decouple the lexer interface from the lexer implementation and reduces | |
402 | // compilation time. | |
403 | // | |
404 | /////////////////////////////////////////////////////////////////////////////// | |
405 | ||
406 | template <typename IteratorT, typename PositionT, typename TokenT> | |
407 | BOOST_WAVE_RE2C_NEW_LEXER_INLINE | |
408 | lex_input_interface<TokenT> * | |
409 | new_lexer_gen<IteratorT, PositionT, TokenT>::new_lexer(IteratorT const &first, | |
410 | IteratorT const &last, PositionT const &pos, | |
411 | boost::wave::language_support language) | |
412 | { | |
413 | using re2clex::lex_functor; | |
414 | return new lex_functor<IteratorT, PositionT, TokenT>(first, last, pos, language); | |
415 | } | |
416 | ||
417 | #undef BOOST_WAVE_RE2C_NEW_LEXER_INLINE | |
418 | ||
419 | /////////////////////////////////////////////////////////////////////////////// | |
420 | } // namespace cpplexer | |
421 | } // namespace wave | |
422 | } // namespace boost | |
423 | ||
424 | // the suffix header occurs after all of the code | |
425 | #ifdef BOOST_HAS_ABI_HEADERS | |
426 | #include BOOST_ABI_SUFFIX | |
427 | #endif | |
428 | ||
429 | #endif // !defined(CPP_RE2C_LEXER_HPP_B81A2629_D5B1_4944_A97D_60254182B9A8_INCLUDED) |