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

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

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