sequence.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_SEQUENCE_HPP)
  10. #define BOOST_SPIRIT_SEQUENCE_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. // sequence 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 in sequence. 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 sequence_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 sequence : public binary<A, B, parser<sequence<A, B> > >
  39. {
  40. typedef sequence<A, B> self_t;
  41. typedef binary_parser_category parser_category_t;
  42. typedef sequence_parser_gen parser_generator_t;
  43. typedef binary<A, B, parser<self_t> > base_t;
  44. sequence(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. if (result_t ma = this->left().parse(scan))
  52. if (result_t mb = this->right().parse(scan))
  53. {
  54. scan.concat_match(ma, mb);
  55. return ma;
  56. }
  57. return scan.no_match();
  58. }
  59. };
  60. #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
  61. #pragma warning(pop)
  62. #endif
  63. struct sequence_parser_gen
  64. {
  65. template <typename A, typename B>
  66. struct result
  67. {
  68. typedef
  69. sequence<
  70. typename as_parser<A>::type
  71. , typename as_parser<B>::type
  72. >
  73. type;
  74. };
  75. template <typename A, typename B>
  76. static sequence<
  77. typename as_parser<A>::type
  78. , typename as_parser<B>::type
  79. >
  80. generate(A const& a, B const& b)
  81. {
  82. return sequence<BOOST_DEDUCED_TYPENAME as_parser<A>::type,
  83. BOOST_DEDUCED_TYPENAME as_parser<B>::type>
  84. (as_parser<A>::convert(a), as_parser<B>::convert(b));
  85. }
  86. };
  87. template <typename A, typename B>
  88. sequence<A, B>
  89. operator>>(parser<A> const& a, parser<B> const& b);
  90. template <typename A>
  91. sequence<A, chlit<char> >
  92. operator>>(parser<A> const& a, char b);
  93. template <typename B>
  94. sequence<chlit<char>, B>
  95. operator>>(char a, parser<B> const& b);
  96. template <typename A>
  97. sequence<A, strlit<char const*> >
  98. operator>>(parser<A> const& a, char const* b);
  99. template <typename B>
  100. sequence<strlit<char const*>, B>
  101. operator>>(char const* a, parser<B> const& b);
  102. template <typename A>
  103. sequence<A, chlit<wchar_t> >
  104. operator>>(parser<A> const& a, wchar_t b);
  105. template <typename B>
  106. sequence<chlit<wchar_t>, B>
  107. operator>>(wchar_t a, parser<B> const& b);
  108. template <typename A>
  109. sequence<A, strlit<wchar_t const*> >
  110. operator>>(parser<A> const& a, wchar_t const* b);
  111. template <typename B>
  112. sequence<strlit<wchar_t const*>, B>
  113. operator>>(wchar_t const* a, parser<B> const& b);
  114. BOOST_SPIRIT_CLASSIC_NAMESPACE_END
  115. }} // namespace BOOST_SPIRIT_CLASSIC_NS
  116. #endif
  117. #include <boost/spirit/home/classic/core/composite/impl/sequence.ipp>