📄 closure.ipp
字号:
/*=============================================================================
Closures
Spirit V1.2
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_IPP
#define SPIRIT_CLOSURE_IPP
///////////////////////////////////////////////////////////////////////////////
#include "boost/spirit/closure.hpp"
#if defined(SPIRIT_DEBUG)
#include "boost/tuple/tuple_io.hpp"
#endif
#include <cassert>
///////////////////////////////////////////////////////////////////////////////
namespace spirit {
///////////////////////////////////////////////////////////////////////////////
//
// closure_parser_action class implementation
//
///////////////////////////////////////////////////////////////////////////////
template <typename ParserT, typename ActionT>
inline closure_parser_action<ParserT, ActionT>::
closure_parser_action(ParserT const& parser_, ActionT const& actor_) :
base_action<ParserT, ActionT, closure_parser_action<ParserT, ActionT> >(parser_, actor_)
{
}
template <typename ParserT, typename ActionT>
template <typename IteratorT>
inline
attr_match<typename ParserT::return_t>
closure_parser_action<ParserT, ActionT>::
parse(IteratorT& first, IteratorT const& last) const
{
attr_match<typename ParserT::return_t> hit (
this->subject().parse(first, last));
if (hit)
actor(hit.attr_value());
return hit;
};
template <typename ParserT, typename ActionT>
template <typename IteratorT, typename MatchTraitsT>
inline
attr_match<typename ParserT::return_t, typename MatchTraitsT::match_t>
closure_parser_action<ParserT, ActionT>::
ast_parse(IteratorT& first, IteratorT const& last, MatchTraitsT const& mt) const
{
attr_match<typename ParserT::return_t, typename MatchTraitsT::match_t>
hit(this->subject().ast_parse(first, last, mt));
if (hit)
actor(hit.attr_value());
return hit;
};
///////////////////////////////////////////////////////////////////////////////
//
// 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 = ¤t; }
~local_saver()
{ ptr = previous; }
T current;
T* previous;
T*& ptr;
};
}
///////////////////////////////////////////////////////////////////////////////
//
// closure_parser class implementation
//
// The parse member function creates a local_saver class before calling
// the subject's parse member function.
//
///////////////////////////////////////////////////////////////////////////////
template <typename TupleT, typename ParserT>
inline closure_parser<TupleT, ParserT>::closure_parser(
TupleT*& ptr_,
ParserT const& parser)
: unary<ParserT>(parser),
ptr(ptr_)
{
#if defined(SPIRIT_DEBUG)
debug.name (std::string("closure parser: ") + parser.debug.name());
#endif
}
//////////////////////////////////
#ifdef SPIRIT_DEBUG
template <typename TupleT>
void
print_closure_info(bool hit, int level, bool close, std::string const &name, TupleT *ptr)
{
if (ptr && !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 << ": "<< *ptr << "\n";
}
}
#endif
//////////////////////////////////
template <typename TupleT, typename ParserT>
template <typename IteratorT>
inline
attr_match<typename element<0, TupleT>::type>
closure_parser<TupleT, ParserT>::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 TupleT, typename ParserT>
template <typename IteratorT, typename MatchTraitsT>
inline
attr_match<typename element<0, TupleT>::type, typename MatchTraitsT::match_t>
closure_parser<TupleT, ParserT>::ast_parse(
IteratorT& first,
IteratorT const& last,
MatchTraitsT const& mt) const
{
impl::local_saver<TupleT> l(ptr);
attr_match<return_t, typename MatchTraitsT::match_t> hit (
this->subject().ast_parse(first, last, mt));
hit.attr_value(boost::tuples::get<0>(*ptr));
return hit;
}
///////////////////////////////////////////////////////////////////////////////
//
// local class implementation
//
// Implements deferred access to a closure variable through the
// get() member function. Closures are constructed as tuples
// (see boost::tuples library) and closure variables are accessed
// through tuple's get<N> function.
//
///////////////////////////////////////////////////////////////////////////////
template <typename TupleT, int N>
inline local<TupleT, N>::local(TupleT*& ptr_)
: ptr(ptr_) {}
//////////////////////////////////
template <typename TupleT, int N>
inline local<TupleT, N>::operator element<N, TupleT>::type&() const
{
return get();
}
//////////////////////////////////
template <typename TupleT, int N>
inline local<TupleT, N>&
local<TupleT, N>::operator=(type const& val)
{
get() = val;
return *this;
}
//////////////////////////////////
template <typename TupleT, int N>
inline local<TupleT, N>::type&
local<TupleT, N>::get() const
{
assert(ptr);
return boost::tuples::get<N>(*ptr);
}
///////////////////////////////////////////////////////////////////////////////
//
// local_type class { utility class } implementation
//
// Used for the construction of the tuple elements in the closure.
//
///////////////////////////////////////////////////////////////////////////////
namespace impl {
template <typename T, typename TupleT, int N>
inline local_type<T, TupleT, N>::type
local_type<T, TupleT, N>::init(TupleT*& ptr)
{
return type(ptr);
}
template <typename TupleT, int N>
inline boost::tuples::null_type
local_type<boost::tuples::null_type, TupleT, N>::init(TupleT*)
{
return boost::tuples::null_type();
}
};
///////////////////////////////////////////////////////////////////////////////
//
// closure class implementation
//
// Closures are implemented using the boost::tuples library. Each
// closure variable is a tuple element. The closure class has two member
// variables: 1) a pointer to the <actual> tuple that is created at run-
// time in the stack area of a parser's parse member function, and 2) a
// tuples holding the local class proxies that refers to the each
// element in the <actual> tuple.
//
// Schematically:
//
// closure { ptr <--- locals }
// | |
// | |
// | +----<-- referencees (semantic actions)
// |
// +----<-- closure_parsers (actual local vars)
//
///////////////////////////////////////////////////////////////////////////////
template <
typename T0, typename T1, typename T2, typename T3, typename T4,
typename T5, typename T6, typename T7, typename T8, typename T9
>
inline base_closure<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9>::base_closure()
: ptr(0),
locals(
LT0::init(ptr), LT1::init(ptr),
LT2::init(ptr), LT3::init(ptr),
LT4::init(ptr), LT5::init(ptr),
LT6::init(ptr), LT7::init(ptr),
LT8::init(ptr), LT9::init(ptr)
)
{
}
template <
typename T0, typename T1, typename T2, typename T3, typename T4,
typename T5, typename T6, typename T7, typename T8, typename T9
>
inline closure<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9>::closure()
{
}
///////////////////////////////////////////////////////////////////////////////
} // namespace Spirit
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -