lexerSource.h

00001 /***************************************************************
00002  *
00003  * Copyright (C) 1990-2007, Condor Team, Computer Sciences Department,
00004  * University of Wisconsin-Madison, WI.
00005  * 
00006  * Licensed under the Apache License, Version 2.0 (the "License"); you
00007  * may not use this file except in compliance with the License.  You may
00008  * obtain a copy of the License at
00009  * 
00010  *    http://www.apache.org/licenses/LICENSE-2.0
00011  * 
00012  * Unless required by applicable law or agreed to in writing, software
00013  * distributed under the License is distributed on an "AS IS" BASIS,
00014  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015  * See the License for the specific language governing permissions and
00016  * limitations under the License.
00017  *
00018  ***************************************************************/
00019 
00020 
00021 #ifndef __CLASSAD_LEXER_SOURCE_H__
00022 #define __CLASSAD_LEXER_SOURCE_H__
00023 
00024 #include "classad/common.h"
00025 #include <iosfwd>
00026 
00027 BEGIN_NAMESPACE(classad)
00028 
00029 /*
00030  * LexerSource is a class that provides an abstract interface to the
00031  * lexer. The lexer reads tokens and gives them to the parser.  The
00032  * lexer reads single characters from a LexerSource. Because we want
00033  * to read characters from different types of sources (files, strings,
00034  * etc.) without burdening the lexer, the lexer uses this LexerSource
00035  * abstract class. There are several implementations of the
00036  * LexerSource that provide access to specific types of sources. 
00037  *
00038  */
00039 
00040 class LexerSource
00041 {
00042 public:
00043     LexerSource()
00044     {
00045         return;
00046     }
00047     virtual ~LexerSource()
00048     {
00049         return;
00050     }
00051     
00052     // Reads a single character from the source
00053     virtual int ReadCharacter(void) = 0;
00054 
00055     // Returns the last character read (from ReadCharacter()) from the
00056     // source
00057     virtual int ReadPreviousCharacter(void) { return _previous_character; };
00058 
00059     // Puts back a character so that when ReadCharacter is called
00060     // again, it returns the character that was previously
00061     // read. Although this interface appears to require the ability to
00062     // put back an arbitrary number of characters, in practice we only
00063     // ever put back a single character. 
00064     virtual void UnreadCharacter(void) = 0;
00065     virtual bool AtEnd(void) const = 0;
00066 protected:
00067     int _previous_character;
00068 private:
00069     // The copy constructor and assignment operator are defined
00070     // to be private so we don't have to write them, or worry about
00071     // them being inappropriately used. The day we want them, we can 
00072     // write them. 
00073     LexerSource(const LexerSource &)            { return;       }
00074     LexerSource &operator=(const LexerSource &) { return *this; }
00075 };
00076 
00077 // This source allows input from a traditional C FILE *
00078 class FileLexerSource : public LexerSource
00079 {
00080 public:
00081     FileLexerSource(FILE *file);
00082     virtual ~FileLexerSource();
00083 
00084     virtual void SetNewSource(FILE *file);
00085     
00086     virtual int ReadCharacter(void);
00087     virtual void UnreadCharacter(void);
00088     virtual bool AtEnd(void) const;
00089 
00090 private:
00091     FILE *_file;
00092     FileLexerSource(const FileLexerSource &) : LexerSource() { return;  }
00093     FileLexerSource &operator=(const FileLexerSource &) { return *this; }
00094 };
00095 
00096 // This source allows input from a C++ stream. Note that
00097 // the user passes in a pointer to the stream.
00098 class InputStreamLexerSource : public LexerSource
00099 {
00100 public:
00101     InputStreamLexerSource(std::istream &stream);
00102     virtual ~InputStreamLexerSource();
00103 
00104     virtual void SetNewSource(std::istream &stream);
00105     
00106     virtual int ReadCharacter(void);
00107     virtual void UnreadCharacter(void);
00108     virtual bool AtEnd(void) const;
00109 
00110 private:
00111     std::istream *_stream;
00112     InputStreamLexerSource(const InputStreamLexerSource &) : LexerSource() { return;       }
00113     InputStreamLexerSource &operator=(const InputStreamLexerSource &) { return *this; }
00114 };
00115 
00116 // This source allows input from a traditional C string.
00117 class CharLexerSource : public LexerSource
00118 {
00119 public:
00120     CharLexerSource(const char *string, int offset=0);
00121     virtual ~CharLexerSource();
00122     
00123     virtual void SetNewSource(const char *string, int offset=0);
00124     virtual int ReadCharacter(void);
00125     virtual void UnreadCharacter(void);
00126     virtual bool AtEnd(void) const;
00127 
00128     virtual int GetCurrentLocation(void) const;
00129 private:
00130     const char *_string;
00131     int         _offset;
00132     CharLexerSource(const CharLexerSource &) : LexerSource() { return;       }
00133     CharLexerSource &operator=(const CharLexerSource &) { return *this; }
00134 };
00135 
00136 // This source allows input from a C++ string.
00137 class StringLexerSource : public LexerSource
00138 {
00139 public:
00140     StringLexerSource(const std::string *string, int offset=0);
00141     virtual ~StringLexerSource();
00142 
00143     virtual void SetNewSource(const std::string *string, int offset=0);
00144     
00145     virtual int ReadCharacter(void);
00146     virtual void UnreadCharacter(void);
00147     virtual bool AtEnd(void) const;
00148 
00149     virtual int GetCurrentLocation(void) const;
00150 private:
00151     const std::string *_string;
00152     int                _offset;
00153     StringLexerSource(const StringLexerSource &) : LexerSource() { return;       }
00154     StringLexerSource &operator=(const StringLexerSource &) { return *this; }
00155 };
00156 
00157 END_NAMESPACE
00158 
00159 #endif /* __CLASSAD_LEXER_SOURCE_H__ */
 All Classes Functions Variables Typedefs Enumerations Enumerator Friends