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