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

equivalence_class.h

00001 /***************************************************************************
00002  *   Copyright (C) 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 BRACKET_EXPRESSIONEQUIVALENCE_CLASS_H
00021 #define BRACKET_EXPRESSIONEQUIVALENCE_CLASS_H
00022 
00023 namespace bracket_expression {
00024 
00025 template<typename E>
00026 class equivalence_class
00027 {
00028 typedef typename grammar_to_parser::basic_parser<E>::parser_list 
00029                                                                                                                                 parser_list;
00030         
00031         class equivalence_base
00032         {
00033         protected:
00034                 int m_size;
00035         public:
00036                 equivalence_base() : m_size(0) {};
00037                 int matched_size() { return m_size; }
00038         };
00039         
00040         class openequal_coll_elem_single_equalclose : public equivalence_base
00041         {
00042                 grammar_to_parser::basic_terminal<E, '['>       m_open;
00043                 grammar_to_parser::basic_terminal<E, '='>       m_left_equal;
00044                 grammar_to_parser::basic_except_terminal<E,meta_char_choice<E> >        
00045                                                                                                         m_coll_elem_single;
00046                 grammar_to_parser::basic_terminal<E, '='>       m_right_equal;
00047                 grammar_to_parser::basic_terminal<E, ']'>       m_close;
00048         public:
00049                                 
00050                 void push_parsers( parser_list &l )
00051                 {
00052                         l.push_back(&m_open);
00053                         l.push_back(&m_left_equal);
00054                         l.push_back(&m_coll_elem_single);
00055                         l.push_back(&m_right_equal);
00056                         l.push_back(&m_close);
00057                 }
00058                 int compare( const E* buf, const unsigned long buf_length )
00059                 {
00060                         int ret = std::use_facet< std::collate<E> >(std::locale()).compare(
00061                                                                                                 buf,
00062                                                                                                 buf,
00063                                                                                                 m_coll_elem_single.get(),
00064                                                                                                 m_coll_elem_single.get() );
00065                         if( ret == 0 )
00066                                 m_size = 1;
00067                         else
00068                                 m_size = 0;
00069                         return ret;
00070                 }       
00071         };
00072         
00073         class openequal_coll_elem_multi_equalclose : public equivalence_base
00074         {
00075                 typedef std::ctype_base ctypebase;
00076                 grammar_to_parser::basic_terminal<E, '['>       m_open;
00077                 grammar_to_parser::basic_terminal<E, '.'>       m_left_equal;
00078                 grammar_to_parser::basic_string_parser<E>       m_coll_elem_multi;
00079                 grammar_to_parser::basic_terminal<E, '.'>       m_right_equal;
00080                 grammar_to_parser::basic_terminal<E, ']'>       m_close;
00081         public:
00082                 openequal_coll_elem_multi_equalclose() : 
00083                         m_coll_elem_multi('.', grammar_to_parser::basic_string_parser<E>::NONE) {};
00084                 void push_parsers( parser_list &l )
00085                 {
00086                         l.push_back(&m_open);
00087                         l.push_back(&m_left_equal);
00088                         l.push_back(&m_coll_elem_multi);
00089                         l.push_back(&m_right_equal);
00090                         l.push_back(&m_close);
00091                 }
00092                 int compare( const E* buf, const unsigned long buf_length )
00093                 {
00094                         unsigned long to_compare_length = 0;
00095                         // check the size of element in buf  - try the longest
00096                         while( to_compare_length < buf_length )
00097                         {
00098                                 to_compare_length++;
00099                                 ctypebase::mask m = ctypebase::print;
00100                                 if( !std::use_facet< std::ctype<E> >(std::locale()).is( buf, 
00101                                                                                                                 buf+to_compare_length, &m ) )
00102                                 {
00103                                         break;
00104                                 }
00105                                         
00106                         }
00107                         std::basic_string<E> multichar = *(m_coll_elem_multi.get());
00108                         int ret = std::use_facet< std::collate<E> >(std::locale()).compare(
00109                                                                                 buf,
00110                                                                                 buf+to_compare_length,
00111                                                                                 multichar.c_str(),
00112                                                                                 multichar.c_str() + multichar.length() );
00113                         m_size = to_compare_length;
00114                         return ret;
00115                 }       
00116         };
00117         int m_size;
00118 public:
00119         grammar_to_parser::basic_non_terminal<E, openequal_coll_elem_single_equalclose>
00120                                                                                                         m_coll_elem_single;
00121         grammar_to_parser::basic_non_terminal<E, openequal_coll_elem_multi_equalclose>
00122                                                                                                         m_multichar;
00123 public:
00124     equivalence_class() : 
00125                 m_coll_elem_single(),
00126                 m_multichar(),
00127                 m_size(0) {};
00128         
00129         int matched_size() { return m_size; }
00130 
00131         void push_parsers( parser_list &l )
00132         {
00133                 l.push_back(&m_coll_elem_single);
00134                 l.push_back(&m_multichar);
00135         }       
00136         int compare( const E* buf, const unsigned long buf_length )
00137         {
00138                 int ret = 0;
00139                 if( m_coll_elem_single.is_parsed() )
00140                 {
00141                         ret = m_coll_elem_single->compare( buf, buf_length );
00142                         m_size = m_coll_elem_single->matched_size();
00143                 }
00144                 else
00145                 {
00146                         ret = m_multichar->compare( buf, buf_length );
00147                         m_size = m_multichar->matched_size();
00148                 }
00149                 return ret;
00150         }
00151 };
00152 
00153 };
00154 
00155 #endif

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