kleene_star.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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_KLEENE_STAR_HPP)
  10. #define BOOST_SPIRIT_KLEENE_STAR_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. // kleene_star class
  21. //
  22. // Handles expressions of the form:
  23. //
  24. // *a
  25. //
  26. // where a is a parser. The expression returns a composite
  27. // parser that matches its subject zero (0) or more times.
  28. //
  29. ///////////////////////////////////////////////////////////////////////////
  30. struct kleene_star_parser_gen;
  31. #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
  32. #pragma warning(push)
  33. #pragma warning(disable:4512) //assignment operator could not be generated
  34. #endif
  35. template <typename S>
  36. struct kleene_star
  37. : public unary<S, parser<kleene_star<S> > >
  38. {
  39. typedef kleene_star<S> self_t;
  40. typedef unary_parser_category parser_category_t;
  41. typedef kleene_star_parser_gen parser_generator_t;
  42. typedef unary<S, parser<self_t> > base_t;
  43. kleene_star(S const& a)
  44. : base_t(a) {}
  45. template <typename ScannerT>
  46. typename parser_result<self_t, ScannerT>::type
  47. parse(ScannerT const& scan) const
  48. {
  49. typedef typename parser_result<self_t, ScannerT>::type result_t;
  50. typedef typename ScannerT::iterator_t iterator_t;
  51. result_t hit = scan.empty_match();
  52. for (;;)
  53. {
  54. iterator_t save = scan.first;
  55. if (result_t next = this->subject().parse(scan))
  56. {
  57. scan.concat_match(hit, next);
  58. }
  59. else
  60. {
  61. scan.first = save;
  62. return hit;
  63. }
  64. }
  65. }
  66. };
  67. #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
  68. #pragma warning(pop)
  69. #endif
  70. struct kleene_star_parser_gen
  71. {
  72. template <typename S>
  73. struct result
  74. {
  75. typedef kleene_star<S> type;
  76. };
  77. template <typename S>
  78. static kleene_star<S>
  79. generate(parser<S> const& a)
  80. {
  81. return kleene_star<S>(a.derived());
  82. }
  83. };
  84. //////////////////////////////////
  85. template <typename S>
  86. kleene_star<S>
  87. operator*(parser<S> const& a);
  88. BOOST_SPIRIT_CLASSIC_NAMESPACE_END
  89. }} // namespace BOOST_SPIRIT_CLASSIC_NS
  90. #endif
  91. #include <boost/spirit/home/classic/core/composite/impl/kleene_star.ipp>