parametric.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*=============================================================================
  2. Copyright (c) 2001-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. #ifndef BOOST_SPIRIT_PARAMETRIC_HPP
  8. #define BOOST_SPIRIT_PARAMETRIC_HPP
  9. ///////////////////////////////////////////////////////////////////////////////
  10. #include <boost/spirit/home/classic/namespace.hpp>
  11. #include <boost/spirit/home/classic/core/parser.hpp>
  12. #include <boost/spirit/home/classic/core/composite/composite.hpp>
  13. #include <boost/spirit/home/classic/core/primitives/primitives.hpp>
  14. namespace boost { namespace spirit {
  15. BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
  16. ///////////////////////////////////////////////////////////////////////////
  17. //
  18. // f_chlit class [ functional version of chlit ]
  19. //
  20. ///////////////////////////////////////////////////////////////////////////
  21. template <typename ChGenT>
  22. struct f_chlit : public char_parser<f_chlit<ChGenT> >
  23. {
  24. f_chlit(ChGenT chgen_)
  25. : chgen(chgen_) {}
  26. template <typename T>
  27. bool test(T ch) const
  28. { return ch == chgen(); }
  29. ChGenT chgen;
  30. };
  31. template <typename ChGenT>
  32. inline f_chlit<ChGenT>
  33. f_ch_p(ChGenT chgen)
  34. { return f_chlit<ChGenT>(chgen); }
  35. ///////////////////////////////////////////////////////////////////////////
  36. //
  37. // f_range class [ functional version of range ]
  38. //
  39. ///////////////////////////////////////////////////////////////////////////
  40. template <typename ChGenAT, typename ChGenBT>
  41. struct f_range : public char_parser<f_range<ChGenAT, ChGenBT> >
  42. {
  43. f_range(ChGenAT first_, ChGenBT last_)
  44. : first(first_), last(last_)
  45. {}
  46. template <typename T>
  47. bool test(T ch) const
  48. {
  49. BOOST_SPIRIT_ASSERT(first() <= last());
  50. return (ch >= first()) && (ch <= last());
  51. }
  52. ChGenAT first;
  53. ChGenBT last;
  54. };
  55. template <typename ChGenAT, typename ChGenBT>
  56. inline f_range<ChGenAT, ChGenBT>
  57. f_range_p(ChGenAT first, ChGenBT last)
  58. { return f_range<ChGenAT, ChGenBT>(first, last); }
  59. ///////////////////////////////////////////////////////////////////////////
  60. //
  61. // f_chseq class [ functional version of chseq ]
  62. //
  63. ///////////////////////////////////////////////////////////////////////////
  64. template <typename IterGenAT, typename IterGenBT>
  65. class f_chseq : public parser<f_chseq<IterGenAT, IterGenBT> >
  66. {
  67. public:
  68. typedef f_chseq<IterGenAT, IterGenBT> self_t;
  69. f_chseq(IterGenAT first_, IterGenBT last_)
  70. : first(first_), last(last_) {}
  71. template <typename ScannerT>
  72. typename parser_result<self_t, ScannerT>::type
  73. parse(ScannerT const& scan) const
  74. {
  75. typedef typename parser_result<self_t, ScannerT>::type result_t;
  76. return impl::string_parser_parse<result_t>(first(), last(), scan);
  77. }
  78. private:
  79. IterGenAT first;
  80. IterGenBT last;
  81. };
  82. template <typename IterGenAT, typename IterGenBT>
  83. inline f_chseq<IterGenAT, IterGenBT>
  84. f_chseq_p(IterGenAT first, IterGenBT last)
  85. { return f_chseq<IterGenAT, IterGenBT>(first, last); }
  86. ///////////////////////////////////////////////////////////////////////////
  87. //
  88. // f_strlit class [ functional version of strlit ]
  89. //
  90. ///////////////////////////////////////////////////////////////////////////
  91. template <typename IterGenAT, typename IterGenBT>
  92. class f_strlit : public parser<f_strlit<IterGenAT, IterGenBT> >
  93. {
  94. public:
  95. typedef f_strlit<IterGenAT, IterGenBT> self_t;
  96. f_strlit(IterGenAT first, IterGenBT last)
  97. : seq(first, last) {}
  98. template <typename ScannerT>
  99. typename parser_result<self_t, ScannerT>::type
  100. parse(ScannerT const& scan) const
  101. {
  102. typedef typename parser_result<self_t, ScannerT>::type result_t;
  103. return impl::contiguous_parser_parse<result_t>
  104. (seq, scan, scan);
  105. }
  106. private:
  107. f_chseq<IterGenAT, IterGenBT> seq;
  108. };
  109. template <typename IterGenAT, typename IterGenBT>
  110. inline f_strlit<IterGenAT, IterGenBT>
  111. f_str_p(IterGenAT first, IterGenBT last)
  112. { return f_strlit<IterGenAT, IterGenBT>(first, last); }
  113. BOOST_SPIRIT_CLASSIC_NAMESPACE_END
  114. }} // namespace BOOST_SPIRIT_CLASSIC_NS
  115. #endif