?? actions.hpp
字號:
/*=============================================================================
Semantic actions
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_ACTION_HPP
#define SPIRIT_ACTION_HPP
///////////////////////////////////////////////////////////////////////////////
#include "boost/spirit/spirit_fwd.hpp"
#include "boost/spirit/MSVC/composite.hpp"
#include "boost/spirit/MSVC/parser.hpp"
#include "boost/type_traits.hpp"
#include "boost/spirit/MSVC/ps_helper.hpp"
///////////////////////////////////////////////////////////////////////////////
namespace spirit {
///////////////////////////////////////////////////////////////////////////////
//
// action class
//
// Links a parser with a user defined semantic action. The semantic
// action may be a function or a functor. A function should be compatible
// with the interface:
//
// void f(IteratorT first, IteratorT last);
//
// Where first points to the current input and last points to one after
// the end of the input (same as STL algorithms).
//
// A functor should have a member operator() with a compatible signature
// as above. Iterators pointing to the matching portion of the input are
// passed into the function/functor.
//
// This is the default class that parsers (see parser::operator[]
// <in parser.hpp>) use when dealing with the construct:
//
// p[f]
//
// where p is a parser and f is a function or functor. Sometimes,
// when appropriate, some parsers overload the parser's operator[]
// (see char_parser in <primitives.hpp> and xx_parser_gen(s) in
// <numerics.hpp>).
//
///////////////////////////////////////////////////////////////////////////////
template <typename ParserT, typename ActionT, typename DerivedT>
class base_action
: public unary<ParserT>,
public parser<DerivedT>
{
public:
typedef action_parser_category parser_category;
template <typename IteratorT>
match
parse(IteratorT& first, IteratorT const& last) const
{
typedef impl::strip_scanner<IteratorT> strip_scanner;
typename strip_scanner::iterator_type
begin = strip_scanner::get(first);
match hit = this->subject().parse(first, last);
if (hit)
actor(begin, strip_scanner::get(first));
return hit;
}
ActionT const &predicate() const { return actor; }
protected:
// do not construct directly, only derived classes possible
base_action(ParserT const& parser, ActionT const& actor_)
: unary<ParserT>(parser), actor(actor_)
{
# if defined(SPIRIT_DEBUG)
debug.name (parser.debug.name());
# endif
}
typename embed_trait<ActionT>::type actor;
};
//////////////////////////////////////////////////////////////////////
template <typename ParserT, typename ActionT>
class action :
public base_action<ParserT, ActionT, action<ParserT, ActionT> >
{
public:
action(ParserT const& parser, ActionT const& actor_)
: base_action<ParserT, ActionT, action<ParserT, ActionT> >(parser, actor_)
{
}
};
///////////////////////////////////////////////////////////////////////////////
//
// reference_wrapper class
//
// Wraps a reference to a variable of type T. This class also does
// the dual purpose of acting like a functor that is compatible
// with the action class above, that expects an interface:
//
// void operator()(IteratorT begin, IteratorT end);
//
// When this operator is invoked, the reference_wrapper expects that
// the referenced object is a container of some sort (e.g. std::string,
// std::vector) that may be constructed given two iterators, and has
// a member function swap, that swaps the contents of two containers.
//
// The reference_wrapper handles the construct:
//
// p[ref(var)]
//
// where p is a parser and var is a variable that will hold the parsed
// result.
//
// ref(var)
//
// is a generator that creates a reference_wrapper.
//
// There is also an alternative:
//
// void operator()(T val);
//
// provided. This may be used by other action classes (apart from
// the one above) that expect this interface (see char_action in
// <primitives.hpp> and numeric_action in <numerics.hpp>).
//
///////////////////////////////////////////////////////////////////////////////
namespace impl
{
/////////////////////////////////////////////////////////////////////////////
// reference wrapper should be convertible to its contained type.
// reference_wrappers which wrap the closure elements should be
// convertible to the closure member type
// So introduce a base class which performs this conversion
///////////////////////////////////////////////////////////////////////////
template <typename DerivedT, typename ElementT, typename ActualT>
struct reference_access_helper_same
{
operator ElementT&() const
{
return static_cast<DerivedT const*>(this)->get();
}
};
////////////////////////////////////////////////////////////////////////////
template <typename DerivedT, typename ElementT, typename ActualT>
struct reference_access_helper_different
{
operator ElementT&() const
{
return static_cast<DerivedT const*>(this)->get();
}
operator ActualT&() const
{
return static_cast<DerivedT const*>(this)->get();
}
};
} //end namespace impl
//////////////////////////////////////////////////////////////////////////
template <typename DerivedT, typename ElementT, typename ActualT>
struct reference_access : public
impl::IF < boost::is_same<ElementT,ActualT>::value,
impl::reference_access_helper_same<DerivedT, ElementT, ActualT>,
impl::reference_access_helper_different<DerivedT, ElementT, ActualT> >::RET
{
};
////////////////////////////////////////////////////////////////////////////
template <typename T>
class reference_wrapper
: public reference_access<
reference_wrapper<T>, T,
typename boost::remove_reference<typename remove_wrap<T>::type>::type
>
{
public:
//contained type
typedef T value_type;
typedef typename remove_wrap<T>::type actual_ref;
typedef typename boost::remove_reference<actual_ref>::type actual_type;
//unwrapped type
typedef actual_type argument_type;
explicit
reference_wrapper(T& ref_)
: ref(ref_) {}
T&
get() const { return ref; }
template <typename TX>
void
operator()(TX const& val) const
{
//ref = (val);
//require this for VC++
ref = TX((val));
}
template <typename IteratorT>
void
operator()(IteratorT const& begin, IteratorT const& end) const
{
// Create an actual_type object given
// begin/end iterators.
actual_type temp(begin, end);
// Swap the contents of our newly created
// object and the one we are referencing.
actual_type& aref = ref;
aref.swap(temp);
}
private:
T& ref;
};
//////////////////////////////////////////////////////////////////////
// Get the underlying wrapper type given a reference_wrapper
// Various non PTS workarounds
//////////////////////////////////////////////////////////////////////
namespace impl
{
///////////////////////////////////////////////////////////////
template<int i>
struct unwrap_reference_wrapper_helper
{
template<typename T>
struct local {
typedef T type;
};
};
template<>
struct unwrap_reference_wrapper_helper<1>
{
template<typename T>
struct local {
typedef typename T::value_type type;
};
};
template<class T>
impl::selector1 reference_wrapper_selector(reference_wrapper<T> const&);
impl::selector2 reference_wrapper_selector(...);
//////////////////////////////////////////////////////////////////
} //end namespace impl
template<class T>
struct unwrap_reference_wrapper
{
static T t();
typedef typename impl::unwrap_reference_wrapper_helper
<(sizeof(impl::reference_wrapper_selector(t())))>
::template local<T>::type type;
};
/////////////////////////////////////////////////////////////////////////////
namespace impl
{
template <int i>
struct is_reference_wrapper_helper
{
enum { value = 0};
};
template <>
struct is_reference_wrapper_helper<1>
{
enum { value = 1};
};
template<class T>
selector1 is_reference_wrapper_selector(reference_wrapper<T> const &);
selector2 is_reference_wrapper_selector(...);
//////////////////////////////////////////////////////////////////////////////
} // end namespace impl
template<class T>
struct is_reference_wrapper
{
static T t();
enum { value = impl::is_reference_wrapper_helper
<
(sizeof(impl::is_reference_wrapper_selector(t())))
>::value };
};
///////////////////////////////////////////////////////////////////////////////
//
// ref generator (Generate a reference_wrapper object)
//
///////////////////////////////////////////////////////////////////////////////
template <class T>
reference_wrapper<T> const
ref(T& t)
{
return reference_wrapper<T>(t); // Generate a reference_wrapper object.
}
} // namespace Spirit
#endif
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -