logical_newline_matcher.hpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // logical_newline_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_LOGICAL_NEWLINE_MATCHER_HPP_EAN_10_04_2005
  8. #define BOOST_XPRESSIVE_DETAIL_CORE_MATCHER_LOGICAL_NEWLINE_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/xpressive/detail/detail_fwd.hpp>
  14. #include <boost/xpressive/detail/core/quant_style.hpp>
  15. #include <boost/xpressive/detail/core/state.hpp>
  16. namespace boost { namespace xpressive { namespace detail
  17. {
  18. //////////////////////////////////////////////////////////////////////////
  19. // logical_newline_matcher
  20. //
  21. template<typename Traits>
  22. struct logical_newline_matcher
  23. : quant_style_variable_width
  24. {
  25. typedef typename Traits::char_type char_type;
  26. typedef typename Traits::char_class_type char_class_type;
  27. logical_newline_matcher(Traits const &tr)
  28. : newline_(lookup_classname(tr, "newline"))
  29. , nl_(tr.widen('\n'))
  30. , cr_(tr.widen('\r'))
  31. {
  32. }
  33. template<typename BidiIter, typename Next>
  34. bool match(match_state<BidiIter> &state, Next const &next) const
  35. {
  36. if(state.eos())
  37. {
  38. return false;
  39. }
  40. char_type ch = *state.cur_;
  41. if(traits_cast<Traits>(state).isctype(ch, this->newline_))
  42. {
  43. ++state.cur_;
  44. if(this->cr_ == ch && !state.eos() && this->nl_ == *state.cur_)
  45. {
  46. ++state.cur_;
  47. if(next.match(state))
  48. {
  49. return true;
  50. }
  51. --state.cur_;
  52. }
  53. else if(next.match(state))
  54. {
  55. return true;
  56. }
  57. --state.cur_;
  58. }
  59. return false;
  60. }
  61. char_class_type newline() const
  62. {
  63. return this->newline_;
  64. }
  65. private:
  66. char_class_type newline_;
  67. char_type nl_, cr_;
  68. };
  69. }}}
  70. #endif