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

📄 rule.ipp

📁 著名的Parser库Spirit在VC6上的Port
💻 IPP
字号:
/*=============================================================================
    The Production rule

    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_RULE_IPP
#define SPIRIT_RULE_IPP

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

#include <cassert>
#include "boost/spirit/MSVC/rule.hpp"
#include "boost/spirit/MSVC/primitives.hpp"

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

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

    //////////////////////////////////
    template <typename IteratorT, typename MatchTraitsT>
    inline abstract_parser<IteratorT, MatchTraitsT>::abstract_parser() {}

    //////////////////////////////////
    template <typename IteratorT, typename MatchTraitsT>
    abstract_parser<IteratorT, MatchTraitsT>::~abstract_parser() {}


    //////////////////////////////////
    template <typename ParserT, typename IteratorT, typename MatchTraitsT>
    inline concrete_parser<ParserT, IteratorT, MatchTraitsT>::concrete_parser(
        ParserT const& parser_)
    :   ParserT(parser_) {}

    //////////////////////////////////
    template <typename ParserT, typename IteratorT, typename MatchTraitsT>
    concrete_parser<ParserT, IteratorT, MatchTraitsT>::~concrete_parser() {}

    //////////////////////////////////
    template <typename ParserT, typename IteratorT, typename MatchTraitsT>
    match
    concrete_parser<ParserT, IteratorT, MatchTraitsT>::
        parse(IteratorT& first, IteratorT const& last) const
    {
        return ParserT::parse(first, last);
    }

    ///////////////////////////////////////////////////////////////////////////////
    //
    //  rule_alias class
    //
    ///////////////////////////////////////////////////////////////////////////////
    template <typename IteratorT, typename MatchTraitsT, typename DerivedT>
    class rule_alias : public abstract_parser<IteratorT, MatchTraitsT> {

    public:

        rule_alias(base_rule<IteratorT, MatchTraitsT, DerivedT> const& alias);
        virtual ~rule_alias();

        virtual match
        parse(IteratorT& first, IteratorT const& last) const;

    private:

        base_rule<IteratorT, MatchTraitsT, DerivedT> const& alias;
    };

    //////////////////////////////////
    template <typename IteratorT, typename MatchTraitsT, typename DerivedT>
    inline rule_alias<IteratorT, MatchTraitsT, DerivedT>::rule_alias(
        base_rule<IteratorT, MatchTraitsT, DerivedT> const& alias_)
    :   alias(alias_) {}

    //////////////////////////////////
    template <typename IteratorT, typename MatchTraitsT, typename DerivedT>
    rule_alias<IteratorT, MatchTraitsT, DerivedT>::~rule_alias() {}

    //////////////////////////////////
    template <typename IteratorT, typename MatchTraitsT, typename DerivedT>
    match
    rule_alias<IteratorT, MatchTraitsT, DerivedT>::
        parse(IteratorT& first, IteratorT const& last) const
    {
        return alias.parse(first, last);
    }


    //////////////////////////////////
    template <typename ParserT, typename IteratorT, typename MatchTraitsT>
    inline alt_parser<ParserT, IteratorT, MatchTraitsT>::alt_parser(
        abstract_parser<IteratorT, MatchTraitsT>* left_, ParserT const& right_)
    :   left(left_),
        right(right_) {}

    //////////////////////////////////
    template <typename ParserT, typename IteratorT, typename MatchTraitsT>
    alt_parser<ParserT, IteratorT, MatchTraitsT>::~alt_parser()
    {
        delete left;
    }

    //////////////////////////////////
    template <typename ParserT, typename IteratorT, typename MatchTraitsT>
    match
    alt_parser<ParserT, IteratorT, MatchTraitsT>::
        parse(IteratorT& first, IteratorT const& last) const
    {
        if (match hit = left->parse(first, last))
            return hit;
        return right.parse(first, last);
    }


} // namespace impl

///////////////////////////////////////////////////////////////////////////////
//
//  rule class implementation.
//
///////////////////////////////////////////////////////////////////////////////
template <typename IteratorT, typename MatchTraitsT, typename DerivedT>
inline base_rule<IteratorT, MatchTraitsT, DerivedT>::base_rule()
:   meta(0) {}
/*
//////////////////////////////////
template <typename IteratorT, typename MatchTraitsT, typename DerivedT>
inline base_rule<IteratorT, MatchTraitsT, DerivedT>::~base_rule()
{
    delete meta;
}
*/
//////////////////////////////////
template <typename IteratorT, typename MatchTraitsT, typename DerivedT>
inline base_rule<IteratorT, MatchTraitsT, DerivedT>
        ::base_rule(base_rule const& other)
:   parser<DerivedT>()
,   meta(new impl::rule_alias<IteratorT, MatchTraitsT, DerivedT>(other))
{
}


//////////////////////////////////
template <typename IteratorT, typename MatchTraitsT, typename DerivedT>
inline base_rule<IteratorT, MatchTraitsT, DerivedT>&
base_rule<IteratorT, MatchTraitsT, DerivedT>
    ::operator = (base_rule<IteratorT, MatchTraitsT, DerivedT> const& other)
{
    if (this != &other)
    {
        if (meta == 0)
            meta = new impl::rule_alias<IteratorT, MatchTraitsT, DerivedT>(other);
        else
            meta = new impl::alt_parser<DerivedT, IteratorT, MatchTraitsT>(meta, 
                    other);
    }
    return *this;
}

//////////////////////////////////
#ifdef SPIRIT_DEBUG
template <typename IteratorT>
void
print_rule_info(
    bool                hit,
    int                 level,
    bool                close,
    std::string const&  name,
    IteratorT           first,
    IteratorT           last)
{
    if (!name.empty())
    {
        for (int i = 0; i < level; ++i)
            SPIRIT_DEBUG_OUT << "  ";
        if (close)
        {
            if (hit)
                SPIRIT_DEBUG_OUT << "/";
            else
                SPIRIT_DEBUG_OUT << "#";
        }
        SPIRIT_DEBUG_OUT << name << ":\t\"";
        typedef impl::strip_scanner<IteratorT> strip_scanner;
        typename strip_scanner::iterator_type
            iter = strip_scanner::get(first);
        typename strip_scanner::iterator_type
            ilast = strip_scanner::get(last);
        for (int j = 0; j < 20; ++j)
        {
            if (iter == ilast)
                break;

            switch (*iter) {

                case '\r': SPIRIT_DEBUG_OUT << "\\r";  break;
                case '\n': SPIRIT_DEBUG_OUT << "\\n";  break;
                default: SPIRIT_DEBUG_OUT << *iter;    break;
            }
            ++iter;
        }
        SPIRIT_DEBUG_OUT << " \"\n";
    }
}
#endif

template <typename IteratorT, typename MatchTraitsT, typename DerivedT>
inline match
base_rule<IteratorT, MatchTraitsT, DerivedT>::parse(IteratorT& first, 
        IteratorT const& last) const
{
#if defined(SPIRIT_DEBUG) && (SPIRIT_DEBUG_LEVEL >= SPIRIT_DEBUG_LEVEL_LEVEL2)

    print_rule_info(false, debug.level, false, debug.name(), first, last);

    int save_level = debug.level;
    debug.level++;
    match hit = meta ? meta->parse(first, last) : match();
    debug.level = save_level;

    print_rule_info(hit, debug.level, true, debug.name(), first, last);
    return hit;

#else
    return meta ? meta->parse(first, last) : match();
#endif
}

template <typename IteratorT, typename MatchTraitsT>
inline rule<IteratorT, MatchTraitsT>::rule()
{
}

template <typename IteratorT, typename MatchTraitsT>
inline rule<IteratorT, MatchTraitsT>::rule(rule const& other) :
    base_rule<IteratorT, MatchTraitsT, rule<IteratorT, MatchTraitsT> >
    (static_cast<base_rule_t const& >(other) )
{
}


template <typename IteratorT, typename MatchTraitsT>
inline rule<IteratorT, MatchTraitsT>&
rule<IteratorT, MatchTraitsT>::operator = (rule const& other)
{
    if (this != &other)
        *static_cast<base_rule_t *>(this) = 
			static_cast<base_rule_t const &>(other);
    return *this;
}


///////////////////////////////////////////////////////////////////////////////
//
//  skipper class implementation.
//
//////////////////////////////////
template <typename IteratorT, typename MatchTraitsT>
inline skipper<IteratorT, MatchTraitsT>::skipper(IteratorT const& last_)
:
    last(last_),
    skip_rule(space)
{
}

//////////////////////////////////
template <typename IteratorT, typename MatchTraitsT>
inline void
skipper<IteratorT, MatchTraitsT>::skip(IteratorT& current) const
{
    while (skip_rule.parse(current, last))
    {}
}

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

#endif

⌨️ 快捷键说明

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