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
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 #ifndef __CONFIGSCANNER_H
00040 #define __CONFIGSCANNER_H
00041
00042 #ifndef __COMMON_H
00043 #include "Common.h"
00044 #endif
00045
00046 #include <iosfwd>
00047
00048 namespace Rulbus
00049 {
00050 DECLARE_CLASS( ConfigScanner );
00051 DECLARE_CLASS( ConfigToken );
00052
00060 class ConfigToken
00061 {
00062 public:
00067 enum TokenCode
00068 {
00069 #define op( a, b, c, d ) a = static_cast<TokenCode>( b ),
00070 #define id( a, b, c, d ) a = static_cast<TokenCode>( b ),
00071 #define kw( a, b, c, d ) a = static_cast<TokenCode>( b ),
00072 #include "ConfigToken.def"
00073 tcLAST,
00074 };
00075
00076 ConfigToken() { ; }
00077 virtual ~ConfigToken() { ; }
00078
00080 virtual void get ( std::istream *is ) = 0;
00081
00083 virtual void print() const = 0;
00084
00085 bool isBool () const;
00086 bool isAsnop() const;
00087 bool isCard () const;
00088 bool isDelim() const;
00089 bool isUnit () const;
00090 bool isKind ( TokenCode t ) const;
00091
00092 TokenCode code () const;
00093
00094 bool toBool () const;
00095 Int toInt () const;
00096 Real toReal () const;
00097 String toString () const;
00098
00099 String tokenString( ) const;
00100 String tokenString( TokenCode t ) const;
00101
00102 protected:
00103 TokenCode theTokenCode;
00104 Int theIntValue;
00105 Real theRealValue;
00106 String theString;
00107
00108 private:
00109 static CharCptr stringTable[];
00110 static TokenCode kindTable[];
00111 };
00112
00117 inline bool ConfigToken::isAsnop() const
00118 {
00119 return isKind( tcASN );
00120 }
00121
00126 inline bool ConfigToken::isBool() const
00127 {
00128 return isKind( tcBOOL );
00129 }
00130
00135 inline bool ConfigToken::isCard() const
00136 {
00137 return isKind( tcCARD );
00138 }
00139
00144 inline bool ConfigToken::isDelim() const
00145 {
00146 return isKind( tcDELIM );
00147 }
00148
00153 inline bool ConfigToken::isUnit () const
00154 {
00155 return isKind( tcUNIT );
00156 }
00157
00162 inline ConfigToken::TokenCode ConfigToken::code () const
00163 {
00164 return theTokenCode;
00165 }
00166
00171 inline bool ConfigToken::toBool() const
00172 {
00173 return tcTRUE == code() || tcYES == code();
00174 }
00175
00180 inline Int ConfigToken::toInt() const
00181 {
00182 return theIntValue;
00183 }
00184
00189 inline Real ConfigToken::toReal() const
00190 {
00191 return theRealValue;
00192 }
00193
00198 inline String ConfigToken::toString() const
00199 {
00200 return theString;
00201 }
00202
00209 class ConfigWordToken: public ConfigToken
00210 {
00211 public:
00212 void get (std::istream *is);
00213 void print ( ) const;
00214 };
00215
00222 class ConfigNumberToken: public ConfigToken
00223 {
00224 public:
00225 ConfigNumberToken() { theTokenCode = tcNUMBER; }
00226
00227 void get ( std::istream *is );
00228 void print ( ) const;
00229
00230 protected:
00231 Real suffix(char chr);
00232 };
00233
00240 class ConfigStringToken: public ConfigToken
00241 {
00242 public:
00243 ConfigStringToken() { theTokenCode = tcSTRING; }
00244
00245 void get ( std::istream *is );
00246 void print ( ) const;
00247 };
00248
00256 class ConfigSpecialToken: public ConfigToken
00257 {
00258 public:
00259 void get ( std::istream *is );
00260 void print ( ) const;
00261 };
00262
00269 class ConfigEofToken: public ConfigToken
00270 {
00271 public:
00272 ConfigEofToken() { theTokenCode = tcEOF; }
00273
00274 #pragma argsused
00275 void get ( std::istream *is ) { }
00276 void print ( ) const;
00277 };
00278
00286 class ConfigErrorToken: public ConfigToken
00287 {
00288 public:
00289 ConfigErrorToken() { theTokenCode = tcERROR; }
00290
00291 void get ( std::istream *is );
00292 void print ( ) const;
00293 };
00294
00317 class ConfigScanner
00318 {
00319 public:
00320 virtual ~ConfigScanner( );
00321
00322 ConfigScanner( std::istream& stream, String aFilename = "" );
00323
00324 ConfigToken *get();
00325 String filename () const;
00326 Int linenumber() const;
00327
00328 protected:
00329 void skipWhitespace();
00330
00331 bool seechar( char& chr );
00332
00333 ConfigWordToken wordToken;
00334 ConfigNumberToken numberToken;
00335 ConfigStringToken stringToken;
00336 ConfigSpecialToken specialToken;
00337 ConfigEofToken eofToken;
00338 ConfigErrorToken errorToken;
00339
00340 private:
00341 std::istream *is;
00342 String theFilename;
00343 Int theLinenumber;
00344 };
00345
00350 inline String ConfigScanner::filename() const
00351 {
00352 return theFilename;
00353 }
00354
00356 inline Int ConfigScanner::linenumber() const
00357 {
00358 return theLinenumber;
00359 }
00360
00361 }
00362
00363 #endif // __CONFIGSCANNER_H
00364
00365
00366
00367
00368