]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/spirit/home/lex/lexer/lexertl/functor.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / spirit / home / lex / lexer / lexertl / functor.hpp
1 // Copyright (c) 2001-2011 Hartmut Kaiser
2 //
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)
5
6 #if !defined(BOOST_SPIRIT_LEX_LEXER_FUNCTOR_NOV_18_2007_1112PM)
7 #define BOOST_SPIRIT_LEX_LEXER_FUNCTOR_NOV_18_2007_1112PM
8
9 #if defined(_MSC_VER)
10 #pragma once
11 #endif
12
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>
18
19 #if 0 != __COMO_VERSION__ || !BOOST_WORKAROUND(BOOST_MSVC, <= 1310)
20 #define BOOST_SPIRIT_STATIC_EOF 1
21 #define BOOST_SPIRIT_EOF_PREFIX static
22 #else
23 #define BOOST_SPIRIT_EOF_PREFIX
24 #endif
25
26 namespace boost { namespace spirit { namespace lex { namespace lexertl
27 {
28 ///////////////////////////////////////////////////////////////////////////
29 //
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.
33 //
34 // Token: the type of the tokens produced by this functor
35 // this needs to expose a constructor with the following
36 // prototype:
37 //
38 // Token(std::size_t id, std::size_t state,
39 // Iterator start, Iterator end)
40 //
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.
45 // FunctorData:
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
50 // SupportsActors:
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.
54 // SupportState:
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.
58 //
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>
65 class functor
66 {
67 public:
68 typedef typename
69 boost::detail::iterator_traits<Iterator>::value_type
70 char_type;
71
72 private:
73 // Needed by compilers not implementing the resolution to DR45. For
74 // reference, see
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
78 , token_value_type>;
79
80 // Helper template allowing to assign a value on exit
81 template <typename T>
82 struct assign_on_exit
83 {
84 assign_on_exit(T& dst, T const& src)
85 : dst_(dst), src_(src) {}
86
87 ~assign_on_exit()
88 {
89 dst_ = src_;
90 }
91
92 T& dst_;
93 T const& src_;
94
95 private:
96 // silence MSVC warning C4512: assignment operator could not be generated
97 assign_on_exit& operator= (assign_on_exit const&);
98 };
99
100 public:
101 functor() {}
102
103 #if BOOST_WORKAROUND(BOOST_MSVC, <= 1310)
104 // somehow VC7.1 needs this (meaningless) assignment operator
105 functor& operator=(functor const& rhs)
106 {
107 return *this;
108 }
109 #endif
110
111 ///////////////////////////////////////////////////////////////////////
112 // interface to the iterator_policies::split_functor_input policy
113 typedef Token result_type;
114 typedef functor unique;
115 typedef FunctorData<Iterator, SupportsActors, SupportsState
116 , token_value_type> shared;
117
118 BOOST_SPIRIT_EOF_PREFIX result_type const eof;
119
120 ///////////////////////////////////////////////////////////////////////
121 typedef Iterator iterator_type;
122 typedef typename shared::semantic_actions_type semantic_actions_type;
123 typedef typename shared::next_token_functor next_token_functor;
124 typedef typename shared::get_state_name_type get_state_name_type;
125
126 // this is needed to wrap the semantic actions in a proper way
127 typedef typename shared::wrap_action_type wrap_action_type;
128
129 ///////////////////////////////////////////////////////////////////////
130 template <typename MultiPass>
131 static result_type& get_next(MultiPass& mp, result_type& result)
132 {
133 typedef typename result_type::id_type id_type;
134
135 shared& data = mp.shared()->ftor;
136 for(;;)
137 {
138 if (data.get_first() == data.get_last())
139 #if defined(BOOST_SPIRIT_STATIC_EOF)
140 return result = eof;
141 #else
142 return result = mp.ftor.eof;
143 #endif
144
145 data.reset_value();
146 Iterator end = data.get_first();
147 std::size_t unique_id = boost::lexer::npos;
148 bool prev_bol = false;
149
150 // lexer matching might change state
151 std::size_t state = data.get_state();
152 std::size_t id = data.next(end, unique_id, prev_bol);
153
154 if (boost::lexer::npos == id) { // no match
155 #if defined(BOOST_SPIRIT_LEXERTL_DEBUG)
156 std::string next;
157 Iterator it = data.get_first();
158 for (std::size_t i = 0; i < 10 && it != data.get_last(); ++it, ++i)
159 next += *it;
160
161 std::cerr << "Not matched, in state: " << state
162 << ", lookahead: >" << next << "<" << std::endl;
163 #endif
164 return result = result_type(0);
165 }
166 else if (0 == id) { // EOF reached
167 #if defined(BOOST_SPIRIT_STATIC_EOF)
168 return result = eof;
169 #else
170 return result = mp.ftor.eof;
171 #endif
172 }
173
174 #if defined(BOOST_SPIRIT_LEXERTL_DEBUG)
175 {
176 std::string next;
177 Iterator it = end;
178 for (std::size_t i = 0; i < 10 && it != data.get_last(); ++it, ++i)
179 next += *it;
180
181 std::cerr << "Matched: " << id << ", in state: "
182 << state << ", string: >"
183 << std::basic_string<char_type>(data.get_first(), end) << "<"
184 << ", lookahead: >" << next << "<" << std::endl;
185 if (data.get_state() != state) {
186 std::cerr << "Switched to state: "
187 << data.get_state() << std::endl;
188 }
189 }
190 #endif
191 // account for a possibly pending lex::more(), i.e. moving
192 // data.first_ back to the start of the previously matched token.
193 bool adjusted = data.adjust_start();
194
195 // set the end of the matched input sequence in the token data
196 data.set_end(end);
197
198 // invoke attached semantic actions, if defined, might change
199 // state, id, data.first_, and/or end
200 BOOST_SCOPED_ENUM(pass_flags) pass =
201 data.invoke_actions(state, id, unique_id, end);
202
203 if (data.has_value()) {
204 // return matched token using the token value as set before
205 // using data.set_value(), advancing 'data.first_' past the
206 // matched sequence
207 assign_on_exit<Iterator> on_exit(data.get_first(), end);
208 return result = result_type(id_type(id), state, data.get_value());
209 }
210 else if (pass_flags::pass_normal == pass) {
211 // return matched token, advancing 'data.first_' past the
212 // matched sequence
213 assign_on_exit<Iterator> on_exit(data.get_first(), end);
214 return result = result_type(id_type(id), state, data.get_first(), end);
215 }
216 else if (pass_flags::pass_fail == pass) {
217 #if defined(BOOST_SPIRIT_LEXERTL_DEBUG)
218 std::cerr << "Matching forced to fail" << std::endl;
219 #endif
220 // if the data.first_ got adjusted above, revert this adjustment
221 if (adjusted)
222 data.revert_adjust_start();
223
224 // one of the semantic actions signaled no-match
225 data.reset_bol(prev_bol);
226 if (state != data.get_state())
227 continue; // retry matching if state has changed
228
229 // if the state is unchanged repeating the match wouldn't
230 // move the input forward, causing an infinite loop
231 return result = result_type(0);
232 }
233
234 #if defined(BOOST_SPIRIT_LEXERTL_DEBUG)
235 std::cerr << "Token ignored, continuing matching" << std::endl;
236 #endif
237 // if this token needs to be ignored, just repeat the matching,
238 // while starting right after the current match
239 data.get_first() = end;
240 }
241 }
242
243 // set_state are propagated up to the iterator interface, allowing to
244 // manipulate the current lexer state through any of the exposed
245 // iterators.
246 template <typename MultiPass>
247 static std::size_t set_state(MultiPass& mp, std::size_t state)
248 {
249 std::size_t oldstate = mp.shared()->ftor.get_state();
250 mp.shared()->ftor.set_state(state);
251
252 #if defined(BOOST_SPIRIT_LEXERTL_DEBUG)
253 std::cerr << "Switching state from: " << oldstate
254 << " to: " << state
255 << std::endl;
256 #endif
257 return oldstate;
258 }
259
260 template <typename MultiPass>
261 static std::size_t get_state(MultiPass& mp)
262 {
263 return mp.shared()->ftor.get_state();
264 }
265
266 template <typename MultiPass>
267 static std::size_t
268 map_state(MultiPass const& mp, char_type const* statename)
269 {
270 return mp.shared()->ftor.get_state_id(statename);
271 }
272
273 // we don't need this, but it must be there
274 template <typename MultiPass>
275 static void destroy(MultiPass const&) {}
276 };
277
278 #if defined(BOOST_SPIRIT_STATIC_EOF)
279 ///////////////////////////////////////////////////////////////////////////
280 // eof token
281 ///////////////////////////////////////////////////////////////////////////
282 template <typename Token
283 , template <typename, typename, typename, typename> class FunctorData
284 , typename Iterator, typename SupportsActors, typename SupportsState>
285 typename functor<Token, FunctorData, Iterator, SupportsActors, SupportsState>::result_type const
286 functor<Token, FunctorData, Iterator, SupportsActors, SupportsState>::eof =
287 typename functor<Token, FunctorData, Iterator, SupportsActors
288 , SupportsState>::result_type();
289 #endif
290
291 }}}}
292
293 #undef BOOST_SPIRIT_EOF_PREFIX
294 #undef BOOST_SPIRIT_STATIC_EOF
295
296 #endif