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

simple_choice.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_CHOICE_H
00021 #define GRAMMAR_TO_PARSERBASIC_SIMPLE_CHOICE_H
00022 
00023 namespace grammar_to_parser {
00024 
00025 const char* SIMPLE_CHOICE = "SimpleChoice";
00026 
00035 template< typename E >
00036 class basic_simple_choice : public basic_simple_non_terminal<E>
00037 {
00038 public:
00039 typedef typename basic_parser<E>::parser_list parser_list;
00040 typedef typename basic_parser<E>::parser_list_iterator parser_list_iterator;
00041 
00042         basic_simple_choice() : basic_simple_non_terminal<E>()
00043         {
00044                 set_name(SIMPLE_CHOICE);
00045         };
00046         
00047         unsigned long parse( const E* buf, const unsigned long buf_length )
00048         {
00049                 prepare_for_parsing();
00050                 parser_list_iterator it = m_parser_list.begin();
00051 
00052                 m_is_parsed = false;
00053                 while( it != m_parser_list.end() && !m_is_parsed )
00054                 {
00055                         (*it)->parse( buf + m_parsed_size, buf_length - m_parsed_size );
00056                         if( (*it)->is_parsed() ) 
00057                         {
00058                                 m_is_parsed = true;
00059                                 m_parsed_size += (*it)->parsed_size();
00060                         }
00061                         it++;
00062                 }
00063                 // unset 'parsed' flag for rest (for parser reuse)
00064                 while( it != m_parser_list.end() )
00065                 {
00066                         (*it)->invalidate();
00067                 }
00068                 return m_parsed_size;
00069         }
00070         virtual std::basic_istream<E>& parse( std::basic_istream<E>& is )
00071         {
00072                 prepare_for_parsing();
00073                 parser_list_iterator it = m_parser_list.begin();
00074                 std::streampos pos = is.tellg();
00075 
00076                 m_is_parsed = false;
00077                 while( it != m_parser_list.end() && !m_is_parsed )
00078                 {
00079                         (*it)->parse( is );
00080                         if( (*it)->is_parsed() ) 
00081                         {
00082                                 m_is_parsed = true;
00083                                 m_parsed_size += (*it)->parsed_size();
00084                         }
00085                         it++;
00086                 }
00087                 // unset 'parsed' flag for rest (for parser reuse)
00088                 while( it != m_parser_list.end() )
00089                 {
00090                         (*it++)->invalidate();
00091                 }
00092 
00093                 if( !m_is_parsed )
00094                 {
00095                         is.clear();
00096                         is.seekg( pos );
00097                 }
00098                 return is;
00099         }
00100 };
00101 
00102 
00103 typedef basic_simple_choice<char> simple_choice;
00104 typedef basic_simple_choice<wchar_t> wsimple_choice;
00105 
00106 };
00107 
00108 #endif

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