00001
00002
00003
00004
00005 #ifndef _TOKENMATCH_H_
00006 #define _TOKENMATCH_H_
00007
00008 #ifndef _POSIX_C_SOURCE
00009 #define _POSIX_C_SOURCE
00010 #endif
00011
00012 #include <iostream>
00013 #include <regex.h>
00014
00015 #ifdef AIDA_STD
00016 # undef AIDA_STD
00017 #endif
00018
00019 #ifdef AIDA_NOT_USE_STD
00020 # define AIDA_STD
00021 #else
00022 # define AIDA_STD std
00023 #endif
00024
00025
00033 class TokenMatch {
00034 public:
00036 TokenMatch (AIDA_STD::string text) : token (text) {}
00038 virtual ~TokenMatch() {}
00040 bool isInt () {
00041 return( match( "^[0-9][0-9]*$" ) || match( "^[-][0-9][0-9]*$" ));
00042 }
00044 bool isReal1 () {
00045 return(match( "^[0-9][0-9]*[.][0-9]*$" ) ||
00046 match( "^[-][0-9][0-9]*[.][0-9]*$"));
00047 }
00049 bool isReal2 () {
00050 return( match( "^[.][0-9][0-9]*$" ) || match( "^[-][.][0-9][0-9]*$" ));
00051 }
00053 bool isReal3 () {
00054 return( match( "^[0-9][0-9]*[.][0-9]*[e][0-9][0-9]*$" ) ||
00055 match( "^[-][0-9][0-9]*[.][0-9]*[e][0-9][0-9]*$" ));
00056 }
00058 bool isReal4 () {
00059 return( match( "^[0-9][0-9]*[.][0-9]*[e][-][0-9][0-9]*$" ) ||
00060 match( "^[-][0-9][0-9]*[.][0-9]*[e][-][0-9][0-9]*$" ));
00061 }
00063 bool isReal5 () {
00064 return( match( "^[0-9][0-9]*[e][0-9][0-9]*$" ) ||
00065 match( "^[-][0-9][0-9]*[e][0-9][0-9]*$" ));
00066 }
00068 bool isReal6 () {
00069 return( match( "^[0-9][0-9]*[e][-][0-9][0-9]*$" ) ||
00070 match( "^[-][0-9][0-9]**[e][-][0-9][0-9]*$" ));
00071 }
00073 bool isReal () {
00074 return (isReal1()||isReal2()||isReal3()||isReal4()||isReal5()||isReal6());
00075 }
00077 bool isNum () {
00078 return (isInt() || isReal());
00079 }
00081 bool isId () {
00082 return match( "^[A-Za-z_][A-Za-z0-9_]*" );
00083 }
00084
00086 bool match( const char *a_regexp ) {
00087 regex_t myReg;
00088 int rc;
00089 bool retval = false;
00090 rc = regcomp (&myReg,a_regexp, REG_NOSUB|REG_EXTENDED);
00091 if (rc == 0) {
00092 rc = regexec (&myReg,token.c_str(), 0, 0 , 0);
00093 if (rc == 0)
00094 retval = true;
00095 } else
00096 AIDA_STD::cerr << "Error compiling regular expression" << AIDA_STD::endl;
00097 return retval;
00098 }
00099 private:
00100 AIDA_STD::string token;
00101 };
00102
00103
00104 #endif // _TOKENMATCH_H_
00105