1 /*=============================================================================
2 Copyright (c) 2001-2011 Hartmut Kaiser
4 Distributed under the Boost Software License, Version 1.0. (See accompanying
5 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 =============================================================================*/
7 #if !defined(SPIRIT_ALTERNATIVE_FUNCTION_APRIL_23_2007_1046AM)
8 #define SPIRIT_ALTERNATIVE_FUNCTION_APRIL_23_2007_1046AM
14 #include <boost/spirit/home/qi/domain.hpp>
15 #include <boost/spirit/home/qi/detail/assign_to.hpp>
16 #include <boost/spirit/home/support/unused.hpp>
17 #include <boost/spirit/home/qi/detail/attributes.hpp>
18 #include <boost/variant.hpp>
19 #include <boost/mpl/bool.hpp>
21 namespace boost { namespace spirit { namespace qi { namespace detail
23 template <typename Variant, typename Expected>
24 struct find_substitute
26 // Get the typr from the variant that can be a substitute for Expected.
27 // If none is found, just return Expected
29 typedef Variant variant_type;
30 typedef typename variant_type::types types;
31 typedef typename mpl::end<types>::type end;
34 mpl::find_if<types, is_same<mpl::_1, Expected> >::type
40 mpl::find_if<types, traits::is_substitute<mpl::_1, Expected> >,
48 mpl::identity<Expected>,
54 template <typename Iterator, typename Context, typename Skipper,
56 struct alternative_function
59 Iterator& first_, Iterator const& last_, Context& context_,
60 Skipper const& skipper_, Attribute& attr_)
61 : first(first_), last(last_), context(context_), skipper(skipper_),
66 template <typename Component>
67 bool call(Component const& component, mpl::true_) const
69 // if Attribute is not a variant, then pass it as-is
70 return component.parse(first, last, context, skipper, attr);
73 template <typename Component>
74 bool call_optional_or_variant(Component const& component, mpl::true_) const
76 // If Attribute is an optional, then create an attribute for the Component
77 // with the type optional::value_type. If the expected attribute is unused type,
80 traits::attribute_of<Component, Context, Iterator>::type
84 is_same<expected_type, unused_type>,
86 typename Attribute::value_type>::type
89 if (component.parse(first, last, context, skipper, val))
91 traits::assign_to(val, attr);
97 template <typename Component>
98 bool call_variant(Component const& component, mpl::false_) const
100 // If Attribute is a variant, then search the variant types for a
101 // suitable substitute type.
104 find_substitute<Attribute,
105 typename traits::attribute_of<Component, Context, Iterator>::type
109 if (component.parse(first, last, context, skipper, val))
111 traits::assign_to(val, attr);
117 template <typename Component>
118 bool call_variant(Component const& component, mpl::true_) const
120 // If Attribute is a variant and the expected attribute is
121 // the same type (pass the variant as-is).
123 return component.parse(first, last, context, skipper, attr);
126 template <typename Component>
127 bool call_optional_or_variant(Component const& component, mpl::false_) const
129 // Attribute is a variant...
132 traits::attribute_of<Component, Context, Iterator>::type
134 return call_variant(component,
135 is_same<Attribute, expected>());
138 template <typename Component>
139 bool call(Component const& component, mpl::false_) const
141 return call_optional_or_variant(
142 component, spirit::traits::not_is_variant<Attribute, qi::domain>());
145 template <typename Component>
146 bool call_unused(Component const& component, mpl::true_) const
148 // return true if the parser succeeds
149 return call(component,
151 spirit::traits::not_is_variant<Attribute, qi::domain>,
152 spirit::traits::not_is_optional<Attribute, qi::domain>
156 template <typename Component>
157 bool call_unused(Component const& component, mpl::false_) const
159 return component.parse(first, last, context, skipper, unused);
162 template <typename Component>
163 bool operator()(Component const& component) const
165 // return true if the parser succeeds
166 typedef typename traits::not_is_unused<
167 typename traits::attribute_of<Component, Context, Iterator>::type
170 return call_unused(component, predicate());
174 Iterator const& last;
176 Skipper const& skipper;
180 // silence MSVC warning C4512: assignment operator could not be generated
181 alternative_function& operator= (alternative_function const&);
184 template <typename Iterator, typename Context, typename Skipper>
185 struct alternative_function<Iterator, Context, Skipper, unused_type const>
187 alternative_function(
188 Iterator& first_, Iterator const& last_, Context& context_,
189 Skipper const& skipper_, unused_type)
190 : first(first_), last(last_), context(context_), skipper(skipper_)
194 template <typename Component>
195 bool operator()(Component const& component) const
197 // return true if the parser succeeds
198 return component.parse(first, last, context, skipper,
203 Iterator const& last;
205 Skipper const& skipper;
208 // silence MSVC warning C4512: assignment operator could not be generated
209 alternative_function& operator= (alternative_function const&);