difference.hpp 4.1 KB

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