distinct.hpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*=============================================================================
  2. Copyright (c) 1998-2003 Joel de Guzman
  3. Copyright (c) 2003 Vaclav Vesely
  4. http://spirit.sourceforge.net/
  5. Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. =============================================================================*/
  8. #if !defined(BOOST_SPIRIT_DISTINCT_HPP)
  9. #define BOOST_SPIRIT_DISTINCT_HPP
  10. #include <boost/spirit/home/classic/core/parser.hpp>
  11. #include <boost/spirit/home/classic/core/primitives/primitives.hpp>
  12. #include <boost/spirit/home/classic/core/composite/operators.hpp>
  13. #include <boost/spirit/home/classic/core/composite/directives.hpp>
  14. #include <boost/spirit/home/classic/core/composite/epsilon.hpp>
  15. #include <boost/spirit/home/classic/core/non_terminal/rule.hpp>
  16. #include <boost/spirit/home/classic/utility/chset.hpp>
  17. #include <boost/spirit/home/classic/utility/distinct_fwd.hpp>
  18. namespace boost {
  19. namespace spirit {
  20. BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
  21. //-----------------------------------------------------------------------------
  22. // distinct_parser class
  23. template <typename CharT, typename TailT>
  24. class distinct_parser
  25. {
  26. public:
  27. typedef
  28. contiguous<
  29. sequence<
  30. chseq<CharT const*>,
  31. negated_empty_match_parser<
  32. TailT
  33. >
  34. >
  35. >
  36. result_t;
  37. distinct_parser()
  38. : tail(chset<CharT>())
  39. {
  40. }
  41. explicit distinct_parser(parser<TailT> const & tail_)
  42. : tail(tail_.derived())
  43. {
  44. }
  45. explicit distinct_parser(CharT const* letters)
  46. : tail(chset_p(letters))
  47. {
  48. }
  49. result_t operator()(CharT const* str) const
  50. {
  51. return lexeme_d[chseq_p(str) >> ~epsilon_p(tail)];
  52. }
  53. TailT tail;
  54. };
  55. //-----------------------------------------------------------------------------
  56. // distinct_directive class
  57. template <typename CharT, typename TailT>
  58. class distinct_directive
  59. {
  60. public:
  61. template<typename ParserT>
  62. struct result {
  63. typedef
  64. contiguous<
  65. sequence<
  66. ParserT,
  67. negated_empty_match_parser<
  68. TailT
  69. >
  70. >
  71. >
  72. type;
  73. };
  74. distinct_directive()
  75. : tail(chset<CharT>())
  76. {
  77. }
  78. explicit distinct_directive(CharT const* letters)
  79. : tail(chset_p(letters))
  80. {
  81. }
  82. explicit distinct_directive(parser<TailT> const & tail_)
  83. : tail(tail_.derived())
  84. {
  85. }
  86. template<typename ParserT>
  87. typename result<typename as_parser<ParserT>::type>::type
  88. operator[](ParserT const &subject) const
  89. {
  90. return
  91. lexeme_d[as_parser<ParserT>::convert(subject) >> ~epsilon_p(tail)];
  92. }
  93. TailT tail;
  94. };
  95. //-----------------------------------------------------------------------------
  96. // dynamic_distinct_parser class
  97. template <typename ScannerT>
  98. class dynamic_distinct_parser
  99. {
  100. public:
  101. typedef typename ScannerT::value_t char_t;
  102. typedef
  103. rule<
  104. typename no_actions_scanner<
  105. typename lexeme_scanner<ScannerT>::type
  106. >::type
  107. >
  108. tail_t;
  109. typedef
  110. contiguous<
  111. sequence<
  112. chseq<char_t const*>,
  113. negated_empty_match_parser<
  114. tail_t
  115. >
  116. >
  117. >
  118. result_t;
  119. dynamic_distinct_parser()
  120. : tail(nothing_p)
  121. {
  122. }
  123. template<typename ParserT>
  124. explicit dynamic_distinct_parser(parser<ParserT> const & tail_)
  125. : tail(tail_.derived())
  126. {
  127. }
  128. explicit dynamic_distinct_parser(char_t const* letters)
  129. : tail(chset_p(letters))
  130. {
  131. }
  132. result_t operator()(char_t const* str) const
  133. {
  134. return lexeme_d[chseq_p(str) >> ~epsilon_p(tail)];
  135. }
  136. tail_t tail;
  137. };
  138. //-----------------------------------------------------------------------------
  139. // dynamic_distinct_directive class
  140. template <typename ScannerT>
  141. class dynamic_distinct_directive
  142. {
  143. public:
  144. typedef typename ScannerT::value_t char_t;
  145. typedef
  146. rule<
  147. typename no_actions_scanner<
  148. typename lexeme_scanner<ScannerT>::type
  149. >::type
  150. >
  151. tail_t;
  152. template<typename ParserT>
  153. struct result {
  154. typedef
  155. contiguous<
  156. sequence<
  157. ParserT,
  158. negated_empty_match_parser<
  159. tail_t
  160. >
  161. >
  162. >
  163. type;
  164. };
  165. dynamic_distinct_directive()
  166. : tail(nothing_p)
  167. {
  168. }
  169. template<typename ParserT>
  170. explicit dynamic_distinct_directive(parser<ParserT> const & tail_)
  171. : tail(tail_.derived())
  172. {
  173. }
  174. explicit dynamic_distinct_directive(char_t const* letters)
  175. : tail(chset_p(letters))
  176. {
  177. }
  178. template<typename ParserT>
  179. typename result<typename as_parser<ParserT>::type>::type
  180. operator[](ParserT const &subject) const
  181. {
  182. return
  183. lexeme_d[as_parser<ParserT>::convert(subject) >> ~epsilon_p(tail)];
  184. }
  185. tail_t tail;
  186. };
  187. //-----------------------------------------------------------------------------
  188. BOOST_SPIRIT_CLASSIC_NAMESPACE_END
  189. } // namespace spirit
  190. } // namespace boost
  191. #endif // !defined(BOOST_SPIRIT_DISTINCT_HPP)