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 8 #ifndef BOOST_XPRESSIVE_DETAIL_CORE_MATCHER_LOGICAL_NEWLINE_MATCHER_HPP_EAN_10_04_2005 9 #define BOOST_XPRESSIVE_DETAIL_CORE_MATCHER_LOGICAL_NEWLINE_MATCHER_HPP_EAN_10_04_2005 10 11 // MS compatible compilers support #pragma once 12 #if defined(_MSC_VER) 13 # pragma once 14 #endif 15 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 20 namespace boost { namespace xpressive { namespace detail 21 { 22 23 ////////////////////////////////////////////////////////////////////////// 24 // logical_newline_matcher 25 // 26 template<typename Traits> 27 struct logical_newline_matcher 28 : quant_style_variable_width 29 { 30 typedef typename Traits::char_type char_type; 31 typedef typename Traits::char_class_type char_class_type; 32 logical_newline_matcherboost::xpressive::detail::logical_newline_matcher33 logical_newline_matcher(Traits const &tr) 34 : newline_(lookup_classname(tr, "newline")) 35 , nl_(tr.widen('\n')) 36 , cr_(tr.widen('\r')) 37 { 38 } 39 40 template<typename BidiIter, typename Next> matchboost::xpressive::detail::logical_newline_matcher41 bool match(match_state<BidiIter> &state, Next const &next) const 42 { 43 if(state.eos()) 44 { 45 return false; 46 } 47 48 char_type ch = *state.cur_; 49 if(traits_cast<Traits>(state).isctype(ch, this->newline_)) 50 { 51 ++state.cur_; 52 if(this->cr_ == ch && !state.eos() && this->nl_ == *state.cur_) 53 { 54 ++state.cur_; 55 if(next.match(state)) 56 { 57 return true; 58 } 59 --state.cur_; 60 } 61 else if(next.match(state)) 62 { 63 return true; 64 } 65 66 --state.cur_; 67 } 68 return false; 69 } 70 newlineboost::xpressive::detail::logical_newline_matcher71 char_class_type newline() const 72 { 73 return this->newline_; 74 } 75 76 private: 77 char_class_type newline_; 78 char_type nl_, cr_; 79 }; 80 81 }}} 82 83 #endif 84