00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef _MPCL_TEXT_REGEX_MATCHER__
00027 #define _MPCL_TEXT_REGEX_MATCHER__
00028
00029 #include <cstring>
00030 #include <sstream>
00031 #include "../../util/collection/string_to_string_map.hh"
00032 #include "../string.hh"
00033 #include "defs.hh"
00034
00035
00037 namespace mpcl
00038 {
00039
00041 namespace text
00042 {
00043
00045 namespace regex
00046 {
00047
00048 using util::collection::TStringToStringMap;
00049
00073 class TMatcher
00074 {
00075
00076 private:
00077
00079 bool gCaseSensitive;
00080
00082 bool gLocalStream;
00083
00085 std::basic_istream<char>* ptSourceIstream;
00086
00088 TStringToStringMap tTerminalMap;
00089
00090
00091 public:
00092
00093
00094
00095
00096
00098 TMatcher (void) :
00099 gCaseSensitive (true) ,
00100 gLocalStream (false) ,
00101 ptSourceIstream (NULL) ,
00102 tTerminalMap () {}
00103
00108 TMatcher (std::basic_istream<char>& rtISTREAM) :
00109 gCaseSensitive (true) ,
00110 gLocalStream (false) ,
00111 ptSourceIstream (&rtISTREAM) ,
00112 tTerminalMap ()
00113 {
00114 checkStream();
00115 }
00116
00121 TMatcher (const std::string& rySTRING) :
00122 gCaseSensitive (true) ,
00123 gLocalStream (true) ,
00124 ptSourceIstream (new std::basic_istringstream<char> (rySTRING)) ,
00125 tTerminalMap ()
00126 {
00127 if ( !rySTRING.empty() )
00128 {
00129 checkStream();
00130 }
00131 }
00132
00137 TMatcher (const char* pkcSTRING) :
00138 gCaseSensitive (true) ,
00139 gLocalStream (true) ,
00140 ptSourceIstream (new std::basic_istringstream<char> (pkcSTRING)) ,
00141 tTerminalMap ()
00142 {
00143 if ( pkcSTRING && std::strlen (pkcSTRING) )
00144 {
00145 checkStream();
00146 }
00147 }
00148
00150 ~TMatcher (void)
00151 {
00152 clearStream();
00153 }
00154
00156 void clearDefinitions (void);
00157
00164 void define (const char* pkcTERMINAL, const char* pkcDEFINITION);
00165
00172 void redefine (const char* pkcTERMINAL, const char* pkcDEFINITION);
00173
00178 void setCaseSensitiveness (bool gTRUTH);
00179
00184 void setInput (const char* pkcSTRING);
00185
00190 void setInput (const std::string& rkySTRING)
00191 {
00192 setInput (rkySTRING.c_str());
00193 }
00194
00199 void setInput (std::basic_istream<char>& rtISTREAM);
00200
00201
00202 public:
00203
00204
00205
00206
00207
00223 size_t scan (const char* pkcPATTERN_STRING...) const;
00224
00232 bool match (const char* pkcPATTERN_STRING) const;
00233
00234
00235 protected:
00236
00237
00238
00239
00240
00242 void clearStream (void);
00243
00244
00245 protected:
00246
00247
00248
00249
00250
00252 void checkStream (void) const;
00253
00261 TString instantiate (const char* pkcPATTERN_STRING) const;
00262
00271 bool matchChars (char cSOURCE, const char* pkcPATTERN_STRING ) const;
00272
00273 };
00274
00275 }
00276
00277 }
00278
00279 }
00280
00281
00282 #endif