utilities.ipp

来自「著名的Parser库Spirit在VC6上的Port」· IPP 代码 · 共 1,305 行 · 第 1/4 页

IPP
1,305
字号
/*=============================================================================
    Utilities

    Spirit V1.2
    Copyright (c) 2001, Daniel Nuffer
    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_UTILITIES_IPP
#define SPIRIT_UTILITIES_IPP

///////////////////////////////////////////////////////////////////////////////
#include <cassert>

#include "boost/spirit/utilities.hpp"

///////////////////////////////////////////////////////////////////////////////
//
//  These seem to creep in as macros when using gcc 2.95.2.
//
///////////////////////////////////////////////////////////////////////////////
#ifdef __GNUC__
#ifdef isdigit
#undef isdigit
#endif

#ifdef isxdigit
#undef isxdigit
#endif
#endif

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

/*=============================================================================

    Escape char parser classes implementation [Dan Nuffer]

=============================================================================*/

namespace impl {

///////////////////////////////////////////////////////////////////////////////
//
//    escape_char_action_traits class used to determine the type of the
//    argument to the action functor.
//
///////////////////////////////////////////////////////////////////////////////

    template <typename T>
    struct escape_char_action_traits {
        typedef typename T::argument_type argument_type;
    };

    template <typename T>
    struct escape_char_action_traits<void (*)(T)> {
        typedef T argument_type;
    };

    template <typename T>
    struct escape_char_action_traits<reference_wrapper<T> > {
        typedef T argument_type;
    };

} // namespace impl

///////////////////////////////////////////////////////////////////////////////
//
//  escape_char_action class implementation
//
///////////////////////////////////////////////////////////////////////////////
template <typename ParserT, typename ActionT, unsigned long Flags>
inline escape_char_action<ParserT, ActionT, Flags>::escape_char_action(
    ParserT const& subject, ActionT const& actor_)
:   unary<ParserT>(subject),
    actor(actor_) {}

//////////////////////////////////
template <typename ParserT, typename ActionT, unsigned long Flags>
template <typename IteratorT>
inline match
escape_char_action<ParserT, ActionT, Flags>::
parse(IteratorT& first, IteratorT const& last) const
{
    // Actually decode the escape char.
    typedef typename impl::escape_char_action_traits<ActionT>
        ::argument_type char_t;

    if (first != last)
    {
        IteratorT s = first;
        if (match hit = this->subject().parse(s, last))
        {
            char_t unescaped;
            if (*first == '\\')
            {
                ++first;
                switch (*first)
                {
                    case 'b':   unescaped = '\b';   ++first; break;
                    case 't':   unescaped = '\t';   ++first; break;
                    case 'n':   unescaped = '\n';   ++first; break;
                    case 'f':   unescaped = '\f';   ++first; break;
                    case 'r':   unescaped = '\r';   ++first; break;
                    case '"':   unescaped = '"';    ++first; break;
                    case '\'':  unescaped = '\'';   ++first; break;
                    case '\\':  unescaped = '\\';   ++first; break;
                    case 'x': case 'X':
                        {
                            char_t hex = 0;
                            ++first;
                            char_t lim = 
                                std::numeric_limits<char_t>::max() >> 4;
                            while (first != last)
                            {
                                char_t c = *first;
                                if (hex > lim && std::isxdigit(c))
                                {
                                    return match(); // overflow detected
                                }
                                if (std::isdigit(c))
                                {
                                    hex <<= 4;
                                    hex |= c - '0';
                                    ++first;
                                }
                                else if (std::isxdigit(c))
                                {
                                    hex <<= 4;
                                    c = toupper(c);
                                    hex |= c - 'A' + 0xA;
                                    ++first;
                                }
                                else
                                {
                                    break; // reached the end of the number
                                }
                            }
                            unescaped = hex;
                        }
                        break;

                    case '0': case '1': case '2': case '3':
                    case '4': case '5': case '6': case '7':
                        {
                            char_t hex = 0;
                            char_t lim = 
                                std::numeric_limits<char_t>::max() >> 3;
                            while (first != last)
                            {
                                char_t c = *first;
                                if (hex > lim && (c >= '0' && c <= '7'))
                                {
                                    return match(); // overflow detected
                                }
                                if (c >= '0' && c <= '7')
                                {
                                    hex <<= 3;
                                    hex |= c - '0';
                                    ++first;
                                }
                                else
                                {
                                    break; // reached end of digits
                                }
                            }
                            unescaped = hex;
                        }

                        break;

                    default:
                        if (Flags & escape_flags::c_escapes)
                        {
                            return match();
                        }
                        else
                        {
                            unescaped = *first;
                            ++first;
                        }
                        break;
                }
            }
            else
            {
                unescaped = *first;
                ++first;
            }

            actor(unescaped);
            return hit;
        }
    }
    return match();
}

//////////////////////////////////
template <typename ParserT, typename ActionT, unsigned long Flags>
template <typename IteratorT, typename MatchTraitsT>
inline typename MatchTraitsT::match_t
escape_char_action<ParserT, ActionT, Flags>::
ast_parse(IteratorT& first, IteratorT const& last, MatchTraitsT const& mt) const
{
    // Actually decode the escape char.
    typedef typename impl::escape_char_action_traits<ActionT>
        ::argument_type char_t;

    if (first != last)
    {
        IteratorT s = first;
        if (typename MatchTraitsT::match_t hit = 
                this->subject().ast_parse(s, last, mt))
        {
            char_t unescaped;
            if (*first == '\\')
            {
                ++first;
                switch (*first)
                {
                    case 'b':   unescaped = '\b';   ++first; break;
                    case 't':   unescaped = '\t';   ++first; break;
                    case 'n':   unescaped = '\n';   ++first; break;
                    case 'f':   unescaped = '\f';   ++first; break;
                    case 'r':   unescaped = '\r';   ++first; break;
                    case '"':   unescaped = '"';    ++first; break;
                    case '\'':  unescaped = '\'';   ++first; break;
                    case '\\':  unescaped = '\\';   ++first; break;
                    case 'x': case 'X':
                        {
                            char_t hex = 0;
                            ++first;
                            char_t lim = 
                                std::numeric_limits<char_t>::max() >> 4;
                            while (first != last)
                            {
                                char_t c = *first;
                                if (hex > lim && std::isxdigit(c))
                                {
                                    // overflow detected
                                    return MatchTraitsT::no_match; 
                                }
                                if (std::isdigit(c))
                                {
                                    hex <<= 4;
                                    hex |= c - '0';
                                    ++first;
                                }
                                else if (std::isxdigit(c))
                                {
                                    hex <<= 4;
                                    c = toupper(c);
                                    hex |= c - 'A' + 0xA;
                                    ++first;
                                }
                                else
                                {
                                    break; // reached the end of the number
                                }
                            }
                            unescaped = hex;
                        }
                        break;

                    case '0': case '1': case '2': case '3':
                    case '4': case '5': case '6': case '7':
                        {
                            char_t hex = 0;
                            char_t lim = 
                                std::numeric_limits<char_t>::max() >> 3;
                            while (first != last)
                            {
                                char_t c = *first;
                                if (hex > lim && (c >= '0' && c <= '7'))
                                {
                                    // overflow detected
                                    return MatchTraitsT::no_match; 
                                }
                                if (c >= '0' && c <= '7')
                                {
                                    hex <<= 3;
                                    hex |= c - '0';
                                    ++first;
                                }
                                else
                                {
                                    break; // reached end of digits

⌨️ 快捷键说明

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