1 /*=============================================================================
2 Boost.Wave: A Standard compliant C++ preprocessor library
4 Definition of the lexertl based lexer iterator
8 Copyright (c) 2001-2010 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(BOOST_WAVE_LEXERTL_ITERATOR_HPP_INCLUDED)
14 #define BOOST_WAVE_LEXERTL_ITERATOR_HPP_INCLUDED
18 #include <boost/assert.hpp>
19 #include <boost/shared_ptr.hpp>
20 #include <boost/spirit/include/support_multi_pass.hpp>
22 #include <boost/wave/language_support.hpp>
23 #include <boost/wave/util/file_position.hpp>
24 #include <boost/wave/util/functor_input.hpp>
26 #include "lexertl_interface.hpp"
28 #if 0 != __COMO_VERSION__ || !BOOST_WORKAROUND(BOOST_MSVC, <= 1310)
29 #define BOOST_WAVE_EOF_PREFIX static
31 #define BOOST_WAVE_EOF_PREFIX
34 ///////////////////////////////////////////////////////////////////////////////
35 namespace boost { namespace wave { namespace cpplexer { namespace lexertl
38 ///////////////////////////////////////////////////////////////////////////////
41 ///////////////////////////////////////////////////////////////////////////////
43 // lex_iterator_functor_shim
45 ///////////////////////////////////////////////////////////////////////////////
47 template <typename TokenT>
48 class iterator_functor_shim
50 typedef typename TokenT::position_type position_type;
53 iterator_functor_shim()
54 #if /*0 != __DECCXX_VER || */defined(__PGI)
56 #endif // 0 != __DECCXX_VER
59 // interface to the boost::spirit::classic::iterator_policies::functor_input
61 typedef TokenT result_type;
62 typedef iterator_functor_shim unique;
63 typedef lex_input_interface<TokenT>* shared;
65 BOOST_WAVE_EOF_PREFIX result_type const eof;
67 template <typename MultiPass>
68 static result_type& get_next(MultiPass& mp, result_type& result)
70 return mp.shared()->ftor->get(result);
73 // this will be called whenever the last reference to a multi_pass will
75 template <typename MultiPass>
76 static void destroy(MultiPass& mp)
78 delete mp.shared()->ftor;
81 template <typename MultiPass>
82 static void set_position(MultiPass& mp, position_type const &pos)
84 mp.shared()->ftor->set_position(pos);
87 #if BOOST_WAVE_SUPPORT_PRAGMA_ONCE != 0
88 template <typename MultiPass>
89 static bool has_include_guards(MultiPass& mp, std::string& guard_name)
91 return mp.shared()->ftor->has_include_guards(guard_name);
96 boost::shared_ptr<lex_input_interface<TokenT> > functor_ptr;
99 #if 0 != __COMO_VERSION__ || !BOOST_WORKAROUND(BOOST_MSVC, <= 1310)
100 ///////////////////////////////////////////////////////////////////////////////
102 template <typename TokenT>
103 typename iterator_functor_shim<TokenT>::result_type const
104 iterator_functor_shim<TokenT>::eof =
105 typename iterator_functor_shim<TokenT>::result_type();
106 #endif // 0 != __COMO_VERSION__
108 ///////////////////////////////////////////////////////////////////////////////
111 ///////////////////////////////////////////////////////////////////////////////
115 // A generic C++ lexer interface class, which allows to plug in different
116 // lexer implementations (template parameter LexT). The following
117 // requirement apply:
119 // - the lexer type should have a function implemented, which returnes
120 // the next lexed token from the input stream:
121 // typename LexT::token_type get();
122 // - at the end of the input stream this function should return the
123 // eof token equivalent
124 // - the lexer should implement a constructor taking two iterators
125 // pointing to the beginning and the end of the input stream and
126 // a third parameter containing the name of the parsed input file,
127 // the 4th parameter contains the information about the mode the
128 // preprocessor is used in (C99/C++ mode etc.)
130 ///////////////////////////////////////////////////////////////////////////////
132 ///////////////////////////////////////////////////////////////////////////////
133 // Divide the given functor type into its components (unique and shared)
134 // and build a std::pair from these parts
135 template <typename FunctorData>
136 struct make_multi_pass
139 std::pair<typename FunctorData::unique, typename FunctorData::shared>
141 typedef typename FunctorData::result_type result_type;
143 typedef boost::spirit::iterator_policies::split_functor_input input_policy;
144 typedef boost::spirit::iterator_policies::ref_counted ownership_policy;
145 #if defined(BOOST_WAVE_DEBUG)
146 typedef boost::spirit::iterator_policies::buf_id_check check_policy;
148 typedef boost::spirit::iterator_policies::no_check check_policy;
150 typedef boost::spirit::iterator_policies::split_std_deque storage_policy;
152 typedef boost::spirit::iterator_policies::default_policy<
153 ownership_policy, check_policy, input_policy, storage_policy>
155 typedef boost::spirit::multi_pass<functor_data_type, policy_type> type;
158 template <typename TokenT>
160 : public make_multi_pass<impl::iterator_functor_shim<TokenT> >::type
162 typedef impl::iterator_functor_shim<TokenT> input_policy_type;
164 typedef typename make_multi_pass<input_policy_type>::type base_type;
165 typedef typename make_multi_pass<input_policy_type>::functor_data_type
168 typedef typename input_policy_type::unique unique_functor_type;
169 typedef typename input_policy_type::shared shared_functor_type;
172 typedef TokenT token_type;
177 template <typename IteratorT>
178 lex_iterator(IteratorT const &first, IteratorT const &last,
179 typename TokenT::position_type const &pos,
180 boost::wave::language_support language)
183 unique_functor_type(),
184 lexertl_input_interface<TokenT>
185 ::new_lexer(first, last, pos, language)
190 void set_position(typename TokenT::position_type const &pos)
192 typedef typename TokenT::position_type position_type;
194 // set the new position in the current token
195 token_type const& currtoken = this->base_type::dereference(*this);
196 position_type currpos = currtoken.get_position();
198 currpos.set_file(pos.get_file());
199 currpos.set_line(pos.get_line());
200 const_cast<token_type&>(currtoken).set_position(currpos);
202 // set the new position for future tokens as well
203 if (token_type::string_type::npos !=
204 currtoken.get_value().find_first_of('\n'))
206 currpos.set_line(pos.get_line() + 1);
208 unique_functor_type::set_position(*this, currpos);
211 #if BOOST_WAVE_SUPPORT_PRAGMA_ONCE != 0
212 // Return, whether the current file has include guards. This function
213 // returns meaningful results only if the file was scanned completely.
214 // For now we always return false, but this can be fixed easily later on.
215 bool has_include_guards(std::string& guard_name) const
217 return base_type::get_functor().has_include_guards(*this, guard_name);
222 ///////////////////////////////////////////////////////////////////////////////
223 }}}} // namespace boost::wave::cpplexer::lexertl
225 #undef BOOST_WAVE_EOF_PREFIX
227 #endif // !defined(BOOST_WAVE_LEXERTL_ITERATOR_HPP_INCLUDED)