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

📄 directives.hpp

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

    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_DIRECTIVES_HPP
#define SPIRIT_DIRECTIVES_HPP

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

#include "boost/spirit/MSVC/composite.hpp"
#include "boost/spirit/MSVC/parser.hpp"
#include "boost/spirit/MSVC/primitives.hpp"
#include "boost/spirit/MSVC/operators.hpp"

///////////////////////////////////////////////////////////////////////////////
namespace spirit {

///////////////////////////////////////////////////////////////////////////////
template <typename A, typename B>   struct alternative;
///////////////////////////////////////////////////////////////////////////////
//
//  contiguous class
//
//      This class handles the directive:
//
//          lexeme[p]
//
//      where p is a parser. Its function parse() strips the scanner, which
//      skips white spaces, off its iterator before delegating the actual
//      parsing to its subject. The lexeme_parser_gen with its single instance
//      lexeme is a generator that creates a contiguous object given a parser.
//
///////////////////////////////////////////////////////////////////////////////
template <typename S>
struct contiguous
:   public unary<S>,
    public parser<contiguous<S> > {

    contiguous(S const& a);

    template <typename IteratorT>
    match
    parse(IteratorT& first, IteratorT const& last) const
    {
       typedef typename impl::strip_scanner<IteratorT>
           ::iterator_type plain_iter;

       plain_iter  i1 = impl::strip_scanner<IteratorT>::get(first);
       plain_iter  i2 = impl::strip_scanner<IteratorT>::get(last);
       match       hit = this->subject().parse(i1, i2);

       if (hit)
          first = i1;
       return hit;
    }

};

//////////////////////////////////
struct lexeme_parser_gen {

    template <typename S>
    contiguous<S>
    operator[](parser<S> const& subject) const
    {
        //  Borland 5.5 reports an internal compiler
        //  error if this is not defined here.
        return contiguous<S>(subject.derived());
    }
};

const lexeme_parser_gen lexeme = lexeme_parser_gen();

///////////////////////////////////////////////////////////////////////////////
//
//  inhibit_case class
//
//      This class handles the directive;
//
//      nocase[p]
//
//      where p is a parser. Its function parse() wraps the iterator in
//      a nocase_iterator that converts characters from the input to
//      lowercase (see nocase_iterator in <iterators.hpp>) before delegating
//      the actual parsing to its subject. The inhibit_case_parser_gen is
//      a generator that creates an inhibit_case object given a parser.
//      Overloads are also provided for literal strings and characters.
//      Examples:
//
//          nocase['x']
//          nocase["begin"].
//
//      It is important to note that only the input is converted to lower
//      case. The parser will fail if it expects any upper case characters.
//      from the input. Example: nocase['X'] will never succeed because
//      it expects an upper case 'X' that the nocase_iterator will never
//      produce.
//
///////////////////////////////////////////////////////////////////////////////
template <typename S>
struct inhibit_case
:   public unary<S>,
    public parser<inhibit_case<S> > {

    inhibit_case(S const& a);

    template <typename IteratorT>
    match
    parse(IteratorT& first, IteratorT const& last) const
    {
      nocase_iterator<IteratorT>  i1(first);
      nocase_iterator<IteratorT>  i2(last);
      match hit = this->subject().parse(i1, i2);

      if (hit)
         first = i1.iterator();
      return hit;
    }

};

//////////////////////////////////
struct inhibit_case_parser_gen {

    template <typename S>
    inhibit_case<S>
    operator[](parser<S> const& subject) const
    {
        //  Borland 5.5 reports an internal compiler
        //  error if this is not defined here.
        return inhibit_case<S>(subject.derived());
    }

    inhibit_case<chlit<char> >
    operator[](char ch) const;

    inhibit_case<chlit<wchar_t> >
    operator[](wchar_t ch) const;

    inhibit_case<strlit<cstring<char> > >
    operator[](char const* str) const;

    inhibit_case<strlit<cstring<wchar_t> > >
    operator[](wchar_t const* str) const;
};

const inhibit_case_parser_gen nocase = inhibit_case_parser_gen();

///////////////////////////////////////////////////////////////////////////////
//
//  longest_alternative class
//
//      This class handles the directive:
//
//          longest[a | b | c | ... ]
//
//      where a, b and c are parsers. All alternatives (which does short-
//      circuit evaluation) are converted to longest_alternative(s) where
//      the alternative that matches the longest part of the input (maximal
//      munch) wins.
//
//      The class to_longest_alternative and its specializations extract
//      all the alternative(s) and converts these to longest_alternative(s).
//      The class longest_parser_gen with its sole instance longest is a
//      generator that creates a longest_alternative object given a parser
//      composite comprising of alternatives.
//
///////////////////////////////////////////////////////////////////////////////

template <typename A, typename B>
struct longest_alternative
:   public binary<A, B>,
    public parser<longest_alternative<A, B> > {

    longest_alternative(A const& a, B const& b);

    template <typename IteratorT>
    match
    parse(IteratorT& first, IteratorT const& last) const
    {
       IteratorT   ls = first;
       IteratorT   rs = first;
       match       l = this->left().parse(ls, last);
       match       r = this->right().parse(rs, last);

       if (l || r)
       {
         bool less = l.length() < r.length();
         first = less ? rs : ls;
         return less ? r : l;
       }

       return match();
    }

};


///////////////////////////////////////////////////////////////////////////
namespace impl
{
//from spirit 1.1
// various workarounds to support longest directives
	template <typename T> struct Select_ToLongest;

	struct True_  { enum { Value = 1 }; };
        struct False_ { enum { Value = 0 }; };

	template <typename T>
	struct ToLongest {

		typedef typename Select_ToLongest<T>::RT	RT;

		static RT Convert(T const& a);

		typedef typename Select_ToLongest<T>::XT	XT;
		typedef typename Select_ToLongest<T>::YT	YT;
	};

	//////////////////////////////////
	template <typename T>
	struct ToLongest_Generic {

		typedef T const&	RT;
		typedef T		XT;
		typedef False_		YT;
	};

	template <typename T>
	inline T const&
	ToLongestConvert(T const& a, False_) { return a; }

	//////////////////////////////////
	template <typename T>
	struct ToLongest_Alternative {

		typedef typename ToLongest<typename T::TypeA>::XT	AT;
		typedef typename ToLongest<typename T::TypeB>::XT	BT;
		typedef longest_alternative<AT, BT>	RT;

		typedef RT					XT;
		typedef True_				YT;
	};

	template <typename A, typename B>
	inline typename ToLongest<alternative<A, B> >::RT
	ToLongestConvert(alternative<A, B> const& alt, True_)
	{
		typedef typename ToLongest<alternative<A, B> >::RT RT;
		return RT(ToLongest<A>::Convert(alt.left()), ToLongest<B>::Convert(alt.right()));
	}

	//////////////////////////////////
	template <typename T>
	inline typename ToLongest<T>::RT
	ToLongest<T>::Convert (T const& a)
	{
		return ToLongestConvert(a, ToLongest<T>::YT());
	}

	//////////////////////////////////
	template <typename T>
	struct Select_ToLongest {

		typedef typename impl::IF< impl::is_alternative<T>::value
							, ToLongest_Alternative<T>
							, ToLongest_Generic<T>
							>::RET Type;

		typedef typename Type::RT RT;
		typedef typename Type::XT XT;
		typedef typename Type::YT YT;
	};

} //end namespace impl


//////////////////////////////////
struct longest_parser_gen {

    template <typename A, typename B>
    //typename longest_alternative<A,B>
    typename impl::ToLongest<alternative<A,B> >::RT  
    operator[](alternative<A, B> const& alt) const
    {
        //  Borland 5.5 reports an internal compiler
        //  error if this is not defined here.
	return impl::ToLongest<alternative<A, B> >::Convert(alt);
    }
};

const longest_parser_gen longest = longest_parser_gen();

///////////////////////////////////////////////////////////////////////////////
//
//  shortest_alternative class
//
//      This class handles the directive:
//
//          shortest[a | b | c | ... ]
//
//      where a, b and c are parsers. All alternatives (which does short-
//      circuit evaluation) are converted to shortest_alternative(s) where
//      the alternative that matches the shortest part of the input (minimal
//      munch) wins.
//
//      The class to_shortest_alternative and its specializations extract
//      all the alternative(s) and converts these to shortest_alternative(s).
//      The class shortest_parser_gen with its sole instance shortest is a
//      generator that creates a shortest_alternative object given a parser
//      composite comprising of alternatives.
//
///////////////////////////////////////////////////////////////////////////////
template <typename A, typename B>
struct shortest_alternative
:   public binary<A, B>,
    public parser<shortest_alternative<A, B> > {

    shortest_alternative(A const& a, B const& b);

    template <typename IteratorT>
    match
    parse(IteratorT& first, IteratorT const& last) const
    {
       IteratorT   ls = first;
       IteratorT   rs = first;
       match       l = this->left().parse(ls, last);
       match       r = this->right().parse(rs, last);

       if (l || r)
       {
          bool less = l.length() < r.length();
          first = less ? ls : rs;
          return less ? l : r;
       }

       return match();
    }
     
};
////////////////////////////////////////////////////////
namespace impl
{
//from spirit 1.1
// various workarounds for shortest directive
	template <typename T> struct Select_ToShortest;

	template <typename T>
	struct ToShortest {

		typedef typename Select_ToShortest<T>::RT	RT;

		static RT Convert(T const& a);

		typedef typename Select_ToShortest<T>::XT	XT;
		typedef typename Select_ToShortest<T>::YT	YT;
	};

	//////////////////////////////////
	template <typename T>
	struct ToShortest_Generic {

		typedef T const&	RT;
		typedef T		XT;
		typedef False_		YT;
	};

	template <typename T>
	inline T const&
	ToShortestConvert(T const& a, False_) { return a; }

	//////////////////////////////////
	template <typename T>
	struct ToShortest_Alternative {

		typedef typename ToShortest<typename T::TypeA>::XT	AT;
		typedef typename ToShortest<typename T::TypeB>::XT	BT;
		typedef shortest_alternative<AT, BT>	RT;

		typedef RT					XT;
		typedef True_				YT;
	};

	template <typename A, typename B>
	inline typename ToShortest<alternative<A, B> >::RT
	ToShortestConvert(alternative<A, B> const& alt, True_)
	{
		typedef typename ToShortest<alternative<A, B> >::RT RT;
		return RT(ToShortest<A>::Convert(alt.left()), ToShortest<B>::Convert(alt.right()));
		
	}

	//////////////////////////////////
	template <typename T>
	inline typename ToShortest<T>::RT
	ToShortest<T>::Convert (T const& a)
	{
		return ToShortestConvert(a, ToShortest<T>::YT());
	}

	//////////////////////////////////
	template <typename T>
	struct Select_ToShortest {

		typedef typename impl::IF< impl::is_alternative<T>::value
							, ToShortest_Alternative<T>
							, ToShortest_Generic<T>
							>::RET Type;

		typedef typename Type::RT RT;
		typedef typename Type::XT XT;
		typedef typename Type::YT YT;
	};

} //end namespace impl

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


//////////////////////////////////
struct shortest_parser_gen {

    template <typename A, typename B>

    typename impl::ToShortest<alternative<A,B> >::RT  
    operator[](alternative<A, B> const& alt) const
    {
        //  Borland 5.5 reports an internal compiler
        //  error if this is not defined here.
	return impl::ToShortest<alternative<A, B> >::Convert(alt);
    }
};

const shortest_parser_gen shortest = shortest_parser_gen();

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

#endif

⌨️ 快捷键说明

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