alternative.hpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*=============================================================================
  2. Copyright (c) 2001-2014 Joel de Guzman
  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(BOOST_SPIRIT_X3_ALTERNATIVE_JAN_07_2013_1131AM)
  7. #define BOOST_SPIRIT_X3_ALTERNATIVE_JAN_07_2013_1131AM
  8. #include <boost/spirit/home/x3/support/traits/attribute_of.hpp>
  9. #include <boost/spirit/home/x3/core/parser.hpp>
  10. #include <boost/spirit/home/x3/operator/detail/alternative.hpp>
  11. namespace boost { namespace spirit { namespace x3
  12. {
  13. template <typename Left, typename Right>
  14. struct alternative : binary_parser<Left, Right, alternative<Left, Right>>
  15. {
  16. typedef binary_parser<Left, Right, alternative<Left, Right>> base_type;
  17. alternative(Left const& left, Right const& right)
  18. : base_type(left, right) {}
  19. template <typename Iterator, typename Context, typename RContext>
  20. bool parse(
  21. Iterator& first, Iterator const& last
  22. , Context const& context, RContext& rcontext, unused_type) const
  23. {
  24. return this->left.parse(first, last, context, rcontext, unused)
  25. || this->right.parse(first, last, context, rcontext, unused);
  26. }
  27. template <typename Iterator, typename Context
  28. , typename RContext, typename Attribute>
  29. bool parse(
  30. Iterator& first, Iterator const& last
  31. , Context const& context, RContext& rcontext, Attribute& attr) const
  32. {
  33. return detail::parse_alternative(this->left, first, last, context, rcontext, attr)
  34. || detail::parse_alternative(this->right, first, last, context, rcontext, attr);
  35. }
  36. };
  37. template <typename Left, typename Right>
  38. inline alternative<
  39. typename extension::as_parser<Left>::value_type
  40. , typename extension::as_parser<Right>::value_type>
  41. operator|(Left const& left, Right const& right)
  42. {
  43. return { as_parser(left), as_parser(right) };
  44. }
  45. }}}
  46. namespace boost { namespace spirit { namespace x3 { namespace traits
  47. {
  48. template <typename Left, typename Right, typename Context>
  49. struct attribute_of<x3::alternative<Left, Right>, Context>
  50. : x3::detail::attribute_of_alternative<Left, Right, Context> {};
  51. }}}}
  52. #endif