cpp_lex_iterator.hpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*=============================================================================
  2. Boost.Wave: A Standard compliant C++ preprocessor library
  3. Definition of the lexer iterator
  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_LEX_ITERATOR_HPP_AF0C37E3_CBD8_4F33_A225_51CF576FA61F_INCLUDED)
  10. #define CPP_LEX_ITERATOR_HPP_AF0C37E3_CBD8_4F33_A225_51CF576FA61F_INCLUDED
  11. #include <string>
  12. #include <boost/assert.hpp>
  13. #include <boost/intrusive_ptr.hpp>
  14. #include <boost/wave/wave_config.hpp>
  15. #include <boost/spirit/include/support_multi_pass.hpp>
  16. #include <boost/wave/util/file_position.hpp>
  17. #include <boost/wave/util/functor_input.hpp>
  18. #include <boost/wave/cpplexer/cpp_lex_interface_generator.hpp>
  19. #include <boost/wave/language_support.hpp>
  20. // this must occur after all of the includes and before any code appears
  21. #ifdef BOOST_HAS_ABI_HEADERS
  22. #include BOOST_ABI_PREFIX
  23. #endif
  24. #if 0 != __COMO_VERSION__ || !BOOST_WORKAROUND(BOOST_MSVC, <= 1310)
  25. #define BOOST_WAVE_EOF_PREFIX static
  26. #else
  27. #define BOOST_WAVE_EOF_PREFIX
  28. #endif
  29. ///////////////////////////////////////////////////////////////////////////////
  30. namespace boost {
  31. namespace wave {
  32. namespace cpplexer {
  33. namespace impl {
  34. ///////////////////////////////////////////////////////////////////////////////
  35. //
  36. // lex_iterator_functor_shim
  37. //
  38. ///////////////////////////////////////////////////////////////////////////////
  39. template <typename TokenT>
  40. class lex_iterator_functor_shim
  41. {
  42. typedef typename TokenT::position_type position_type;
  43. public:
  44. lex_iterator_functor_shim()
  45. #if /*0 != __DECCXX_VER || */defined(__PGI)
  46. : eof()
  47. #endif
  48. {}
  49. #if BOOST_WORKAROUND(BOOST_MSVC, <= 1310)
  50. lex_iterator_functor_shim& operator= (lex_iterator_functor_shim const& rhs)
  51. { return *this; } // nothing to do here
  52. #endif
  53. // interface to the iterator_policies::split_functor_input policy
  54. typedef TokenT result_type;
  55. typedef lex_iterator_functor_shim unique;
  56. typedef lex_input_interface<TokenT>* shared;
  57. BOOST_WAVE_EOF_PREFIX result_type const eof;
  58. template <typename MultiPass>
  59. static result_type& get_next(MultiPass& mp, result_type& result)
  60. {
  61. return mp.shared()->ftor->get(result);
  62. }
  63. // this will be called whenever the last reference to a multi_pass will
  64. // be released
  65. template <typename MultiPass>
  66. static void destroy(MultiPass& mp)
  67. {
  68. delete mp.shared()->ftor;
  69. }
  70. template <typename MultiPass>
  71. static void set_position(MultiPass& mp, position_type const &pos)
  72. {
  73. mp.shared()->ftor->set_position(pos);
  74. }
  75. #if BOOST_WAVE_SUPPORT_PRAGMA_ONCE != 0
  76. template <typename MultiPass>
  77. static bool has_include_guards(MultiPass& mp, std::string& guard_name)
  78. {
  79. return mp.shared()->ftor->has_include_guards(guard_name);
  80. }
  81. #endif
  82. };
  83. ///////////////////////////////////////////////////////////////////////////////
  84. // eof token
  85. #if 0 != __COMO_VERSION__ || !BOOST_WORKAROUND(BOOST_MSVC, <= 1310)
  86. template <typename TokenT>
  87. typename lex_iterator_functor_shim<TokenT>::result_type const
  88. lex_iterator_functor_shim<TokenT>::eof;
  89. #endif // 0 != __COMO_VERSION__
  90. ///////////////////////////////////////////////////////////////////////////////
  91. } // namespace impl
  92. ///////////////////////////////////////////////////////////////////////////////
  93. //
  94. // lex_iterator
  95. //
  96. // A generic C++ lexer interface class, which allows to plug in different
  97. // lexer implementations. The interface between the lexer type used and
  98. // the preprocessor component depends on the token type only (template
  99. // parameter TokenT).
  100. // Additionally, the following requirements apply:
  101. //
  102. // - the lexer type should have a function implemented, which returnes
  103. // the next lexed token from the input stream:
  104. // typename TokenT get();
  105. // - at the end of the input stream this function should return the
  106. // eof token equivalent
  107. // - the lexer should implement a constructor taking two iterators
  108. // pointing to the beginning and the end of the input stream,
  109. // a third parameter containing the name of the parsed input file
  110. // and a 4th parameter of the type boost::wave::language_support
  111. // which specifies, which language subset should be supported (C++,
  112. // C99, C++11 etc.).
  113. //
  114. ///////////////////////////////////////////////////////////////////////////////
  115. ///////////////////////////////////////////////////////////////////////////////
  116. // Divide the given functor type into its components (unique and shared)
  117. // and build a std::pair from these parts
  118. template <typename FunctorData>
  119. struct make_multi_pass
  120. {
  121. typedef
  122. std::pair<typename FunctorData::unique, typename FunctorData::shared>
  123. functor_data_type;
  124. typedef typename FunctorData::result_type result_type;
  125. typedef boost::spirit::iterator_policies::split_functor_input input_policy;
  126. typedef boost::spirit::iterator_policies::ref_counted ownership_policy;
  127. #if defined(BOOST_WAVE_DEBUG)
  128. typedef boost::spirit::iterator_policies::buf_id_check check_policy;
  129. #else
  130. typedef boost::spirit::iterator_policies::no_check check_policy;
  131. #endif
  132. typedef boost::spirit::iterator_policies::split_std_deque storage_policy;
  133. typedef boost::spirit::iterator_policies::default_policy<
  134. ownership_policy, check_policy, input_policy, storage_policy>
  135. policy_type;
  136. typedef boost::spirit::multi_pass<functor_data_type, policy_type> type;
  137. };
  138. ///////////////////////////////////////////////////////////////////////////////
  139. template <typename TokenT>
  140. class lex_iterator
  141. : public make_multi_pass<impl::lex_iterator_functor_shim<TokenT> >::type
  142. {
  143. typedef impl::lex_iterator_functor_shim<TokenT> input_policy_type;
  144. typedef typename make_multi_pass<input_policy_type>::type base_type;
  145. typedef typename make_multi_pass<input_policy_type>::functor_data_type
  146. functor_data_type;
  147. typedef typename input_policy_type::unique unique_functor_type;
  148. typedef typename input_policy_type::shared shared_functor_type;
  149. public:
  150. typedef TokenT token_type;
  151. lex_iterator()
  152. {}
  153. template <typename IteratorT>
  154. lex_iterator(IteratorT const &first, IteratorT const &last,
  155. typename TokenT::position_type const &pos,
  156. boost::wave::language_support language)
  157. : base_type(
  158. functor_data_type(
  159. unique_functor_type(),
  160. lex_input_interface_generator<TokenT>
  161. ::new_lexer(first, last, pos, language)
  162. )
  163. )
  164. {}
  165. void set_position(typename TokenT::position_type const &pos)
  166. {
  167. typedef typename TokenT::position_type position_type;
  168. // set the new position in the current token
  169. token_type const& currtoken = this->base_type::dereference(*this);
  170. position_type currpos = currtoken.get_position();
  171. currpos.set_file(pos.get_file());
  172. currpos.set_line(pos.get_line());
  173. const_cast<token_type&>(currtoken).set_position(currpos);
  174. // set the new position for future tokens as well
  175. if (token_type::string_type::npos !=
  176. currtoken.get_value().find_first_of('\n'))
  177. {
  178. currpos.set_line(pos.get_line() + 1);
  179. }
  180. unique_functor_type::set_position(*this, currpos);
  181. }
  182. #if BOOST_WAVE_SUPPORT_PRAGMA_ONCE != 0
  183. // return, whether the current file has include guards
  184. // this function returns meaningful results only if the file was scanned
  185. // completely
  186. bool has_include_guards(std::string& guard_name) const
  187. {
  188. return unique_functor_type::has_include_guards(*this, guard_name);
  189. }
  190. #endif
  191. };
  192. ///////////////////////////////////////////////////////////////////////////////
  193. } // namespace cpplexer
  194. } // namespace wave
  195. } // namespace boost
  196. // the suffix header occurs after all of the code
  197. #ifdef BOOST_HAS_ABI_HEADERS
  198. #include BOOST_ABI_SUFFIX
  199. #endif
  200. #undef BOOST_WAVE_EOF_PREFIX
  201. #endif // !defined(CPP_LEX_ITERATOR_HPP_AF0C37E3_CBD8_4F33_A225_51CF576FA61F_INCLUDED)