// Copyright (c) 2001-2011 Hartmut Kaiser // Copyright (c) 2001-2011 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_KARMA_ALTERNATIVE_MAR_01_2007_1117AM) #define SPIRIT_KARMA_ALTERNATIVE_MAR_01_2007_1117AM #if defined(_MSC_VER) #pragma once #endif #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace boost { namespace spirit { /////////////////////////////////////////////////////////////////////////// // Enablers /////////////////////////////////////////////////////////////////////////// template <> struct use_operator // enables | : mpl::true_ {}; template <> struct flatten_tree // flattens | : mpl::true_ {}; }} /////////////////////////////////////////////////////////////////////////////// namespace boost { namespace spirit { namespace traits { // specialization for sequences template struct alternative_properties { struct element_properties { template struct result; template struct result { typedef properties_of type; }; // never called, but needed for decltype-based result_of (C++0x) #ifndef BOOST_NO_RVALUE_REFERENCES template typename result::type operator()(Element&&) const; #endif }; typedef typename mpl::accumulate< typename fusion::result_of::transform< Elements, element_properties>::type , mpl::int_ , mpl::bitor_ >::type type; }; }}} namespace boost { namespace spirit { namespace karma { template struct base_alternative : nary_generator { typedef typename traits::alternative_properties::type properties; template struct attribute { // Put all the element attributes in a tuple typedef typename traits::build_attribute_sequence< Elements, Context, traits::alternative_attribute_transform , Iterator, karma::domain >::type all_attributes; // Ok, now make a variant over the attribute sequence. Note that // build_variant makes sure that 1) all attributes in the variant // are unique 2) puts the unused attribute, if there is any, to // the front and 3) collapses single element variants, variant // to T. typedef typename traits::build_variant::type type; }; base_alternative(Elements const& elements) : elements(elements) {} template < typename OutputIterator, typename Context, typename Delimiter , typename Attribute> bool generate(OutputIterator& sink, Context& ctx , Delimiter const& d, Attribute const& attr) const { typedef detail::alternative_generate_function< OutputIterator, Context, Delimiter, Attribute, Strict > functor; // f return true if *any* of the parser succeeds functor f (sink, ctx, d, attr); return fusion::any(elements, f); } template info what(Context& context) const { info result("alternative"); fusion::for_each(elements, spirit::detail::what_function(result, context)); return result; } Elements elements; }; template struct alternative : base_alternative > { typedef base_alternative base_alternative_; alternative(Elements const& elements) : base_alternative_(elements) {} }; template struct strict_alternative : base_alternative > { typedef base_alternative base_alternative_; strict_alternative(Elements const& elements) : base_alternative_(elements) {} }; /////////////////////////////////////////////////////////////////////////// // Generator generators: make_xxx function (objects) /////////////////////////////////////////////////////////////////////////// namespace detail { template struct make_alternative : make_nary_composite {}; template struct make_alternative : make_nary_composite {}; } template struct make_composite : detail::make_alternative::value> {}; }}} namespace boost { namespace spirit { namespace traits { /////////////////////////////////////////////////////////////////////////// template struct has_semantic_action > : nary_has_semantic_action {}; template struct has_semantic_action > : nary_has_semantic_action {}; /////////////////////////////////////////////////////////////////////////// template struct handles_container , Attribute, Context, Iterator> : nary_handles_container {}; template struct handles_container , Attribute, Context, Iterator> : nary_handles_container {}; }}} #endif