difference.hpp 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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_DIFFERENCE_FEBRUARY_11_2007_1250PM)
  9. #define BOOST_SPIRIT_X3_DIFFERENCE_FEBRUARY_11_2007_1250PM
  10. #include <boost/spirit/home/x3/support/traits/attribute_of.hpp>
  11. #include <boost/spirit/home/x3/support/traits/has_attribute.hpp>
  12. #include <boost/spirit/home/x3/support/expectation.hpp>
  13. #include <boost/spirit/home/x3/core/parser.hpp>
  14. namespace boost { namespace spirit { namespace x3
  15. {
  16. template <typename Left, typename Right>
  17. struct difference : binary_parser<Left, Right, difference<Left, Right>>
  18. {
  19. typedef binary_parser<Left, Right, difference<Left, Right>> base_type;
  20. static bool const handles_container = Left::handles_container;
  21. constexpr difference(Left const& left, Right const& right)
  22. : base_type(left, right) {}
  23. template <typename Iterator, typename Context
  24. , typename RContext, typename Attribute>
  25. bool parse(Iterator& first, Iterator const& last
  26. , Context const& context, RContext& rcontext, Attribute& attr) const
  27. {
  28. // Try Right first
  29. Iterator start = first;
  30. if (this->right.parse(first, last, context, rcontext, unused))
  31. {
  32. // Right succeeds, we fail.
  33. first = start;
  34. return false;
  35. }
  36. // In case of `Left - expect[r]`,
  37. // if Right yielded expectation error,
  38. // the whole difference expression (*this) should also yield error.
  39. // In other words, when the THROW macro was 1 (i.e. traditional behavior),
  40. // Right should already have thrown an exception.
  41. #if !BOOST_SPIRIT_X3_THROW_EXPECTATION_FAILURE
  42. if (has_expectation_failure(context))
  43. {
  44. // don't rollback iterator (mimicking exception-like behavior)
  45. return false;
  46. }
  47. #endif
  48. // Right fails, now try Left
  49. return this->left.parse(first, last, context, rcontext, attr);
  50. }
  51. template <typename Left_, typename Right_>
  52. constexpr difference<Left_, Right_>
  53. make(Left_ const& left, Right_ const& right) const
  54. {
  55. return { left, right };
  56. }
  57. };
  58. template <typename Left, typename Right>
  59. constexpr difference<
  60. typename extension::as_parser<Left>::value_type
  61. , typename extension::as_parser<Right>::value_type>
  62. operator-(Left const& left, Right const& right)
  63. {
  64. return { as_parser(left), as_parser(right) };
  65. }
  66. }}}
  67. namespace boost { namespace spirit { namespace x3 { namespace traits
  68. {
  69. template <typename Left, typename Right, typename Context>
  70. struct attribute_of<x3::difference<Left, Right>, Context>
  71. : attribute_of<Left, Context> {};
  72. template <typename Left, typename Right, typename Context>
  73. struct has_attribute<x3::difference<Left, Right>, Context>
  74. : has_attribute<Left, Context> {};
  75. }}}}
  76. #endif