difference.hpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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_DIFFERENCE_FEBRUARY_11_2007_1250PM)
  7. #define BOOST_SPIRIT_X3_DIFFERENCE_FEBRUARY_11_2007_1250PM
  8. #include <boost/spirit/home/x3/support/traits/attribute_of.hpp>
  9. #include <boost/spirit/home/x3/support/traits/has_attribute.hpp>
  10. #include <boost/spirit/home/x3/core/parser.hpp>
  11. namespace boost { namespace spirit { namespace x3
  12. {
  13. template <typename Left, typename Right>
  14. struct difference : binary_parser<Left, Right, difference<Left, Right>>
  15. {
  16. typedef binary_parser<Left, Right, difference<Left, Right>> base_type;
  17. static bool const handles_container = Left::handles_container;
  18. difference(Left const& left, Right const& right)
  19. : base_type(left, right) {}
  20. template <typename Iterator, typename Context
  21. , typename RContext, typename Attribute>
  22. bool parse(Iterator& first, Iterator const& last
  23. , Context const& context, RContext& rcontext, Attribute& attr) const
  24. {
  25. // Try Right first
  26. Iterator start = first;
  27. if (this->right.parse(first, last, context, rcontext, unused))
  28. {
  29. // Right succeeds, we fail.
  30. first = start;
  31. return false;
  32. }
  33. // Right fails, now try Left
  34. return this->left.parse(first, last, context, rcontext, attr);
  35. }
  36. template <typename Left_, typename Right_>
  37. difference<Left_, Right_>
  38. make(Left_ const& left, Right_ const& right) const
  39. {
  40. return { left, right };
  41. }
  42. };
  43. template <typename Left, typename Right>
  44. inline difference<
  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::difference<Left, Right>, Context>
  56. : attribute_of<Left, Context> {};
  57. template <typename Left, typename Right, typename Context>
  58. struct has_attribute<x3::difference<Left, Right>, Context>
  59. : has_attribute<Left, Context> {};
  60. }}}}
  61. #endif