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