peeker.hpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // peeker.hpp
  3. //
  4. // Copyright 2008 Eric Niebler. Distributed under the Boost
  5. // Software License, Version 1.0. (See accompanying file
  6. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. #ifndef BOOST_XPRESSIVE_DETAIL_CORE_PEEKER_HPP_EAN_10_04_2005
  8. #define BOOST_XPRESSIVE_DETAIL_CORE_PEEKER_HPP_EAN_10_04_2005
  9. // MS compatible compilers support #pragma once
  10. #if defined(_MSC_VER)
  11. # pragma once
  12. #endif
  13. #include <string>
  14. #include <typeinfo>
  15. #include <boost/assert.hpp>
  16. #include <boost/mpl/bool.hpp>
  17. #include <boost/mpl/assert.hpp>
  18. #include <boost/mpl/size_t.hpp>
  19. #include <boost/mpl/equal_to.hpp>
  20. #include <boost/utility/enable_if.hpp>
  21. #include <boost/xpressive/detail/detail_fwd.hpp>
  22. #include <boost/xpressive/detail/core/matchers.hpp>
  23. #include <boost/xpressive/detail/utility/hash_peek_bitset.hpp>
  24. #include <boost/xpressive/detail/utility/never_true.hpp>
  25. #include <boost/xpressive/detail/utility/algorithm.hpp>
  26. namespace boost { namespace xpressive { namespace detail
  27. {
  28. ///////////////////////////////////////////////////////////////////////////////
  29. // peeker_string
  30. //
  31. template<typename Char>
  32. struct peeker_string
  33. {
  34. Char const *begin_;
  35. Char const *end_;
  36. bool icase_;
  37. };
  38. ///////////////////////////////////////////////////////////////////////////////
  39. // char_sink
  40. //
  41. template<typename Traits, bool ICase>
  42. struct char_sink
  43. {
  44. typedef typename Traits::char_type char_type;
  45. char_sink(hash_peek_bitset<char_type> &bset, Traits const &tr)
  46. : bset_(bset)
  47. , traits_(tr)
  48. {}
  49. void operator()(char_type ch) const
  50. {
  51. this->bset_.set_char(ch, ICase, this->traits_);
  52. }
  53. hash_peek_bitset<char_type> &bset_;
  54. Traits const &traits_;
  55. private:
  56. char_sink &operator =(char_sink const &);
  57. };
  58. ///////////////////////////////////////////////////////////////////////////////
  59. // xpression_peeker
  60. //
  61. template<typename Char>
  62. struct xpression_peeker
  63. {
  64. template<typename Traits>
  65. xpression_peeker(hash_peek_bitset<Char> &bset, Traits const &tr, bool has_backrefs = false)
  66. : bset_(bset)
  67. , str_()
  68. , line_start_(false)
  69. , traits_(0)
  70. , traits_type_(0)
  71. , leading_simple_repeat_(0)
  72. , has_backrefs_(has_backrefs)
  73. {
  74. this->set_traits(tr);
  75. }
  76. ///////////////////////////////////////////////////////////////////////////////
  77. // accessors
  78. peeker_string<Char> const &get_string() const
  79. {
  80. return this->str_;
  81. }
  82. bool line_start() const
  83. {
  84. return this->line_start_;
  85. }
  86. bool leading_simple_repeat() const
  87. {
  88. return 0 < this->leading_simple_repeat_;
  89. }
  90. hash_peek_bitset<Char> const &bitset() const
  91. {
  92. return this->bset_;
  93. }
  94. ///////////////////////////////////////////////////////////////////////////////
  95. // modifiers
  96. void fail()
  97. {
  98. this->bset_.set_all();
  99. }
  100. template<typename Matcher>
  101. mpl::false_ accept(Matcher const &)
  102. {
  103. this->fail();
  104. return mpl::false_();
  105. }
  106. mpl::true_ accept(mark_begin_matcher const &)
  107. {
  108. if(this->has_backrefs_)
  109. {
  110. --this->leading_simple_repeat_;
  111. }
  112. return mpl::true_();
  113. }
  114. mpl::true_ accept(repeat_begin_matcher const &)
  115. {
  116. --this->leading_simple_repeat_;
  117. return mpl::true_();
  118. }
  119. template<typename Traits>
  120. mpl::true_ accept(assert_bol_matcher<Traits> const &)
  121. {
  122. this->line_start_ = true;
  123. return mpl::true_();
  124. }
  125. template<typename Traits, typename ICase>
  126. mpl::false_ accept(literal_matcher<Traits, ICase, mpl::false_> const &xpr)
  127. {
  128. this->bset_.set_char(xpr.ch_, ICase(), this->get_traits_<Traits>());
  129. return mpl::false_();
  130. }
  131. template<typename Traits, typename ICase>
  132. mpl::false_ accept(string_matcher<Traits, ICase> const &xpr)
  133. {
  134. this->bset_.set_char(xpr.str_[0], ICase(), this->get_traits_<Traits>());
  135. this->str_.begin_ = detail::data_begin(xpr.str_);
  136. this->str_.end_ = detail::data_end(xpr.str_);
  137. this->str_.icase_ = ICase::value;
  138. return mpl::false_();
  139. }
  140. template<typename Alternates, typename Traits>
  141. mpl::false_ accept(alternate_matcher<Alternates, Traits> const &xpr)
  142. {
  143. BOOST_ASSERT(0 != xpr.bset_.count());
  144. this->bset_.set_bitset(xpr.bset_);
  145. return mpl::false_();
  146. }
  147. template<typename Matcher, typename Traits, typename ICase>
  148. mpl::false_ accept(attr_matcher<Matcher, Traits, ICase> const &xpr)
  149. {
  150. xpr.sym_.peek(char_sink<Traits, ICase::value>(this->bset_, this->get_traits_<Traits>()));
  151. return mpl::false_();
  152. }
  153. template<typename Xpr, typename Greedy>
  154. mpl::false_ accept(optional_matcher<Xpr, Greedy> const &)
  155. {
  156. this->fail(); // a union of xpr and next
  157. return mpl::false_();
  158. }
  159. template<typename Xpr, typename Greedy>
  160. mpl::false_ accept(optional_mark_matcher<Xpr, Greedy> const &)
  161. {
  162. this->fail(); // a union of xpr and next
  163. return mpl::false_();
  164. }
  165. //template<typename Xpr, typename Greedy>
  166. //mpl::true_ accept(optional_matcher<Xpr, Greedy> const &xpr)
  167. //{
  168. // xpr.xpr_.peek(*this); // a union of xpr and next
  169. // return mpl::true_();
  170. //}
  171. //template<typename Xpr, typename Greedy>
  172. //mpl::true_ accept(optional_mark_matcher<Xpr, Greedy> const &xpr)
  173. //{
  174. // xpr.xpr_.peek(*this); // a union of xpr and next
  175. // return mpl::true_();
  176. //}
  177. template<typename Traits>
  178. mpl::false_ accept(posix_charset_matcher<Traits> const &xpr)
  179. {
  180. this->bset_.set_class(xpr.mask_, xpr.not_, this->get_traits_<Traits>());
  181. return mpl::false_();
  182. }
  183. template<typename ICase, typename Traits>
  184. typename enable_if<is_narrow_char<typename Traits::char_type>, mpl::false_>::type
  185. accept(charset_matcher<Traits, ICase, basic_chset<Char> > const &xpr)
  186. {
  187. BOOST_ASSERT(0 != xpr.charset_.base().count());
  188. this->bset_.set_charset(xpr.charset_, ICase());
  189. return mpl::false_();
  190. }
  191. template<typename Traits, typename ICase>
  192. mpl::false_ accept(range_matcher<Traits, ICase> const &xpr)
  193. {
  194. this->bset_.set_range(xpr.ch_min_, xpr.ch_max_, xpr.not_, ICase(), this->get_traits_<Traits>());
  195. return mpl::false_();
  196. }
  197. template<typename Xpr, typename Greedy>
  198. mpl::false_ accept(simple_repeat_matcher<Xpr, Greedy> const &xpr)
  199. {
  200. if(Greedy() && 1U == xpr.width_)
  201. {
  202. ++this->leading_simple_repeat_;
  203. xpr.leading_ = this->leading_simple_repeat();
  204. }
  205. 0 != xpr.min_ ? xpr.xpr_.peek(*this) : this->fail(); // could be a union of xpr and next
  206. return mpl::false_();
  207. }
  208. template<typename Xpr>
  209. mpl::false_ accept(keeper_matcher<Xpr> const &xpr)
  210. {
  211. xpr.xpr_.peek(*this);
  212. return mpl::false_();
  213. }
  214. template<typename Traits>
  215. void set_traits(Traits const &tr)
  216. {
  217. if(0 == this->traits_)
  218. {
  219. this->traits_ = &tr;
  220. this->traits_type_ = &typeid(Traits);
  221. }
  222. else if(*this->traits_type_ != typeid(Traits) || this->get_traits_<Traits>() != tr)
  223. {
  224. this->fail(); // traits mis-match! set all and bail
  225. }
  226. }
  227. private:
  228. xpression_peeker(xpression_peeker const &);
  229. xpression_peeker &operator =(xpression_peeker const &);
  230. template<typename Traits>
  231. Traits const &get_traits_() const
  232. {
  233. BOOST_ASSERT(!!(*this->traits_type_ == typeid(Traits)));
  234. return *static_cast<Traits const *>(this->traits_);
  235. }
  236. hash_peek_bitset<Char> &bset_;
  237. peeker_string<Char> str_;
  238. bool str_icase_;
  239. bool line_start_;
  240. void const *traits_;
  241. std::type_info const *traits_type_;
  242. int leading_simple_repeat_;
  243. bool has_backrefs_;
  244. };
  245. }}} // namespace boost::xpressive::detail
  246. #endif