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

📄 closure.hpp

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

    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_CLOSURE_HPP
#define SPIRIT_CLOSURE_HPP

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

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

///////////////////////////////////////////////////////////////////////////////
namespace spirit {
//////////////////////////////////
#ifdef SPIRIT_DEBUG
template <typename TupleT>
void
print_closure_info(bool hit, int level, bool close, std::string const &name, TupleT *ptr);
#endif

/////////////////////////////////////////////////
struct closure_null_type
{
   closure_null_type() {};
   //closure_null_type accepts any other type
   //The choice was wether to use this or write more code  
   //to simulate PTS to distiguish b/w the case where
   //attr_match (see attr_rule.hpp) was paramterized with a valid
   // type or a closure_null_type.
   template <class T>
   closure_null_type(T const& ) {};
};


///////////////////////////////////////////////////////////////////////////////
//
//  local_saver class
//
//      Stack based class that holds the actual local variables and links it
//      to all linked local proxy objects (see local class).
//
///////////////////////////////////////////////////////////////////////////////
namespace impl {

    template <typename T>
    struct local_saver {

        local_saver(T*& ptr_)
        : current(), previous(ptr_), ptr(ptr_)
	{ ptr = &current; }

        ~local_saver()
        { ptr = previous; }

        T   current;
        T*  previous;
        T*& ptr;
    };
} //end namespace impl

/////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
//
//  closure_parser_action class
//
//      The closure_parser_action class is used, when there should be an actor 
//      attached to a closure_parser. This is required, if the return value of 
//      the closure_parser rule should be made accessible.
//
//      A closure_parser has a return value, contained in a special
//      match class returned by the corresponding parse function (see 
//      closure.hpp --> attr_match<> class). 
//
//      Some background information: 
//      Closures are designed to be bound to the context of a concrete parser 
//      (the parser embedded within the closure) and by default it is not 
//      possible to propagate some closure member value up the parser 
//      hierarchy. 
//
//      By attaching an actor to the closure (see attr_match comment below)
//      we can make available the first element in the closure tuple (it's the 
//      element with the index '0': closure_member<0>). This value is passed to 
//      the attached actor:
//
//          closure<string> clos;
//          string s;
//          clos[str_p("cool")[clos(m0)] >> str_p("huh?")] [ref(s)].parse(...)
//                                                         
//          -----------------------------------------------^ attached actor 
//
//      will assign "cool" to 's';
//
///////////////////////////////////////////////////////////////////////////////
template <typename ParserT, typename ActionT>
struct closure_parser_action :
    public base_action<ParserT, ActionT, closure_parser_action<ParserT, ActionT> >
{
    typedef typename ParserT::return_t return_t;

    closure_parser_action(ParserT const& parser, ActionT const& actor);

    template <typename IteratorT>
    attr_match<return_t>
    parse(IteratorT& first, IteratorT const& last) const
    {
       attr_match<ParserT::return_t> hit (
       this->subject().parse(first, last));

       if (hit) {
         actor(hit.attr_value()); 
       }	  
       return hit;
    };

};

///////////////////////////////////////////////////////////////////////////////
//
//  closure_parser class
//
//      Encloses a parser in a closure (see closure class below). This class
//      handles the creation of stack variables prior to parsing and the
//      consequent destruction of the created stack variables after parsing.
//
//      closure_parser objects are not directly instantiated. Instead, a
//      closure_parser is indirectly created by the closure (see below).
//      The construct:
//
//          c[p]
//
//      where c is a closure and p is a parser generates a closure_parser
//      object.
//
///////////////////////////////////////////////////////////////////////////////

template <typename TupleT, typename ParserT>
class closure_parser
:   public unary<ParserT>,
    public parser<closure_parser<TupleT, ParserT> > {

public:
    typedef TupleT tuple_t;
    typedef typename boost::tuples::element<0, TupleT>::type return_t;

    typedef closure_parser_category parser_category;

    closure_parser(TupleT*& ptr, ParserT const& parser);

    template <typename IteratorT>
    attr_match<return_t>
    parse(IteratorT& first, IteratorT const& last) const
    {
#if defined(SPIRIT_DEBUG) && (SPIRIT_DEBUG_LEVEL >= SPIRIT_DEBUG_LEVEL_MAX)
        impl::local_saver<TupleT> l(ptr);
    
        print_closure_info(false, debug.level, false, debug.name(), ptr);
        int save_level = debug.level;
        debug.level++;
        attr_match<return_t> hit (this->subject().parse(first, last));
        debug.level = save_level;
    
        print_closure_info(false, debug.level, false, debug.name(), ptr);

        hit.attr_value(boost::tuples::get<0>(*ptr));
        return hit;
#else
        impl::local_saver<TupleT> l(ptr);
        attr_match<return_t> hit (this->subject().parse(first, last));
    
        hit.attr_value(boost::tuples::get<0>(*ptr));
        return hit;
#endif 
    }


    template <typename ActionT>
    closure_parser_action<closure_parser<TupleT, ParserT>, ActionT>
    operator[](ActionT const& actor) const
    {
        //  Borland 5.5 reports an internal compiler
        //  error if this is not defined here.
        typedef
            closure_parser_action<closure_parser<TupleT, ParserT>, ActionT>
            actor_t;
        return actor_t(*this, actor);
    }

private:
    TupleT*& ptr;
};

///////////////////////////////////////////////////////////////////////////////
//
//  local class
//
//      Proxies a local variable in a function to make it accessible to inner
//      nested functions. This facility provides a means to exchange data to
//      and from various points in a grammar. A point in a recursive descent
//      may expose a couple of local variables to pass and extract data to
//      and from other points down the descent.
//
//      local objects are not directly instantiated. Instead, a local is
//      indirectly created by the closure (see below) The construct:
//
//          c(mem)
//
//      where c is a closure and mem is a closure_member (see below),
//      generates a reference_wrapper to a local object. This may then be
//      used as a semantic action (see action.hpp).
//
///////////////////////////////////////////////////////////////////////////////
template <typename TupleT, int N>
class local {

public:

    typedef typename boost::tuples::element<N, TupleT>::type type;

⌨️ 快捷键说明

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