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

list_parser.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_PARSERLIST_PARSER_H
00021 #define GRAMMAR_TO_PARSERLIST_PARSER_H
00022 
00023 #include "object_parser.h"
00024 
00025 namespace grammar_to_parser {
00026 
00027 const char* LIST = "List";
00028 
00038 template< class E, class A, class S = basic_terminal<E,'\n'> >
00039 class basic_list_parser : public basic_object_parser< E, std::list<A> >
00040 {
00041 typedef std::list<A> std_list_A;
00042 typedef typename std_list_A::iterator std_list_A_iterator;
00043 typedef typename basic_parser<E>::parser_list parser_list;
00044 typedef typename basic_parser<E>::parser_list_iterator parser_list_iterator;
00045 
00046 public:
00047 typedef std::list<A> parser_list;
00048 
00049     basic_list_parser() :
00050                         basic_object_parser< E, std_list_A >(LIST,non_terminal_type),
00051                         m_min(0),
00052                         m_max(-1)
00053         {};
00054         
00055         // max_size = -1 - unbounded size (more exactly, limited by INT_MAX)
00056     basic_list_parser( int min_size, int max_size) :
00057                         basic_object_parser< E, std_list_A >(LIST,non_terminal_type),
00058                         m_min(min_size),
00059                         m_max(max_size)
00060         {};
00061 
00062     ~basic_list_parser()
00063         {};
00064 
00065         template< class R >
00066         void get_list( std::list<R>& l )
00067         {
00068                 std_list_A_iterator it = get_valid().begin();
00069                 while( it != get_valid().end() )
00070                 {
00071                         l.push_back( (*it).get_valid() );
00072                         it++;
00073                 }
00074         }
00075         
00079         unsigned long parse(const E *buf, const unsigned long buf_length)
00080         {
00081                 prepare_for_parsing();
00082 
00083                 S separator_parser;
00084                 bool to_continue = true;
00085                 do
00086                 {
00087                         A a_parser;
00088                         // A is inherited from auto_ptr, if is parser inherited from basic_parser
00089                         // from this when assigned to list the ownership is transfered
00090                         m_parsed_size += 
00091                                         a_parser.parse(buf+m_parsed_size, buf_length-m_parsed_size);
00092                         if( a_parser.is_parsed() )
00093                                 get_valid().push_back(a_parser);
00094                         else
00095                                 to_continue = false;
00096                         // eat out separator
00097                         m_parsed_size += separator_parser.parse( buf+m_parsed_size, 
00098                                                                                                         buf_length-m_parsed_size );
00099                 }
00100                 while( to_continue );
00101 
00102                 m_is_parsed = get_valid().size() >= m_min && 
00103                                                 ( m_max == -1  || get_valid().size() < m_max );
00104                                                 
00105                 return m_parsed_size;
00106         }
00107 
00111         std::basic_istream<E>& parse( std::basic_istream<E>& is )
00112         {
00113                 prepare_for_parsing();
00114                 
00115                 S separator_parser;
00116                 bool to_continue = true;
00117                 do
00118                 {
00119                         A a_parser;
00120                         // A is inherited from auto_ptr, if is parser inherited from basic_parser
00121                         // from this when assigned to list the ownership is transfered
00122                         a_parser.parse( is );
00123                         m_parsed_size += a_parser.parsed_size();
00124                         if( a_parser.is_parsed() )
00125                                 get_valid().push_back(a_parser);
00126                         else
00127                                 to_continue = false;
00128                         // eat out separator
00129                         separator_parser.parse( is );
00130                         m_parsed_size += separator_parser.parsed_size(); 
00131                 }
00132                 while( to_continue );
00133 
00134                 m_is_parsed = true;
00135                 return is;
00136         }
00137         
00141         std::basic_ostream<E>& format( std::basic_ostream<E>&os )
00142         {
00143                 prepare_for_formatting();
00144                 
00145                 std_list_A_iterator it = get_valid().begin();
00146                 while( it != get_valid().end() )
00147                 {
00148                         (*it).format(os);
00149                         it++;
00150                         if( it != get_valid().end() ) 
00151                         {
00152                                 S separator_parser;
00153                                 separator_parser.format( os );
00154                         }
00155 
00156                 }
00157                 return os;
00158         }
00159 protected:
00160         int m_min;
00161         int m_max;
00162 
00163 };
00164 
00168 template< class A, class S = terminal<'\n'> >
00169 class list_parser : public basic_list_parser< char, A, S >
00170 {
00171 typedef std::list<A> std_list_A;
00172 typedef std::list<A> std_list_A_iterator;
00173 
00174 public:
00175         list_parser() : basic_list_parser<char, A, S >() {};
00176     list_parser( int min_size, int max_size ) : 
00177                 basic_list_parser<char, A, S >( min_size, max_size ) {};
00178     ~list_parser() {};
00179 };
00180 
00184 template< class A, class S = wterminal<'\n'> >
00185 class wlist_parser : public basic_list_parser< wchar_t, A, S >
00186 {
00187 typedef std::list<A> std_list_A;
00188 typedef std::list<A> std_list_A_iterator;
00189 
00190 public:
00191     wlist_parser() : basic_list_parser< wchar_t, A, S >() {};
00192     wlist_parser( int min_size, int max_size ) : 
00193                 basic_list_parser< wchar_t, A, S >( min_size, max_size ) {};
00194     ~wlist_parser() {};
00195 };
00196 
00197 };
00198 #endif

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