ConfigScanner.h

00001 /*
00002  * ConfigScanner.h - lexical scanner for Rulbus configuration file.
00003  *
00004  * This file is part of the Rulbus Device Class Library (RDCL).
00005  *
00006  * Copyright (C) 2003-2004, Leiden University.
00007  * This library is free software, see below.
00008  *
00009  * $Revision: $
00010  * $Date: $
00011  * $Source: $
00012  * %Author: Martin J. Moene %
00013  *
00014  * Compiler        : Borland-C++ 5.6
00015  * Operating system: 32-bit Windows
00016  * Hardware system : PC
00017  *
00018  * This library is free software; you can redistribute it and/or modify
00019  * it under the terms of the GNU General Public License as published by
00020  * the Free Software Foundation; either version 2 of the License, or
00021  * (at your option) any later version.
00022  *
00023  * The library is distributed in the hope that it will be useful,
00024  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00025  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00026  * GNU General Public License for more details.
00027  *
00028  * You should have received a copy of the GNU General Public License
00029  * along with mngdriver; if not, write to the Free Software
00030  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00031  */
00032 
00033 /*
00034  * $Header: $
00035  *
00036  * $Log: $
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>                               // for std::istream
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 //                     ConfigScanner( String aFilename );
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 } // namespace Rulbus
00362 
00363 #endif  // __CONFIGSCANNER_H
00364 
00365 /*
00366  * end of file
00367  */
00368 

Generated on Tue Oct 12 14:11:57 2004 for Rulbus Device Class Library for Microsoft Windows by doxygen 1.3.4