alternative_function.hpp 7.0 KB

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