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

📄 rule.hpp

📁 著名的Parser库Spirit在VC6上的Port
💻 HPP
字号:
/*=============================================================================
    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_HPP
#define SPIRIT_RULE_HPP

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

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

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

namespace impl {

    ///////////////////////////////////////////////////////////////////////////////
    //
    //  abstract_parser class
    //
    ///////////////////////////////////////////////////////////////////////////////
    template <typename IteratorT, typename MatchTraitsT>
    class abstract_parser {

    public:

        abstract_parser();
        virtual ~abstract_parser();

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

    ///////////////////////////////////////////////////////////////////////////////
    //
    //  concrete_parser class
    //
    ///////////////////////////////////////////////////////////////////////////////
    template <typename ParserT, typename IteratorT, typename MatchTraitsT>
    class concrete_parser
    :  public abstract_parser<IteratorT, MatchTraitsT> ,
       public ParserT {

    public:

        concrete_parser(ParserT const& parser_);
		 
        virtual ~concrete_parser();

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

	private:
		bool dummy;		// needed for correct memory allocation
    };

    ///////////////////////////////////////////////////////////////////////////////
    //
    //  alt_parser class
    //
    ///////////////////////////////////////////////////////////////////////////////
    template <typename ParserT, typename IteratorT, typename MatchTraitsT>
    class alt_parser : public abstract_parser<IteratorT, MatchTraitsT> {

    public:

        alt_parser(abstract_parser<IteratorT, MatchTraitsT>* left, 
                ParserT const& right);
        virtual ~alt_parser();

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

    private:

        abstract_parser<IteratorT, MatchTraitsT>* left;
        ParserT right;
    };

}

///////////////////////////////////////////////////////////////////////////////
//
//  rule class
//
//      The rule is a polymorphic variable that acts as a named place-
//      holder capturing the behavior of an EBNF expression assigned to
//      it. Naming an EBNF expression allows it to be referenced later
//      on by another EBNF expression. Rules may be forward declared.
//      This enables rules to be recursive. A rule may even reference
//      itself. The limitation is that direct or indirect left recursion
//      is not allowed.
//
//      The rule class models EBNF's production rule. Example:
//
//          Rule<>   a_rule = *(a | b) & +(c | d | e);
//
//      The type and functionality of the right-hand expression, which
//      may be arbitrarily complex, is encoded in the rule named a_rule.
//      a_rule may now be referenced.
//
//      Rules may be declared before it is defined to allow cyclic
//      structures typically found in EBNF declarations. An undefined
//      rule matches nothing. Multiple definitions of a rule are taken
//      as alternatives. Example, the definition:
//
//          r = a;
//          r = b;
//
//      behaves as:
//
//          r = a | b;
//
//      When a rule is invoked by an EBNF expression, the rule is held
//      by the expression by reference. It is the responsibility of the
//      client to ensure that the referenced rule stays in scope and
//      does not get destructed while it is being referenced.
//
///////////////////////////////////////////////////////////////////////////////
template <typename IteratorT, typename MatchTraitsT, typename DerivedT>
class base_rule : public parser<DerivedT> 
{
public:
    typedef IteratorT   iterator_type;
    typedef MatchTraitsT  matchtraits_type;
    typedef DerivedT derived_type;

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

    template <typename ParserT>
    base_rule&
    operator=(ParserT const& parser_)
    {
       if (meta == 0)
          meta = new impl::concrete_parser<ParserT, IteratorT, MatchTraitsT>(
               parser_);
       else
          meta = new impl::alt_parser<ParserT, IteratorT, MatchTraitsT>(meta, 
                parser_);
       return *this;
    }

    base_rule&
    operator = (base_rule const& other);

// the destructor need to be public for VC++
    ~base_rule() 
      { 
        delete meta; 
      } 

protected:  

// do not construct directly, only derived classes possible
    base_rule();

    //~base_rule();

    template <typename ParserT>
    base_rule(ParserT const& parser_)
    : meta(new impl::concrete_parser<ParserT, IteratorT, MatchTraitsT>(parser_))
   {
   }

    base_rule(base_rule const& other);

private:
    impl::abstract_parser<IteratorT, MatchTraitsT>* meta;
};


//////////////////////////////////////////////////////////
//
////////////////////////////////////////////////////////////
template <typename IteratorT =  scanner<>, typename MatchTraitsT = match_traits>
class rule : 
    public base_rule<IteratorT, MatchTraitsT, rule<IteratorT, MatchTraitsT> > 
{
    typedef 
        base_rule<IteratorT, MatchTraitsT, rule<IteratorT, MatchTraitsT> >
        base_rule_t;

public:
    typedef rule<IteratorT, MatchTraitsT> self_; 
    rule();

    template <typename ParserT>
    rule(ParserT const& parser_)
    : base_rule<IteratorT, MatchTraitsT, rule<IteratorT, MatchTraitsT> >(parser_)
    {
    }

    rule(rule const& other);

    template <typename ParserT>
    rule&
    operator= (ParserT const& parser_)
    {
       *static_cast<base_rule_t *>(this) = parser_;
       return *this;
    }

    rule&
    operator= (rule const& other);

};

///////////////////////////////////////////////////////////////////////////////
//
//  skipper class
//
//      Utility class that facilitates the skipping of characters.
//      The single member function 'skip' enters a loop incrementing
//      the 'current' iterator until the supplied skip rule fails
//      to match.
//
///////////////////////////////////////////////////////////////////////////////
template <typename IteratorT = char const*, 
    typename MatchTraitsT = match_traits>
class skipper {

public:

    template <typename ParserT>
    skipper(ParserT const& skip_rule_, IteratorT const& last_)
    : last(last_),
      skip_rule(skip_rule_)
    {
    }

    skipper(IteratorT const& last);

    void
    skip(IteratorT& current) const;

private:

    IteratorT           last;
    rule<IteratorT, MatchTraitsT>     skip_rule;
};

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

#endif

⌨️ 快捷键说明

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