| 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 |
| 34 | mpl::find_if<types, is_same<mpl::_1, Expected> >::type |
| 35 | iter_1; |
| 36 | |
| 37 | typedef typename |
| 38 | mpl::eval_if< |
| 39 | is_same<iter_1, end>, |
| 40 | mpl::find_if<types, traits::is_substitute<mpl::_1, Expected> >, |
| 41 | mpl::identity<iter_1> |
| 42 | >::type |
| 43 | iter; |
| 44 | |
| 45 | typedef typename |
| 46 | mpl::eval_if< |
| 47 | is_same<iter, end>, |
| 48 | mpl::identity<Expected>, |
| 49 | mpl::deref<iter> |
| 50 | >::type |
| 51 | type; |
| 52 | }; |
| 53 | |
| 54 | template <typename Iterator, typename Context, typename Skipper, |
| 55 | typename Attribute> |
| 56 | struct alternative_function |
| 57 | { |
| 58 | 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_), |
| 62 | attr(attr_) |
| 63 | { |
| 64 | } |
| 65 | |
| 66 | template <typename Component> |
| 67 | bool call(Component const& component, mpl::true_) const |
| 68 | { |
| 69 | // if Attribute is not a variant, then pass it as-is |
| 70 | return component.parse(first, last, context, skipper, attr); |
| 71 | } |
| 72 | |
| 73 | template <typename Component> |
| 74 | bool call_optional_or_variant(Component const& component, mpl::true_) const |
| 75 | { |
| 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, |
| 78 | // use it instead. |
| 79 | typedef typename |
| 80 | traits::attribute_of<Component, Context, Iterator>::type |
| 81 | expected_type; |
| 82 | |
| 83 | typename mpl::if_< |
| 84 | is_same<expected_type, unused_type>, |
| 85 | unused_type, |
| 86 | typename Attribute::value_type>::type |
| 87 | val; |
| 88 | |
| 89 | if (component.parse(first, last, context, skipper, val)) |
| 90 | { |
| 91 | traits::assign_to(val, attr); |
| 92 | return true; |
| 93 | } |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | template <typename Component> |
| 98 | bool call_variant(Component const& component, mpl::false_) const |
| 99 | { |
| 100 | // If Attribute is a variant, then search the variant types for a |
| 101 | // suitable substitute type. |
| 102 | |
| 103 | typename |
| 104 | find_substitute<Attribute, |
| 105 | typename traits::attribute_of<Component, Context, Iterator>::type |
| 106 | >::type |
| 107 | val; |
| 108 | |
| 109 | if (component.parse(first, last, context, skipper, val)) |
| 110 | { |
| 111 | traits::assign_to(val, attr); |
| 112 | return true; |
| 113 | } |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | template <typename Component> |
| 118 | bool call_variant(Component const& component, mpl::true_) const |
| 119 | { |
| 120 | // If Attribute is a variant and the expected attribute is |
| 121 | // the same type (pass the variant as-is). |
| 122 | |
| 123 | return component.parse(first, last, context, skipper, attr); |
| 124 | } |
| 125 | |
| 126 | template <typename Component> |
| 127 | bool call_optional_or_variant(Component const& component, mpl::false_) const |
| 128 | { |
| 129 | // Attribute is a variant... |
| 130 | |
| 131 | typedef typename |
| 132 | traits::attribute_of<Component, Context, Iterator>::type |
| 133 | expected; |
| 134 | return call_variant(component, |
| 135 | is_same<Attribute, expected>()); |
| 136 | } |
| 137 | |
| 138 | template <typename Component> |
| 139 | bool call(Component const& component, mpl::false_) const |
| 140 | { |
| 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> >()); |
| 144 | } |
| 145 | |
| 146 | template <typename Component> |
| 147 | bool call_unused(Component const& component, mpl::true_) const |
| 148 | { |
| 149 | // return true if the parser succeeds |
| 150 | return call(component, |
| 151 | mpl::and_< |
| 152 | spirit::traits::not_is_variant<Attribute, qi::domain>, |
| 153 | spirit::traits::not_is_optional<Attribute, qi::domain> |
| 154 | >()); |
| 155 | } |
| 156 | |
| 157 | template <typename Component> |
| 158 | bool call_unused(Component const& component, mpl::false_) const |
| 159 | { |
| 160 | return component.parse(first, last, context, skipper, unused); |
| 161 | } |
| 162 | |
| 163 | template <typename Component> |
| 164 | bool operator()(Component const& component) const |
| 165 | { |
| 166 | // return true if the parser succeeds |
| 167 | typedef typename traits::not_is_unused< |
| 168 | typename traits::attribute_of<Component, Context, Iterator>::type |
| 169 | >::type predicate; |
| 170 | |
| 171 | return call_unused(component, predicate()); |
| 172 | } |
| 173 | |
| 174 | Iterator& first; |
| 175 | Iterator const& last; |
| 176 | Context& context; |
| 177 | Skipper const& skipper; |
| 178 | Attribute& attr; |
| 179 | |
| 180 | private: |
| 181 | // silence MSVC warning C4512: assignment operator could not be generated |
| 182 | alternative_function& operator= (alternative_function const&); |
| 183 | }; |
| 184 | |
| 185 | template <typename Iterator, typename Context, typename Skipper> |
| 186 | struct alternative_function<Iterator, Context, Skipper, unused_type const> |
| 187 | { |
| 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_) |
| 192 | { |
| 193 | } |
| 194 | |
| 195 | template <typename Component> |
| 196 | bool operator()(Component const& component) const |
| 197 | { |
| 198 | // return true if the parser succeeds |
| 199 | return component.parse(first, last, context, skipper, |
| 200 | unused); |
| 201 | } |
| 202 | |
| 203 | Iterator& first; |
| 204 | Iterator const& last; |
| 205 | Context& context; |
| 206 | Skipper const& skipper; |
| 207 | |
| 208 | private: |
| 209 | // silence MSVC warning C4512: assignment operator could not be generated |
| 210 | alternative_function& operator= (alternative_function const&); |
| 211 | }; |
| 212 | |
| 213 | }}}} |
| 214 | |
| 215 | #endif |
| 216 | |