]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/spirit/home/lex/qi/state_switcher.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / spirit / home / lex / qi / state_switcher.hpp
1 // Copyright (c) 2001-2011 Hartmut Kaiser
2 // Copyright (c) 2010 Bryce Lelbach
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6
7 #if !defined(BOOST_SPIRIT_LEX_STATE_SWITCHER_SEP_23_2007_0714PM)
8 #define BOOST_SPIRIT_LEX_STATE_SWITCHER_SEP_23_2007_0714PM
9
10 #if defined(_MSC_VER)
11 #pragma once
12 #endif
13
14 #include <boost/spirit/home/support/info.hpp>
15 #include <boost/spirit/home/qi/detail/attributes.hpp>
16 #include <boost/spirit/home/support/common_terminals.hpp>
17 #include <boost/spirit/home/support/string_traits.hpp>
18 #include <boost/spirit/home/support/has_semantic_action.hpp>
19 #include <boost/spirit/home/support/handles_container.hpp>
20 #include <boost/spirit/home/qi/skip_over.hpp>
21 #include <boost/spirit/home/qi/domain.hpp>
22 #include <boost/spirit/home/qi/parser.hpp>
23 #include <boost/spirit/home/qi/meta_compiler.hpp>
24 #include <boost/mpl/print.hpp>
25
26 namespace boost { namespace spirit
27 {
28 ///////////////////////////////////////////////////////////////////////////
29 // Enablers
30 ///////////////////////////////////////////////////////////////////////////
31
32 // enables set_state(s)
33 template <typename A0>
34 struct use_terminal<qi::domain
35 , terminal_ex<tag::set_state, fusion::vector1<A0> >
36 > : traits::is_string<A0> {};
37
38 // enables *lazy* set_state(s)
39 template <>
40 struct use_lazy_terminal<
41 qi::domain, tag::set_state, 1
42 > : mpl::true_ {};
43
44 // enables in_state(s)[p]
45 template <typename A0>
46 struct use_directive<qi::domain
47 , terminal_ex<tag::in_state, fusion::vector1<A0> >
48 > : traits::is_string<A0> {};
49
50 // enables *lazy* in_state(s)[p]
51 template <>
52 struct use_lazy_directive<
53 qi::domain, tag::in_state, 1
54 > : mpl::true_ {};
55
56 }}
57
58 namespace boost { namespace spirit { namespace qi
59 {
60 #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
61 using spirit::set_state;
62 using spirit::in_state;
63 #endif
64 using spirit::set_state_type;
65 using spirit::in_state_type;
66
67 ///////////////////////////////////////////////////////////////////////////
68 namespace detail
69 {
70 template <typename Iterator>
71 inline std::size_t
72 set_lexer_state(Iterator& it, std::size_t state)
73 {
74 return it.set_state(state);
75 }
76
77 template <typename Iterator, typename Char>
78 inline std::size_t
79 set_lexer_state(Iterator& it, Char const* statename)
80 {
81 std::size_t state = it.map_state(statename);
82
83 // If the following assertion fires you probably used the
84 // set_state(...) or in_state(...)[...] lexer state switcher with
85 // a lexer state name unknown to the lexer (no token definitions
86 // have been associated with this lexer state).
87 BOOST_ASSERT(std::size_t(~0) != state);
88 return it.set_state(state);
89 }
90 }
91
92 ///////////////////////////////////////////////////////////////////////////
93 // Parser switching the state of the underlying lexer component.
94 // This parser gets used for the set_state(...) construct.
95 ///////////////////////////////////////////////////////////////////////////
96 template <typename State>
97 struct state_switcher
98 : primitive_parser<state_switcher<State> >
99 {
100 typedef typename
101 remove_const<typename traits::char_type_of<State>::type>::type
102 char_type;
103 typedef std::basic_string<char_type> string_type;
104
105 template <typename Context, typename Iterator>
106 struct attribute
107 {
108 typedef unused_type type;
109 };
110
111 state_switcher(char_type const* state)
112 : state(state) {}
113
114 template <typename Iterator, typename Context
115 , typename Skipper, typename Attribute>
116 bool parse(Iterator& first, Iterator const& last
117 , Context& /*context*/, Skipper const& skipper
118 , Attribute& /*attr*/) const
119 {
120 qi::skip_over(first, last, skipper); // always do a pre-skip
121
122 // just switch the state and return success
123 detail::set_lexer_state(first, state.c_str());
124 return true;
125 }
126
127 template <typename Context>
128 info what(Context& /*context*/) const
129 {
130 return info("set_state");
131 }
132
133 string_type state;
134 };
135
136 ///////////////////////////////////////////////////////////////////////////
137 namespace detail
138 {
139 template <typename Iterator>
140 struct reset_state_on_exit
141 {
142 template <typename State>
143 reset_state_on_exit(Iterator& it_, State state_)
144 : it(it_)
145 , state(set_lexer_state(it_, traits::get_c_string(state_)))
146 {}
147
148 ~reset_state_on_exit()
149 {
150 // reset the state of the underlying lexer instance
151 set_lexer_state(it, state);
152 }
153
154 Iterator& it;
155 std::size_t state;
156
157 private:
158 // silence MSVC warning C4512: assignment operator could not be generated
159 reset_state_on_exit& operator= (reset_state_on_exit const&);
160 };
161 }
162
163 ///////////////////////////////////////////////////////////////////////////
164 // Parser, which switches the state of the underlying lexer component
165 // for the execution of the embedded sub-parser, switching the state back
166 // afterwards. This parser gets used for the in_state(...)[p] construct.
167 ///////////////////////////////////////////////////////////////////////////
168 template <typename Subject, typename State>
169 struct state_switcher_context
170 : unary_parser<state_switcher_context<Subject, State> >
171 {
172 typedef Subject subject_type;
173 typedef typename traits::char_type_of<State>::type char_type;
174 typedef typename remove_const<char_type>::type non_const_char_type;
175
176 template <typename Context, typename Iterator>
177 struct attribute
178 {
179 typedef typename
180 traits::attribute_of<subject_type, Context, Iterator>::type
181 type;
182 };
183
184 state_switcher_context(Subject const& subject
185 , typename add_reference<State>::type state)
186 : subject(subject), state(state) {}
187
188 // The following conversion constructors are needed to make the
189 // in_state_switcher template usable
190 template <typename String>
191 state_switcher_context(
192 state_switcher_context<Subject, String> const& rhs)
193 : subject(rhs.subject), state(traits::get_c_string(rhs.state)) {}
194
195 template <typename Iterator, typename Context
196 , typename Skipper, typename Attribute>
197 bool parse(Iterator& first, Iterator const& last
198 , Context& context, Skipper const& skipper
199 , Attribute& attr) const
200 {
201 qi::skip_over(first, last, skipper); // always do a pre-skip
202
203 // the state has to be reset at exit in any case
204 detail::reset_state_on_exit<Iterator> guard(first, state);
205 return subject.parse(first, last, context, skipper, attr);
206 }
207
208 template <typename Context>
209 info what(Context& context) const
210 {
211 return info("in_state", subject.what(context));
212 }
213
214 Subject subject;
215 State state;
216
217 private:
218 // silence MSVC warning C4512: assignment operator could not be generated
219 state_switcher_context& operator= (state_switcher_context const&);
220 };
221
222 ///////////////////////////////////////////////////////////////////////////
223 // Parser generators: make_xxx function (objects)
224 ///////////////////////////////////////////////////////////////////////////
225 template <typename Modifiers, typename State>
226 struct make_primitive<terminal_ex<tag::set_state, fusion::vector1<State> >
227 , Modifiers, typename enable_if<traits::is_string<State> >::type>
228 {
229 typedef typename add_const<State>::type const_string;
230 typedef state_switcher<const_string> result_type;
231
232 template <typename Terminal>
233 result_type operator()(Terminal const& term, unused_type) const
234 {
235 return result_type(traits::get_c_string(fusion::at_c<0>(term.args)));
236 }
237 };
238
239 template <typename State, typename Subject, typename Modifiers>
240 struct make_directive<terminal_ex<tag::in_state, fusion::vector1<State> >
241 , Subject, Modifiers>
242 {
243 typedef typename add_const<State>::type const_string;
244 typedef state_switcher_context<Subject, const_string> result_type;
245
246 template <typename Terminal>
247 result_type operator()(Terminal const& term, Subject const& subject
248 , unused_type) const
249 {
250 return result_type(subject, fusion::at_c<0>(term.args));
251 }
252 };
253 }}}
254
255 namespace boost { namespace spirit { namespace traits
256 {
257 ///////////////////////////////////////////////////////////////////////////
258 template <typename Subject, typename State>
259 struct has_semantic_action<qi::state_switcher_context<Subject, State> >
260 : unary_has_semantic_action<Subject> {};
261
262 ///////////////////////////////////////////////////////////////////////////
263 template <typename Subject, typename State, typename Attribute
264 , typename Context, typename Iterator>
265 struct handles_container<qi::state_switcher_context<Subject, State>
266 , Attribute, Context, Iterator>
267 : unary_handles_container<Subject, Attribute, Context, Iterator> {};
268 }}}
269
270 #endif