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 type 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 // fix for alternative.cpp test case, FHE 2016-07-28
142 return call_optional_or_variant(
143 component, mpl::not_<spirit::traits::not_is_optional<Attribute, qi::domain> >());
146 template <typename Component>
147 bool call_unused(Component const& component, mpl::true_) const
149 // return true if the parser succeeds
150 return call(component,
152 spirit::traits::not_is_variant<Attribute, qi::domain>,
153 spirit::traits::not_is_optional<Attribute, qi::domain>
157 template <typename Component>
158 bool call_unused(Component const& component, mpl::false_) const
160 return component.parse(first, last, context, skipper, unused);
163 template <typename Component>
164 bool operator()(Component const& component) const
166 // return true if the parser succeeds
167 typedef typename traits::not_is_unused<
168 typename traits::attribute_of<Component, Context, Iterator>::type
171 return call_unused(component, predicate());
175 Iterator const& last;
177 Skipper const& skipper;
181 // silence MSVC warning C4512: assignment operator could not be generated
182 alternative_function& operator= (alternative_function const&);
185 template <typename Iterator, typename Context, typename Skipper>
186 struct alternative_function<Iterator, Context, Skipper, unused_type const>
188 alternative_function(
189 Iterator& first_, Iterator const& last_, Context& context_,
190 Skipper const& skipper_, unused_type)
191 : first(first_), last(last_), context(context_), skipper(skipper_)
195 template <typename Component>
196 bool operator()(Component const& component) const
198 // return true if the parser succeeds
199 return component.parse(first, last, context, skipper,
204 Iterator const& last;
206 Skipper const& skipper;
209 // silence MSVC warning C4512: assignment operator could not be generated
210 alternative_function& operator= (alternative_function const&);