/*============================================================================= Copyright (c) 2001-2014 Joel de Guzman 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) =============================================================================*/ #if !defined(BOOST_SPIRIT_X3_ALTERNATIVE_DETAIL_JAN_07_2013_1245PM) #define BOOST_SPIRIT_X3_ALTERNATIVE_DETAIL_JAN_07_2013_1245PM #include #include #include #include #include #include #include #include #include #include #include #include namespace boost { namespace spirit { namespace x3 { template struct alternative; }}} namespace boost { namespace spirit { namespace x3 { namespace detail { struct pass_variant_unused { typedef unused_type type; template static unused_type call(T&) { return unused_type(); } }; template struct pass_variant_used { typedef Attribute& type; static Attribute& call(Attribute& v) { return v; } }; template <> struct pass_variant_used : pass_variant_unused {}; template struct pass_parser_attribute { typedef typename traits::attribute_of::type attribute_type; typedef typename traits::variant_find_substitute::type substitute_type; typedef typename mpl::if_< is_same , Attribute& , substitute_type >::type type; template static Attribute_& call(Attribute_& attribute, mpl::true_) { return attribute; } template static type call(Attribute_&, mpl::false_) { return type(); } template static type call(Attribute_& attribute) { return call(attribute, is_same::type>()); } }; // Pass non-variant attributes as-is template struct pass_non_variant_attribute { typedef Attribute& type; static Attribute& call(Attribute& attribute) { return attribute; } }; // Unwrap single element sequences template struct pass_non_variant_attribute>::type> { typedef typename remove_reference< typename fusion::result_of::front::type>::type attr_type; typedef pass_parser_attribute pass; typedef typename pass::type type; template static type call(Attribute_& attr) { return pass::call(fusion::front(attr)); } }; template struct pass_parser_attribute::value)>::type> : pass_non_variant_attribute {}; template struct pass_parser_attribute : pass_variant_unused {}; template struct pass_variant_attribute : mpl::if_c::value , pass_parser_attribute , pass_variant_unused>::type { }; template struct pass_variant_attribute, Attribute, Context> : mpl::if_c, Context>::value , pass_variant_used , pass_variant_unused>::type { }; template struct move_if { template static void call(T1& /* attr_ */, T2& /* attr */) {} }; template <> struct move_if { template static void call(T1& attr_, T2& attribute) { traits::move_to(attr_, attribute); } }; template bool parse_alternative(Parser const& p, Iterator& first, Iterator const& last , Context const& context, RContext& rcontext, Attribute& attribute) { using pass = detail::pass_variant_attribute; using pseudo = traits::pseudo_attribute; typename pseudo::type attr_ = pseudo::call(first, last, pass::call(attribute)); if (p.parse(first, last, context, rcontext, attr_)) { move_if::value>::call(attr_, attribute); return true; } return false; } template struct alternative_helper : unary_parser> { static bool const is_pass_through_unary = true; using unary_parser>::unary_parser; template bool parse(Iterator& first, Iterator const& last , Context const& context, RContext& rcontext, Attribute& attr) const { return detail::parse_alternative(this->subject, first, last, context, rcontext, attr); } }; template struct parse_into_container_impl, Context, RContext> { typedef alternative parser_type; template static bool call( parser_type const& parser , Iterator& first, Iterator const& last , Context const& context, RContext& rcontext, Attribute& attribute, mpl::false_) { return detail::parse_into_container(parser.left, first, last, context, rcontext, attribute) || detail::parse_into_container(parser.right, first, last, context, rcontext, attribute); } template static bool call( parser_type const& parser , Iterator& first, Iterator const& last , Context const& context, RContext& rcontext, Attribute& attribute, mpl::true_) { return detail::parse_into_container(alternative_helper{parser.left}, first, last, context, rcontext, attribute) || detail::parse_into_container(alternative_helper{parser.right}, first, last, context, rcontext, attribute); } template static bool call( parser_type const& parser , Iterator& first, Iterator const& last , Context const& context, RContext& rcontext, Attribute& attribute) { return call(parser, first, last, context, rcontext, attribute, typename traits::is_variant::type>::type{}); } }; }}}} #endif