1 // Copyright (c) 2001-2011 Hartmut Kaiser
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 #if !defined(BOOST_SPIRIT_LEX_LEXER_FUNCTOR_NOV_18_2007_1112PM)
7 #define BOOST_SPIRIT_LEX_LEXER_FUNCTOR_NOV_18_2007_1112PM
13 #include <boost/mpl/bool.hpp>
14 #include <boost/detail/iterator.hpp>
15 #include <boost/detail/workaround.hpp>
16 #include <boost/spirit/home/lex/lexer/pass_flags.hpp>
17 #include <boost/assert.hpp>
19 #if 0 != __COMO_VERSION__ || !BOOST_WORKAROUND(BOOST_MSVC, <= 1310)
20 #define BOOST_SPIRIT_STATIC_EOF 1
21 #define BOOST_SPIRIT_EOF_PREFIX static
23 #define BOOST_SPIRIT_EOF_PREFIX
26 namespace boost { namespace spirit { namespace lex { namespace lexertl
28 ///////////////////////////////////////////////////////////////////////////
30 // functor is a template usable as the functor object for the
31 // multi_pass iterator allowing to wrap a lexertl based dfa into a
32 // iterator based interface.
34 // Token: the type of the tokens produced by this functor
35 // this needs to expose a constructor with the following
38 // Token(std::size_t id, std::size_t state,
39 // Iterator start, Iterator end)
41 // where 'id' is the token id, state is the lexer state,
42 // this token has been matched in, and 'first' and 'end'
43 // mark the start and the end of the token with respect
44 // to the underlying character stream.
46 // this is expected to encapsulate the shared part of the
47 // functor (see lex/lexer/lexertl/functor_data.hpp for an
48 // example and documentation).
49 // Iterator: the type of the underlying iterator
51 // this is expected to be a mpl::bool_, if mpl::true_ the
52 // functor invokes functors which (optionally) have
53 // been attached to the token definitions.
55 // this is expected to be a mpl::bool_, if mpl::true_ the
56 // functor supports different lexer states,
57 // otherwise no lexer state is supported.
59 ///////////////////////////////////////////////////////////////////////////
60 template <typename Token
61 , template <typename, typename, typename, typename> class FunctorData
62 , typename Iterator = typename Token::iterator_type
63 , typename SupportsActors = mpl::false_
64 , typename SupportsState = typename Token::has_state>
69 boost::detail::iterator_traits<Iterator>::value_type
73 // Needed by compilers not implementing the resolution to DR45. For
75 // http://www.open-std.org/JTC1/SC22/WG21/docs/cwg_defects.html#45.
76 typedef typename Token::token_value_type token_value_type;
77 friend class FunctorData<Iterator, SupportsActors, SupportsState
80 // Helper template allowing to assign a value on exit
84 assign_on_exit(T& dst, T const& src)
85 : dst_(dst), src_(src) {}
96 // silence MSVC warning C4512: assignment operator could not be generated
97 assign_on_exit& operator= (assign_on_exit const&);
107 #if BOOST_WORKAROUND(BOOST_MSVC, <= 1310)
108 // somehow VC7.1 needs this (meaningless) assignment operator
109 functor& operator=(functor const& rhs)
115 ///////////////////////////////////////////////////////////////////////
116 // interface to the iterator_policies::split_functor_input policy
117 typedef Token result_type;
118 typedef functor unique;
119 typedef FunctorData<Iterator, SupportsActors, SupportsState
120 , token_value_type> shared;
122 BOOST_SPIRIT_EOF_PREFIX result_type const eof;
124 ///////////////////////////////////////////////////////////////////////
125 typedef Iterator iterator_type;
126 typedef typename shared::semantic_actions_type semantic_actions_type;
127 typedef typename shared::next_token_functor next_token_functor;
128 typedef typename shared::get_state_name_type get_state_name_type;
130 // this is needed to wrap the semantic actions in a proper way
131 typedef typename shared::wrap_action_type wrap_action_type;
133 ///////////////////////////////////////////////////////////////////////
134 template <typename MultiPass>
135 static result_type& get_next(MultiPass& mp, result_type& result)
137 typedef typename result_type::id_type id_type;
139 shared& data = mp.shared()->ftor;
142 if (data.get_first() == data.get_last())
143 #if defined(BOOST_SPIRIT_STATIC_EOF)
146 return result = mp.ftor.eof;
150 Iterator end = data.get_first();
151 std::size_t unique_id = boost::lexer::npos;
152 bool prev_bol = false;
154 // lexer matching might change state
155 std::size_t state = data.get_state();
156 std::size_t id = data.next(end, unique_id, prev_bol);
158 if (boost::lexer::npos == id) { // no match
159 #if defined(BOOST_SPIRIT_LEXERTL_DEBUG)
161 Iterator it = data.get_first();
162 for (std::size_t i = 0; i < 10 && it != data.get_last(); ++it, ++i)
165 std::cerr << "Not matched, in state: " << state
166 << ", lookahead: >" << next << "<" << std::endl;
168 return result = result_type(0);
170 else if (0 == id) { // EOF reached
171 #if defined(BOOST_SPIRIT_STATIC_EOF)
174 return result = mp.ftor.eof;
178 #if defined(BOOST_SPIRIT_LEXERTL_DEBUG)
182 for (std::size_t i = 0; i < 10 && it != data.get_last(); ++it, ++i)
185 std::cerr << "Matched: " << id << ", in state: "
186 << state << ", string: >"
187 << std::basic_string<char_type>(data.get_first(), end) << "<"
188 << ", lookahead: >" << next << "<" << std::endl;
189 if (data.get_state() != state) {
190 std::cerr << "Switched to state: "
191 << data.get_state() << std::endl;
195 // account for a possibly pending lex::more(), i.e. moving
196 // data.first_ back to the start of the previously matched token.
197 bool adjusted = data.adjust_start();
199 // set the end of the matched input sequence in the token data
202 // invoke attached semantic actions, if defined, might change
203 // state, id, data.first_, and/or end
204 BOOST_SCOPED_ENUM(pass_flags) pass =
205 data.invoke_actions(state, id, unique_id, end);
207 if (data.has_value()) {
208 // return matched token using the token value as set before
209 // using data.set_value(), advancing 'data.first_' past the
211 assign_on_exit<Iterator> on_exit(data.get_first(), end);
212 return result = result_type(id_type(id), state, data.get_value());
214 else if (pass_flags::pass_normal == pass) {
215 // return matched token, advancing 'data.first_' past the
217 assign_on_exit<Iterator> on_exit(data.get_first(), end);
218 return result = result_type(id_type(id), state, data.get_first(), end);
220 else if (pass_flags::pass_fail == pass) {
221 #if defined(BOOST_SPIRIT_LEXERTL_DEBUG)
222 std::cerr << "Matching forced to fail" << std::endl;
224 // if the data.first_ got adjusted above, revert this adjustment
226 data.revert_adjust_start();
228 // one of the semantic actions signaled no-match
229 data.reset_bol(prev_bol);
230 if (state != data.get_state())
231 continue; // retry matching if state has changed
233 // if the state is unchanged repeating the match wouldn't
234 // move the input forward, causing an infinite loop
235 return result = result_type(0);
238 #if defined(BOOST_SPIRIT_LEXERTL_DEBUG)
239 std::cerr << "Token ignored, continuing matching" << std::endl;
241 // if this token needs to be ignored, just repeat the matching,
242 // while starting right after the current match
243 data.get_first() = end;
247 // set_state are propagated up to the iterator interface, allowing to
248 // manipulate the current lexer state through any of the exposed
250 template <typename MultiPass>
251 static std::size_t set_state(MultiPass& mp, std::size_t state)
253 std::size_t oldstate = mp.shared()->ftor.get_state();
254 mp.shared()->ftor.set_state(state);
256 #if defined(BOOST_SPIRIT_LEXERTL_DEBUG)
257 std::cerr << "Switching state from: " << oldstate
264 template <typename MultiPass>
265 static std::size_t get_state(MultiPass& mp)
267 return mp.shared()->ftor.get_state();
270 template <typename MultiPass>
272 map_state(MultiPass const& mp, char_type const* statename)
274 return mp.shared()->ftor.get_state_id(statename);
277 // we don't need this, but it must be there
278 template <typename MultiPass>
279 static void destroy(MultiPass const&) {}
282 #if defined(BOOST_SPIRIT_STATIC_EOF)
283 ///////////////////////////////////////////////////////////////////////////
285 ///////////////////////////////////////////////////////////////////////////
286 template <typename Token
287 , template <typename, typename, typename, typename> class FunctorData
288 , typename Iterator, typename SupportsActors, typename SupportsState>
289 typename functor<Token, FunctorData, Iterator, SupportsActors, SupportsState>::result_type const
290 functor<Token, FunctorData, Iterator, SupportsActors, SupportsState>::eof =
291 typename functor<Token, FunctorData, Iterator, SupportsActors
292 , SupportsState>::result_type();
297 #undef BOOST_SPIRIT_EOF_PREFIX
298 #undef BOOST_SPIRIT_STATIC_EOF