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

📄 operators.hpp

📁 著名的Parser库Spirit在VC6上的Port
💻 HPP
📖 第 1 页 / 共 2 页
字号:
/*=============================================================================
    Parser operators

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

    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_OPERATORS_HPP
#define SPIRIT_OPERATORS_HPP

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

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

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

//  Note: G++2.95 reports an internal compiler error if some
//  parse(...) member functions are defined outside its class
//  declaration body.

///////////////////////////////////////////////////////////////////////////////
//
//  sequence class
//
//      Handles expressions of the form:
//
//          a >> b
//
//      where a and b are parsers. The expression returns a composite
//      parser that matches a and b in sequence. One (not both) of the
//      operands may be a literal char, wchar_t or a primitive string
//      char const*, wchar_t const*.
//
///////////////////////////////////////////////////////////////////////////////
template <typename A, typename B>
struct sequence
:   public binary<A, B>,
    public parser<sequence<A, B> > {

    sequence(A const& a, B const& b)
    :   binary<A, B>(a, b) {}

    template <typename IteratorT>
    match
    parse(IteratorT& first, IteratorT const& last) const
    {
        match       ma, mb;

        if ((ma = this->left().parse(first, last)) &&
            (mb = this->right().parse(first, last)))
        {
            return ma + mb;
        }
        return match();
    }

};

template <typename A, typename B>
inline sequence<A, B>
operator>>(parser<A> const& a, parser<B> const& b)
{
    return sequence<A, B>(a.derived(), b.derived());
}

//////////////////////////////////
template <typename A>
inline sequence<A, chlit<char> >
operator>>(parser<A> const& a, char b)
{
    return sequence<A, chlit<char> >(a.derived(), chlit<char>(b));
}

//////////////////////////////////
template <typename B>
inline sequence<chlit<char>, B>
operator>>(char a, parser<B> const& b)
{
    return sequence<chlit<char>, B>(chlit<char>(a), b.derived());
}

//////////////////////////////////
template <typename A>
inline sequence<A, strlit<cstring<char> > >
operator>>(parser<A> const& a, char const* b)
{
    return sequence<A, strlit<cstring<char> > >(a.derived(), b);
}

//////////////////////////////////
template <typename B>
inline sequence<strlit<cstring<char> >, B>
operator>>(char const* a, parser<B> const& b)
{
    return sequence<strlit<cstring<char> >, B>(a, b.derived());
}

//////////////////////////////////
template <typename A>
inline sequence<A, chlit<wchar_t> >
operator>>(parser<A> const& a, wchar_t b)
{
    return sequence<A, chlit<wchar_t> >(a.derived(), b);
}

//////////////////////////////////
template <typename B>
inline sequence<chlit<wchar_t>, B>
operator>>(wchar_t a, parser<B> const& b)
{
    return sequence<chlit<wchar_t>, B>(a, b.derived());
}

//////////////////////////////////
template <typename A>
inline sequence<A, strlit<cstring<wchar_t> > >
operator>>(parser<A> const& a, wchar_t const* b)
{
    return sequence<A, strlit<cstring<wchar_t> > >(a.derived(), b);
}

//////////////////////////////////
template <typename B>
inline sequence<strlit<cstring<wchar_t> >, B>
operator>>(wchar_t const* a, parser<B> const& b)
{
    return sequence<strlit<cstring<wchar_t> >, B>(a, b.derived());
}

///////////////////////////////////////////////////////////////////////////////
//
//  sequential-and operators
//
//      Handles expressions of the form:
//
//          a && b
//
//      Same as a >> b.
//
///////////////////////////////////////////////////////////////////////////////

template <typename a, typename b>
inline sequence<a, b>
operator&&(parser<a> const& a, parser<b> const& b)
{
    return sequence<a, b>(a.derived(), b.derived());
}

//////////////////////////////////
template <typename a>
inline sequence<a, chlit<char> >
operator&&(parser<a> const& a, char b)
{
    return sequence<a, chlit<char> >(a.derived(), b);
}

//////////////////////////////////
template <typename b>
inline sequence<chlit<char>, b>
operator&&(char a, parser<b> const& b)
{
    return sequence<chlit<char>, b>(a, b.derived());
}

//////////////////////////////////
template <typename a>
inline sequence<a, strlit<cstring<char> > >
operator&&(parser<a> const& a, char const* b)
{
    return sequence<a, strlit<cstring<char> > >(a.derived(), b);
}

//////////////////////////////////
template <typename b>
inline sequence<strlit<cstring<char> >, b>
operator&&(char const* a, parser<b> const& b)
{
    return sequence<strlit<cstring<char> >, b>(a, b.derived());
}

//////////////////////////////////
template <typename a>
inline sequence<a, chlit<wchar_t> >
operator&&(parser<a> const& a, wchar_t b)
{
    return sequence<a, chlit<wchar_t> >(a.derived(), b);
}

//////////////////////////////////
template <typename b>
inline sequence<chlit<wchar_t>, b>
operator&&(wchar_t a, parser<b> const& b)
{
    return sequence<chlit<wchar_t>, b>(a, b.derived());
}

//////////////////////////////////
template <typename a>
inline sequence<a, strlit<cstring<wchar_t> > >
operator&&(parser<a> const& a, wchar_t const* b)
{
    return sequence<a, strlit<cstring<wchar_t> > >(a.derived(), b);
}

//////////////////////////////////
template <typename b>
inline sequence<strlit<cstring<wchar_t> >, b>
operator&&(wchar_t const* a, parser<b> const& b)
{
    return sequence<strlit<cstring<wchar_t> >, b>(a, b.derived());
}

///////////////////////////////////////////////////////////////////////////////
//
//  sequential-or class
//
//      Handles expressions of the form:
//
//          a || b
//
//      Equivalent to
//
//          a | b | a >> b;
//
//      where a and b are parsers. The expression returns a composite
//      parser that matches matches a or b in sequence. One (not both) of
//      the operands may be a literal char, wchar_t or a primitive string
//      char const*, wchar_t const*.
//
///////////////////////////////////////////////////////////////////////////////
template <typename A, typename B>
struct sequential_or
:   public binary<A, B>,
    public parser<sequential_or<A, B> > {

    sequential_or(A const& a, B const& b)
    :   binary<A, B>(a, b) {}

    template <typename IteratorT>
    match
    parse(IteratorT& first, IteratorT const& last) const
    {
        { // scope for s
            IteratorT   s = first;
            match       ma = this->left().parse(s, last);
            if (ma)
            {
                IteratorT t = s;
                match mb = this->right().parse(t, last);
                if (mb)
                {
                    // matched a b
                    first = t;
                    return ma + mb;
                }
                else
                {
                    // matched a
                    first = s;
                    return ma;
                }
            }
        }

        return this->right().parse(first, last);
    }

};

//////////////////////////////////
template <typename A, typename B>
inline sequential_or<A, B>
operator||(parser<A> const& a, parser<B> const& b)
{
    return sequential_or<A, B>(a.derived(), b.derived());
}

//////////////////////////////////
template <typename A>
inline sequential_or<A, chlit<char> >
operator||(parser<A> const& a, char b)
{
    return sequential_or<A, chlit<char> >(a.derived(), b);
}

//////////////////////////////////
template <typename B>
inline sequential_or<chlit<char>, B>
operator||(char a, parser<B> const& b)
{
    return sequential_or<chlit<char>, B>(a, b.derived());
}

//////////////////////////////////
template <typename A>
inline sequential_or<A, strlit<cstring<char> > >
operator||(parser<A> const& a, char const* b)
{
    return sequential_or<A, strlit<cstring<char> > >(a.derived(), b);
}

//////////////////////////////////
template <typename B>
inline sequential_or<strlit<cstring<char> >, B>
operator||(char const* a, parser<B> const& b)
{
    return sequential_or<strlit<cstring<char> >, B>(a, b.derived());
}

//////////////////////////////////
template <typename A>
inline sequential_or<A, chlit<wchar_t> >
operator||(parser<A> const& a, wchar_t b)
{
    return sequential_or<A, chlit<wchar_t> >(a.derived(), b);
}

//////////////////////////////////
template <typename B>
inline sequential_or<chlit<wchar_t>, B>
operator||(wchar_t a, parser<B> const& b)
{
    return sequential_or<chlit<wchar_t>, B>(a, b.derived());
}

//////////////////////////////////
template <typename A>
inline sequential_or<A, strlit<cstring<wchar_t> > >
operator||(parser<A> const& a, wchar_t const* b)
{
    return sequential_or<A, strlit<cstring<wchar_t> > >(a.derived(), b);
}

//////////////////////////////////
template <typename B>
inline sequential_or<strlit<cstring<wchar_t> >, B>
operator||(wchar_t const* a, parser<B> const& b)
{
    return sequential_or<strlit<cstring<wchar_t> >, B>(a, b.derived());
}


///////////////////////////////////////////////////////////////////////////////
//
//  alternative class
//
//      Handles expressions of the form:
//
//          a | b
//
//      where a and b are parsers. The expression returns a composite
//      parser that matches a or b. One (not both) of the operands may
//      be a literal char, wchar_t or a primitive string char const*,
//      wchar_t const*.
//
//      The expression is short circuit evaluated. b is never touched
//      when a is returns a successful match.
//
///////////////////////////////////////////////////////////////////////////////

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

    alternative(A const& a, B const& b)
    :   binary<A, B>(a, b) {}

    template <typename IteratorT>
    match
    parse(IteratorT& first, IteratorT const& last) const
    {
        { // scope for s
            IteratorT s = first;
            if (match hit = this->left().parse(s, last))
            {
                first = s;
                return hit;
            }
        }
        return this->right().parse(first, last);
    }

};

/////////////////////////////////////////////////////////////
namespace impl
{
	//////////////////////////////////
	//	Determine at compile time (without partial specialization) whether
	//	a given type is an instance of the Alternative<A,B> template above.
	//////////////////////////////////
	template <typename T>
	class is_alternative
	{
		static T t(); 
		template <typename A, typename B>
		static char Test_ (alternative<A,B> const&  );	// no implementation
		static int	Test_ ( ... );				// no implementation
	public:
		enum { value = sizeof(char) == sizeof(Test_(t())) };
	};
}

//////////////////////////////////
template <typename A, typename B>
inline alternative<A, B>
operator|(parser<A> const& a, parser<B> const& b)
{
    return alternative<A, B>(a.derived(), b.derived());
}

//////////////////////////////////
template <typename A>
inline alternative<A, chlit<char> >
operator|(parser<A> const& a, char b)
{
    return alternative<A, chlit<char> >(a.derived(), 
		      chlit<char>(b) );
}

//////////////////////////////////
template <typename B>
inline alternative<chlit<char>, B>
operator|(char a, parser<B> const& b)
{
    return alternative<chlit<char>, B>(a, b.derived());
}

//////////////////////////////////
template <typename A>
inline alternative<A, strlit<cstring<char> > >
operator|(parser<A> const& a, char const* b)
{
    return alternative<A, strlit<cstring<char> > >(a.derived(), b);
}

//////////////////////////////////
template <typename B>
inline alternative<strlit<cstring<char> >, B>
operator|(char const* a, parser<B> const& b)
{
    return alternative<strlit<cstring<char> >, B>(a, b.derived());
}

//////////////////////////////////
template <typename A>
inline alternative<A, chlit<wchar_t> >
operator|(parser<A> const& a, wchar_t b)
{
    return alternative<A, chlit<wchar_t> >(a.derived(), b);
}

//////////////////////////////////
template <typename B>
inline alternative<chlit<wchar_t>, B>
operator|(wchar_t a, parser<B> const& b)
{
    return alternative<chlit<wchar_t>, B>(a, b.derived());
}

//////////////////////////////////
template <typename A>
inline alternative<A, strlit<cstring<wchar_t> > >
operator|(parser<A> const& a, wchar_t const* b)
{
    return alternative<A, strlit<cstring<wchar_t> > >(a.derived(), b);
}

//////////////////////////////////
template <typename B>

⌨️ 快捷键说明

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