00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef __LEXER_SOURCE_H__
00025 #define __LEXER_SOURCE_H__
00026
00027 #include "common.h"
00028 #include <iostream>
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 class LexerSource
00042 {
00043 public:
00044 LexerSource()
00045 {
00046 return;
00047 }
00048 virtual ~LexerSource()
00049 {
00050 return;
00051 }
00052
00053
00054 virtual int ReadCharacter(void) = 0;
00055
00056
00057
00058 virtual int ReadPreviousCharacter(void) { return _previous_character; };
00059
00060
00061
00062
00063
00064
00065 virtual void UnreadCharacter(void) = 0;
00066 virtual bool AtEnd(void) const = 0;
00067 protected:
00068 int _previous_character;
00069 private:
00070
00071
00072
00073
00074 LexerSource(const LexerSource &source) { return; }
00075 LexerSource &operator=(const LexerSource &source) { return *this; }
00076 };
00077
00078
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
00098
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
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
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