/*============================================================================= Copyright (c) 2001-2011 Joel de Guzman http://spirit.sourceforge.net/ Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) =============================================================================*/ #ifndef BOOST_SPIRIT_MAKE_COMPONENT_OCTOBER_16_2008_1250PM #define BOOST_SPIRIT_MAKE_COMPONENT_OCTOBER_16_2008_1250PM #if defined(_MSC_VER) #pragma once #endif #include #include #include #include namespace boost { namespace spirit { // There is no real "component" class. Each domain is responsible // for creating its own components. You need to specialize this for // each component in your domain. Use this as a guide. template struct make_component { template struct result; template struct result; template typename result::type operator()(Elements const& elements, Modifiers const& modifiers) const; }; namespace tag { // Normally, we use proto tags as-is to distinguish operators. // The special case is proto::tag::subscript. Spirit uses this // as either sementic actions or directives. To distinguish between // the two, we use these special tags below. struct directive; struct action; } template struct flatten_tree; }} namespace boost { namespace spirit { namespace detail { template struct make_terminal_impl : proto::transform_impl { typedef typename proto::result_of::value::type value; typedef typename result_of::make_cons::type elements; typedef make_component make_component_; typedef typename make_component_::template result::type result_type; result_type operator()( typename make_terminal_impl::expr_param expr , typename make_terminal_impl::state_param /*state*/ , typename make_terminal_impl::data_param data ) const { return typename make_terminal_impl::make_component_()( detail::make_cons(proto::value(expr)) , data ); } }; template struct make_terminal_impl, State, Data, Domain> : proto::transform_impl, State, Data> { typedef phoenix::actor value; typedef typename result_of::make_cons::type elements; typedef make_component make_component_; typedef typename make_component_::template result::type result_type; result_type operator()( typename make_terminal_impl::expr_param expr , typename make_terminal_impl::state_param /*state*/ , typename make_terminal_impl::data_param data ) const { return typename make_terminal_impl::make_component_()( detail::make_cons(expr) , data ); } }; template struct make_terminal_impl &, State, Data, Domain> : make_terminal_impl, State, Data, Domain> {}; template struct make_terminal_impl const &, State, Data, Domain> : make_terminal_impl, State, Data, Domain> {}; template struct make_terminal : proto::transform > { template struct impl : make_terminal_impl {}; }; template struct make_unary : proto::transform > { template struct impl : proto::transform_impl { typedef typename proto::result_of::child_c::type child; typedef typename Grammar:: template result::type child_component; typedef typename result_of::make_cons::type elements; typedef make_component make_component_; typedef typename make_component_::template result::type result_type; result_type operator()( typename impl::expr_param expr , typename impl::state_param state , typename impl::data_param data ) const { return typename impl::make_component_()( detail::make_cons( Grammar()(proto::child(expr), state, data)) , data ); } }; }; // un-flattened version template ::value> struct make_binary { template struct impl : proto::transform_impl { typedef typename Grammar:: template result::type , State, Data)>::type lhs_component; typedef typename Grammar:: template result::type , State, Data)>::type rhs_component; typedef typename result_of::make_cons< lhs_component , typename result_of::make_cons::type >::type elements_type; typedef make_component make_component_; typedef typename make_component_::template result::type result_type; result_type operator()( typename impl::expr_param expr , typename impl::state_param state , typename impl::data_param data ) const { elements_type elements = detail::make_cons( Grammar()( proto::child_c<0>(expr), state, data) // LHS , detail::make_cons( Grammar()( proto::child_c<1>(expr), state, data) // RHS ) ); return make_component_()(elements, data); } }; }; template struct make_binary_helper : proto::transform > { template struct impl : proto::transform_impl { typedef typename Grammar:: template result::type lhs; typedef typename result_of::make_cons::type result_type; result_type operator()( typename impl::expr_param expr , typename impl::state_param state , typename impl::data_param data ) const { return detail::make_cons(Grammar()(expr, state, data), state); } }; }; // Flattened version template struct make_binary : proto::transform > { template struct impl : proto::transform_impl { typedef typename proto::reverse_fold_tree< proto::_ , proto::make , make_binary_helper >::template impl reverse_fold_tree; typedef typename reverse_fold_tree::result_type elements; typedef make_component make_component_; typedef typename make_component_::template result::type result_type; result_type operator()( typename impl::expr_param expr , typename impl::state_param state , typename impl::data_param data ) const { return make_component_()( reverse_fold_tree()(expr, state, data), data); } }; }; template struct make_directive : proto::transform > { template struct impl : proto::transform_impl { typedef typename proto::result_of::child_c::type lhs; typedef typename proto::result_of::value::type tag_type; typedef typename modify:: template result(tag_type, Data)>::type modifier_type; typedef typename Grammar:: template result::type , State , modifier_type )>::type rhs_component; typedef typename result_of::make_cons< tag_type , typename result_of::make_cons::type >::type elements_type; typedef make_component make_component_; typedef typename make_component_::template result::type result_type; result_type operator()( typename impl::expr_param expr , typename impl::state_param state , typename impl::data_param data ) const { tag_type tag = proto::value(proto::child_c<0>(expr)); typename remove_reference::type modifier = modify()(tag, data); elements_type elements = detail::make_cons( tag // LHS , detail::make_cons( Grammar()( proto::child_c<1>(expr) // RHS , state, modifier) ) ); return make_component_()(elements, data); } }; }; template struct make_action : proto::transform > { template struct impl : proto::transform_impl { typedef typename Grammar:: template result::type , State , Data )>::type lhs_component; #ifndef BOOST_SPIRIT_USE_PHOENIX_V3 typedef typename proto::result_of::value< typename proto::result_of::child_c::type >::type rhs_component; #else typedef typename mpl::eval_if_c< phoenix::is_actor< typename proto::result_of::child_c::type >::type::value , proto::result_of::child_c , proto::result_of::value< typename proto::result_of::child_c::type > >::type rhs_component; #endif typedef typename result_of::make_cons< lhs_component , typename result_of::make_cons::type >::type elements_type; typedef make_component make_component_; typedef typename make_component_::template result::type result_type; #ifndef BOOST_SPIRIT_USE_PHOENIX_V3 result_type operator()( typename impl::expr_param expr , typename impl::state_param state , typename impl::data_param data ) const { elements_type elements = detail::make_cons( Grammar()( proto::child_c<0>(expr), state, data) // LHS , detail::make_cons( proto::value(proto::child_c<1>(expr))) // RHS ); return make_component_()(elements, data); } #else result_type operator()( typename impl::expr_param expr , typename impl::state_param state , typename impl::data_param data ) const { return (*this)( expr , state , data , typename phoenix::is_actor< typename proto::result_of::child_c::type >::type() ); } result_type operator()( typename impl::expr_param expr , typename impl::state_param state , typename impl::data_param data , mpl::false_ ) const { elements_type elements = detail::make_cons( Grammar()( proto::child_c<0>(expr), state, data) // LHS , detail::make_cons( proto::value(proto::child_c<1>(expr))) // RHS ); return make_component_()(elements, data); } result_type operator()( typename impl::expr_param expr , typename impl::state_param state , typename impl::data_param data , mpl::true_ ) const { elements_type elements = detail::make_cons( Grammar()( proto::child_c<0>(expr), state, data) // LHS , detail::make_cons( proto::child_c<1>(expr)) // RHS ); return make_component_()(elements, data); } #endif }; }; }}} #endif