Chris@16: /*============================================================================= 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_PERMUTATION_OR_MARCH_13_2007_1145PM) Chris@16: #define SPIRIT_PERMUTATION_OR_MARCH_13_2007_1145PM 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: 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: namespace boost { namespace spirit { namespace qi Chris@16: { Chris@16: template Chris@16: struct permutation : nary_parser > Chris@16: { Chris@16: template Chris@16: struct attribute Chris@16: { Chris@16: // Put all the element attributes in a tuple, Chris@16: // wrapping each element in a boost::optional Chris@16: typedef typename traits::build_attribute_sequence< Chris@16: Elements, Context, traits::permutation_attribute_transform Chris@16: , Iterator, qi::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: Chris@16: permutation(Elements const& elements_) Chris@16: : elements(elements_) {} Chris@16: Chris@16: template Chris@16: bool parse(Iterator& first, Iterator const& last Chris@16: , Context& context, Skipper const& skipper Chris@16: , Attribute& attr_) const Chris@16: { Chris@16: typedef traits::attribute_not_unused predicate; Chris@16: detail::permute_function Chris@16: f(first, last, context, skipper); Chris@16: Chris@16: boost::array::value> flags; Chris@16: BOOST_FOREACH(bool& taken, flags) Chris@16: { Chris@16: taken = false; Chris@16: } Chris@16: Chris@16: // wrap the attribute in a tuple if it is not a tuple Chris@16: typename traits::wrap_if_not_tuple::type attr_local(attr_); Chris@16: Chris@16: // We have a bool array 'flags' with one flag for each parser. Chris@16: // permute_function sets the slot to true when the corresponding Chris@16: // parser successful matches. We loop until there are no more Chris@16: // successful parsers. Chris@16: Chris@16: bool result = false; Chris@16: f.taken = flags.begin(); Chris@16: while (spirit::any_if_ns(elements, attr_local, f, predicate())) Chris@16: { Chris@16: f.taken = flags.begin(); Chris@16: result = true; Chris@16: } Chris@16: return result; Chris@16: } Chris@16: Chris@16: template Chris@16: info what(Context& context) const Chris@16: { Chris@16: info result("permutation"); 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: /////////////////////////////////////////////////////////////////////////// Chris@16: // Parser generators: make_xxx function (objects) Chris@16: /////////////////////////////////////////////////////////////////////////// Chris@16: template Chris@16: struct make_composite Chris@16: : make_nary_composite Chris@16: {}; Chris@16: }}} Chris@16: Chris@16: namespace boost { namespace spirit { namespace traits Chris@16: { Chris@16: /////////////////////////////////////////////////////////////////////////// Chris@16: // We specialize this for permutation (see support/attributes.hpp). Chris@16: // For permutation, we only wrap the attribute in a tuple IFF Chris@16: // it is not already a fusion tuple. Chris@16: template Chris@16: struct pass_attribute, Attribute> Chris@16: : wrap_if_not_tuple {}; Chris@16: 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: : nary_handles_container {}; Chris@16: }}} Chris@16: Chris@16: #endif