1 /*=============================================================================
2 Copyright (c) 2001-2011 Joel de Guzman
3 Copyright (c) 2001-2011 Hartmut Kaiser
4 Copyright (c) 2011 Thomas Heller
6 Distributed under the Boost Software License, Version 1.0. (See accompanying
7 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8 ==============================================================================*/
9 #if !defined(BOOST_SPIRIT_TERMINAL_NOVEMBER_04_2008_0906AM)
10 #define BOOST_SPIRIT_TERMINAL_NOVEMBER_04_2008_0906AM
16 #include <boost/config.hpp>
17 #include <boost/spirit/include/phoenix_core.hpp>
18 #include <boost/spirit/include/phoenix_function.hpp>
19 #include <boost/proto/proto.hpp>
20 #include <boost/spirit/home/support/meta_compiler.hpp>
21 #include <boost/spirit/home/support/detail/make_vector.hpp>
22 #include <boost/spirit/home/support/unused.hpp>
23 #include <boost/spirit/home/support/detail/is_spirit_tag.hpp>
24 #include <boost/preprocessor/tuple/elem.hpp>
26 #include <boost/spirit/home/support/terminal_expression.hpp>
28 namespace boost { namespace spirit
30 template <typename Terminal, typename Args>
33 typedef Terminal terminal_type;
34 typedef Args args_type;
36 terminal_ex(Args const& args_)
38 terminal_ex(Args const& args_, Terminal const& term_)
39 : args(args_), term(term_) {}
41 Args args; // Args is guaranteed to be a fusion::vectorN so you
42 // can use that template for detection and specialization
46 template <typename Terminal, typename Actor, int Arity>
49 typedef Terminal terminal_type;
50 typedef Actor actor_type;
51 static int const arity = Arity;
53 lazy_terminal(Actor const& actor_)
55 lazy_terminal(Actor const& actor_, Terminal const& term_)
56 : actor(actor_), term(term_) {}
62 template <typename Domain, typename Terminal, int Arity, typename Enable = void>
63 struct use_lazy_terminal : mpl::false_ {};
65 template <typename Domain, typename Terminal, int Arity, typename Enable = void>
66 struct use_lazy_directive : mpl::false_ {};
68 template <typename Terminal>
71 template <typename Domain, typename Terminal>
72 struct use_terminal<Domain, terminal<Terminal> >
73 : use_terminal<Domain, Terminal> {};
75 template <typename Domain, typename Terminal, int Arity, typename Actor>
76 struct use_terminal<Domain, lazy_terminal<Terminal, Actor, Arity> >
77 : use_lazy_terminal<Domain, Terminal, Arity> {};
79 template <typename Domain, typename Terminal, int Arity, typename Actor>
80 struct use_directive<Domain, lazy_terminal<Terminal, Actor, Arity> >
81 : use_lazy_directive<Domain, Terminal, Arity> {};
85 , typename A0 = unused_type
86 , typename A1 = unused_type
87 , typename A2 = unused_type
88 , typename Unused = unused_type
92 template <typename F, typename A0>
93 struct make_lazy<F, A0>
98 typename F::terminal_type
99 , typename phoenix::detail::expression::function_eval<F, A0>::type
104 typedef result_type type;
107 operator()(F f, A0 const& _0_) const
109 typedef typename result_type::proto_child0 child_type;
110 return result_type::make(child_type(
111 phoenix::detail::expression::function_eval<F, A0>::make(f, _0_)
112 , f.proto_base().child0
117 template <typename F, typename A0, typename A1>
118 struct make_lazy<F, A0, A1>
123 typename F::terminal_type
124 , typename phoenix::detail::expression::function_eval<F, A0, A1>::type
129 typedef result_type type;
132 operator()(F f, A0 const& _0_, A1 const& _1_) const
134 typedef typename result_type::proto_child0 child_type;
135 return result_type::make(child_type(
136 phoenix::detail::expression::function_eval<F, A0, A1>::make(f, _0_, _1_)
137 , f.proto_base().child0
142 template <typename F, typename A0, typename A1, typename A2>
143 struct make_lazy<F, A0, A1, A2>
148 typename F::terminal_type
149 , typename phoenix::detail::expression::function_eval<F, A0, A1, A2>::type
154 typedef result_type type;
157 operator()(F f, A0 const& _0_, A1 const& _1_, A2 const& _2_) const
159 typedef typename result_type::proto_child0 child_type;
160 return result_type::make(child_type(
161 phoenix::detail::expression::function_eval<F, A0, A1, A2>::make(f, _0_, _1_, _2_)
162 , f.proto_base().child0
169 // Helper struct for SFINAE purposes
170 template <bool C> struct bool_;
173 struct bool_<true> : mpl::bool_<true>
175 typedef bool_<true>* is_true;
179 struct bool_<false> : mpl::bool_<false>
181 typedef bool_<false>* is_false;
184 // Metafunction to detect if at least one arg is a Phoenix actor
187 , typename A1 = unused_type
188 , typename A2 = unused_type
190 struct contains_actor
192 phoenix::is_actor<A0>::value
193 || phoenix::is_actor<A1>::value
194 || phoenix::is_actor<A2>::value
198 // to_lazy_arg: convert a terminal arg type to the type make_lazy needs
199 template <typename A>
201 : phoenix::as_actor<A> // wrap A in a Phoenix actor if not already one
204 template <typename A>
205 struct to_lazy_arg<const A>
209 template <typename A>
210 struct to_lazy_arg<A &>
215 struct to_lazy_arg<unused_type>
217 // unused arg: make_lazy wants unused_type
218 typedef unused_type type;
221 // to_nonlazy_arg: convert a terminal arg type to the type make_vector needs
222 template <typename A>
223 struct to_nonlazy_arg
229 template <typename A>
230 struct to_nonlazy_arg<const A>
234 template <typename A>
235 struct to_nonlazy_arg<A &>
239 // incomplete type: should not be appeared unused_type in nonlazy arg.
241 struct to_nonlazy_arg<unused_type>;
244 template <typename Terminal>
247 typename proto::terminal<Terminal>::type
251 typedef terminal<Terminal> this_type;
252 typedef Terminal terminal_type;
254 typedef proto::extends<
255 typename proto::terminal<Terminal>::type
261 terminal(Terminal const& t)
262 : base_type(proto::terminal<Terminal>::type::make(t))
265 #if defined(BOOST_MSVC)
266 #pragma warning(push)
267 // warning C4348: 'boost::spirit::terminal<...>::result_helper': redefinition of default parameter: parameter 3, 4
268 #pragma warning(disable: 4348)
274 , typename A1 = unused_type
275 , typename A2 = unused_type
277 struct result_helper;
279 #if defined(BOOST_MSVC)
286 struct result_helper<false, A0>
292 , typename detail::result_of::make_vector<
293 typename detail::to_nonlazy_arg<A0>::type>::type>
302 struct result_helper<false, A0, A1>
308 , typename detail::result_of::make_vector<
309 typename detail::to_nonlazy_arg<A0>::type
310 , typename detail::to_nonlazy_arg<A1>::type>::type>
320 struct result_helper<false, A0, A1, A2>
326 , typename detail::result_of::make_vector<
327 typename detail::to_nonlazy_arg<A0>::type
328 , typename detail::to_nonlazy_arg<A1>::type
329 , typename detail::to_nonlazy_arg<A2>::type>::type>
339 struct result_helper<true, A0, A1, A2>
343 , typename detail::to_lazy_arg<A0>::type
344 , typename detail::to_lazy_arg<A1>::type
345 , typename detail::to_lazy_arg<A2>::type>::type
349 // FIXME: we need to change this to conform to the result_of protocol
352 , typename A1 = unused_type
353 , typename A2 = unused_type // Support up to 3 args
359 detail::contains_actor<A0, A1, A2>::value
365 template <typename This, typename A0>
366 struct result<This(A0)>
370 detail::contains_actor<A0, unused_type, unused_type>::value
371 , A0, unused_type, unused_type
376 template <typename This, typename A0, typename A1>
377 struct result<This(A0, A1)>
381 detail::contains_actor<A0, A1, unused_type>::value
382 , A0, A1, unused_type
388 template <typename This, typename A0, typename A1, typename A2>
389 struct result<This(A0, A1, A2)>
393 detail::contains_actor<A0, A1, A2>::value
399 // Note: in the following overloads, SFINAE cannot
400 // be done on return type because of gcc bug #24915:
401 // http://gcc.gnu.org/bugzilla/show_bug.cgi?id=24915
402 // Hence an additional, fake argument is used for SFINAE,
403 // using a type which can never be a real argument type.
405 // Non-lazy overloads. Only enabled when all
406 // args are immediates (no Phoenix actor).
408 template <typename A0>
409 typename result<A0>::type
410 operator()(A0 const& _0_
411 , typename detail::contains_actor<A0>::is_false = 0) const
413 typedef typename result<A0>::type result_type;
414 typedef typename result_type::proto_child0 child_type;
415 return result_type::make(
417 detail::make_vector(_0_)
418 , this->proto_base().child0)
422 template <typename A0, typename A1>
423 typename result<A0, A1>::type
424 operator()(A0 const& _0_, A1 const& _1_
425 , typename detail::contains_actor<A0, A1>::is_false = 0) const
427 typedef typename result<A0, A1>::type result_type;
428 typedef typename result_type::proto_child0 child_type;
429 return result_type::make(
431 detail::make_vector(_0_, _1_)
432 , this->proto_base().child0)
436 template <typename A0, typename A1, typename A2>
437 typename result<A0, A1, A2>::type
438 operator()(A0 const& _0_, A1 const& _1_, A2 const& _2_
439 , typename detail::contains_actor<A0, A1, A2>::is_false = 0) const
441 typedef typename result<A0, A1, A2>::type result_type;
442 typedef typename result_type::proto_child0 child_type;
443 return result_type::make(
445 detail::make_vector(_0_, _1_, _2_)
446 , this->proto_base().child0)
450 // Lazy overloads. Enabled when at
451 // least one arg is a Phoenix actor.
452 template <typename A0>
453 typename result<A0>::type
454 operator()(A0 const& _0_
455 , typename detail::contains_actor<A0>::is_true = 0) const
457 return make_lazy<this_type
458 , typename phoenix::as_actor<A0>::type>()(*this
459 , phoenix::as_actor<A0>::convert(_0_));
462 template <typename A0, typename A1>
463 typename result<A0, A1>::type
464 operator()(A0 const& _0_, A1 const& _1_
465 , typename detail::contains_actor<A0, A1>::is_true = 0) const
467 return make_lazy<this_type
468 , typename phoenix::as_actor<A0>::type
469 , typename phoenix::as_actor<A1>::type>()(*this
470 , phoenix::as_actor<A0>::convert(_0_)
471 , phoenix::as_actor<A1>::convert(_1_));
474 template <typename A0, typename A1, typename A2>
475 typename result<A0, A1, A2>::type
476 operator()(A0 const& _0_, A1 const& _1_, A2 const& _2_
477 , typename detail::contains_actor<A0, A1, A2>::is_true = 0) const
479 return make_lazy<this_type
480 , typename phoenix::as_actor<A0>::type
481 , typename phoenix::as_actor<A1>::type
482 , typename phoenix::as_actor<A2>::type>()(*this
483 , phoenix::as_actor<A0>::convert(_0_)
484 , phoenix::as_actor<A1>::convert(_1_)
485 , phoenix::as_actor<A2>::convert(_2_));
489 // silence MSVC warning C4512: assignment operator could not be generated
490 terminal& operator= (terminal const&);
493 ///////////////////////////////////////////////////////////////////////////
496 // Calculate the type of the compound terminal if generated by one of
497 // the spirit::terminal::operator() overloads above
499 // The terminal type itself is passed through without modification
500 template <typename Tag>
503 typedef spirit::terminal<Tag> type;
506 template <typename Tag, typename A0>
507 struct terminal<Tag(A0)>
509 typedef typename spirit::terminal<Tag>::
510 template result<A0>::type type;
513 template <typename Tag, typename A0, typename A1>
514 struct terminal<Tag(A0, A1)>
516 typedef typename spirit::terminal<Tag>::
517 template result<A0, A1>::type type;
520 template <typename Tag, typename A0, typename A1, typename A2>
521 struct terminal<Tag(A0, A1, A2)>
523 typedef typename spirit::terminal<Tag>::
524 template result<A0, A1, A2>::type type;
528 ///////////////////////////////////////////////////////////////////////////
529 // support for stateful tag types
533 typename Data, typename Tag
534 , typename DataTag1 = unused_type, typename DataTag2 = unused_type>
537 BOOST_SPIRIT_IS_TAG()
539 typedef Data data_type;
542 stateful_tag(data_type const& data) : data_(data) {}
547 // silence MSVC warning C4512: assignment operator could not be generated
548 stateful_tag& operator= (stateful_tag const&);
553 typename Data, typename Tag
554 , typename DataTag1 = unused_type, typename DataTag2 = unused_type>
555 struct stateful_tag_type
556 : spirit::terminal<tag::stateful_tag<Data, Tag, DataTag1, DataTag2> >
558 typedef tag::stateful_tag<Data, Tag, DataTag1, DataTag2> tag_type;
560 stateful_tag_type() {}
561 stateful_tag_type(Data const& data)
562 : spirit::terminal<tag_type>(data)
566 // silence MSVC warning C4512: assignment operator could not be generated
567 stateful_tag_type& operator= (stateful_tag_type const&);
572 // extract expression if this is a Tag
573 template <typename StatefulTag>
574 struct get_stateful_data
576 typedef typename StatefulTag::data_type data_type;
578 // is invoked if given tag is != Tag
579 template <typename Tag_>
580 static data_type call(Tag_) { return data_type(); }
582 // this is invoked if given tag is same as'Tag'
583 static data_type const& call(StatefulTag const& t) { return t.data_; }
589 namespace boost { namespace phoenix
591 template <typename Tag>
592 struct is_custom_terminal<Tag, typename Tag::is_spirit_tag>
596 template <typename Tag>
597 struct custom_terminal<Tag, typename Tag::is_spirit_tag>
599 #ifndef BOOST_PHOENIX_NO_SPECIALIZE_CUSTOM_TERMINAL
600 typedef void _is_default_custom_terminal; // fix for #7730
603 typedef spirit::terminal<Tag> result_type;
605 template <typename Context>
606 result_type operator()(Tag const & t, Context const &)
608 return spirit::terminal<Tag>(t);
613 // Define a spirit terminal. This macro may be placed in any namespace.
614 // Common placeholders are placed in the main boost::spirit namespace
615 // (see common_terminals.hpp)
617 #define BOOST_SPIRIT_TERMINAL_X(x, y) ((x, y)) BOOST_SPIRIT_TERMINAL_Y
618 #define BOOST_SPIRIT_TERMINAL_Y(x, y) ((x, y)) BOOST_SPIRIT_TERMINAL_X
619 #define BOOST_SPIRIT_TERMINAL_X0
620 #define BOOST_SPIRIT_TERMINAL_Y0
622 #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
624 #define BOOST_SPIRIT_TERMINAL_NAME(name, type_name) \
625 namespace tag { struct name { BOOST_SPIRIT_IS_TAG() }; } \
626 typedef boost::proto::terminal<tag::name>::type type_name; \
627 type_name const name = {{}}; \
628 inline void BOOST_PP_CAT(silence_unused_warnings_, name)() { (void) name; } \
633 #define BOOST_SPIRIT_TERMINAL_NAME(name, type_name) \
634 namespace tag { struct name { BOOST_SPIRIT_IS_TAG() }; } \
635 typedef boost::proto::terminal<tag::name>::type type_name; \
640 #define BOOST_SPIRIT_TERMINAL(name) \
641 BOOST_SPIRIT_TERMINAL_NAME(name, name ## _type) \
644 #define BOOST_SPIRIT_DEFINE_TERMINALS_NAME_A(r, _, names) \
645 BOOST_SPIRIT_TERMINAL_NAME( \
646 BOOST_PP_TUPLE_ELEM(2, 0, names), \
647 BOOST_PP_TUPLE_ELEM(2, 1, names) \
651 #define BOOST_SPIRIT_DEFINE_TERMINALS_NAME(seq) \
652 BOOST_PP_SEQ_FOR_EACH(BOOST_SPIRIT_DEFINE_TERMINALS_NAME_A, _, \
653 BOOST_PP_CAT(BOOST_SPIRIT_TERMINAL_X seq, 0)) \
656 // Define a spirit extended terminal. This macro may be placed in any namespace.
657 // Common placeholders are placed in the main boost::spirit namespace
658 // (see common_terminals.hpp)
660 #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
662 #define BOOST_SPIRIT_TERMINAL_NAME_EX(name, type_name) \
663 namespace tag { struct name { BOOST_SPIRIT_IS_TAG() }; } \
664 typedef boost::spirit::terminal<tag::name> type_name; \
665 type_name const name = type_name(); \
666 inline void BOOST_PP_CAT(silence_unused_warnings_, name)() { (void) name; } \
671 #define BOOST_SPIRIT_TERMINAL_NAME_EX(name, type_name) \
672 namespace tag { struct name { BOOST_SPIRIT_IS_TAG() }; } \
673 typedef boost::spirit::terminal<tag::name> type_name; \
678 #define BOOST_SPIRIT_TERMINAL_EX(name) \
679 BOOST_SPIRIT_TERMINAL_NAME_EX(name, name ## _type) \
682 #define BOOST_SPIRIT_DEFINE_TERMINALS_NAME_EX_A(r, _, names) \
683 BOOST_SPIRIT_TERMINAL_NAME_EX( \
684 BOOST_PP_TUPLE_ELEM(2, 0, names), \
685 BOOST_PP_TUPLE_ELEM(2, 1, names) \
689 #define BOOST_SPIRIT_DEFINE_TERMINALS_NAME_EX(seq) \
690 BOOST_PP_SEQ_FOR_EACH(BOOST_SPIRIT_DEFINE_TERMINALS_NAME_EX_A, _, \
691 BOOST_PP_CAT(BOOST_SPIRIT_TERMINAL_X seq, 0)) \