alternative.hpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*=============================================================================
  2. Copyright (c) 2001-2014 Joel de Guzman
  3. Copyright (c) 2017 wanghan02
  4. Copyright (c) 2024 Nana Sakisaka
  5. Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. =============================================================================*/
  8. #if !defined(BOOST_SPIRIT_X3_ALTERNATIVE_JAN_07_2013_1131AM)
  9. #define BOOST_SPIRIT_X3_ALTERNATIVE_JAN_07_2013_1131AM
  10. #include <boost/spirit/home/x3/support/traits/attribute_of_binary.hpp>
  11. #include <boost/spirit/home/x3/support/expectation.hpp>
  12. #include <boost/spirit/home/x3/core/parser.hpp>
  13. #include <boost/spirit/home/x3/operator/detail/alternative.hpp>
  14. #include <boost/variant/variant_fwd.hpp>
  15. namespace boost { namespace spirit { namespace x3
  16. {
  17. template <typename Left, typename Right>
  18. struct alternative : binary_parser<Left, Right, alternative<Left, Right>>
  19. {
  20. typedef binary_parser<Left, Right, alternative<Left, Right>> base_type;
  21. constexpr alternative(Left const& left, Right const& right)
  22. : base_type(left, right) {}
  23. template <typename Iterator, typename Context, typename RContext>
  24. bool parse(
  25. Iterator& first, Iterator const& last
  26. , Context const& context, RContext& rcontext, unused_type) const
  27. {
  28. return this->left.parse(first, last, context, rcontext, unused)
  29. || (!has_expectation_failure(context)
  30. && this->right.parse(first, last, context, rcontext, unused));
  31. }
  32. template <typename Iterator, typename Context
  33. , typename RContext, typename Attribute>
  34. bool parse(
  35. Iterator& first, Iterator const& last
  36. , Context const& context, RContext& rcontext, Attribute& attr) const
  37. {
  38. return detail::parse_alternative(this->left, first, last, context, rcontext, attr)
  39. || (!has_expectation_failure(context)
  40. && detail::parse_alternative(this->right, first, last, context, rcontext, attr));
  41. }
  42. };
  43. template <typename Left, typename Right>
  44. constexpr alternative<
  45. typename extension::as_parser<Left>::value_type
  46. , typename extension::as_parser<Right>::value_type>
  47. operator|(Left const& left, Right const& right)
  48. {
  49. return { as_parser(left), as_parser(right) };
  50. }
  51. }}}
  52. namespace boost { namespace spirit { namespace x3 { namespace traits
  53. {
  54. template <typename Left, typename Right, typename Context>
  55. struct attribute_of<x3::alternative<Left, Right>, Context>
  56. : x3::detail::attribute_of_binary<boost::variant, x3::alternative, Left, Right, Context> {};
  57. }}}}
  58. #endif