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

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

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