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