sequential_or.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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_SEQUENTIAL_OR_HPP)
  10. #define BOOST_SPIRIT_SEQUENTIAL_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. // sequential-or class
  21. //
  22. // Handles expressions of the form:
  23. //
  24. // a || b
  25. //
  26. // Equivalent to
  27. //
  28. // a | b | a >> b;
  29. //
  30. // where a and b are parsers. The expression returns a composite
  31. // parser that matches matches a or b in sequence. One (not both) of
  32. // the operands may be a literal char, wchar_t or a primitive string
  33. // char const*, wchar_t const*.
  34. //
  35. ///////////////////////////////////////////////////////////////////////////
  36. struct sequential_or_parser_gen;
  37. template <typename A, typename B>
  38. struct sequential_or : public binary<A, B, parser<sequential_or<A, B> > >
  39. {
  40. typedef sequential_or<A, B> self_t;
  41. typedef binary_parser_category parser_category_t;
  42. typedef sequential_or_parser_gen parser_generator_t;
  43. typedef binary<A, B, parser<self_t> > base_t;
  44. sequential_or(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. { // scope for save
  53. iterator_t save = scan.first;
  54. if (result_t ma = this->left().parse(scan))
  55. {
  56. save = scan.first;
  57. if (result_t mb = this->right().parse(scan))
  58. {
  59. // matched a b
  60. scan.concat_match(ma, mb);
  61. return ma;
  62. }
  63. else
  64. {
  65. // matched a
  66. scan.first = save;
  67. return ma;
  68. }
  69. }
  70. scan.first = save;
  71. }
  72. // matched b
  73. return this->right().parse(scan);
  74. }
  75. };
  76. struct sequential_or_parser_gen
  77. {
  78. template <typename A, typename B>
  79. struct result
  80. {
  81. typedef
  82. sequential_or<
  83. typename as_parser<A>::type
  84. , typename as_parser<B>::type
  85. >
  86. type;
  87. };
  88. template <typename A, typename B>
  89. static sequential_or<
  90. typename as_parser<A>::type
  91. , typename as_parser<B>::type
  92. >
  93. generate(A const& a, B const& b)
  94. {
  95. return sequential_or<BOOST_DEDUCED_TYPENAME as_parser<A>::type,
  96. BOOST_DEDUCED_TYPENAME as_parser<B>::type>
  97. (as_parser<A>::convert(a), as_parser<B>::convert(b));
  98. }
  99. };
  100. template <typename A, typename B>
  101. sequential_or<A, B>
  102. operator||(parser<A> const& a, parser<B> const& b);
  103. template <typename A>
  104. sequential_or<A, chlit<char> >
  105. operator||(parser<A> const& a, char b);
  106. template <typename B>
  107. sequential_or<chlit<char>, B>
  108. operator||(char a, parser<B> const& b);
  109. template <typename A>
  110. sequential_or<A, strlit<char const*> >
  111. operator||(parser<A> const& a, char const* b);
  112. template <typename B>
  113. sequential_or<strlit<char const*>, B>
  114. operator||(char const* a, parser<B> const& b);
  115. template <typename A>
  116. sequential_or<A, chlit<wchar_t> >
  117. operator||(parser<A> const& a, wchar_t b);
  118. template <typename B>
  119. sequential_or<chlit<wchar_t>, B>
  120. operator||(wchar_t a, parser<B> const& b);
  121. template <typename A>
  122. sequential_or<A, strlit<wchar_t const*> >
  123. operator||(parser<A> const& a, wchar_t const* b);
  124. template <typename B>
  125. sequential_or<strlit<wchar_t const*>, B>
  126. operator||(wchar_t const* a, parser<B> const& b);
  127. BOOST_SPIRIT_CLASSIC_NAMESPACE_END
  128. }} // namespace BOOST_SPIRIT_CLASSIC_NS
  129. #endif
  130. #include <boost/spirit/home/classic/core/composite/impl/sequential_or.ipp>