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