9
3

positive.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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_POSITIVE_HPP)
  10. #define BOOST_SPIRIT_POSITIVE_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. // positive 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 one (1) or more times.
  28. //
  29. ///////////////////////////////////////////////////////////////////////////
  30. struct positive_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 positive
  37. : public unary<S, parser<positive<S> > >
  38. {
  39. typedef positive<S> self_t;
  40. typedef unary_parser_category parser_category_t;
  41. typedef positive_parser_gen parser_generator_t;
  42. typedef unary<S, parser<self_t> > base_t;
  43. positive(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 = this->subject().parse(scan);
  52. if (hit)
  53. {
  54. for (;;)
  55. {
  56. iterator_t save = scan.first;
  57. if (result_t next = this->subject().parse(scan))
  58. {
  59. scan.concat_match(hit, next);
  60. }
  61. else
  62. {
  63. scan.first = save;
  64. break;
  65. }
  66. }
  67. }
  68. return hit;
  69. }
  70. };
  71. #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
  72. #pragma warning(pop)
  73. #endif
  74. struct positive_parser_gen
  75. {
  76. template <typename S>
  77. struct result
  78. {
  79. typedef positive<S> type;
  80. };
  81. template <typename S>
  82. static positive<S>
  83. generate(parser<S> const& a)
  84. {
  85. return positive<S>(a.derived());
  86. }
  87. };
  88. template <typename S>
  89. inline positive<S>
  90. operator+(parser<S> const& a);
  91. BOOST_SPIRIT_CLASSIC_NAMESPACE_END
  92. }} // namespace BOOST_SPIRIT_CLASSIC_NS
  93. #endif
  94. #include <boost/spirit/home/classic/core/composite/impl/positive.ipp>