⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 iterators.hpp

📁 著名的Parser库Spirit在VC6上的Port
💻 HPP
字号:
/*=============================================================================
    Iterators

    Spirit V1.3.1
    Copyright (c) 2001, Joel de Guzman

    This software is provided 'as-is', without any express or implied
    warranty. In no event will the copyright holder be held liable for
    any damages arising from the use of this software.

    Permission is granted to anyone to use this software for any purpose,
    including commercial applications, and to alter it and redistribute
    it freely, subject to the following restrictions:

    1.  The origin of this software must not be misrepresented; you must
        not claim that you wrote the original software. If you use this
        software in a product, an acknowledgment in the product documentation
        would be appreciated but is not required.

    2.  Altered source versions must be plainly marked as such, and must
        not be misrepresented as being the original software.

    3.  This notice may not be removed or altered from any source
        distribution.

    Acknowledgements:

        Special thanks to Dan Nuffer, John (EBo) David, Chris Uzdavinis,
        and Doug Gregor. These people are most instrumental in steering
        Spirit in the right direction.

        Special thanks also to people who have contributed to the code base
        and sample code, ported Spirit to various platforms and compilers,
        gave suggestions, reported and provided bug fixes. Alexander
        Hirner, Andy Elvey, Bogdan Kushnir, Brett Calcott, Bruce Florman,
        Changzhe Han, Colin McPhail, Hakki Dogusan, Jan Bares, Joseph
        Smith, Martijn W. van der Lee, Raghavendra Satish, Remi Delcos, Tom
        Spilman, Vladimir Prus, W. Scott Dillman, David A. Greene, Bob
        Bailey, Hartmut Kaiser.

        Finally special thanks also to people who gave feedback and
        valuable comments, particularly members of Spirit's Source Forge
        mailing list and boost.org.

    URL: http://spirit.sourceforge.net/

=============================================================================*/
#ifndef SPIRIT_ITERATORS_HPP
#define SPIRIT_ITERATORS_HPP

///////////////////////////////////////////////////////////////////////////////

#include "boost/spirit/MSVC/ps_helper.hpp"

///////////////////////////////////////////////////////////////////////////////

namespace spirit {

//////////////////////////////////
namespace impl {

    template <typename IteratorTag> struct iterator_kind
    { /* At least a forward iterator is required */ };

    template <> struct iterator_kind<std::forward_iterator_tag>
    { typedef std::forward_iterator_tag T; };

    template <> struct iterator_kind<std::bidirectional_iterator_tag>
    { typedef std::forward_iterator_tag T; };

    template <> struct iterator_kind<std::random_access_iterator_tag>
    { typedef std::forward_iterator_tag T; };

} // namespace impl

///////////////////////////////////////////////////////////////////////////////
//
//  scanner class
//
//      The scanner wraps an iterator and a skip-parser. The skipper
//      handles the skipping of characters in between words that form
//      sentences in a language. The scanner extracts data from the
//      input stream and filters this data to exclude skip- characters
//      as recognized by the skipper. The scanner invokes the skipper
//      filter when tasked to scan the next character from the input.
//      The scanner conforms to a standard STL forward iterator.
//
//      The scanner is not a full-blown lexical analyzer. It does not
//      extract tokens such as reserved words and operators. Nor does it
//      extract numbers and literal strings
//
///////////////////////////////////////////////////////////////////////////////
template <typename IteratorT = char const*,
          typename MatchTraitsT = match_traits, 
	  typename SkipT = skipper<IteratorT, MatchTraitsT>
	 >  
class scanner {

public:

    typedef IteratorT   iterator_type;
    typedef IteratorT  iterator_without_scanner;
    typedef SkipT       skip_type;

    typedef scanner<IteratorT, MatchTraitsT, SkipT> self_; 

#if defined (BOOST_MSVC)
    typedef spirit::iterator_traits<IteratorT> iterator_traits_;
#else
    typedef std::iterator_traits<IteratorT> iterator_traits_;
#endif

    typedef typename iterator_traits_::iterator_category category;
    typedef typename impl::iterator_kind<category>::T iterator_category;
    typedef typename iterator_traits_::difference_type   difference_type;
    typedef typename iterator_traits_::pointer           pointer;
    typedef typename iterator_traits_::reference         reference;
    typedef typename iterator_traits_::value_type        value_type;

    scanner(iterator_type const& iter_, SkipT const* skip_);

    self_& operator=(iterator_type const& iter_);

    IteratorT           iterator() const;
    value_type          operator*() const;
    pointer             operator->() const;
    self_&            operator++();
    self_             operator++(int);

    bool                operator==(self_ const& other) const;
    bool                operator!=(self_ const& other) const;

private:

    void                next() const;
    IteratorT           get() const;

    SkipT const*        skip;
    mutable IteratorT   iter;
    mutable bool        dirty;
};

///////////////////////////////////////////////////////////////////////////////
//
//  nocase_iterator class
//
//      The nocase_iterator class adapts an iterator and converts
//      all upper-case characters from the input to lower-case. The
//      nocase_iterator conforms to a standard STL forward iterator.
//
///////////////////////////////////////////////////////////////////////////////
template <typename IteratorT>
class nocase_iterator ;

namespace impl
{
    template <int  i> 	 
    struct nocase_scanner_stripper 
    {
       template<typename IteratorT> 
       struct local {
         typedef  nocase_iterator<IteratorT> stripped_scanner;
       };
    };

    template <> struct nocase_scanner_stripper<1>
    {
       template<typename ScannerT> 
       struct local {
         typedef typename 
            nocase_iterator<typename ScannerT::iterator_type> stripped_scanner;
       };
    };    
            
           
    template<typename  IteratorT, typename MatchTraitsT ,typename SkipT>
    selector1 
    nocase_scanner_stripper_helper(scanner<IteratorT, MatchTraitsT, SkipT> );
      
    selector2 nocase_scanner_stripper_helper(...);

} //end namespace impl

         
template <typename IteratorT>
class nocase_iterator {

public:

    typedef IteratorT   iterator_type;

    static IteratorT t();
    typedef typename 
    impl::nocase_scanner_stripper< 
            sizeof(impl::nocase_scanner_stripper_helper(t()))
	    >:: 
    template local<IteratorT>::stripped_scanner iterator_without_scanner; 

#if defined (BOOST_MSVC)
    typedef spirit::iterator_traits<IteratorT> iterator_traits_;
#else
    typedef std::iterator_traits<IteratorT> iterator_traits_;
#endif

    typedef typename impl::iterator_kind
        <typename iterator_traits_::iterator_category>::T iterator_category;

    typedef typename iterator_traits_::difference_type   difference_type;
    typedef typename iterator_traits_::pointer           pointer;
    typedef typename iterator_traits_::reference         reference;
    typedef typename iterator_traits_::value_type        value_type;

    nocase_iterator(IteratorT const& iter_);

    template <typename IteratorTB>
    nocase_iterator&    operator=(nocase_iterator<IteratorTB> const& iter_)
    {
      iter = iter_.iterator();
      return *this;
    }   
        
    nocase_iterator&    operator=(IteratorT const& iter_);
    IteratorT           iterator() const;

    reference           operator*();
    pointer             operator->() const;
    nocase_iterator&    operator++();
    nocase_iterator     operator++(int);

    bool                operator==(nocase_iterator const& other) const;
    bool                operator!=(nocase_iterator const& other) const;

private:

    iterator_type       iter;
    value_type          next;
};


///////////////////////////////////////////////////////////////////////////////
//
//  strip_scanner class and specializations
//
//      Helper class to strip the scanner wrapper off an iterator.
//      This utility is used when we want to perform character
//      level parsing (see scanner above).
//
///////////////////////////////////////////////////////////////////////////////
namespace impl {

    template <int  i> 	 
    struct stripped_scanner_iterator 
    {
       template<typename T> 
       struct local {
        typedef T iterator_type;
        typedef selector2 selector_type;
       };
    };

    template <> struct stripped_scanner_iterator<1>
    {
       template<typename T> 
       struct local {
         typedef typename T::iterator_without_scanner iterator_type;
         typedef selector1 selector_type;
       };
    };    
            
           
    template<typename  IteratorT, typename MatchTraitsT , typename SkipT>
    selector1 strip_scanner_helper(scanner<IteratorT, MatchTraitsT, SkipT> );
      
    template <typename IteratorT, typename MatchTraitsT, typename SkipT>
    selector1 strip_scanner_helper(
	 nocase_iterator<scanner<IteratorT, MatchTraitsT, SkipT> > );

    selector2 strip_scanner_helper(...);


    template <typename T>
    struct strip_scanner {
        static T t();
	typedef typename 
		 stripped_scanner_iterator<sizeof(strip_scanner_helper(t()))>:: 
		 template local<T>::iterator_type iterator_type; 

	template <typename IteratorT, typename MatchTraitsT, typename SkipT>
        static  iterator_type 
	get_helper(selector1 s, 
		  scanner<IteratorT, MatchTraitsT, SkipT> const& scanner_)
	{
           return scanner_.iterator();
	}

        template <typename IteratorT, typename MatchTraitsT, typename SkipT>
        static iterator_type  
        get_helper(selector1 s,
		   nocase_iterator<scanner<IteratorT,MatchTraitsT, SkipT> >
		   const& nocase_iter)
        {
           return strip_scanner<scanner<IteratorT, SkipT> >::
            get_helper(s,nocase_iter.iterator());
        }

	template <typename U>
        static iterator_type get_helper(selector2 s, U ptr) 
	{
           return ptr;
	}

	static iterator_type get(T const& t_ )
	{
	  typedef typename 
		 stripped_scanner_iterator<sizeof(strip_scanner_helper(t()))>:: 
		 template local<T>::selector_type selector_type; 
	 return  get_helper(selector_type(), t_);
	} 
            

    };

///////////////////////////////////////////////////////////////////////////////
//
//
//      Helper class to strip the nocase wrapper off an iterator.
//      This utility is used when we want to perform character
//      level parsing (see scanner above).
//
///////////////////////////////////////////////////////////////////////////////

    template <int  i> 	 
    struct nocase_stripper 
    {
       template<typename IteratorT> 
       struct local {
         typedef  IteratorT stripped_nocase_iterator;
	 typedef selector2 selector_type; 
       };
    };
    ////////////////////////////////////////////////////// 
    template <> struct nocase_stripper<1>
    {
       template<typename NoCaseIteratorT> 
       struct local {
         typedef typename NoCaseIteratorT::iterator_type 
		           stripped_nocase_iterator;
	 typedef selector1 selector_type; 
       };
    };    
            
    ////////////////////////////////////////////////////// 
    template<typename  IteratorT>
    selector1 nocase_stripper_helper(nocase_iterator<IteratorT> );
      
    selector2 nocase_stripper_helper(...);

    template <typename T>
    struct strip_nocase {
        
	static T t();     
	typedef typename 
		 nocase_stripper<sizeof(nocase_stripper_helper(t()))>:: 
		 template local<T>::stripped_nocase_iterator iterator_type; 


        template<class U>
        static iterator_type get_helper(selector2 s, U ptr) { return ptr;}

	template<class IteratorT>
        static iterator_type get_helper(selector1 s,
			        nocase_iterator<IteratorT> const & nocase) 
	{
           return nocase.iterator();
	}    

	static iterator_type get(T& const t_)
	{          
	  typedef typename 
		 stripped_scanner_iterator<sizeof(strip_scanner_helper(t()))>:: 
		 template local<T>::selector_type selector_type; 
                 get_helper(selector_type(), t_);
	}   
         		 
    };

} // namespace impl
///////////////////////////////////////////////////////////////////////////////
}   //  namespace Spirit

#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -