cpp_re2c_lexer.hpp 14 KB

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