difference.hpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*=============================================================================
  2. Copyright (c) 1998-2003 Joel de Guzman
  3. Copyright (c) 2001 Daniel Nuffer
  4. Copyright (c) 2002 Hartmut Kaiser
  5. http://spirit.sourceforge.net/
  6. Distributed under the Boost Software License, Version 1.0. (See accompanying
  7. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  8. =============================================================================*/
  9. #if !defined(BOOST_SPIRIT_DIFFERENCE_HPP)
  10. #define BOOST_SPIRIT_DIFFERENCE_HPP
  11. #include <boost/spirit/home/classic/namespace.hpp>
  12. #include <boost/spirit/home/classic/core/parser.hpp>
  13. #include <boost/spirit/home/classic/core/primitives/primitives.hpp>
  14. #include <boost/spirit/home/classic/core/composite/composite.hpp>
  15. #include <boost/spirit/home/classic/meta/as_parser.hpp>
  16. namespace boost { namespace spirit {
  17. BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
  18. ///////////////////////////////////////////////////////////////////////////
  19. //
  20. // difference: a - b; Matches a but not b
  21. //
  22. // Handles expressions of the form:
  23. //
  24. // a - b
  25. //
  26. // where a and b are parsers. The expression returns a composite
  27. // parser that matches a but not b. One (not both) of the operands
  28. // may be a literal char, wchar_t or a primitive string char const*,
  29. // wchar_t const*.
  30. //
  31. ///////////////////////////////////////////////////////////////////////////
  32. struct difference_parser_gen;
  33. #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
  34. #pragma warning(push)
  35. #pragma warning(disable:4512) //assignment operator could not be generated
  36. #endif
  37. template <typename A, typename B>
  38. struct difference
  39. : public binary<A, B, parser<difference<A, B> > >
  40. {
  41. typedef difference<A, B> self_t;
  42. typedef binary_parser_category parser_category_t;
  43. typedef difference_parser_gen parser_generator_t;
  44. typedef binary<A, B, parser<self_t> > base_t;
  45. difference(A const& a, B const& b)
  46. : base_t(a, b) {}
  47. template <typename ScannerT>
  48. typename parser_result<self_t, ScannerT>::type
  49. parse(ScannerT const& scan) const
  50. {
  51. typedef typename parser_result<self_t, ScannerT>::type result_t;
  52. typedef typename ScannerT::iterator_t iterator_t;
  53. iterator_t save = scan.first;
  54. if (result_t hl = this->left().parse(scan))
  55. {
  56. std::swap(save, scan.first);
  57. result_t hr = this->right().parse(scan);
  58. if (!hr || (hr.length() < hl.length()))
  59. {
  60. scan.first = save;
  61. return hl;
  62. }
  63. }
  64. return scan.no_match();
  65. }
  66. };
  67. #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
  68. #pragma warning(pop)
  69. #endif
  70. struct difference_parser_gen
  71. {
  72. template <typename A, typename B>
  73. struct result
  74. {
  75. typedef
  76. difference<
  77. typename as_parser<A>::type
  78. , typename as_parser<B>::type
  79. >
  80. type;
  81. };
  82. template <typename A, typename B>
  83. static difference<
  84. typename as_parser<A>::type
  85. , typename as_parser<B>::type
  86. >
  87. generate(A const& a, B const& b)
  88. {
  89. return difference<BOOST_DEDUCED_TYPENAME as_parser<A>::type,
  90. BOOST_DEDUCED_TYPENAME as_parser<B>::type>
  91. (as_parser<A>::convert(a), as_parser<B>::convert(b));
  92. }
  93. };
  94. template <typename A, typename B>
  95. difference<A, B>
  96. operator-(parser<A> const& a, parser<B> const& b);
  97. template <typename A>
  98. difference<A, chlit<char> >
  99. operator-(parser<A> const& a, char b);
  100. template <typename B>
  101. difference<chlit<char>, B>
  102. operator-(char a, parser<B> const& b);
  103. template <typename A>
  104. difference<A, strlit<char const*> >
  105. operator-(parser<A> const& a, char const* b);
  106. template <typename B>
  107. difference<strlit<char const*>, B>
  108. operator-(char const* a, parser<B> const& b);
  109. template <typename A>
  110. difference<A, chlit<wchar_t> >
  111. operator-(parser<A> const& a, wchar_t b);
  112. template <typename B>
  113. difference<chlit<wchar_t>, B>
  114. operator-(wchar_t a, parser<B> const& b);
  115. template <typename A>
  116. difference<A, strlit<wchar_t const*> >
  117. operator-(parser<A> const& a, wchar_t const* b);
  118. template <typename B>
  119. difference<strlit<wchar_t const*>, B>
  120. operator-(wchar_t const* a, parser<B> const& b);
  121. BOOST_SPIRIT_CLASSIC_NAMESPACE_END
  122. }} // namespace BOOST_SPIRIT_CLASSIC_NS
  123. #endif
  124. #include <boost/spirit/home/classic/core/composite/impl/difference.ipp>