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

📄 utilities.hpp

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

    Spirit V1.3.1
    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_HPP
#define SPIRIT_UTILITIES_HPP

#include <string>
#include <iterator>
#include "boost/spirit/MSVC/ps_helper.hpp"

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

namespace impl {

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

///////////////////////////////////////////////////////////////////////////// 
// escape action traits helper classes
    template<int i>
    struct ecat_helper 
    {
       //once you've identified that this is a function there is no
       //way to determine the argument type.  
       //escape parser will most likely be used with char, wchar_t or
       //ints 
       template<class T> 	     
       struct local
       { 	      
          typedef char type;
       };
       template<> 	     
       struct local<void (*)(wchar_t) >
       { 	      
          typedef wchar_t type;
       };
       template<> 	     
       struct local<void (*)(int) >
       { 	      
          typedef int type;
       };
        
    };

  //////////////////////////////////////////////////////////////////////// 
    template<>
    struct ecat_helper<1> 
    {
       template<class T> 	     
       struct local
       { 	      
          typedef typename impl::IF<  is_reference_wrapper<T>::value,
	           typename unwrap_reference_wrapper<T>::type,
		   typename T::argument_type  >::RET type;
       };
    };   

    template<typename T>  	 
    selector2 ecat_selector(void (*)(T));
    
    selector1 ecat_selector(...);


    template <typename T>
    struct escape_char_action_traits {
      static T t();  	     
      typedef typename ecat_helper< (sizeof(ecat_selector(t())))>::
	      template local<T>::type argument_type; 
    };

} // namespace impl

///////////////////////////////////////////////////////////////////////////////
//
//  escape_char_action class
//
//      Links an escape char 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(CharT ch);
//
//      A functor should have a member operator() with a compatible signature
//      as above. The matching character is passed into the function/functor.
//      This is the default class that character parsers use when dealing with
//      the construct:
//
//          p[f]
//
//      where p is a parser and f is a function or functor.
//
///////////////////////////////////////////////////////////////////////////////
template <typename ParserT, typename ActionT, unsigned long Flags>
class escape_char_action
:   public unary<ParserT>,
    public parser<escape_char_action<ParserT, ActionT, Flags> > {

public:

    typedef action_parser_category parser_category;

    escape_char_action(
        ParserT const& subject,
        ActionT const& actor_);

    template <typename IteratorT>
    match
    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 && isxdigit(c))
                                      {
                                          return match(); // overflow detected
                                      }
                                      if (isdigit(c))
                                      {
                                          hex <<= 4;
                                          hex |= c - '0';
                                          ++first;
                                      }
                                      else if (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();
      }

    ActionT const &predicate() const { return actor; }

private:

    typename embed_trait<ActionT>::type actor;
};

///////////////////////////////////////////////////////////////////////////////
//
//  escape_char_parser class
//
///////////////////////////////////////////////////////////////////////////////
struct escape_flags {

    BOOST_STATIC_CONSTANT( long ,  c_escapes = 1);
    BOOST_STATIC_CONSTANT( long ,  lex_escapes = c_escapes<<1);
};

//////////////////////////////////
template <unsigned long Flags>
class escape_char_parser : public parser<escape_char_parser<Flags> > {

public:

    template <typename ActionT>
    escape_char_action<escape_char_parser<Flags>, ActionT, Flags>
    operator[](ActionT const& actor) const
    {
        return escape_char_action<escape_char_parser, ActionT, Flags>(
            *this, actor);
    }

    template <typename IteratorT>
    match
    parse(IteratorT& first, IteratorT const& last) const
    {
      static range<> octal('0','7');
      static rule<IteratorT> escape
        =   +octal
        |   nocase['x'] >> +xdigit
        |   (anychar - nocase['x'] - octal);

      return ((anychar - '\\') | ('\\' >> escape)).parse(first, last);
    }

};

///////////////////////////////////////////////////////////////////////////////
//
//  predefined escape_char_parser classes
//
///////////////////////////////////////////////////////////////////////////////
const escape_char_parser<escape_flags::lex_escapes> lex_escape_ch_p =
    escape_char_parser<escape_flags::lex_escapes>();

const escape_char_parser<escape_flags::c_escapes> c_escape_ch_p =
    escape_char_parser<escape_flags::c_escapes>();

///////////////////////////////////////////////////////////////////////////////
//
//  Types to distinguish nested and non-nested confix parsers
//
///////////////////////////////////////////////////////////////////////////////
struct nested {};
struct non_nested {};

///////////////////////////////////////////////////////////////////////////////
//
//  Helper templates to derive the parser type.
//
///////////////////////////////////////////////////////////////////////////////
namespace impl {

/////////////////////////////////////////////////////////////////////////////////
// Inferencing the parser_type turned out to be real ugly. For some
// reason defining the static get methods in parser type causes an ice
// the parser_type is hence split into two . One for inferecing the parser_type
// the other for constructing it
/////////////////////////////////////////////////////////////////////////////////
    template<int I>   
    struct parser_type_helper
    {
      template <class T>  	     
      struct inner   	     
      {
         typedef T parser_t;
	 typedef parser_t const& return_t; 
	 typedef parser_t const& parameter_t; 
	 typedef selector2 dispatcher_t ; 
      };
    } ;
    
    //////////////////////////////////////////////////////// 
    template<>   
    struct parser_type_helper<1>
    {
      template <class T> 	     
      struct inner   	     
      {
         typedef strlit<cstring<char> >  parser_t;
	 typedef parser_t  return_t; 
	 typedef const T parameter_t; 
	 typedef selector1 dispatcher_t ; 
      };
    } ;

    
    //////////////////////////////////////////////////////// 
    template <class T>
    struct parser_type
    {
       static T* t(void);  	     
        typedef  impl::IF<boost::is_array<T>::value,
              selector1,
	      selector2>::RET selector_t;  

       typedef typename parser_type_helper<(sizeof(selector_t))>
        ::template inner<T>::parser_t parser_t;

       typedef typename parser_type_helper<(sizeof(selector_t))>
        ::template inner<T>::return_t return_t;

       typedef typename parser_type_helper<(sizeof(selector_t))>
        ::template inner<T>::parameter_t parameter_t;


    };

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

⌨️ 快捷键说明

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