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

basic_simple_non_terminal.h

00001 /***************************************************************************
00002  *   Copyright (C) 2005 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_PARSERBASIC_SIMPLE_NON_TERMINAL_H
00021 #define GRAMMAR_TO_PARSERBASIC_SIMPLE_NON_TERMINAL_H
00022 
00023 #include "parser.h"
00024 
00025 
00026 namespace grammar_to_parser {
00027 
00036 template< typename E >
00037 class basic_simple_non_terminal public basic_parser<wchar_t>
00038 {
00039 public:
00040         virtual void get_symbols( parser_list& l ) = 0;
00041         virtual void invalidate() 
00042         { 
00043                 m_parser_list.clear();
00044         }
00045         virtual bool is_valid() const
00046         {
00047                 return !m_parser_list.empty();
00048         }
00049 
00050         unsigned long parse( const E* buf, const unsigned long buf_length )
00051         {
00052                 prepare_for_parsing();
00053                 parser_list::iterator it = m_parser_list.begin();
00054 
00055                 m_is_parsed = true;
00056                 while( it != m_parser_list.end() && m_is_parsed )
00057                 {
00058                         (*it)->parse( buf + m_parsed_size, buf_length - m_parsed_size );
00059                         if( (*it)->is_parsed() ) 
00060                         {
00061                                 m_parsed_size += (*it)->parsed_size();
00062                                 it++;
00063                         }
00064                         else
00065                         {
00066                                 m_is_parsed = false;
00067                                 m_parsed_size = 0;
00068                         }
00069                 }
00070                 return m_parsed_size;
00071         }
00072         virtual std::basic_istream<E>& parse( std::basic_istream<E>& is )
00073         {
00074                 prepare_for_parsing();
00075                 parser_list::iterator it = m_parser_list.begin();
00076                 std::streampos pos = is.tellg();
00077 
00078                 m_is_parsed = true;
00079                 while( it != m_parser_list.end() && m_is_parsed )
00080                 {
00081                         (*it)->parse( is );
00082                         if( (*it)->is_parsed() ) 
00083                         {
00084                                 m_parsed_size += (*it)->parsed_size();
00085                                 it++;
00086                         }
00087                         else
00088                         {
00089                                 m_is_parsed = false;
00090                                 m_parsed_size = 0;
00091                                 is.clear();
00092                                 is.seekg( pos );
00093                         }
00094                 }
00095                 return is;
00096         }
00097     virtual std::basic_ostream<E>& format( std::basic_ostream<E>& os )
00098         {
00099                 prepare_for_parsing();
00100                 parser_list::iterator it = m_parser_list.begin();
00101                 std::streampos pos = os.tellp();
00102                 if( (std::streamoff) pos < 0 ) pos  = 0;
00103 
00104                 m_is_formatted = true;
00105                 while( it != m_parser_list.end() && m_is_formatted )
00106                 {
00107                         if( (*it)->is_valid() )
00108                         {
00109                                 (*it)->format( os );
00110                                 m_formatted_size += (*it)->formatted_size();
00111                                 m_is_formatted = (*it)->is_formatted();
00112                         }
00113                         it++;
00114                 }
00115                 if( !m_is_formatted )
00116                 {
00117                         m_formatted_size = 0;
00118                         os.clear();
00119                         os.seekp( pos );
00120                 }
00121                 return os;
00122         }
00123 protected:
00124         parser_list                     m_parser_list;
00125         virtual void prepare_for_parsing()
00126         {
00127                 basic_parser<wchar_t>::prepare_for_parsing();
00128                 if( m_parser_list.empty() ) get_symbols( m_parser_list );
00129         }
00130         virtual void prepare_for_formatting()
00131         {
00132                 basic_parser<wchar_t>::prepare_for_formatting();
00133                 if( m_parser_list.empty() ) get_symbols( m_parser_list );
00134         }
00135 
00136 };
00137 
00138 typedef basic_simple_non_terminal<char> simple_non_terminal;
00139 typedef basic_simple_non_terminal<wchar_t> wsimple_non_terminal_parser;
00140 
00141 };
00142 
00143 #endif

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