optional_matcher.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // optional_matcher.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_MATCHER_OPTIONAL_MATCHER_HPP_EAN_10_04_2005
  8. #define BOOST_XPRESSIVE_DETAIL_CORE_MATCHER_OPTIONAL_MATCHER_HPP_EAN_10_04_2005
  9. // MS compatible compilers support #pragma once
  10. #if defined(_MSC_VER)
  11. # pragma once
  12. #endif
  13. #include <boost/mpl/bool.hpp>
  14. #include <boost/xpressive/detail/detail_fwd.hpp>
  15. #include <boost/xpressive/detail/core/quant_style.hpp>
  16. #include <boost/xpressive/detail/core/state.hpp>
  17. namespace boost { namespace xpressive { namespace detail
  18. {
  19. ///////////////////////////////////////////////////////////////////////////////
  20. // optional_matcher
  21. template<typename Xpr, typename Greedy>
  22. struct optional_matcher
  23. : quant_style<quant_variable_width, unknown_width::value, Xpr::pure>
  24. {
  25. Xpr xpr_;
  26. explicit optional_matcher(Xpr const &xpr)
  27. : xpr_(xpr)
  28. {
  29. }
  30. template<typename BidiIter, typename Next>
  31. bool match(match_state<BidiIter> &state, Next const &next) const
  32. {
  33. return this->match_(state, next, Greedy());
  34. }
  35. private:
  36. template<typename BidiIter, typename Next>
  37. bool match_(match_state<BidiIter> &state, Next const &next, mpl::true_) const // Greedy
  38. {
  39. return this->xpr_.BOOST_NESTED_TEMPLATE push_match<Next>(state)
  40. || next.match(state);
  41. }
  42. template<typename BidiIter, typename Next>
  43. bool match_(match_state<BidiIter> &state, Next const &next, mpl::false_) const // Non-greedy
  44. {
  45. return next.match(state)
  46. || this->xpr_.BOOST_NESTED_TEMPLATE push_match<Next>(state);
  47. }
  48. optional_matcher &operator =(optional_matcher const &);
  49. };
  50. ///////////////////////////////////////////////////////////////////////////////
  51. // optional_mark_matcher
  52. template<typename BidiIter, typename Next>
  53. inline bool match_next(match_state<BidiIter> &state, Next const &next, int mark_number)
  54. {
  55. sub_match_impl<BidiIter> &br = state.sub_match(mark_number);
  56. bool old_matched = br.matched;
  57. br.matched = false;
  58. if(next.match(state))
  59. {
  60. return true;
  61. }
  62. br.matched = old_matched;
  63. return false;
  64. }
  65. ///////////////////////////////////////////////////////////////////////////////
  66. // optional_mark_matcher
  67. template<typename Xpr, typename Greedy>
  68. struct optional_mark_matcher
  69. : quant_style<quant_variable_width, unknown_width::value, Xpr::pure>
  70. {
  71. Xpr xpr_;
  72. int mark_number_;
  73. explicit optional_mark_matcher(Xpr const &xpr, int mark_number)
  74. : xpr_(xpr)
  75. , mark_number_(mark_number)
  76. {
  77. }
  78. template<typename BidiIter, typename Next>
  79. bool match(match_state<BidiIter> &state, Next const &next) const
  80. {
  81. return this->match_(state, next, Greedy());
  82. }
  83. private:
  84. template<typename BidiIter, typename Next>
  85. bool match_(match_state<BidiIter> &state, Next const &next, mpl::true_) const // Greedy
  86. {
  87. return this->xpr_.BOOST_NESTED_TEMPLATE push_match<Next>(state)
  88. || match_next(state, next, this->mark_number_);
  89. }
  90. template<typename BidiIter, typename Next>
  91. bool match_(match_state<BidiIter> &state, Next const &next, mpl::false_) const // Non-greedy
  92. {
  93. return match_next(state, next, this->mark_number_)
  94. || this->xpr_.BOOST_NESTED_TEMPLATE push_match<Next>(state);
  95. }
  96. optional_mark_matcher &operator =(optional_mark_matcher const &);
  97. };
  98. }}}
  99. #endif