00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef GRAMMAR_TO_PARSERWHITE_SPACE_PARSER_H
00021 #define GRAMMAR_TO_PARSERWHITE_SPACE_PARSER_H
00022
00023 #include "default_type_parser.h"
00024
00025 namespace grammar_to_parser {
00026
00031 template< typename E >
00032 class basic_white_space_parser :
00033 public basic_default_type_parser< E,std::basic_string<E> >
00034 {
00035 typedef std::basic_string<E> std_string;
00036 typedef std::basic_istream<E> std_istream;
00037 typedef std::ctype<E> std_ctype;
00038 typedef std::char_traits<E> std_char_traits;
00039 public:
00040 basic_white_space_parser() :
00041 basic_default_type_parser< E,std::basic_string<E> >() {};
00042
00043 basic_white_space_parser( const basic_white_space_parser &rhs ) :
00044 basic_default_type_parser< E,std::basic_string<E> >(rhs) {};
00045
00046 basic_white_space_parser( const std::basic_string<E> &space ) :
00047 basic_default_type_parser< E,std::basic_string<E> >(space) {};
00048
00049 virtual ~basic_white_space_parser() {};
00050 virtual unsigned long parse( const E *buf, const unsigned long buf_length )
00051 {
00052 prepare_for_parsing();
00053
00054 typename std_string::iterator it = get_valid().begin();
00055
00056 while( m_parsed_size < buf_length && is_space( buf[m_parsed_size] ) )
00057 {
00058 m_parsed_size++;
00059 get_valid() += buf[m_parsed_size];
00060 }
00061 m_is_parsed = true;
00062 return m_parsed_size;
00063 }
00064
00065 virtual std::basic_istream<E>& parse( std::basic_istream<E>& is )
00066 {
00067 prepare_for_parsing();
00068
00069 typename std_string::iterator it = get_valid().begin();
00070 std::streampos strPos = is.tellg();
00071 E input = 0;
00072
00073 while( is.good() && is_space( is ) )
00074 {
00075 is.read( &input, 1 );
00076 m_parsed_size++;
00077 get_valid() += input;
00078 strPos = is.tellg();
00079 }
00080 m_is_parsed = true;
00081 is.clear();
00082 is.seekg( strPos );
00083 return is;
00084 }
00085 protected:
00086 bool is_space( E input )
00087 {
00088 const std::locale loc;
00089 const std_ctype& fac = std::use_facet( loc, (std::ctype<E>*)0, true );
00090 return fac.is( std_ctype::space, input );
00091 }
00092 bool is_space( std::basic_istream<E>& is )
00093 {
00094 E input = 0;
00095 is.read( &input, 1 );
00096 if( is.good() )
00097 {
00098 const std::locale loc;
00099 const std_ctype& fac = std::use_facet( loc, (std::ctype<E>*)0, true );
00100 return fac.is( std_ctype::space, input );
00101 }
00102 return false;
00103 }
00104 };
00105
00106 typedef basic_white_space_parser<char> white_space_parser;
00107 typedef basic_white_space_parser<wchar_t> wwhite_space_parser;
00108
00109 };
00110
00111 #endif