1 /*=============================================================================
2     Copyright (c) 2001-2011 Hartmut Kaiser
3 
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
9 
10 #if defined(_MSC_VER)
11 #pragma once
12 #endif
13 
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>
20 
21 namespace boost { namespace spirit { namespace qi { namespace detail
22 {
23     template <typename Variant, typename Expected>
24     struct find_substitute
25     {
26         // Get the type from the variant that can be a substitute for Expected.
27         // If none is found, just return Expected
28 
29         typedef Variant variant_type;
30         typedef typename variant_type::types types;
31         typedef typename mpl::end<types>::type end;
32 
33         typedef typename mpl::find<types, Expected>::type iter_1;
34 
35         typedef typename
36             mpl::eval_if<
37                 is_same<iter_1, end>,
38                 mpl::find_if<types, traits::is_substitute<mpl::_1, Expected> >,
39                 mpl::identity<iter_1>
40             >::type
41         iter;
42 
43         typedef typename
44             mpl::eval_if<
45                 is_same<iter, end>,
46                 mpl::identity<Expected>,
47                 mpl::deref<iter>
48             >::type
49         type;
50     };
51 
52     template <typename Iterator, typename Context, typename Skipper,
53         typename Attribute>
54     struct alternative_function
55     {
alternative_functionboost::spirit::qi::detail::alternative_function56         alternative_function(
57             Iterator& first_, Iterator const& last_, Context& context_,
58             Skipper const& skipper_, Attribute& attr_)
59           : first(first_), last(last_), context(context_), skipper(skipper_),
60             attr(attr_)
61         {
62         }
63 
64         template <typename Component>
callboost::spirit::qi::detail::alternative_function65         bool call(Component const& component, mpl::true_) const
66         {
67             // if Attribute is not a variant, then pass it as-is
68             return component.parse(first, last, context, skipper, attr);
69         }
70 
71         template <typename Component>
call_optional_or_variantboost::spirit::qi::detail::alternative_function72         bool call_optional_or_variant(Component const& component, mpl::true_) const
73         {
74             // If Attribute is an optional, then create an attribute for the Component
75             // with the type optional::value_type. If the expected attribute is unused type,
76             // use it instead.
77             typedef typename
78                 traits::attribute_of<Component, Context, Iterator>::type
79             expected_type;
80 
81             typename mpl::if_<
82                 is_same<expected_type, unused_type>,
83                 unused_type,
84                 typename Attribute::value_type>::type
85             val;
86 
87             if (component.parse(first, last, context, skipper, val))
88             {
89                 traits::assign_to(val, attr);
90                 return true;
91             }
92             return false;
93         }
94 
95         template <typename Component>
call_variantboost::spirit::qi::detail::alternative_function96         bool call_variant(Component const& component, mpl::false_) const
97         {
98             // If Attribute is a variant, then search the variant types for a
99             // suitable substitute type.
100 
101             typename
102                 find_substitute<Attribute,
103                     typename traits::attribute_of<Component, Context, Iterator>::type
104                 >::type
105             val;
106 
107             if (component.parse(first, last, context, skipper, val))
108             {
109                 traits::assign_to(val, attr);
110                 return true;
111             }
112             return false;
113         }
114 
115         template <typename Component>
call_variantboost::spirit::qi::detail::alternative_function116         bool call_variant(Component const& component, mpl::true_) const
117         {
118             // If Attribute is a variant and the expected attribute is
119             // the same type (pass the variant as-is).
120 
121             return component.parse(first, last, context, skipper, attr);
122         }
123 
124         template <typename Component>
call_optional_or_variantboost::spirit::qi::detail::alternative_function125         bool call_optional_or_variant(Component const& component, mpl::false_) const
126         {
127             // Attribute is a variant...
128 
129             typedef typename
130                 traits::attribute_of<Component, Context, Iterator>::type
131             expected;
132             return call_variant(component,
133                 is_same<Attribute, expected>());
134         }
135 
136         template <typename Component>
callboost::spirit::qi::detail::alternative_function137         bool call(Component const& component, mpl::false_) const
138         {
139             return call_optional_or_variant(
140                 component, spirit::traits::not_is_variant<Attribute, qi::domain>());
141         }
142 
143         template <typename Component>
call_unusedboost::spirit::qi::detail::alternative_function144         bool call_unused(Component const& component, mpl::true_) const
145         {
146             // return true if the parser succeeds
147             return call(component,
148                 mpl::and_<
149                     spirit::traits::not_is_variant<Attribute, qi::domain>,
150                     spirit::traits::not_is_optional<Attribute, qi::domain>
151                 >());
152         }
153 
154         template <typename Component>
call_unusedboost::spirit::qi::detail::alternative_function155         bool call_unused(Component const& component, mpl::false_) const
156         {
157             return component.parse(first, last, context, skipper, unused);
158         }
159 
160         template <typename Component>
operator ()boost::spirit::qi::detail::alternative_function161         bool operator()(Component const& component) const
162         {
163             // return true if the parser succeeds
164             typedef typename traits::not_is_unused<
165                 typename traits::attribute_of<Component, Context, Iterator>::type
166             >::type predicate;
167 
168             return call_unused(component, predicate());
169         }
170 
171         Iterator& first;
172         Iterator const& last;
173         Context& context;
174         Skipper const& skipper;
175         Attribute& attr;
176 
177         // silence MSVC warning C4512: assignment operator could not be generated
178         BOOST_DELETED_FUNCTION(alternative_function& operator= (alternative_function const&))
179     };
180 
181     template <typename Iterator, typename Context, typename Skipper>
182     struct alternative_function<Iterator, Context, Skipper, unused_type const>
183     {
alternative_functionboost::spirit::qi::detail::alternative_function184         alternative_function(
185             Iterator& first_, Iterator const& last_, Context& context_,
186             Skipper const& skipper_, unused_type)
187           : first(first_), last(last_), context(context_), skipper(skipper_)
188         {
189         }
190 
191         template <typename Component>
operator ()boost::spirit::qi::detail::alternative_function192         bool operator()(Component const& component) const
193         {
194             // return true if the parser succeeds
195             return component.parse(first, last, context, skipper,
196                 unused);
197         }
198 
199         Iterator& first;
200         Iterator const& last;
201         Context& context;
202         Skipper const& skipper;
203 
204         // silence MSVC warning C4512: assignment operator could not be generated
205         BOOST_DELETED_FUNCTION(alternative_function& operator= (alternative_function const&))
206     };
207 
208 }}}}
209 
210 #endif
211