idl_lex_iterator.hpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*=============================================================================
  2. Boost.Wave: A Standard compliant C++ preprocessor library
  3. Sample: Re2C based IDL lexer
  4. Definition of the lexer iterator
  5. http://www.boost.org/
  6. Copyright (c) 2001-2010 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. #if !defined(IDL_LEX_ITERATOR_HPP_7926F865_E02F_4950_9EB5_5F453C9FF953_INCLUDED)
  11. #define IDL_LEX_ITERATOR_HPP_7926F865_E02F_4950_9EB5_5F453C9FF953_INCLUDED
  12. #include <string>
  13. #include <iostream>
  14. #include <boost/assert.hpp>
  15. #include <boost/shared_ptr.hpp>
  16. #include <boost/spirit/include/support_multi_pass.hpp>
  17. #include <boost/wave/language_support.hpp>
  18. #include <boost/wave/util/file_position.hpp>
  19. #include <boost/wave/util/functor_input.hpp>
  20. #include "idl_lex_interface.hpp"
  21. #if 0 != __COMO_VERSION__ || !BOOST_WORKAROUND(BOOST_MSVC, <= 1310)
  22. #define BOOST_WAVE_EOF_PREFIX static
  23. #else
  24. #define BOOST_WAVE_EOF_PREFIX
  25. #endif
  26. ///////////////////////////////////////////////////////////////////////////////
  27. namespace boost {
  28. namespace wave {
  29. namespace idllexer {
  30. namespace impl {
  31. ///////////////////////////////////////////////////////////////////////////////
  32. //
  33. // lex_iterator_functor_shim
  34. //
  35. ///////////////////////////////////////////////////////////////////////////////
  36. template <typename TokenT>
  37. class lex_iterator_functor_shim
  38. {
  39. typedef typename TokenT::position_type position_type;
  40. public:
  41. lex_iterator_functor_shim()
  42. #if /*0 != __DECCXX_VER || */defined(__PGI)
  43. : eof()
  44. #endif // 0 != __DECCXX_VER
  45. {}
  46. // interface to the boost::spirit::classic::iterator_policies::functor_input
  47. // policy
  48. typedef TokenT result_type;
  49. BOOST_WAVE_EOF_PREFIX result_type const eof;
  50. typedef lex_iterator_functor_shim unique;
  51. typedef cpplexer::lex_input_interface<TokenT>* shared;
  52. template <typename MultiPass>
  53. static result_type& get_next(MultiPass& mp, result_type& result)
  54. {
  55. return mp.shared()->ftor->get(result);
  56. }
  57. // this will be called whenever the last reference to a multi_pass will
  58. // be released
  59. template <typename MultiPass>
  60. static void destroy(MultiPass& mp)
  61. {
  62. delete mp.shared()->ftor;
  63. }
  64. template <typename MultiPass>
  65. static void set_position(MultiPass& mp, position_type const &pos)
  66. {
  67. mp.shared()->ftor->set_position(pos);
  68. }
  69. private:
  70. boost::shared_ptr<cpplexer::lex_input_interface<TokenT> > functor_ptr;
  71. };
  72. #if 0 != __COMO_VERSION__ || !BOOST_WORKAROUND(BOOST_MSVC, <= 1310)
  73. ///////////////////////////////////////////////////////////////////////////////
  74. // eof token
  75. template <typename TokenT>
  76. typename lex_iterator_functor_shim<TokenT>::result_type const
  77. lex_iterator_functor_shim<TokenT>::eof =
  78. typename lex_iterator_functor_shim<TokenT>::result_type();
  79. #endif // 0 != __COMO_VERSION__
  80. ///////////////////////////////////////////////////////////////////////////////
  81. } // namespace impl
  82. ///////////////////////////////////////////////////////////////////////////////
  83. //
  84. // lex_iterator
  85. //
  86. // A generic C++ lexer interface class, which allows to plug in different
  87. // lexer implementations (template parameter LexT). The following
  88. // requirement apply:
  89. //
  90. // - the lexer type should have a function implemented, which returnes
  91. // the next lexed token from the input stream:
  92. // typename LexT::token_type get();
  93. // - at the end of the input stream this function should return the
  94. // eof token equivalent
  95. // - the lexer should implement a constructor taking two iterators
  96. // pointing to the beginning and the end of the input stream and
  97. // a third parameter containing the name of the parsed input file
  98. //
  99. ///////////////////////////////////////////////////////////////////////////////
  100. ///////////////////////////////////////////////////////////////////////////////
  101. // Divide the given functor type into its components (unique and shared)
  102. // and build a std::pair from these parts
  103. template <typename FunctorData>
  104. struct make_multi_pass
  105. {
  106. typedef
  107. std::pair<typename FunctorData::unique, typename FunctorData::shared>
  108. functor_data_type;
  109. typedef typename FunctorData::result_type result_type;
  110. typedef boost::spirit::iterator_policies::split_functor_input input_policy;
  111. typedef boost::spirit::iterator_policies::ref_counted ownership_policy;
  112. #if defined(BOOST_WAVE_DEBUG)
  113. typedef boost::spirit::iterator_policies::buf_id_check check_policy;
  114. #else
  115. typedef boost::spirit::iterator_policies::no_check check_policy;
  116. #endif
  117. typedef boost::spirit::iterator_policies::split_std_deque storage_policy;
  118. typedef boost::spirit::iterator_policies::default_policy<
  119. ownership_policy, check_policy, input_policy, storage_policy>
  120. policy_type;
  121. typedef boost::spirit::multi_pass<functor_data_type, policy_type> type;
  122. };
  123. ///////////////////////////////////////////////////////////////////////////////
  124. template <typename TokenT>
  125. class lex_iterator
  126. : public make_multi_pass<impl::lex_iterator_functor_shim<TokenT> >::type
  127. {
  128. typedef impl::lex_iterator_functor_shim<TokenT> input_policy_type;
  129. typedef typename make_multi_pass<input_policy_type>::type base_type;
  130. typedef typename make_multi_pass<input_policy_type>::functor_data_type
  131. functor_data_type;
  132. typedef typename input_policy_type::unique unique_functor_type;
  133. typedef typename input_policy_type::shared shared_functor_type;
  134. public:
  135. typedef TokenT token_type;
  136. lex_iterator()
  137. {}
  138. template <typename IteratorT>
  139. lex_iterator(IteratorT const &first, IteratorT const &last,
  140. typename TokenT::position_type const &pos,
  141. boost::wave::language_support language)
  142. : base_type(
  143. functor_data_type(
  144. unique_functor_type(),
  145. cpplexer::lex_input_interface_generator<TokenT>
  146. ::new_lexer(first, last, pos, language)
  147. )
  148. )
  149. {}
  150. void set_position(typename TokenT::position_type const &pos)
  151. {
  152. typedef typename TokenT::position_type position_type;
  153. // set the new position in the current token
  154. token_type const& currtoken = this->base_type::dereference(*this);
  155. position_type currpos = currtoken.get_position();
  156. currpos.set_file(pos.get_file());
  157. currpos.set_line(pos.get_line());
  158. const_cast<token_type&>(currtoken).set_position(currpos);
  159. // set the new position for future tokens as well
  160. unique_functor_type::set_position(*this, currpos);
  161. }
  162. #if BOOST_WAVE_SUPPORT_PRAGMA_ONCE != 0
  163. // this sample does no include guard detection
  164. bool has_include_guards(std::string&) const { return false; }
  165. #endif
  166. };
  167. ///////////////////////////////////////////////////////////////////////////////
  168. } // namespace idllexer
  169. } // namespace wave
  170. } // namespace boost
  171. #undef BOOST_WAVE_EOF_PREFIX
  172. #endif // !defined(IDL_LEX_ITERATOR_HPP_7926F865_E02F_4950_9EB5_5F453C9FF953_INCLUDED)