Chris@16: // Copyright (c) 2001-2011 Hartmut Kaiser Chris@16: // Copyright (c) 2001-2011 Joel de Guzman Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. (See accompanying Chris@16: // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: Chris@16: #if !defined(SPIRIT_KARMA_SEQUENCE_FEB_28_2007_0247PM) Chris@16: #define SPIRIT_KARMA_SEQUENCE_FEB_28_2007_0247PM Chris@16: Chris@16: #if defined(_MSC_VER) Chris@16: #pragma once Chris@16: #endif Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: /////////////////////////////////////////////////////////////////////////////// Chris@16: namespace boost { namespace spirit Chris@16: { Chris@16: /////////////////////////////////////////////////////////////////////////// Chris@16: // Enablers Chris@16: /////////////////////////////////////////////////////////////////////////// Chris@16: template <> Chris@16: struct use_operator // enables << Chris@16: : mpl::true_ {}; Chris@16: Chris@16: template <> Chris@16: struct flatten_tree // flattens << Chris@16: : mpl::true_ {}; Chris@16: }} Chris@16: Chris@16: /////////////////////////////////////////////////////////////////////////////// Chris@16: namespace boost { namespace spirit { namespace traits Chris@16: { Chris@16: // specialization for sequences Chris@16: template Chris@16: struct sequence_properties Chris@16: { Chris@16: struct element_properties Chris@16: { Chris@16: template Chris@16: struct result; Chris@16: Chris@16: template Chris@16: struct result Chris@16: { Chris@16: typedef properties_of type; Chris@16: }; Chris@16: Chris@16: // never called, but needed for decltype-based result_of (C++0x) Chris@16: #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES Chris@16: template Chris@16: typename result::type Chris@16: operator()(Element&&) const; Chris@16: #endif Chris@16: }; Chris@16: Chris@16: typedef typename mpl::accumulate< Chris@16: typename fusion::result_of::transform< Chris@16: Elements, element_properties>::type Chris@16: , mpl::int_ Chris@16: , mpl::bitor_ Chris@16: >::type type; Chris@16: }; Chris@16: }}} Chris@16: Chris@16: /////////////////////////////////////////////////////////////////////////////// Chris@16: namespace boost { namespace spirit { namespace karma Chris@16: { Chris@16: template Chris@16: struct base_sequence : nary_generator Chris@16: { Chris@16: typedef typename traits::sequence_properties::type properties; Chris@16: Chris@16: base_sequence(Elements const& elements) Chris@16: : elements(elements) {} Chris@16: Chris@16: typedef Elements elements_type; Chris@16: struct sequence_base_id; Chris@16: Chris@16: template Chris@16: struct attribute Chris@16: { Chris@16: // Put all the element attributes in a tuple Chris@16: typedef typename traits::build_attribute_sequence< Chris@16: Elements, Context, traits::sequence_attribute_transform Chris@16: , Iterator, karma::domain Chris@16: >::type all_attributes; Chris@16: Chris@16: // Now, build a fusion vector over the attributes. Note Chris@16: // that build_fusion_vector 1) removes all unused attributes Chris@16: // and 2) may return unused_type if all elements have Chris@16: // unused_type(s). Chris@16: typedef typename Chris@16: traits::build_fusion_vector::type Chris@16: type_; Chris@16: Chris@16: // Finally, strip single element vectors into its Chris@16: // naked form: vector1 --> T Chris@16: typedef typename Chris@16: traits::strip_single_element_vector::type Chris@16: type; Chris@16: }; Chris@16: Chris@16: // standard case. Attribute is a fusion tuple Chris@16: template < Chris@16: typename OutputIterator, typename Context, typename Delimiter Chris@16: , typename Attribute, typename Pred1, typename Pred2> Chris@16: bool generate_impl(OutputIterator& sink, Context& ctx Chris@16: , Delimiter const& d, Attribute& attr_, Pred1, Pred2) const Chris@16: { Chris@16: typedef detail::fail_function< Chris@16: OutputIterator, Context, Delimiter> fail_function; Chris@16: typedef traits::attribute_not_unused predicate; Chris@16: Chris@16: // wrap the attribute in a tuple if it is not a tuple or if the Chris@16: // attribute of this sequence is a single element tuple Chris@16: typedef typename attribute::type_ attr_type_; Chris@16: typename traits::wrap_if_not_tuple Chris@16: , mpl::not_ > Chris@16: >::type Chris@16: >::type attr(attr_); Chris@16: Chris@16: // return false if *any* of the generators fail Chris@16: bool r = spirit::any_if(elements, attr Chris@16: , fail_function(sink, ctx, d), predicate()); Chris@16: Chris@16: typedef typename traits::attribute_size::type size_type; Chris@16: Chris@16: // fail generating if sequences have not the same (logical) length Chris@16: return !r && (!Strict::value || Chris@16: // This ignores container element count (which is not good), Chris@16: // but allows valid attributes to succeed. This will lead to Chris@16: // false positives (failing generators, even if they shouldn't) Chris@16: // if the embedded component is restricting the number of Chris@16: // container elements it consumes (i.e. repeat). This solution Chris@16: // is not optimal but much better than letting _all_ repetitive Chris@16: // components fail. Chris@16: Pred1::value || Chris@16: size_type(traits::sequence_size::value) == traits::size(attr_)); Chris@16: } Chris@16: Chris@16: // Special case when Attribute is an stl container and the sequence's Chris@16: // attribute is not a one element sequence Chris@16: template < Chris@16: typename OutputIterator, typename Context, typename Delimiter Chris@16: , typename Attribute> Chris@16: bool generate_impl(OutputIterator& sink, Context& ctx Chris@16: , Delimiter const& d, Attribute const& attr_ Chris@16: , mpl::true_, mpl::false_) const Chris@16: { Chris@16: // return false if *any* of the generators fail Chris@16: typedef detail::fail_function< Chris@16: OutputIterator, Context, Delimiter> fail_function; Chris@16: Chris@16: typedef typename traits::container_iterator< Chris@16: typename add_const::type Chris@16: >::type iterator_type; Chris@16: Chris@16: typedef Chris@16: typename traits::make_indirect_iterator::type Chris@16: indirect_iterator_type; Chris@16: typedef detail::pass_container< Chris@16: fail_function, Attribute, indirect_iterator_type, mpl::true_> Chris@16: pass_container; Chris@16: Chris@16: iterator_type begin = traits::begin(attr_); Chris@16: iterator_type end = traits::end(attr_); Chris@16: Chris@16: pass_container pass(fail_function(sink, ctx, d), Chris@16: indirect_iterator_type(begin), indirect_iterator_type(end)); Chris@16: bool r = fusion::any(elements, pass); Chris@16: Chris@16: // fail generating if sequences have not the same (logical) length Chris@16: return !r && (!Strict::value || begin == end); Chris@16: } Chris@16: Chris@16: // main generate function. Dispatches to generate_impl depending Chris@16: // on the Attribute type. Chris@16: template < Chris@16: typename OutputIterator, typename Context, typename Delimiter Chris@16: , typename Attribute> Chris@16: bool generate(OutputIterator& sink, Context& ctx, Delimiter const& d Chris@16: , Attribute const& attr) const Chris@16: { Chris@16: typedef typename traits::is_container::type Chris@16: is_container; Chris@16: Chris@16: typedef typename attribute::type_ attr_type_; Chris@16: typedef typename traits::one_element_sequence::type Chris@16: is_one_element_sequence; Chris@16: Chris@16: return generate_impl(sink, ctx, d, attr, is_container() Chris@16: , is_one_element_sequence()); Chris@16: } Chris@16: Chris@16: template Chris@16: info what(Context& context) const Chris@16: { Chris@16: info result("sequence"); Chris@16: fusion::for_each(elements, Chris@16: spirit::detail::what_function(result, context)); Chris@16: return result; Chris@16: } Chris@16: Chris@16: Elements elements; Chris@16: }; Chris@16: Chris@16: template Chris@16: struct sequence Chris@16: : base_sequence > Chris@16: { Chris@16: typedef base_sequence base_sequence_; Chris@16: Chris@16: sequence(Elements const& subject) Chris@16: : base_sequence_(subject) {} Chris@16: }; Chris@16: Chris@16: template Chris@16: struct strict_sequence Chris@16: : base_sequence > Chris@16: { Chris@16: typedef base_sequence Chris@16: base_sequence_; Chris@16: Chris@16: strict_sequence(Elements const& subject) Chris@16: : base_sequence_(subject) {} Chris@16: }; Chris@16: Chris@16: /////////////////////////////////////////////////////////////////////////// Chris@16: // Generator generators: make_xxx function (objects) Chris@16: /////////////////////////////////////////////////////////////////////////// Chris@16: namespace detail Chris@16: { Chris@16: template Chris@16: struct make_sequence Chris@16: : make_nary_composite Chris@16: {}; Chris@16: Chris@16: template Chris@16: struct make_sequence Chris@16: : make_nary_composite Chris@16: {}; Chris@16: } Chris@16: Chris@16: template Chris@16: struct make_composite Chris@16: : detail::make_sequence::value> Chris@16: {}; Chris@16: Chris@16: /////////////////////////////////////////////////////////////////////////// Chris@16: // Helper template allowing to get the required container type for a rule Chris@16: // attribute, which is part of a sequence. Chris@16: template Chris@16: struct make_sequence_iterator_range Chris@16: { Chris@16: typedef iterator_range > type; Chris@16: }; Chris@16: }}} Chris@16: Chris@16: namespace boost { namespace spirit { namespace traits Chris@16: { Chris@16: /////////////////////////////////////////////////////////////////////////// Chris@16: template Chris@16: struct has_semantic_action > Chris@16: : nary_has_semantic_action {}; Chris@16: Chris@16: template Chris@16: struct has_semantic_action > Chris@16: : nary_has_semantic_action {}; Chris@16: Chris@16: /////////////////////////////////////////////////////////////////////////// Chris@16: template Chris@16: struct handles_container, Attribute, Context Chris@16: , Iterator> Chris@16: : mpl::true_ {}; Chris@16: Chris@16: template Chris@16: struct handles_container, Attribute Chris@16: , Context, Iterator> Chris@16: : mpl::true_ {}; Chris@16: }}} Chris@16: Chris@16: #endif