Main Page | Class Hierarchy | Class List | Directories | File List | Class Members

parser.h

00001 /***************************************************************************
00002  *   Copyright (C) 2004-2006 by Radko Mihal                                *
00003  *   rmihal@pobox.sk                                                       *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.              *
00019  ***************************************************************************/
00020 #ifndef GRAMMAR_TO_PARSERPARSER_H
00021 #define GRAMMAR_TO_PARSERPARSER_H
00022 
00023 #include <sstream>
00024 #include <string>
00025 #include <iostream>
00026 #include <list>
00027 
00028 
00029 namespace grammar_to_parser {
00030 
00031 //template< typename E > class basic_parser;
00032 
00038 template< typename E >
00039 class basic_parser
00040 {
00041 public:
00042 typedef std::list< basic_parser<E> * > parser_list;
00043 typedef typename parser_list::iterator parser_list_iterator;
00044 
00046         enum e_symbol_type {  terminal_type,
00047                                                 non_terminal_type, 
00048                                                 choice_type, 
00049                                                 undefined_type 
00050                                         };
00051         typedef e_symbol_type symbol_type;
00052         
00054         basic_parser(const char* name, symbol_type type = terminal_type) :
00055                         m_is_parsed(false), 
00056                         m_is_formatted(false), 
00057                         m_parsed_size(0),
00058                         m_formatted_size(0),
00059                         m_type(type),
00060                         m_name(name) {};
00061                         
00063         basic_parser( const basic_parser& rhs ) :
00064                 m_is_parsed( rhs.m_is_parsed ),
00065                 m_is_formatted( rhs.m_is_formatted ),
00066                 m_parsed_size( rhs.m_parsed_size ),
00067                 m_formatted_size( rhs.m_formatted_size ),
00068                 m_type( rhs.m_type ),
00069                 m_name( rhs.m_name ) {};
00070                                 
00072         virtual ~basic_parser()
00073         {       }               
00074         
00075         
00077         symbol_type get_symbol_type() { return m_type; }
00078 
00080         virtual bool is_valid() const = 0;
00082         virtual void invalidate() = 0;
00083 
00091         virtual unsigned long parse( const E *buf, const unsigned long buf_length ) = 0;
00092 
00100         virtual unsigned long parse( const E *buf, 
00101                                                                 const unsigned long buf_length,  
00102                                                                 basic_parser_strategy<E>& strategy )
00103         {
00104                 return parse( buf, buf_length );
00105         }
00106         
00114         virtual std::basic_istream<E>& parse( std::basic_istream<E>& is ) = 0;
00115         
00117         virtual std::basic_ostream<E>& format( std::basic_ostream<E>& os ) = 0;
00118 
00120         unsigned long parsed_size() const { return m_parsed_size; }
00122         unsigned long formatted_size() const { return m_formatted_size; }
00124 
00132         bool is_parsed() const { return m_is_parsed; }
00134         bool is_formatted() const { return m_is_formatted; }
00135 
00140         virtual void push_parsers( std::list< basic_parser<E> * >& the_parser_list )
00141         { 
00142                 the_parser_list.push_back(this);
00143         }
00144 
00150         bool change_parsed_flag( const bool the_parsed_flag = true ) 
00151         { 
00152                 bool b = m_is_parsed; 
00153                 m_is_parsed= the_parsed_flag; 
00154                 return b;
00155         }
00156         
00160         virtual const char* get_name() { return m_name; }
00161         
00162         virtual bool try_next() {m_is_parsed=false;return false;}
00163         virtual void set_to_begin() {};
00164         
00165 protected:
00167         bool                                            m_is_parsed;
00169         bool                                            m_is_formatted;
00171         unsigned long                           m_parsed_size;
00173         unsigned long                           m_formatted_size;
00175         const char*                                     m_name;
00176 
00178 
00183         symbol_type             m_type;         
00184         
00189         virtual void prepare_for_parsing()
00190         {
00191                 // cout << get_name() << endl;
00192                 m_is_parsed = false;
00193                 m_parsed_size = 0;
00194 #ifdef TRACE
00195                 cout << get_name() << endl;
00196 #endif
00197         }
00198         
00202         virtual void trace_on_fly() {};
00203         
00208         virtual void prepare_for_formatting() 
00209         {
00210                 m_is_formatted = false;
00211                 m_formatted_size = 0;
00212         };
00218         bool change_formatted_flag( const bool the_formatted_flag = true ) 
00219         { 
00220                 bool b = m_is_formatted; 
00221                 m_is_formatted = the_formatted_flag; 
00222                 return b;
00223         }
00229         unsigned long change_parsed_size( const unsigned long the_parsed_size )
00230         {
00231                 unsigned long size = m_parsed_size;
00232                 m_parsed_size = the_parsed_size;
00233                 return size;
00234         }
00240         unsigned long change_formatted_size( const unsigned long the_formatted_size )
00241         {
00242                 unsigned long size = m_formatted_size;
00243                 m_formatted_size = the_formatted_size;
00244                 return size;
00245         }
00246         
00250         void set_name( const char* name ) { m_name = name; }    
00251 };
00252 
00253 
00254 template< typename E >
00255 std::basic_ostream<E>& operator <<( std::basic_ostream<E>& os, 
00256                                                                         basic_parser<E>& parser )
00257 {
00258         return parser.format(os);
00259 }
00260         
00261 template< typename E >
00262 inline std::basic_istream<E>& operator >>( std::basic_istream<E>& is,
00263                                                                                   basic_parser<E>& parser )
00264 {
00265         return parser.parse( is );
00266 }
00267 
00268 typedef basic_parser<char>::parser_list parsers;
00269 typedef basic_parser<wchar_t>::parser_list wparsers;
00270 
00271 
00272 };
00273 
00274 
00275 #endif

Generated on Sun Jul 2 18:39:43 2006 for grammar2parser.kdevelop by  doxygen 1.4.1