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

📄 attr_rule.ipp

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

    Spirit V1.2
    Copyright (c) 2001, Hartmut Kaiser

    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_ATTR_RULE_IPP
#define SPIRIT_ATTR_RULE_IPP

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

#include "boost/spirit/attr_rule.hpp"

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

///////////////////////////////////////////////////////////////////////////////
// 
//  attr_match class implementation
//
///////////////////////////////////////////////////////////////////////////////
template <typename TypeT, typename BaseT>
inline 
attr_match<TypeT, BaseT>::attr_match() : value()
{
}

template <typename TypeT, typename BaseT>
inline 
attr_match<TypeT, BaseT>::attr_match(unsigned length) : value(), BaseT(length)
{
}

//template <typename TypeT, typename BaseT>
//inline attr_match<TypeT, BaseT>::attr_match(match const &hit) :
//    BaseT(hit.length())
//{
//}

template <typename TypeT, typename BaseT>
inline 
attr_match<TypeT, BaseT>::attr_match(BaseT const &hit) : value(), BaseT(hit)
{
}

template <typename TypeT, typename BaseT>
template <typename OtherTypeT>
inline 
attr_match<TypeT, BaseT>::attr_match(attr_match<OtherTypeT, BaseT> const &hit) : 
    value(hit.attr_value()), BaseT(hit)
{
}

template <typename TypeT, typename BaseT>
inline 
attr_match<TypeT, BaseT>&
attr_match<TypeT, BaseT>::operator += (match const& other)
{
    assert(*this && other);
    data += other.data;
    return *this;
}

template <typename TypeT, typename BaseT>
inline TypeT const &
attr_match<TypeT, BaseT>::attr_value() const 
{ 
    return value; 
}

template <typename TypeT, typename BaseT>
inline void
attr_match<TypeT, BaseT>::attr_value(TypeT const &value_) 
{ 
    value = value_; 
}

template <typename TypeT, typename BaseT>
inline attr_match<TypeT, BaseT> 
operator+(attr_match<TypeT, BaseT> const& a, match const& b)
{
    return attr_match<TypeT, BaseT>(a) += b;
}

template <typename TypeT, typename BaseT>
inline attr_match<TypeT, BaseT> 
operator+(match const& a, attr_match<TypeT, BaseT> const& b)
{
    return attr_match<TypeT, BaseT>(b) += a;
}

///////////////////////////////////////////////////////////////////////////////
// 
//  param_parser class implementation
//
///////////////////////////////////////////////////////////////////////////////
template <typename TupleT, typename ParserT, typename InitTupleT>
inline
param_parser<TupleT, ParserT, InitTupleT>::param_parser(
        InitTupleT const &init_, TupleT *&ptr_, ParserT const& parser) :
    unary<ParserT>(parser), init(init_), ptr(ptr_)
{
#if defined(SPIRIT_DEBUG)
    debug.name (std::string("param 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);
#endif

template <typename TupleT, typename ParserT, typename InitTupleT>
template <typename IteratorT>
inline attr_match<typename element<0, TupleT>::type>
param_parser<TupleT, ParserT, InitTupleT>::parse(
    IteratorT& first, IteratorT const& last) const
{
#ifdef SPIRIT_DEBUG
    impl::local_saver<TupleT> l(ptr);

    print_closure_info(false, debug.level, false, debug.name() + " (before init)", ptr);
    *ptr = init;
    print_closure_info(false, debug.level, false, debug.name() + " (after init)", 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);
    *ptr = init;

    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, typename InitTupleT>
template <typename IteratorT, typename MatchTraitsT>
inline 
attr_match<typename element<0, TupleT>::type, typename MatchTraitsT::match_t>
param_parser<TupleT, ParserT, InitTupleT>::ast_parse(
    IteratorT& first, IteratorT const& last, MatchTraitsT const &mt) const
{
    impl::local_saver<TupleT> l(ptr);
    *ptr = init;

    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;
}

///////////////////////////////////////////////////////////////////////////////
// 
// Helper functions for generating the param_parser
//
///////////////////////////////////////////////////////////////////////////////

template <typename TupleT, typename ParserT>
template <typename T0>
inline
param_parser<
    TupleT, ParserT, 
    typename boost::tuples::detail::make_tuple_mapper<T0>::type
> 
param_parser_gen<TupleT, ParserT>
    ::operator()(T0 const &t0)
{
    typedef 
        typename boost::tuples::detail::make_tuple_mapper<T0>::type 
        init_tuple_t;
    return param_parser<TupleT, ParserT, init_tuple_t>(
        boost::tuples::make_tuple(t0), ptr, parser);
}

///////////////////////////////////////////////////////////////////////////////
template <typename TupleT, typename ParserT>
template <typename T0, typename T1>
inline
param_parser<
    TupleT, ParserT, 
    typename boost::tuples::detail::make_tuple_mapper<T0, T1>::type
> 
param_parser_gen<TupleT, ParserT>
    ::operator()(T0 const &t0, T1 const &t1)
{
    typedef 
        typename boost::tuples::detail::make_tuple_mapper<T0, T1>::type 
        init_tuple_t;

    return param_parser<TupleT, ParserT, init_tuple_t>(
        boost::tuples::make_tuple(t0, t1), ptr, parser);
}

///////////////////////////////////////////////////////////////////////////////
template <typename TupleT, typename ParserT>
template <typename T0, typename T1, typename T2>
inline
param_parser<
    TupleT, ParserT, 
    typename boost::tuples::detail::make_tuple_mapper<T0, T1, T2>::type
> 
param_parser_gen<TupleT, ParserT>
    ::operator()(T0 const &t0, T1 const &t1, T2 const &t2)
{
    typedef 
        typename boost::tuples::detail::make_tuple_mapper<T0, T1, T2>::type 
        init_tuple_t;

    return param_parser<TupleT, ParserT, init_tuple_t>(
        boost::tuples::make_tuple(t0, t1, t2), ptr, parser);
}

///////////////////////////////////////////////////////////////////////////////
template <typename TupleT, typename ParserT>
template <typename T0, typename T1, typename T2, typename T3>
inline
param_parser<

⌨️ 快捷键说明

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