| 1 | /*============================================================================= |
| 2 | Copyright (c) 2001-2014 Joel de Guzman |
| 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(BOOST_SPIRIT_X3_ALTERNATIVE_DETAIL_JAN_07_2013_1245PM) |
| 8 | #define BOOST_SPIRIT_X3_ALTERNATIVE_DETAIL_JAN_07_2013_1245PM |
| 9 | |
| 10 | #include <boost/spirit/home/x3/support/traits/attribute_of.hpp> |
| 11 | #include <boost/spirit/home/x3/support/traits/pseudo_attribute.hpp> |
| 12 | #include <boost/spirit/home/x3/support/traits/is_variant.hpp> |
| 13 | #include <boost/spirit/home/x3/support/traits/tuple_traits.hpp> |
| 14 | #include <boost/spirit/home/x3/support/traits/move_to.hpp> |
| 15 | #include <boost/spirit/home/x3/support/traits/variant_has_substitute.hpp> |
| 16 | #include <boost/spirit/home/x3/support/traits/variant_find_substitute.hpp> |
| 17 | #include <boost/spirit/home/x3/core/detail/parse_into_container.hpp> |
| 18 | |
| 19 | #include <boost/mpl/if.hpp> |
| 20 | |
| 21 | #include <boost/fusion/include/front.hpp> |
| 22 | |
| 23 | #include <boost/type_traits/is_same.hpp> |
| 24 | #include <type_traits> |
| 25 | |
| 26 | namespace boost { namespace spirit { namespace x3 |
| 27 | { |
| 28 | template <typename Left, typename Right> |
| 29 | struct alternative; |
| 30 | }}} |
| 31 | |
| 32 | namespace boost { namespace spirit { namespace x3 { namespace detail |
| 33 | { |
| 34 | struct pass_variant_unused |
| 35 | { |
| 36 | typedef unused_type type; |
| 37 | |
| 38 | template <typename T> |
| 39 | static unused_type |
| 40 | call(T&) |
| 41 | { |
| 42 | return unused_type(); |
| 43 | } |
| 44 | }; |
| 45 | |
| 46 | template <typename Attribute> |
| 47 | struct pass_variant_used |
| 48 | { |
| 49 | typedef Attribute& type; |
| 50 | |
| 51 | static Attribute& |
| 52 | call(Attribute& v) |
| 53 | { |
| 54 | return v; |
| 55 | } |
| 56 | }; |
| 57 | |
| 58 | template <> |
| 59 | struct pass_variant_used<unused_type> : pass_variant_unused {}; |
| 60 | |
| 61 | template <typename Parser, typename Attribute, typename Context |
| 62 | , typename Enable = void> |
| 63 | struct pass_parser_attribute |
| 64 | { |
| 65 | typedef typename |
| 66 | traits::attribute_of<Parser, Context>::type |
| 67 | attribute_type; |
| 68 | typedef typename |
| 69 | traits::variant_find_substitute<Attribute, attribute_type>::type |
| 70 | substitute_type; |
| 71 | |
| 72 | typedef typename |
| 73 | mpl::if_< |
| 74 | is_same<Attribute, substitute_type> |
| 75 | , Attribute& |
| 76 | , substitute_type |
| 77 | >::type |
| 78 | type; |
| 79 | |
| 80 | template <typename Attribute_> |
| 81 | static Attribute_& |
| 82 | call(Attribute_& attribute, mpl::true_) |
| 83 | { |
| 84 | return attribute; |
| 85 | } |
| 86 | |
| 87 | template <typename Attribute_> |
| 88 | static type |
| 89 | call(Attribute_&, mpl::false_) |
| 90 | { |
| 91 | return type(); |
| 92 | } |
| 93 | |
| 94 | template <typename Attribute_> |
| 95 | static type |
| 96 | call(Attribute_& attribute) |
| 97 | { |
| 98 | return call(attribute, is_same<Attribute_, typename remove_reference<type>::type>()); |
| 99 | } |
| 100 | }; |
| 101 | |
| 102 | // Pass non-variant attributes as-is |
| 103 | template <typename Parser, typename Attribute, typename Context |
| 104 | , typename Enable = void> |
| 105 | struct pass_non_variant_attribute |
| 106 | { |
| 107 | typedef Attribute& type; |
| 108 | |
| 109 | static Attribute& |
| 110 | call(Attribute& attribute) |
| 111 | { |
| 112 | return attribute; |
| 113 | } |
| 114 | }; |
| 115 | |
| 116 | // Unwrap single element sequences |
| 117 | template <typename Parser, typename Attribute, typename Context> |
| 118 | struct pass_non_variant_attribute<Parser, Attribute, Context, |
| 119 | typename enable_if<traits::is_size_one_sequence<Attribute>>::type> |
| 120 | { |
| 121 | typedef typename remove_reference< |
| 122 | typename fusion::result_of::front<Attribute>::type>::type |
| 123 | attr_type; |
| 124 | |
| 125 | typedef pass_parser_attribute<Parser, attr_type, Context> pass; |
| 126 | typedef typename pass::type type; |
| 127 | |
| 128 | template <typename Attribute_> |
| 129 | static type |
| 130 | call(Attribute_& attr) |
| 131 | { |
| 132 | return pass::call(fusion::front(attr)); |
| 133 | } |
| 134 | }; |
| 135 | |
| 136 | template <typename Parser, typename Attribute, typename Context> |
| 137 | struct pass_parser_attribute<Parser, Attribute, Context, |
| 138 | typename enable_if_c<(!traits::is_variant<Attribute>::value)>::type> |
| 139 | : pass_non_variant_attribute<Parser, Attribute, Context> |
| 140 | {}; |
| 141 | |
| 142 | template <typename Parser, typename Context> |
| 143 | struct pass_parser_attribute<Parser, unused_type, Context> |
| 144 | : pass_variant_unused {}; |
| 145 | |
| 146 | template <typename Parser, typename Attribute, typename Context> |
| 147 | struct pass_variant_attribute : |
| 148 | mpl::if_c<traits::has_attribute<Parser, Context>::value |
| 149 | , pass_parser_attribute<Parser, Attribute, Context> |
| 150 | , pass_variant_unused>::type |
| 151 | { |
| 152 | }; |
| 153 | |
| 154 | template <typename L, typename R, typename Attribute, typename Context> |
| 155 | struct pass_variant_attribute<alternative<L, R>, Attribute, Context> : |
| 156 | mpl::if_c<traits::has_attribute<alternative<L, R>, Context>::value |
| 157 | , pass_variant_used<Attribute> |
| 158 | , pass_variant_unused>::type |
| 159 | { |
| 160 | }; |
| 161 | |
| 162 | template <bool Condition> |
| 163 | struct move_if |
| 164 | { |
| 165 | template<typename T1, typename T2> |
| 166 | static void call(T1& /* attr_ */, T2& /* attr */) {} |
| 167 | }; |
| 168 | |
| 169 | template <> |
| 170 | struct move_if<true> |
| 171 | { |
| 172 | template<typename T1, typename T2> |
| 173 | static void call(T1& attr_, T2& attribute) |
| 174 | { |
| 175 | traits::move_to(attr_, attribute); |
| 176 | } |
| 177 | }; |
| 178 | |
| 179 | template <typename Parser, typename Iterator, typename Context |
| 180 | , typename RContext, typename Attribute> |
| 181 | bool parse_alternative(Parser const& p, Iterator& first, Iterator const& last |
| 182 | , Context const& context, RContext& rcontext, Attribute& attribute) |
| 183 | { |
| 184 | using pass = detail::pass_variant_attribute<Parser, Attribute, Context>; |
| 185 | using pseudo = traits::pseudo_attribute<Context, typename pass::type, Iterator>; |
| 186 | |
| 187 | typename pseudo::type attr_ = pseudo::call(first, last, pass::call(attribute)); |
| 188 | |
| 189 | if (p.parse(first, last, context, rcontext, attr_)) |
| 190 | { |
| 191 | move_if<!std::is_reference<decltype(attr_)>::value>::call(attr_, attribute); |
| 192 | return true; |
| 193 | } |
| 194 | return false; |
| 195 | } |
| 196 | |
| 197 | template <typename Subject> |
| 198 | struct alternative_helper : unary_parser<Subject, alternative_helper<Subject>> |
| 199 | { |
| 200 | static bool const is_pass_through_unary = true; |
| 201 | |
| 202 | using unary_parser<Subject, alternative_helper<Subject>>::unary_parser; |
| 203 | |
| 204 | template <typename Iterator, typename Context |
| 205 | , typename RContext, typename Attribute> |
| 206 | bool parse(Iterator& first, Iterator const& last |
| 207 | , Context const& context, RContext& rcontext, Attribute& attr) const |
| 208 | { |
| 209 | return detail::parse_alternative(this->subject, first, last, context, rcontext, attr); |
| 210 | } |
| 211 | }; |
| 212 | |
| 213 | template <typename Left, typename Right, typename Context, typename RContext> |
| 214 | struct parse_into_container_impl<alternative<Left, Right>, Context, RContext> |
| 215 | { |
| 216 | typedef alternative<Left, Right> parser_type; |
| 217 | |
| 218 | template <typename Iterator, typename Attribute> |
| 219 | static bool call( |
| 220 | parser_type const& parser |
| 221 | , Iterator& first, Iterator const& last |
| 222 | , Context const& context, RContext& rcontext, Attribute& attribute, mpl::false_) |
| 223 | { |
| 224 | return detail::parse_into_container(parser.left, first, last, context, rcontext, attribute) |
| 225 | || detail::parse_into_container(parser.right, first, last, context, rcontext, attribute); |
| 226 | } |
| 227 | |
| 228 | template <typename Iterator, typename Attribute> |
| 229 | static bool call( |
| 230 | parser_type const& parser |
| 231 | , Iterator& first, Iterator const& last |
| 232 | , Context const& context, RContext& rcontext, Attribute& attribute, mpl::true_) |
| 233 | { |
| 234 | return detail::parse_into_container(alternative_helper<Left>{parser.left}, first, last, context, rcontext, attribute) |
| 235 | || detail::parse_into_container(alternative_helper<Right>{parser.right}, first, last, context, rcontext, attribute); |
| 236 | } |
| 237 | |
| 238 | template <typename Iterator, typename Attribute> |
| 239 | static bool call( |
| 240 | parser_type const& parser |
| 241 | , Iterator& first, Iterator const& last |
| 242 | , Context const& context, RContext& rcontext, Attribute& attribute) |
| 243 | { |
| 244 | return call(parser, first, last, context, rcontext, attribute, |
| 245 | typename traits::is_variant<typename traits::container_value<Attribute>::type>::type{}); |
| 246 | } |
| 247 | }; |
| 248 | |
| 249 | }}}} |
| 250 | |
| 251 | #endif |
| 252 | |