/*============================================================================= 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(SPIRIT_SEQUENCE_DETAIL_JAN_06_2013_1015AM) #define SPIRIT_SEQUENCE_DETAIL_JAN_06_2013_1015AM #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace boost { namespace spirit { namespace x3 { template struct sequence; }}} namespace boost { namespace spirit { namespace x3 { namespace detail { template struct sequence_size { static int const value = traits::has_attribute::value; }; template struct sequence_size_subject : sequence_size {}; template struct sequence_size::type> : sequence_size_subject {}; template struct sequence_size, Context> { static int const value = sequence_size::value + sequence_size::value; }; struct pass_sequence_attribute_unused { typedef unused_type type; template static unused_type call(T&) { return unused_type(); } }; template struct pass_sequence_attribute_front { typedef typename fusion::result_of::front::type type; static typename add_reference::type call(Attribute& attr) { return fusion::front(attr); } }; template struct pass_through_sequence_attribute { typedef Attribute& type; template static Attribute_& call(Attribute_& attr) { return attr; } }; template struct pass_sequence_attribute_used : mpl::if_< traits::is_size_one_sequence , pass_sequence_attribute_front , pass_through_sequence_attribute>::type {}; template struct pass_sequence_attribute : mpl::if_< fusion::result_of::empty , pass_sequence_attribute_unused , pass_sequence_attribute_used>::type {}; template struct pass_sequence_attribute, Attribute> : pass_through_sequence_attribute {}; template struct pass_sequence_attribute_subject : pass_sequence_attribute {}; template struct pass_sequence_attribute::type> : pass_sequence_attribute_subject {}; template struct partition_attribute { static int const l_size = sequence_size::value; static int const r_size = sequence_size::value; // If you got an error here, then you are trying to pass // a fusion sequence with the wrong number of elements // as that expected by the (sequence) parser. static_assert( fusion::result_of::size::value == (l_size + r_size) , "Attribute does not have the expected size." ); typedef typename fusion::result_of::begin::type l_begin; typedef typename fusion::result_of::advance_c::type l_end; typedef typename fusion::result_of::end::type r_end; typedef fusion::iterator_range l_part; typedef fusion::iterator_range r_part; typedef pass_sequence_attribute l_pass; typedef pass_sequence_attribute r_pass; static l_part left(Attribute& s) { auto i = fusion::begin(s); return l_part(i, fusion::advance_c(i)); } static r_part right(Attribute& s) { return r_part( fusion::advance_c(fusion::begin(s)) , fusion::end(s)); } }; template struct partition_attribute::value && traits::has_attribute::value)>::type> { typedef unused_type l_part; typedef Attribute& r_part; typedef pass_sequence_attribute_unused l_pass; typedef pass_sequence_attribute r_pass; static unused_type left(Attribute&) { return unused; } static Attribute& right(Attribute& s) { return s; } }; template struct partition_attribute::value && !traits::has_attribute::value)>::type> { typedef Attribute& l_part; typedef unused_type r_part; typedef pass_sequence_attribute l_pass; typedef pass_sequence_attribute_unused r_pass; static Attribute& left(Attribute& s) { return s; } static unused_type right(Attribute&) { return unused; } }; template struct partition_attribute::value && !traits::has_attribute::value)>::type> { typedef unused_type l_part; typedef unused_type r_part; typedef pass_sequence_attribute_unused l_pass; typedef pass_sequence_attribute_unused r_pass; static unused_type left(Attribute&) { return unused; } static unused_type right(Attribute&) { return unused; } }; template struct get_sequence_types { typedef mpl::vector< typename traits::attribute_of::type , typename traits::attribute_of::type > type; }; template struct get_sequence_types, R, C> : mpl::push_back< typename get_sequence_types::type , typename traits::attribute_of::type> {}; template struct get_sequence_types, C> : mpl::push_front< typename get_sequence_types::type , typename traits::attribute_of::type> {}; template struct get_sequence_types, sequence, C> { typedef typename get_sequence_types::type left; typedef typename get_sequence_types::type right; typedef typename mpl::insert_range::type, right>::type type; }; template struct attribute_of_sequence { // Get all sequence attribute types typedef typename get_sequence_types::type all_types; // Filter all unused_types typedef typename mpl::copy_if< all_types , mpl::not_> , mpl::back_inserter> >::type filtered_types; // Build a fusion::deque if filtered_types is not empty, // else just return unused_type typedef typename mpl::eval_if< mpl::empty , mpl::identity , mpl::if_, mpl::int_<1> >, typename mpl::front::type , typename fusion::result_of::as_deque::type > >::type type; }; template bool parse_sequence( Parser const& parser, Iterator& first, Iterator const& last , Context const& context, RContext& rcontext, Attribute& attr , traits::tuple_attribute) { typedef typename Parser::left_type Left; typedef typename Parser::right_type Right; typedef partition_attribute partition; typedef typename partition::l_pass l_pass; typedef typename partition::r_pass r_pass; typename partition::l_part l_part = partition::left(attr); typename partition::r_part r_part = partition::right(attr); typename l_pass::type l_attr = l_pass::call(l_part); typename r_pass::type r_attr = r_pass::call(r_part); Iterator save = first; if (parser.left.parse(first, last, context, rcontext, l_attr) && parser.right.parse(first, last, context, rcontext, r_attr)) return true; first = save; return false; } template bool parse_sequence_plain( Parser const& parser, Iterator& first, Iterator const& last , Context const& context, RContext& rcontext, Attribute& attr) { typedef typename Parser::left_type Left; typedef typename Parser::right_type Right; typedef typename traits::attribute_of::type l_attr_type; typedef typename traits::attribute_of::type r_attr_type; typedef traits::make_attribute l_make_attribute; typedef traits::make_attribute r_make_attribute; typename l_make_attribute::type l_attr = l_make_attribute::call(attr); typename r_make_attribute::type r_attr = r_make_attribute::call(attr); Iterator save = first; if (parser.left.parse(first, last, context, rcontext, l_attr) && parser.right.parse(first, last, context, rcontext, r_attr)) return true; first = save; return false; } template bool parse_sequence( Parser const& parser, Iterator& first, Iterator const& last , Context const& context, RContext& rcontext, Attribute& attr , traits::plain_attribute) { return parse_sequence_plain(parser, first, last, context, rcontext, attr); } template bool parse_sequence( Parser const& parser, Iterator& first, Iterator const& last , Context const& context, RContext& rcontext, Attribute& attr , traits::variant_attribute) { return parse_sequence_plain(parser, first, last, context, rcontext, attr); } template bool parse_sequence( Left const& left, Right const& right , Iterator& first, Iterator const& last , Context const& context, RContext& rcontext, Attribute& attr , traits::container_attribute); template bool parse_sequence( Parser const& parser , Iterator& first, Iterator const& last , Context const& context, RContext& rcontext, Attribute& attr , traits::container_attribute) { Iterator save = first; if (parse_into_container(parser.left, first, last, context, rcontext, attr) && parse_into_container(parser.right, first, last, context, rcontext, attr)) return true; first = save; return false; } template bool parse_sequence_assoc( Parser const& parser , Iterator& first, Iterator const& last , Context const& context, RContext& rcontext, Attribute& attr, mpl::false_ /*should_split*/) { return parse_into_container(parser, first, last, context, rcontext, attr); } template bool parse_sequence_assoc( Parser const& parser , Iterator& first, Iterator const& last , Context const& context, RContext& rcontext, Attribute& attr, mpl::true_ /*should_split*/) { Iterator save = first; if (parser.left.parse( first, last, context, rcontext, attr) && parser.right.parse(first, last, context, rcontext, attr)) return true; first = save; return false; } template bool parse_sequence( Parser const& parser, Iterator& first, Iterator const& last , Context const& context, RContext& rcontext, Attribute& attr , traits::associative_attribute) { // we can come here in 2 cases: // - when sequence is key >> value and therefore must // be parsed with tuple synthesized attribute and then // that tuple is used to save into associative attribute provided here. // Example: key >> value; // // - when either this->left or this->right provides full key-value // pair (like in case 1) and another one provides nothing. // Example: eps >> rule > // // first case must be parsed as whole, and second one should // be parsed separately for left and right. typedef typename traits::attribute_of< decltype(parser.left), Context>::type l_attr_type; typedef typename traits::attribute_of< decltype(parser.right), Context>::type r_attr_type; typedef typename mpl::or_< is_same , is_same > should_split; return parse_sequence_assoc(parser, first, last, context, rcontext, attr , should_split()); } template struct parse_into_container_impl, Context, RContext> { typedef sequence parser_type; template static bool call( parser_type const& parser , Iterator& first, Iterator const& last , Context const& context, RContext& rcontext, Attribute& attr, mpl::false_) { // inform user what went wrong if we jumped here in attempt to // parse incompatible sequence into fusion::map static_assert(!is_same< typename traits::attribute_category::type, traits::associative_attribute>::value, "To parse directly into fusion::map sequence must produce tuple attribute " "where type of first element is existing key in fusion::map and second element " "is value to be stored under that key"); Attribute attr_; if (!parse_sequence(parser , first, last, context, rcontext, attr_, traits::container_attribute())) { return false; } traits::append(attr, traits::begin(attr_), traits::end(attr_)); return true; } template static bool call( parser_type const& parser , Iterator& first, Iterator const& last , Context const& context, RContext& rcontext, Attribute& attr, mpl::true_) { return parse_into_container_base_impl::call( parser, first, last, context, rcontext, attr); } template static bool call( parser_type const& parser , Iterator& first, Iterator const& last , Context const& context, RContext& rcontext, Attribute& attr) { typedef typename traits::attribute_of::type attribute_type; typedef typename traits::container_value::type value_type; return call(parser, first, last, context, rcontext, attr , typename traits::is_substitute::type()); } }; }}}} #endif