Chris@16
|
1 /*=============================================================================
|
Chris@16
|
2 Copyright (c) 2001-2011 Joel de Guzman
|
Chris@16
|
3 http://spirit.sourceforge.net/
|
Chris@16
|
4
|
Chris@16
|
5 Distributed under the Boost Software License, Version 1.0. (See accompanying
|
Chris@16
|
6 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
7 =============================================================================*/
|
Chris@16
|
8 #ifndef BOOST_SPIRIT_MAKE_COMPONENT_OCTOBER_16_2008_1250PM
|
Chris@16
|
9 #define BOOST_SPIRIT_MAKE_COMPONENT_OCTOBER_16_2008_1250PM
|
Chris@16
|
10
|
Chris@16
|
11 #if defined(_MSC_VER)
|
Chris@16
|
12 #pragma once
|
Chris@16
|
13 #endif
|
Chris@16
|
14
|
Chris@16
|
15 #include <boost/spirit/include/phoenix_core.hpp>
|
Chris@16
|
16 #include <boost/proto/proto.hpp>
|
Chris@16
|
17 #include <boost/spirit/home/support/detail/make_cons.hpp>
|
Chris@16
|
18 #include <boost/spirit/home/support/modify.hpp>
|
Chris@16
|
19
|
Chris@16
|
20 namespace boost { namespace spirit
|
Chris@16
|
21 {
|
Chris@16
|
22 // There is no real "component" class. Each domain is responsible
|
Chris@16
|
23 // for creating its own components. You need to specialize this for
|
Chris@16
|
24 // each component in your domain. Use this as a guide.
|
Chris@16
|
25
|
Chris@16
|
26 template <typename Domain, typename Tag, typename Enable = void>
|
Chris@16
|
27 struct make_component
|
Chris@16
|
28 {
|
Chris@16
|
29 template <typename Sig>
|
Chris@16
|
30 struct result;
|
Chris@16
|
31
|
Chris@16
|
32 template <typename This, typename Elements, typename Modifiers>
|
Chris@16
|
33 struct result<This(Elements, Modifiers)>;
|
Chris@16
|
34
|
Chris@16
|
35 template <typename Elements, typename Modifiers>
|
Chris@16
|
36 typename result<make_component(Elements, Modifiers)>::type
|
Chris@16
|
37 operator()(Elements const& elements, Modifiers const& modifiers) const;
|
Chris@16
|
38 };
|
Chris@16
|
39
|
Chris@16
|
40 namespace tag
|
Chris@16
|
41 {
|
Chris@16
|
42 // Normally, we use proto tags as-is to distinguish operators.
|
Chris@16
|
43 // The special case is proto::tag::subscript. Spirit uses this
|
Chris@16
|
44 // as either sementic actions or directives. To distinguish between
|
Chris@16
|
45 // the two, we use these special tags below.
|
Chris@16
|
46
|
Chris@16
|
47 struct directive;
|
Chris@16
|
48 struct action;
|
Chris@16
|
49 }
|
Chris@16
|
50
|
Chris@16
|
51 template <typename Domain, typename T, typename Enable = void>
|
Chris@16
|
52 struct flatten_tree;
|
Chris@16
|
53 }}
|
Chris@16
|
54
|
Chris@16
|
55 namespace boost { namespace spirit { namespace detail
|
Chris@16
|
56 {
|
Chris@16
|
57 template <typename Expr, typename State, typename Data, typename Domain>
|
Chris@16
|
58 struct make_terminal_impl
|
Chris@16
|
59 : proto::transform_impl<Expr, State, Data>
|
Chris@16
|
60 {
|
Chris@16
|
61 typedef typename
|
Chris@16
|
62 proto::result_of::value<Expr>::type
|
Chris@16
|
63 value;
|
Chris@16
|
64
|
Chris@16
|
65 typedef typename result_of::make_cons<value>::type elements;
|
Chris@16
|
66
|
Chris@16
|
67 typedef
|
Chris@16
|
68 make_component<Domain, proto::tag::terminal>
|
Chris@16
|
69 make_component_;
|
Chris@16
|
70
|
Chris@16
|
71 typedef typename
|
Chris@16
|
72 make_component_::template
|
Chris@16
|
73 result<make_component_(elements, Data)>::type
|
Chris@16
|
74 result_type;
|
Chris@16
|
75
|
Chris@16
|
76 result_type operator()(
|
Chris@16
|
77 typename make_terminal_impl::expr_param expr
|
Chris@16
|
78 , typename make_terminal_impl::state_param /*state*/
|
Chris@16
|
79 , typename make_terminal_impl::data_param data
|
Chris@16
|
80 ) const
|
Chris@16
|
81 {
|
Chris@16
|
82 return typename make_terminal_impl::make_component_()(
|
Chris@16
|
83 detail::make_cons(proto::value(expr))
|
Chris@16
|
84 , data
|
Chris@16
|
85 );
|
Chris@16
|
86 }
|
Chris@16
|
87 };
|
Chris@16
|
88
|
Chris@16
|
89 template <typename Expr, typename State, typename Data, typename Domain>
|
Chris@16
|
90 struct make_terminal_impl<phoenix::actor<Expr>, State, Data, Domain>
|
Chris@16
|
91 : proto::transform_impl<phoenix::actor<Expr>, State, Data>
|
Chris@16
|
92 {
|
Chris@16
|
93 typedef phoenix::actor<Expr> value;
|
Chris@16
|
94 typedef typename result_of::make_cons<value>::type elements;
|
Chris@16
|
95 typedef make_component<Domain, proto::tag::terminal> make_component_;
|
Chris@16
|
96
|
Chris@16
|
97 typedef typename
|
Chris@16
|
98 make_component_::template
|
Chris@16
|
99 result<make_component_(elements, Data)>::type
|
Chris@16
|
100 result_type;
|
Chris@16
|
101
|
Chris@16
|
102 result_type operator()(
|
Chris@16
|
103 typename make_terminal_impl::expr_param expr
|
Chris@16
|
104 , typename make_terminal_impl::state_param /*state*/
|
Chris@16
|
105 , typename make_terminal_impl::data_param data
|
Chris@16
|
106 ) const
|
Chris@16
|
107 {
|
Chris@16
|
108 return typename make_terminal_impl::make_component_()(
|
Chris@16
|
109 detail::make_cons(expr)
|
Chris@16
|
110 , data
|
Chris@16
|
111 );
|
Chris@16
|
112 }
|
Chris@16
|
113 };
|
Chris@16
|
114
|
Chris@16
|
115 template <typename Expr, typename State, typename Data, typename Domain>
|
Chris@16
|
116 struct make_terminal_impl<phoenix::actor<Expr> &, State, Data, Domain>
|
Chris@16
|
117 : make_terminal_impl<phoenix::actor<Expr>, State, Data, Domain>
|
Chris@16
|
118 {};
|
Chris@16
|
119
|
Chris@16
|
120 template <typename Expr, typename State, typename Data, typename Domain>
|
Chris@16
|
121 struct make_terminal_impl<phoenix::actor<Expr> const &, State, Data, Domain>
|
Chris@16
|
122 : make_terminal_impl<phoenix::actor<Expr>, State, Data, Domain>
|
Chris@16
|
123 {};
|
Chris@16
|
124
|
Chris@16
|
125 template <typename Domain>
|
Chris@16
|
126 struct make_terminal : proto::transform<make_terminal<Domain> >
|
Chris@16
|
127 {
|
Chris@16
|
128 template<typename Expr, typename State, typename Data>
|
Chris@16
|
129 struct impl : make_terminal_impl<Expr, State, Data, Domain> {};
|
Chris@16
|
130 };
|
Chris@16
|
131
|
Chris@16
|
132 template <typename Domain, typename Tag, typename Grammar>
|
Chris@16
|
133 struct make_unary : proto::transform<make_unary<Domain, Tag, Grammar> >
|
Chris@16
|
134 {
|
Chris@16
|
135 template<typename Expr, typename State, typename Data>
|
Chris@16
|
136 struct impl : proto::transform_impl<Expr, State, Data>
|
Chris@16
|
137 {
|
Chris@16
|
138 typedef typename
|
Chris@16
|
139 proto::result_of::child_c<Expr, 0>::type
|
Chris@16
|
140 child;
|
Chris@16
|
141
|
Chris@16
|
142 typedef typename Grammar::
|
Chris@16
|
143 template result<Grammar(child, State, Data)>::type
|
Chris@16
|
144 child_component;
|
Chris@16
|
145
|
Chris@16
|
146 typedef typename
|
Chris@16
|
147 result_of::make_cons<child_component>::type
|
Chris@16
|
148 elements;
|
Chris@16
|
149
|
Chris@16
|
150 typedef make_component<Domain, Tag> make_component_;
|
Chris@16
|
151
|
Chris@16
|
152 typedef typename
|
Chris@16
|
153 make_component_::template
|
Chris@16
|
154 result<make_component_(elements, Data)>::type
|
Chris@16
|
155 result_type;
|
Chris@16
|
156
|
Chris@16
|
157 result_type operator()(
|
Chris@16
|
158 typename impl::expr_param expr
|
Chris@16
|
159 , typename impl::state_param state
|
Chris@16
|
160 , typename impl::data_param data
|
Chris@16
|
161 ) const
|
Chris@16
|
162 {
|
Chris@16
|
163 return typename impl::make_component_()(
|
Chris@16
|
164 detail::make_cons(
|
Chris@16
|
165 Grammar()(proto::child(expr), state, data))
|
Chris@16
|
166 , data
|
Chris@16
|
167 );
|
Chris@16
|
168 }
|
Chris@16
|
169 };
|
Chris@16
|
170 };
|
Chris@16
|
171
|
Chris@16
|
172 // un-flattened version
|
Chris@16
|
173 template <typename Domain, typename Tag, typename Grammar,
|
Chris@16
|
174 bool flatten = flatten_tree<Domain, Tag>::value>
|
Chris@16
|
175 struct make_binary
|
Chris@16
|
176 {
|
Chris@16
|
177 template<typename Expr, typename State, typename Data>
|
Chris@16
|
178 struct impl : proto::transform_impl<Expr, State, Data>
|
Chris@16
|
179 {
|
Chris@16
|
180 typedef typename Grammar::
|
Chris@16
|
181 template result<Grammar(
|
Chris@16
|
182 typename proto::result_of::child_c<Expr, 0>::type
|
Chris@16
|
183 , State, Data)>::type
|
Chris@16
|
184 lhs_component;
|
Chris@16
|
185
|
Chris@16
|
186 typedef typename Grammar::
|
Chris@16
|
187 template result<Grammar(
|
Chris@16
|
188 typename proto::result_of::child_c<Expr, 1>::type
|
Chris@16
|
189 , State, Data)>::type
|
Chris@16
|
190 rhs_component;
|
Chris@16
|
191
|
Chris@16
|
192 typedef typename
|
Chris@16
|
193 result_of::make_cons<
|
Chris@16
|
194 lhs_component
|
Chris@16
|
195 , typename result_of::make_cons<rhs_component>::type
|
Chris@16
|
196 >::type
|
Chris@16
|
197 elements_type;
|
Chris@16
|
198
|
Chris@16
|
199 typedef make_component<Domain, Tag> make_component_;
|
Chris@16
|
200
|
Chris@16
|
201 typedef typename
|
Chris@16
|
202 make_component_::template
|
Chris@16
|
203 result<make_component_(elements_type, Data)>::type
|
Chris@16
|
204 result_type;
|
Chris@16
|
205
|
Chris@16
|
206 result_type operator()(
|
Chris@16
|
207 typename impl::expr_param expr
|
Chris@16
|
208 , typename impl::state_param state
|
Chris@16
|
209 , typename impl::data_param data
|
Chris@16
|
210 ) const
|
Chris@16
|
211 {
|
Chris@16
|
212 elements_type elements =
|
Chris@16
|
213 detail::make_cons(
|
Chris@16
|
214 Grammar()(
|
Chris@16
|
215 proto::child_c<0>(expr), state, data) // LHS
|
Chris@16
|
216 , detail::make_cons(
|
Chris@16
|
217 Grammar()(
|
Chris@16
|
218 proto::child_c<1>(expr), state, data) // RHS
|
Chris@16
|
219 )
|
Chris@16
|
220 );
|
Chris@16
|
221
|
Chris@16
|
222 return make_component_()(elements, data);
|
Chris@16
|
223 }
|
Chris@16
|
224 };
|
Chris@16
|
225 };
|
Chris@16
|
226
|
Chris@16
|
227 template <typename Grammar>
|
Chris@16
|
228 struct make_binary_helper : proto::transform<make_binary_helper<Grammar> >
|
Chris@16
|
229 {
|
Chris@16
|
230 template<typename Expr, typename State, typename Data>
|
Chris@16
|
231 struct impl : proto::transform_impl<Expr, State, Data>
|
Chris@16
|
232 {
|
Chris@16
|
233 typedef typename Grammar::
|
Chris@16
|
234 template result<Grammar(Expr, State, Data)>::type
|
Chris@16
|
235 lhs;
|
Chris@16
|
236
|
Chris@16
|
237 typedef typename result_of::make_cons<lhs, State>::type result_type;
|
Chris@16
|
238
|
Chris@16
|
239 result_type operator()(
|
Chris@16
|
240 typename impl::expr_param expr
|
Chris@16
|
241 , typename impl::state_param state
|
Chris@16
|
242 , typename impl::data_param data
|
Chris@16
|
243 ) const
|
Chris@16
|
244 {
|
Chris@16
|
245 return detail::make_cons(Grammar()(expr, state, data), state);
|
Chris@16
|
246 }
|
Chris@16
|
247 };
|
Chris@16
|
248 };
|
Chris@16
|
249
|
Chris@16
|
250 // Flattened version
|
Chris@16
|
251 template <typename Domain, typename Tag, typename Grammar>
|
Chris@16
|
252 struct make_binary<Domain, Tag, Grammar, true>
|
Chris@16
|
253 : proto::transform<make_binary<Domain, Tag, Grammar> >
|
Chris@16
|
254 {
|
Chris@16
|
255 template<typename Expr, typename State, typename Data>
|
Chris@16
|
256 struct impl : proto::transform_impl<Expr, State, Data>
|
Chris@16
|
257 {
|
Chris@16
|
258 typedef typename
|
Chris@16
|
259 proto::reverse_fold_tree<
|
Chris@16
|
260 proto::_
|
Chris@101
|
261 , proto::make<fusion::nil_>
|
Chris@16
|
262 , make_binary_helper<Grammar>
|
Chris@16
|
263 >::template impl<Expr, State, Data>
|
Chris@16
|
264 reverse_fold_tree;
|
Chris@16
|
265
|
Chris@16
|
266 typedef typename reverse_fold_tree::result_type elements;
|
Chris@16
|
267 typedef make_component<Domain, Tag> make_component_;
|
Chris@16
|
268
|
Chris@16
|
269 typedef typename
|
Chris@16
|
270 make_component_::template
|
Chris@16
|
271 result<make_component_(elements, Data)>::type
|
Chris@16
|
272 result_type;
|
Chris@16
|
273
|
Chris@16
|
274 result_type operator()(
|
Chris@16
|
275 typename impl::expr_param expr
|
Chris@16
|
276 , typename impl::state_param state
|
Chris@16
|
277 , typename impl::data_param data
|
Chris@16
|
278 ) const
|
Chris@16
|
279 {
|
Chris@16
|
280 return make_component_()(
|
Chris@16
|
281 reverse_fold_tree()(expr, state, data), data);
|
Chris@16
|
282 }
|
Chris@16
|
283 };
|
Chris@16
|
284 };
|
Chris@16
|
285
|
Chris@16
|
286 template <typename Domain, typename Grammar>
|
Chris@16
|
287 struct make_directive : proto::transform<make_directive<Domain, Grammar> >
|
Chris@16
|
288 {
|
Chris@16
|
289 template<typename Expr, typename State, typename Data>
|
Chris@16
|
290 struct impl : proto::transform_impl<Expr, State, Data>
|
Chris@16
|
291 {
|
Chris@16
|
292 typedef typename
|
Chris@16
|
293 proto::result_of::child_c<Expr, 0>::type
|
Chris@16
|
294 lhs;
|
Chris@16
|
295
|
Chris@16
|
296 typedef typename
|
Chris@16
|
297 proto::result_of::value<lhs>::type
|
Chris@16
|
298 tag_type;
|
Chris@16
|
299
|
Chris@16
|
300 typedef typename modify<Domain>::
|
Chris@16
|
301 template result<modify<Domain>(tag_type, Data)>::type
|
Chris@16
|
302 modifier_type;
|
Chris@16
|
303
|
Chris@16
|
304 typedef typename Grammar::
|
Chris@16
|
305 template result<Grammar(
|
Chris@16
|
306 typename proto::result_of::child_c<Expr, 1>::type
|
Chris@16
|
307 , State
|
Chris@16
|
308 , modifier_type
|
Chris@16
|
309 )>::type
|
Chris@16
|
310 rhs_component;
|
Chris@16
|
311
|
Chris@16
|
312 typedef typename
|
Chris@16
|
313 result_of::make_cons<
|
Chris@16
|
314 tag_type
|
Chris@16
|
315 , typename result_of::make_cons<rhs_component>::type
|
Chris@16
|
316 >::type
|
Chris@16
|
317 elements_type;
|
Chris@16
|
318
|
Chris@16
|
319 typedef make_component<Domain, tag::directive> make_component_;
|
Chris@16
|
320
|
Chris@16
|
321 typedef typename
|
Chris@16
|
322 make_component_::template
|
Chris@16
|
323 result<make_component_(elements_type, Data)>::type
|
Chris@16
|
324 result_type;
|
Chris@16
|
325
|
Chris@16
|
326 result_type operator()(
|
Chris@16
|
327 typename impl::expr_param expr
|
Chris@16
|
328 , typename impl::state_param state
|
Chris@16
|
329 , typename impl::data_param data
|
Chris@16
|
330 ) const
|
Chris@16
|
331 {
|
Chris@16
|
332 tag_type tag = proto::value(proto::child_c<0>(expr));
|
Chris@16
|
333 typename remove_reference<modifier_type>::type
|
Chris@16
|
334 modifier = modify<Domain>()(tag, data);
|
Chris@16
|
335
|
Chris@16
|
336 elements_type elements =
|
Chris@16
|
337 detail::make_cons(
|
Chris@16
|
338 tag // LHS
|
Chris@16
|
339 , detail::make_cons(
|
Chris@16
|
340 Grammar()(
|
Chris@16
|
341 proto::child_c<1>(expr) // RHS
|
Chris@16
|
342 , state, modifier)
|
Chris@16
|
343 )
|
Chris@16
|
344 );
|
Chris@16
|
345
|
Chris@16
|
346 return make_component_()(elements, data);
|
Chris@16
|
347 }
|
Chris@16
|
348 };
|
Chris@16
|
349 };
|
Chris@16
|
350
|
Chris@16
|
351 template <typename Domain, typename Grammar>
|
Chris@16
|
352 struct make_action : proto::transform<make_action<Domain, Grammar> >
|
Chris@16
|
353 {
|
Chris@16
|
354 template<typename Expr, typename State, typename Data>
|
Chris@16
|
355 struct impl : proto::transform_impl<Expr, State, Data>
|
Chris@16
|
356 {
|
Chris@16
|
357 typedef typename Grammar::
|
Chris@16
|
358 template result<Grammar(
|
Chris@16
|
359 typename proto::result_of::child_c<Expr, 0>::type
|
Chris@16
|
360 , State
|
Chris@16
|
361 , Data
|
Chris@16
|
362 )>::type
|
Chris@16
|
363 lhs_component;
|
Chris@16
|
364
|
Chris@16
|
365 typedef
|
Chris@16
|
366 typename mpl::eval_if_c<
|
Chris@16
|
367 phoenix::is_actor<
|
Chris@16
|
368 typename proto::result_of::child_c<Expr, 1>::type
|
Chris@16
|
369 >::type::value
|
Chris@16
|
370 , proto::result_of::child_c<Expr, 1>
|
Chris@16
|
371 , proto::result_of::value<
|
Chris@16
|
372 typename proto::result_of::child_c<Expr, 1>::type
|
Chris@16
|
373 >
|
Chris@16
|
374 >::type
|
Chris@16
|
375 rhs_component;
|
Chris@16
|
376
|
Chris@16
|
377 typedef typename
|
Chris@16
|
378 result_of::make_cons<
|
Chris@16
|
379 lhs_component
|
Chris@16
|
380 , typename result_of::make_cons<rhs_component>::type
|
Chris@16
|
381 >::type
|
Chris@16
|
382 elements_type;
|
Chris@16
|
383
|
Chris@16
|
384 typedef make_component<Domain, tag::action> make_component_;
|
Chris@16
|
385
|
Chris@16
|
386 typedef typename
|
Chris@16
|
387 make_component_::template
|
Chris@16
|
388 result<make_component_(elements_type, Data)>::type
|
Chris@16
|
389 result_type;
|
Chris@16
|
390
|
Chris@16
|
391 result_type operator()(
|
Chris@16
|
392 typename impl::expr_param expr
|
Chris@16
|
393 , typename impl::state_param state
|
Chris@16
|
394 , typename impl::data_param data
|
Chris@16
|
395 ) const
|
Chris@16
|
396 {
|
Chris@16
|
397 return
|
Chris@16
|
398 (*this)(
|
Chris@16
|
399 expr
|
Chris@16
|
400 , state
|
Chris@16
|
401 , data
|
Chris@16
|
402 , typename phoenix::is_actor<
|
Chris@16
|
403 typename proto::result_of::child_c<Expr, 1>::type
|
Chris@16
|
404 >::type()
|
Chris@16
|
405 );
|
Chris@16
|
406 }
|
Chris@16
|
407
|
Chris@16
|
408 result_type operator()(
|
Chris@16
|
409 typename impl::expr_param expr
|
Chris@16
|
410 , typename impl::state_param state
|
Chris@16
|
411 , typename impl::data_param data
|
Chris@16
|
412 , mpl::false_
|
Chris@16
|
413 ) const
|
Chris@16
|
414 {
|
Chris@16
|
415 elements_type elements =
|
Chris@16
|
416 detail::make_cons(
|
Chris@16
|
417 Grammar()(
|
Chris@16
|
418 proto::child_c<0>(expr), state, data) // LHS
|
Chris@16
|
419 , detail::make_cons(
|
Chris@16
|
420 proto::value(proto::child_c<1>(expr))) // RHS
|
Chris@16
|
421 );
|
Chris@16
|
422
|
Chris@16
|
423 return make_component_()(elements, data);
|
Chris@16
|
424 }
|
Chris@16
|
425
|
Chris@16
|
426 result_type operator()(
|
Chris@16
|
427 typename impl::expr_param expr
|
Chris@16
|
428 , typename impl::state_param state
|
Chris@16
|
429 , typename impl::data_param data
|
Chris@16
|
430 , mpl::true_
|
Chris@16
|
431 ) const
|
Chris@16
|
432 {
|
Chris@16
|
433 elements_type elements =
|
Chris@16
|
434 detail::make_cons(
|
Chris@16
|
435 Grammar()(
|
Chris@16
|
436 proto::child_c<0>(expr), state, data) // LHS
|
Chris@16
|
437 , detail::make_cons(
|
Chris@16
|
438 proto::child_c<1>(expr)) // RHS
|
Chris@16
|
439 );
|
Chris@16
|
440
|
Chris@16
|
441 return make_component_()(elements, data);
|
Chris@16
|
442 }
|
Chris@16
|
443 };
|
Chris@16
|
444 };
|
Chris@16
|
445 }}}
|
Chris@16
|
446
|
Chris@16
|
447 #endif
|