alternative.hpp 2.5 KB

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