attr_matcher.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // attr_matcher.hpp
  3. //
  4. // Copyright 2008 Eric Niebler.
  5. // Copyright 2008 David Jenkins.
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See
  8. // accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. #ifndef BOOST_XPRESSIVE_DETAIL_CORE_MATCHER_ATTR_MATCHER_HPP_EAN_06_09_2007
  11. #define BOOST_XPRESSIVE_DETAIL_CORE_MATCHER_ATTR_MATCHER_HPP_EAN_06_09_2007
  12. // MS compatible compilers support #pragma once
  13. #if defined(_MSC_VER)
  14. # pragma once
  15. #endif
  16. #include <boost/xpressive/detail/detail_fwd.hpp>
  17. #include <boost/xpressive/detail/core/quant_style.hpp>
  18. #include <boost/xpressive/detail/core/state.hpp>
  19. #include <boost/xpressive/detail/utility/symbols.hpp>
  20. namespace boost { namespace xpressive { namespace detail
  21. {
  22. ///////////////////////////////////////////////////////////////////////////////
  23. // char_translate
  24. //
  25. template<typename Traits, bool ICase>
  26. struct char_translate
  27. {
  28. typedef typename Traits::char_type char_type;
  29. Traits const &traits_;
  30. explicit char_translate(Traits const &tr)
  31. : traits_(tr)
  32. {}
  33. char_type operator ()(char_type ch1) const
  34. {
  35. return this->traits_.translate(ch1);
  36. }
  37. private:
  38. char_translate &operator =(char_translate const &);
  39. };
  40. ///////////////////////////////////////////////////////////////////////////////
  41. // char_translate
  42. //
  43. template<typename Traits>
  44. struct char_translate<Traits, true>
  45. {
  46. typedef typename Traits::char_type char_type;
  47. Traits const &traits_;
  48. explicit char_translate(Traits const &tr)
  49. : traits_(tr)
  50. {}
  51. char_type operator ()(char_type ch1) const
  52. {
  53. return this->traits_.translate_nocase(ch1);
  54. }
  55. private:
  56. char_translate &operator =(char_translate const &);
  57. };
  58. ///////////////////////////////////////////////////////////////////////////////
  59. // attr_matcher
  60. // Note: the Matcher is a std::map
  61. template<typename Matcher, typename Traits, typename ICase>
  62. struct attr_matcher
  63. : quant_style<quant_none, 0, false>
  64. {
  65. typedef typename Matcher::value_type::second_type const* result_type;
  66. attr_matcher(int slot, Matcher const &matcher, Traits const& tr)
  67. : slot_(slot-1)
  68. {
  69. char_translate<Traits, ICase::value> trans(tr);
  70. this->sym_.load(matcher, trans);
  71. }
  72. template<typename BidiIter, typename Next>
  73. bool match(match_state<BidiIter> &state, Next const &next) const
  74. {
  75. BidiIter tmp = state.cur_;
  76. char_translate<Traits, ICase::value> trans(traits_cast<Traits>(state));
  77. result_type const &result = this->sym_(state.cur_, state.end_, trans);
  78. if(result)
  79. {
  80. void const *old_slot = state.attr_context_.attr_slots_[this->slot_];
  81. state.attr_context_.attr_slots_[this->slot_] = &*result;
  82. if(next.match(state))
  83. {
  84. return true;
  85. }
  86. state.attr_context_.attr_slots_[this->slot_] = old_slot;
  87. }
  88. state.cur_ = tmp;
  89. return false;
  90. }
  91. int slot_;
  92. boost::xpressive::detail::symbols<Matcher> sym_;
  93. };
  94. }}}
  95. #endif