difference.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 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(SPIRIT_DIFFERENCE_FEBRUARY_11_2007_1250PM)
  7. #define SPIRIT_DIFFERENCE_FEBRUARY_11_2007_1250PM
  8. #if defined(_MSC_VER)
  9. #pragma once
  10. #endif
  11. #include <boost/spirit/home/qi/domain.hpp>
  12. #include <boost/spirit/home/qi/meta_compiler.hpp>
  13. #include <boost/spirit/home/qi/parser.hpp>
  14. #include <boost/spirit/home/qi/detail/attributes.hpp>
  15. #include <boost/spirit/home/support/info.hpp>
  16. #include <boost/spirit/home/support/has_semantic_action.hpp>
  17. #include <boost/spirit/home/support/handles_container.hpp>
  18. #include <boost/fusion/include/at.hpp>
  19. namespace boost { namespace spirit
  20. {
  21. ///////////////////////////////////////////////////////////////////////////
  22. // Enablers
  23. ///////////////////////////////////////////////////////////////////////////
  24. template <>
  25. struct use_operator<qi::domain, proto::tag::minus> // enables -
  26. : mpl::true_ {};
  27. }}
  28. namespace boost { namespace spirit { namespace qi
  29. {
  30. template <typename Left, typename Right>
  31. struct difference : binary_parser<difference<Left, Right> >
  32. {
  33. typedef Left left_type;
  34. typedef Right right_type;
  35. template <typename Context, typename Iterator>
  36. struct attribute
  37. {
  38. typedef typename
  39. traits::attribute_of<left_type, Context, Iterator>::type
  40. type;
  41. };
  42. difference(Left const& left_, Right const& right_)
  43. : left(left_), right(right_) {}
  44. template <typename Iterator, typename Context
  45. , typename Skipper, typename Attribute>
  46. bool parse(Iterator& first, Iterator const& last
  47. , Context& context, Skipper const& skipper
  48. , Attribute& attr_) const
  49. {
  50. // Unlike classic Spirit, with this version of difference, the rule
  51. // lit("policeman") - "police" will always fail to match.
  52. // Spirit2 does not count the matching chars while parsing and
  53. // there is no reliable and fast way to check if the LHS matches
  54. // more than the RHS.
  55. // Try RHS first
  56. Iterator start = first;
  57. if (right.parse(first, last, context, skipper, unused))
  58. {
  59. // RHS succeeds, we fail.
  60. first = start;
  61. return false;
  62. }
  63. // RHS fails, now try LHS
  64. return left.parse(first, last, context, skipper, attr_);
  65. }
  66. template <typename Context>
  67. info what(Context& context) const
  68. {
  69. return info("difference",
  70. std::make_pair(left.what(context), right.what(context)));
  71. }
  72. Left left;
  73. Right right;
  74. };
  75. ///////////////////////////////////////////////////////////////////////////
  76. // Parser generators: make_xxx function (objects)
  77. ///////////////////////////////////////////////////////////////////////////
  78. template <typename Elements, typename Modifiers>
  79. struct make_composite<proto::tag::minus, Elements, Modifiers>
  80. : make_binary_composite<Elements, difference>
  81. {};
  82. }}}
  83. namespace boost { namespace spirit { namespace traits
  84. {
  85. ///////////////////////////////////////////////////////////////////////////
  86. template <typename Left, typename Right>
  87. struct has_semantic_action<qi::difference<Left, Right> >
  88. : binary_has_semantic_action<Left, Right> {};
  89. ///////////////////////////////////////////////////////////////////////////
  90. template <typename Left, typename Right, typename Attribute
  91. , typename Context, typename Iterator>
  92. struct handles_container<qi::difference<Left, Right>, Attribute, Context
  93. , Iterator>
  94. : binary_handles_container<Left, Right, Attribute, Context, Iterator> {};
  95. }}}
  96. #endif