9
3

skipper.hpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*=============================================================================
  2. Copyright (c) 1998-2003 Joel de Guzman
  3. http://spirit.sourceforge.net/
  4. Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. =============================================================================*/
  7. #if !defined(BOOST_SPIRIT_SKIPPER_HPP)
  8. #define BOOST_SPIRIT_SKIPPER_HPP
  9. ///////////////////////////////////////////////////////////////////////////////
  10. #include <cctype>
  11. #include <boost/spirit/home/classic/namespace.hpp>
  12. #include <boost/spirit/home/classic/core/scanner/scanner.hpp>
  13. #include <boost/spirit/home/classic/core/primitives/impl/primitives.ipp>
  14. #include <boost/spirit/home/classic/core/scanner/skipper_fwd.hpp>
  15. namespace boost { namespace spirit {
  16. BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
  17. ///////////////////////////////////////////////////////////////////////////
  18. //
  19. // skipper_iteration_policy class
  20. //
  21. ///////////////////////////////////////////////////////////////////////////
  22. template <typename BaseT>
  23. struct skipper_iteration_policy : public BaseT
  24. {
  25. typedef BaseT base_t;
  26. skipper_iteration_policy()
  27. : BaseT() {}
  28. template <typename PolicyT>
  29. skipper_iteration_policy(PolicyT const& other)
  30. : BaseT(other) {}
  31. template <typename ScannerT>
  32. void
  33. advance(ScannerT const& scan) const
  34. {
  35. BaseT::advance(scan);
  36. scan.skip(scan);
  37. }
  38. template <typename ScannerT>
  39. bool
  40. at_end(ScannerT const& scan) const
  41. {
  42. scan.skip(scan);
  43. return BaseT::at_end(scan);
  44. }
  45. template <typename ScannerT>
  46. void
  47. skip(ScannerT const& scan) const
  48. {
  49. while (!BaseT::at_end(scan) && impl::isspace_(BaseT::get(scan)))
  50. BaseT::advance(scan);
  51. }
  52. };
  53. ///////////////////////////////////////////////////////////////////////////
  54. //
  55. // no_skipper_iteration_policy class
  56. //
  57. ///////////////////////////////////////////////////////////////////////////
  58. template <typename BaseT>
  59. struct no_skipper_iteration_policy : public BaseT
  60. {
  61. typedef BaseT base_t;
  62. no_skipper_iteration_policy()
  63. : BaseT() {}
  64. template <typename PolicyT>
  65. no_skipper_iteration_policy(PolicyT const& other)
  66. : BaseT(other) {}
  67. template <typename ScannerT>
  68. void
  69. skip(ScannerT const& /*scan*/) const {}
  70. };
  71. ///////////////////////////////////////////////////////////////////////////
  72. //
  73. // skip_parser_iteration_policy class
  74. //
  75. ///////////////////////////////////////////////////////////////////////////
  76. namespace impl
  77. {
  78. template <typename ST, typename ScannerT, typename BaseT>
  79. void
  80. skipper_skip(
  81. ST const& s,
  82. ScannerT const& scan,
  83. skipper_iteration_policy<BaseT> const&);
  84. template <typename ST, typename ScannerT, typename BaseT>
  85. void
  86. skipper_skip(
  87. ST const& s,
  88. ScannerT const& scan,
  89. no_skipper_iteration_policy<BaseT> const&);
  90. template <typename ST, typename ScannerT>
  91. void
  92. skipper_skip(
  93. ST const& s,
  94. ScannerT const& scan,
  95. iteration_policy const&);
  96. }
  97. template <typename ParserT, typename BaseT>
  98. class skip_parser_iteration_policy : public skipper_iteration_policy<BaseT>
  99. {
  100. public:
  101. typedef skipper_iteration_policy<BaseT> base_t;
  102. skip_parser_iteration_policy(
  103. ParserT const& skip_parser,
  104. base_t const& base = base_t())
  105. : base_t(base), subject(skip_parser) {}
  106. template <typename PolicyT>
  107. skip_parser_iteration_policy(PolicyT const& other)
  108. : base_t(other), subject(other.skipper()) {}
  109. template <typename ScannerT>
  110. void
  111. skip(ScannerT const& scan) const
  112. {
  113. impl::skipper_skip(subject, scan, scan);
  114. }
  115. ParserT const&
  116. skipper() const
  117. {
  118. return subject;
  119. }
  120. private:
  121. ParserT const& subject;
  122. };
  123. ///////////////////////////////////////////////////////////////////////////////
  124. //
  125. // Free parse functions using the skippers
  126. //
  127. ///////////////////////////////////////////////////////////////////////////////
  128. template <typename IteratorT, typename ParserT, typename SkipT>
  129. parse_info<IteratorT>
  130. parse(
  131. IteratorT const& first,
  132. IteratorT const& last,
  133. parser<ParserT> const& p,
  134. parser<SkipT> const& skip);
  135. ///////////////////////////////////////////////////////////////////////////////
  136. //
  137. // Parse function for null terminated strings using the skippers
  138. //
  139. ///////////////////////////////////////////////////////////////////////////////
  140. template <typename CharT, typename ParserT, typename SkipT>
  141. parse_info<CharT const*>
  142. parse(
  143. CharT const* str,
  144. parser<ParserT> const& p,
  145. parser<SkipT> const& skip);
  146. ///////////////////////////////////////////////////////////////////////////////
  147. //
  148. // phrase_scanner_t and wide_phrase_scanner_t
  149. //
  150. // The most common scanners. Use these typedefs when you need
  151. // a scanner that skips white spaces.
  152. //
  153. ///////////////////////////////////////////////////////////////////////////////
  154. typedef skipper_iteration_policy<> iter_policy_t;
  155. typedef scanner_policies<iter_policy_t> scanner_policies_t;
  156. typedef scanner<char const*, scanner_policies_t> phrase_scanner_t;
  157. typedef scanner<wchar_t const*, scanner_policies_t> wide_phrase_scanner_t;
  158. ///////////////////////////////////////////////////////////////////////////////
  159. BOOST_SPIRIT_CLASSIC_NAMESPACE_END
  160. }} // namespace BOOST_SPIRIT_CLASSIC_NS
  161. #include <boost/spirit/home/classic/core/scanner/impl/skipper.ipp>
  162. #endif