Main Page | Class Hierarchy | Class List | File List | Class Members

lexerSource.h

00001 /*********************************************************************
00002  *
00003  * Condor ClassAd library
00004  * Copyright (C) 1990-2003, Condor Team, Computer Sciences Department,
00005  * University of Wisconsin-Madison, WI and Rajesh Raman.
00006  *
00007  * This source code is covered by the Condor Public License, which can
00008  * be found in the accompanying LICENSE file, or online at
00009  * www.condorproject.org.
00010  *
00011  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00012  * AND THE UNIVERSITY OF WISCONSIN-MADISON "AS IS" AND ANY EXPRESS OR
00013  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00014  * WARRANTIES OF MERCHANTABILITY, OF SATISFACTORY QUALITY, AND FITNESS
00015  * FOR A PARTICULAR PURPOSE OR USE ARE DISCLAIMED. THE COPYRIGHT
00016  * HOLDERS AND CONTRIBUTORS AND THE UNIVERSITY OF WISCONSIN-MADISON
00017  * MAKE NO MAKE NO REPRESENTATION THAT THE SOFTWARE, MODIFICATIONS,
00018  * ENHANCEMENTS OR DERIVATIVE WORKS THEREOF, WILL NOT INFRINGE ANY
00019  * PATENT, COPYRIGHT, TRADEMARK, TRADE SECRET OR OTHER PROPRIETARY
00020  * RIGHT.
00021  *
00022  *********************************************************************/
00023 
00024 #ifndef __LEXER_SOURCE_H__
00025 #define __LEXER_SOURCE_H__
00026 
00027 #include "common.h"
00028 #include <iostream>
00029 
00030 /*
00031  * LexerSource is a class that provides an abstract interface to the
00032  * lexer. The lexer reads tokens and gives them to the parser.  The
00033  * lexer reads single characters from a LexerSource. Because we want
00034  * to read characters from different types of sources (files, strings,
00035  * etc.) without burdening the lexer, the lexer uses this LexerSource
00036  * abstract class. There are several implementations of the
00037  * LexerSource that provide access to specific types of sources. 
00038  *
00039  */
00040 
00041 class LexerSource
00042 {
00043 public:
00044         LexerSource()
00045         {
00046                 return;
00047         }
00048         virtual ~LexerSource()
00049         {
00050                 return;
00051         }
00052         
00053         // Reads a single character from the source
00054         virtual int ReadCharacter(void) = 0;
00055 
00056         // Returns the last character read (from ReadCharacter()) from the
00057         // source
00058         virtual int ReadPreviousCharacter(void) { return _previous_character; };
00059 
00060         // Puts back a character so that when ReadCharacter is called
00061         // again, it returns the character that was previously
00062         // read. Although this interface appears to require the ability to
00063         // put back an arbitrary number of characters, in practice we only
00064         // ever put back a single character. 
00065         virtual void UnreadCharacter(void) = 0;
00066         virtual bool AtEnd(void) const = 0;
00067 protected:
00068         int _previous_character;
00069 private:
00070     // The copy constructor and assignment operator are defined
00071     // to be private so we don't have to write them, or worry about
00072     // them being inappropriately used. The day we want them, we can 
00073     // write them. 
00074     LexerSource(const LexerSource &source)            { return;       }
00075     LexerSource &operator=(const LexerSource &source) { return *this; }
00076 };
00077 
00078 // This source allows input from a traditional C FILE *
00079 class FileLexerSource : public LexerSource
00080 {
00081 public:
00082         FileLexerSource(FILE *file);
00083         virtual ~FileLexerSource();
00084 
00085         virtual void SetNewSource(FILE *file);
00086         
00087         virtual int ReadCharacter(void);
00088         virtual void UnreadCharacter(void);
00089         virtual bool AtEnd(void) const;
00090 
00091 private:
00092         FILE *_file;
00093     FileLexerSource(const FileLexerSource &source)            { return;       }
00094     FileLexerSource &operator=(const FileLexerSource &source) { return *this; }
00095 };
00096 
00097 // This source allows input from a C++ stream. Note that
00098 // the user passes in a pointer to the stream.
00099 class InputStreamLexerSource : public LexerSource
00100 {
00101 public:
00102         InputStreamLexerSource(std::istream &stream);
00103         virtual ~InputStreamLexerSource();
00104 
00105         virtual void SetNewSource(std::istream &stream);
00106         
00107         virtual int ReadCharacter(void);
00108         virtual void UnreadCharacter(void);
00109         virtual bool AtEnd(void) const;
00110 
00111 private:
00112         std::istream *_stream;
00113     InputStreamLexerSource(const InputStreamLexerSource &source)            { return;       }
00114     InputStreamLexerSource &operator=(const InputStreamLexerSource &source) { return *this; }
00115 };
00116 
00117 // This source allows input from a traditional C string.
00118 class CharLexerSource : public LexerSource
00119 {
00120 public:
00121         CharLexerSource(const char *string, int offset=0);
00122         virtual ~CharLexerSource();
00123         
00124         virtual void SetNewSource(const char *string, int offset=0);
00125         virtual int ReadCharacter(void);
00126         virtual void UnreadCharacter(void);
00127         virtual bool AtEnd(void) const;
00128 
00129         virtual int GetCurrentLocation(void) const;
00130 private:
00131         const char *_source_start;
00132         const char *_current;
00133     CharLexerSource(const CharLexerSource &source)            { return;       }
00134     CharLexerSource &operator=(const CharLexerSource &source) { return *this; }
00135 };
00136 
00137 // This source allows input from a C++ string.
00138 class StringLexerSource : public LexerSource
00139 {
00140 public:
00141         StringLexerSource(const std::string *string, int offset=0);
00142         virtual ~StringLexerSource();
00143 
00144         virtual void SetNewSource(const std::string *string, int offset=0);
00145         
00146         virtual int ReadCharacter(void);
00147         virtual void UnreadCharacter(void);
00148         virtual bool AtEnd(void) const;
00149 
00150         virtual int GetCurrentLocation(void) const;
00151 private:
00152         const std::string *_string;
00153         int           _offset;
00154     StringLexerSource(const StringLexerSource &source)            { return;       }
00155     StringLexerSource &operator=(const StringLexerSource &source) { return *this; }
00156 };
00157 
00158 #endif /* __LEXER_SOURCE_H__ */