alternative_function.hpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 Hartmut Kaiser
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. =============================================================================*/
  6. #if !defined(SPIRIT_ALTERNATIVE_FUNCTION_APRIL_23_2007_1046AM)
  7. #define SPIRIT_ALTERNATIVE_FUNCTION_APRIL_23_2007_1046AM
  8. #if defined(_MSC_VER)
  9. #pragma once
  10. #endif
  11. #include <boost/spirit/home/qi/domain.hpp>
  12. #include <boost/spirit/home/qi/detail/assign_to.hpp>
  13. #include <boost/spirit/home/support/unused.hpp>
  14. #include <boost/spirit/home/qi/detail/attributes.hpp>
  15. #include <boost/variant.hpp>
  16. #include <boost/mpl/bool.hpp>
  17. namespace boost { namespace spirit { namespace qi { namespace detail
  18. {
  19. template <typename Variant, typename Expected>
  20. struct find_substitute
  21. {
  22. // Get the type from the variant that can be a substitute for Expected.
  23. // If none is found, just return Expected
  24. typedef Variant variant_type;
  25. typedef typename variant_type::types types;
  26. typedef typename mpl::end<types>::type end;
  27. typedef typename mpl::find<types, Expected>::type iter_1;
  28. typedef typename
  29. mpl::eval_if<
  30. is_same<iter_1, end>,
  31. mpl::find_if<types, traits::is_substitute<mpl::_1, Expected> >,
  32. mpl::identity<iter_1>
  33. >::type
  34. iter;
  35. typedef typename
  36. mpl::eval_if<
  37. is_same<iter, end>,
  38. mpl::identity<Expected>,
  39. mpl::deref<iter>
  40. >::type
  41. type;
  42. };
  43. template <typename Iterator, typename Context, typename Skipper,
  44. typename Attribute>
  45. struct alternative_function
  46. {
  47. alternative_function(
  48. Iterator& first_, Iterator const& last_, Context& context_,
  49. Skipper const& skipper_, Attribute& attr_)
  50. : first(first_), last(last_), context(context_), skipper(skipper_),
  51. attr(attr_)
  52. {
  53. }
  54. template <typename Component>
  55. bool call(Component const& component, mpl::true_) const
  56. {
  57. // if Attribute is not a variant, then pass it as-is
  58. return component.parse(first, last, context, skipper, attr);
  59. }
  60. template <typename Component>
  61. bool call_optional_or_variant(Component const& component, mpl::true_) const
  62. {
  63. // If Attribute is an optional, then create an attribute for the Component
  64. // with the type optional::value_type. If the expected attribute is unused type,
  65. // use it instead.
  66. typedef typename
  67. traits::attribute_of<Component, Context, Iterator>::type
  68. expected_type;
  69. typename mpl::if_<
  70. is_same<expected_type, unused_type>,
  71. unused_type,
  72. typename Attribute::value_type>::type
  73. val;
  74. if (component.parse(first, last, context, skipper, val))
  75. {
  76. traits::assign_to(val, attr);
  77. return true;
  78. }
  79. return false;
  80. }
  81. template <typename Component>
  82. bool call_variant(Component const& component, mpl::false_) const
  83. {
  84. // If Attribute is a variant, then search the variant types for a
  85. // suitable substitute type.
  86. typename
  87. find_substitute<Attribute,
  88. typename traits::attribute_of<Component, Context, Iterator>::type
  89. >::type
  90. val;
  91. if (component.parse(first, last, context, skipper, val))
  92. {
  93. traits::assign_to(val, attr);
  94. return true;
  95. }
  96. return false;
  97. }
  98. template <typename Component>
  99. bool call_variant(Component const& component, mpl::true_) const
  100. {
  101. // If Attribute is a variant and the expected attribute is
  102. // the same type (pass the variant as-is).
  103. return component.parse(first, last, context, skipper, attr);
  104. }
  105. template <typename Component>
  106. bool call_optional_or_variant(Component const& component, mpl::false_) const
  107. {
  108. // Attribute is a variant...
  109. typedef typename
  110. traits::attribute_of<Component, Context, Iterator>::type
  111. expected;
  112. return call_variant(component,
  113. is_same<Attribute, expected>());
  114. }
  115. template <typename Component>
  116. bool call(Component const& component, mpl::false_) const
  117. {
  118. return call_optional_or_variant(
  119. component, spirit::traits::not_is_variant<Attribute, qi::domain>());
  120. }
  121. template <typename Component>
  122. bool call_unused(Component const& component, mpl::true_) const
  123. {
  124. // return true if the parser succeeds
  125. return call(component,
  126. mpl::and_<
  127. spirit::traits::not_is_variant<Attribute, qi::domain>,
  128. spirit::traits::not_is_optional<Attribute, qi::domain>
  129. >());
  130. }
  131. template <typename Component>
  132. bool call_unused(Component const& component, mpl::false_) const
  133. {
  134. return component.parse(first, last, context, skipper, unused);
  135. }
  136. template <typename Component>
  137. bool operator()(Component const& component) const
  138. {
  139. // return true if the parser succeeds
  140. typedef typename traits::not_is_unused<
  141. typename traits::attribute_of<Component, Context, Iterator>::type
  142. >::type predicate;
  143. return call_unused(component, predicate());
  144. }
  145. Iterator& first;
  146. Iterator const& last;
  147. Context& context;
  148. Skipper const& skipper;
  149. Attribute& attr;
  150. // silence MSVC warning C4512: assignment operator could not be generated
  151. BOOST_DELETED_FUNCTION(alternative_function& operator= (alternative_function const&))
  152. };
  153. template <typename Iterator, typename Context, typename Skipper>
  154. struct alternative_function<Iterator, Context, Skipper, unused_type const>
  155. {
  156. alternative_function(
  157. Iterator& first_, Iterator const& last_, Context& context_,
  158. Skipper const& skipper_, unused_type)
  159. : first(first_), last(last_), context(context_), skipper(skipper_)
  160. {
  161. }
  162. template <typename Component>
  163. bool operator()(Component const& component) const
  164. {
  165. // return true if the parser succeeds
  166. return component.parse(first, last, context, skipper,
  167. unused);
  168. }
  169. Iterator& first;
  170. Iterator const& last;
  171. Context& context;
  172. Skipper const& skipper;
  173. // silence MSVC warning C4512: assignment operator could not be generated
  174. BOOST_DELETED_FUNCTION(alternative_function& operator= (alternative_function const&))
  175. };
  176. }}}}
  177. #endif