1 /*=============================================================================
2 Boost.Wave: A Standard compliant C++ preprocessor library
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 =============================================================================*/
13 #if !defined(CPP_RE_HPP_B76C4F5E_63E9_4B8A_9975_EC32FA6BF027_INCLUDED)
14 #define CPP_RE_HPP_B76C4F5E_63E9_4B8A_9975_EC32FA6BF027_INCLUDED
16 #include <boost/assert.hpp>
18 #include <boost/wave/wave_config.hpp>
19 #include <boost/wave/token_ids.hpp>
20 #include <boost/wave/cpplexer/cpplexer_exceptions.hpp>
22 // this must occur after all of the includes and before any code appears
23 #ifdef BOOST_HAS_ABI_HEADERS
24 #include BOOST_ABI_PREFIX
27 // suppress warnings about dependent classes not being exported from the dll
30 #pragma warning(disable : 4251 4231 4660)
33 ///////////////////////////////////////////////////////////////////////////////
36 #define YYCURSOR cursor
38 #define YYMARKER marker
41 cursor = uchar_wrapper(fill(s, cursor), cursor.column); \
42 limit = uchar_wrapper (s->lim); \
48 ///////////////////////////////////////////////////////////////////////////////
49 #define BOOST_WAVE_UPDATE_CURSOR() \
51 s->line += count_backslash_newlines(s, cursor); \
52 s->curr_column = cursor.column; \
59 ///////////////////////////////////////////////////////////////////////////////
60 #define BOOST_WAVE_RET(i) \
62 BOOST_WAVE_UPDATE_CURSOR() \
63 if (s->cur > s->lim) \
64 return T_EOF; /* may happen for empty files */ \
69 ///////////////////////////////////////////////////////////////////////////////
76 template<typename Iterator>
79 ///////////////////////////////////////////////////////////////////////////////
80 // The scanner function to call whenever a new token is requested
81 template<typename Iterator>
82 BOOST_WAVE_DECL boost::wave::token_id scan(Scanner<Iterator> *s);
83 ///////////////////////////////////////////////////////////////////////////////
85 ///////////////////////////////////////////////////////////////////////////////
88 #define RE2C_ASSERT BOOST_ASSERT
90 template<typename Iterator>
91 int get_one_char(Scanner<Iterator> *s)
93 RE2C_ASSERT(s->first <= s->act && s->act <= s->last);
99 template<typename Iterator>
100 std::ptrdiff_t rewind_stream (Scanner<Iterator> *s, int cnt)
102 std::advance(s->act, cnt);
103 RE2C_ASSERT(s->first <= s->act && s->act <= s->last);
104 return std::distance(s->first, s->act);
107 template<typename Iterator>
108 std::size_t get_first_eol_offset(Scanner<Iterator>* s)
110 if (!AQ_EMPTY(s->eol_offsets))
112 return s->eol_offsets->queue[s->eol_offsets->head];
116 return (unsigned int)-1;
120 template<typename Iterator>
121 void adjust_eol_offsets(Scanner<Iterator>* s, std::size_t adjustment)
127 s->eol_offsets = aq_create();
137 if (adjustment > q->queue[i])
140 q->queue[i] -= adjustment;
142 if (i == q->max_size)
145 if (adjustment > q->queue[i])
148 q->queue[i] -= adjustment;
151 template<typename Iterator>
152 int count_backslash_newlines(Scanner<Iterator> *s, uchar *cursor)
154 std::size_t diff, offset;
157 /* figure out how many backslash-newlines skipped over unknowingly. */
158 diff = cursor - s->bot;
159 offset = get_first_eol_offset(s);
160 while (offset <= diff && offset != (unsigned int)-1)
163 aq_pop(s->eol_offsets);
164 offset = get_first_eol_offset(s);
169 BOOST_WAVE_DECL bool is_backslash(uchar *p, uchar *end, int &len);
171 #define BOOST_WAVE_BSIZE 196608
172 template<typename Iterator>
173 uchar *fill(Scanner<Iterator> *s, uchar *cursor)
175 using namespace std; // some systems have memcpy etc. in namespace std
179 std::ptrdiff_t cnt = s->tok - s->bot;
184 memmove(s->bot, s->tok, s->lim - s->tok);
185 s->tok = s->cur = s->bot;
189 adjust_eol_offsets(s, cnt);
192 if((s->top - s->lim) < BOOST_WAVE_BSIZE)
194 uchar *buf = (uchar*) malloc(((s->lim - s->bot) + BOOST_WAVE_BSIZE)*sizeof(uchar));
197 (*s->error_proc)(s, lexing_exception::unexpected_error,
200 /* get the scanner to stop */
205 memmove(buf, s->tok, s->lim - s->tok);
206 s->tok = s->cur = buf;
207 s->ptr = &buf[s->ptr - s->bot];
208 cursor = &buf[cursor - s->bot];
209 s->lim = &buf[s->lim - s->bot];
210 s->top = &s->lim[BOOST_WAVE_BSIZE];
215 cnt = std::distance(s->act, s->last);
216 if (cnt > BOOST_WAVE_BSIZE)
217 cnt = BOOST_WAVE_BSIZE;
218 uchar * dst = s->lim;
219 for (std::ptrdiff_t idx = 0; idx < cnt; ++idx)
224 if (cnt != BOOST_WAVE_BSIZE)
226 s->eof = &s->lim[cnt]; *(s->eof)++ = '\0';
229 /* backslash-newline erasing time */
231 /* first scan for backslash-newline and erase them */
232 for (p = s->lim; p < s->lim + cnt - 2; ++p)
235 if (is_backslash(p, s->lim + cnt, len))
237 if (*(p+len) == '\n')
239 int offset = len + 1;
240 memmove(p, p + offset, s->lim + cnt - p - offset);
243 aq_enqueue(s->eol_offsets, p - s->bot + 1);
245 else if (*(p+len) == '\r')
247 if (*(p+len+1) == '\n')
249 int offset = len + 2;
250 memmove(p, p + offset, s->lim + cnt - p - offset);
256 int offset = len + 1;
257 memmove(p, p + offset, s->lim + cnt - p - offset);
261 aq_enqueue(s->eol_offsets, p - s->bot + 1);
266 /* FIXME: the following code should be fixed to recognize correctly the
267 trigraph backslash token */
269 /* check to see if what we just read ends in a backslash */
272 uchar last = s->lim[cnt-1];
273 uchar last2 = s->lim[cnt-2];
277 int next = get_one_char(s);
278 /* check for \ \n or \ \r or \ \r \n straddling the border */
281 --cnt; /* chop the final \, we've already read the \n. */
282 aq_enqueue(s->eol_offsets, cnt + (s->lim - s->bot));
284 else if (next == '\r')
286 int next2 = get_one_char(s);
289 --cnt; /* skip the backslash */
293 /* rewind one, and skip one char */
294 rewind_stream(s, -1);
297 aq_enqueue(s->eol_offsets, cnt + (s->lim - s->bot));
299 else if (next != -1) /* -1 means end of file */
301 /* next was something else, so rewind the stream */
302 rewind_stream(s, -1);
306 else if (last == '\r' && last2 == '\\')
308 int next = get_one_char(s);
311 cnt -= 2; /* skip the \ \r */
315 /* rewind one, and skip two chars */
316 rewind_stream(s, -1);
319 aq_enqueue(s->eol_offsets, cnt + (s->lim - s->bot));
322 else if (last == '\n' && last2 == '\\')
325 aq_enqueue(s->eol_offsets, cnt + (s->lim - s->bot));
330 if (s->eof) /* eof needs adjusting if we erased backslash-newlines */
338 #undef BOOST_WAVE_BSIZE
340 ///////////////////////////////////////////////////////////////////////////////
341 // Special wrapper class holding the current cursor position
342 struct BOOST_WAVE_DECL uchar_wrapper
344 uchar_wrapper (uchar *base_cursor, std::size_t column = 1);
346 uchar_wrapper& operator++();
348 uchar_wrapper& operator--();
350 uchar operator* () const;
352 operator uchar *() const;
354 friend BOOST_WAVE_DECL std::ptrdiff_t
355 operator- (uchar_wrapper const& lhs, uchar_wrapper const& rhs);
362 ///////////////////////////////////////////////////////////////////////////////
363 template<typename Iterator>
364 boost::wave::token_id scan(Scanner<Iterator> *s)
366 BOOST_ASSERT(0 != s->error_proc); // error handler must be given
368 uchar_wrapper cursor (s->tok = s->cur, s->column = s->curr_column);
369 uchar_wrapper marker (s->ptr);
370 uchar_wrapper limit (s->lim);
372 typedef BOOST_WAVE_STRINGTYPE string_type;
373 string_type rawstringdelim; // for use with C++11 raw string literals
375 // include the correct Re2C token definition rules
376 #if (defined (__FreeBSD__) || defined (__DragonFly__) || defined (__OpenBSD__)) && defined (T_DIVIDE)
379 #if BOOST_WAVE_USE_STRICT_LEXER != 0
380 #include "strict_cpp_re.inc"
382 #include "cpp_re.inc"
387 ///////////////////////////////////////////////////////////////////////////////
389 } // namespace re2clex
390 } // namespace cpplexer
398 #undef BOOST_WAVE_RET
405 // the suffix header occurs after all of the code
406 #ifdef BOOST_HAS_ABI_HEADERS
407 #include BOOST_ABI_SUFFIX
410 #endif // !defined(CPP_RE_HPP_B76C4F5E_63E9_4B8A_9975_EC32FA6BF027_INCLUDED)