exclusive_or.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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_EXCLUSIVE_OR_HPP)
  10. #define BOOST_SPIRIT_EXCLUSIVE_OR_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. // exclusive_or class
  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 or b but not both. One (not both) of the
  28. // operands may be a literal char, wchar_t or a primitive string
  29. // char const*, wchar_t const*.
  30. //
  31. ///////////////////////////////////////////////////////////////////////////
  32. struct exclusive_or_parser_gen;
  33. template <typename A, typename B>
  34. struct exclusive_or
  35. : public binary<A, B, parser<exclusive_or<A, B> > >
  36. {
  37. typedef exclusive_or<A, B> self_t;
  38. typedef binary_parser_category parser_category_t;
  39. typedef exclusive_or_parser_gen parser_generator_t;
  40. typedef binary<A, B, parser<self_t> > base_t;
  41. exclusive_or(A const& a, B const& b)
  42. : base_t(a, b) {}
  43. template <typename ScannerT>
  44. typename parser_result<self_t, ScannerT>::type
  45. parse(ScannerT const& scan) const
  46. {
  47. typedef typename parser_result<self_t, ScannerT>::type result_t;
  48. typedef typename ScannerT::iterator_t iterator_t;
  49. iterator_t save = scan.first;
  50. result_t l = this->left().parse(scan);
  51. std::swap(save, scan.first);
  52. result_t r = this->right().parse(scan);
  53. if (l ? !bool(r) : bool(r))
  54. {
  55. if (l)
  56. scan.first = save;
  57. return l ? l : r;
  58. }
  59. return scan.no_match();
  60. }
  61. };
  62. struct exclusive_or_parser_gen
  63. {
  64. template <typename A, typename B>
  65. struct result
  66. {
  67. typedef
  68. exclusive_or<
  69. typename as_parser<A>::type
  70. , typename as_parser<B>::type
  71. >
  72. type;
  73. };
  74. template <typename A, typename B>
  75. static exclusive_or<
  76. typename as_parser<A>::type
  77. , typename as_parser<B>::type
  78. >
  79. generate(A const& a, B const& b)
  80. {
  81. return exclusive_or<BOOST_DEDUCED_TYPENAME as_parser<A>::type,
  82. BOOST_DEDUCED_TYPENAME as_parser<B>::type>
  83. (as_parser<A>::convert(a), as_parser<B>::convert(b));
  84. }
  85. };
  86. template <typename A, typename B>
  87. exclusive_or<A, B>
  88. operator^(parser<A> const& a, parser<B> const& b);
  89. template <typename A>
  90. exclusive_or<A, chlit<char> >
  91. operator^(parser<A> const& a, char b);
  92. template <typename B>
  93. exclusive_or<chlit<char>, B>
  94. operator^(char a, parser<B> const& b);
  95. template <typename A>
  96. exclusive_or<A, strlit<char const*> >
  97. operator^(parser<A> const& a, char const* b);
  98. template <typename B>
  99. exclusive_or<strlit<char const*>, B>
  100. operator^(char const* a, parser<B> const& b);
  101. template <typename A>
  102. exclusive_or<A, chlit<wchar_t> >
  103. operator^(parser<A> const& a, wchar_t b);
  104. template <typename B>
  105. exclusive_or<chlit<wchar_t>, B>
  106. operator^(wchar_t a, parser<B> const& b);
  107. template <typename A>
  108. exclusive_or<A, strlit<wchar_t const*> >
  109. operator^(parser<A> const& a, wchar_t const* b);
  110. template <typename B>
  111. exclusive_or<strlit<wchar_t const*>, B>
  112. operator^(wchar_t const* a, parser<B> const& b);
  113. BOOST_SPIRIT_CLASSIC_NAMESPACE_END
  114. }} // namespace BOOST_SPIRIT_CLASSIC_NS
  115. #endif
  116. #include <boost/spirit/home/classic/core/composite/impl/exclusive_or.ipp>