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

set.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_PARSERSET_H
00021 #define GRAMMAR_TO_PARSERSET_H
00022 
00023 #include "non_terminal.h"
00024 
00025 namespace grammar_to_parser {
00026 
00031 template <typename E, typename A>
00032 class basic_set : public basic_non_terminal<E, A>
00033 {
00034 public:
00035 typedef typename basic_parser<E>::parser_list parser_list;
00036 typedef typename basic_parser<E>::parser_list_iterator parser_list_iterator;
00037     basic_set();
00038     virtual ~basic_set();
00039     virtual unsigned long parse( const E *buf, const unsigned long buf_length );
00040         virtual std::basic_istream<E>& parse( std::basic_istream<E>& is );
00041 };
00042 
00043 template< typename E, typename A >
00044 basic_set<E,A>::basic_set() : basic_non_terminal<E,A>()
00045 {
00046 }
00047 
00048 
00049 template< typename E, typename A >
00050 unsigned long basic_set<E,A>::parse( const E *buf, 
00051                                                                                 const unsigned long buf_length )
00052 {
00053         prepare_for_parsing();
00054 
00055         parser_list symbols;
00056         parser_list_iterator parse_iterator = m_symbols.begin();
00057         bool is_parsed = true;
00058 
00059         // copy symbols to temporary list seems to be best to me
00060         while( parse_iterator != m_symbols.end() ) symbols.push_back( *(parse_iterator++) );
00061         while( !symbols.empty() && is_parsed )
00062         {
00063                 parse_iterator = symbols.begin();
00064                 is_parsed = false;
00065                 while( parse_iterator != symbols.end() && !is_parsed && m_parsed_size <= buf_length )
00066                 {
00067                         (*parse_iterator)->parse( buf+m_parsed_size, buf_length-m_parsed_size );
00068                         if( is_parsed = (*parse_iterator)->is_parsed() )
00069                         {
00070                                 m_parsed_size += (*parse_iterator)->parsed_size();
00071                                 parse_iterator = symbols.erase(parse_iterator); // exclude from parsing
00072                         }
00073                         else parse_iterator++; // continue with next
00074                 }
00075         }
00076         if( !symbols.empty() ) m_parsed_size = 0;
00077         else m_is_parsed = true;
00078 
00079         return m_parsed_size;
00080 }
00081 
00082         
00083 template <typename E, typename A >
00084 std::basic_istream<E>& basic_set<E,A>::parse( std::basic_istream<E>& is )
00085 {
00086         prepare_for_parsing();
00087 
00088         parser_list symbols;
00089         parser_list_iterator parse_iterator = m_symbols.begin();
00090         std::streampos str_pos = is.tellg();
00091         bool is_parsed = true;
00092 
00093         // copy symbols to temporary list seems to be best to me
00094         while( parse_iterator != m_symbols.end() ) symbols.push_back( *(parse_iterator++) );
00095         while( !symbols.empty() && is_parsed )
00096         {
00097                 parse_iterator = symbols.begin();
00098                 is_parsed = false;
00099                 while( parse_iterator != symbols.end() && !is_parsed && is.good() )
00100                 {
00101                         (*parse_iterator)->parse( is );
00102                         if( is_parsed = (*parse_iterator)->is_parsed() )
00103                         {
00104                                 m_parsed_size += (*parse_iterator)->parsed_size();
00105                                 parse_iterator = symbols.erase(parse_iterator); // exclude from parsing
00106                         }
00107                         else parse_iterator++; // continue with next
00108                 }
00109         }
00110         if( !symbols.empty() )
00111         {
00112                 m_parsed_size = 0;
00113                 is.clear();
00114                 is.seekg( str_pos );
00115         }
00116         else m_is_parsed = true;
00117 
00118         return is;
00119 }
00120 
00121 template <typename E, typename A >
00122 basic_set<E,A>::~basic_set()
00123 {
00124 }
00125 
00129 template <typename A>
00130 class set : public basic_set<char, A>
00131 {
00132 public:
00133     set() : basic_set<char,A>() {};
00134     ~set() {};
00135 };
00136 
00140 template <typename A>
00141 class wset : public basic_set<wchar_t, A>
00142 {
00143 public:
00144     wset() : basic_set<wchar_t,A>() {};
00145     ~wset() {};
00146 };
00147 
00148 };
00149 
00150 #endif

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