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 #ifndef _MOOT_GENERIC_LEXER_H
00030 #define _MOOT_GENERIC_LEXER_H
00031
00032 #include <stdio.h>
00033 #include <string.h>
00034 #include <stdarg.h>
00035 #include <string>
00036
00037 #include <mootIO.h>
00038 #include <mootCIO.h>
00039 #include <mootBufferIO.h>
00040
00041 namespace moot {
00042 using namespace std;
00043
00089 class GenericLexer {
00090 public:
00091
00092
00093
00094 static const int MGL_DEFAULT_BUFFER_SIZE = 8192;
00095
00096 public:
00097
00098
00099
00100
00103 mootio::mistream *mglin;
00104 mootio::mostream *mglout;
00105
00106 bool mgl_in_created;
00107 bool mgl_out_created;
00108
00109
00110
00113 size_t theLine;
00114 size_t theColumn;
00115 size_t theByte;
00116
00117
00118
00121 std::string lexname;
00122
00123
00124
00125
00128 std::string tokbuf;
00129 bool tokbuf_clear;
00130
00131
00132 public:
00133
00134
00135
00136
00140 GenericLexer(const std::string &myname="moot::GenericLexer",
00141 size_t line=0, size_t column=0, size_t byte=0)
00142 : mglin(NULL),
00143 mglout(NULL),
00144 mgl_in_created(false),
00145 mgl_out_created(false),
00146 theLine(line),
00147 theColumn(column),
00148 theByte(byte),
00149 lexname(myname),
00150 tokbuf_clear(false)
00151 {};
00152
00154 virtual ~GenericLexer(void);
00155
00157 virtual void reset(void);
00158
00160 virtual void clear(bool clear_input=true, bool clear_output=true);
00162
00163
00166 virtual void **mgl_yy_current_buffer_p(void)
00167 { return NULL; };
00168
00169
00174 virtual void mgl_begin(int stateno) {};
00175
00176
00178 virtual void mgl_yy_delete_buffer(void *buf)
00179 {
00180 yycarp("abstract method mgl_yy_delete_buffer() called!");
00181 abort();
00182 };
00183
00185 virtual void *mgl_yy_create_buffer(int size, FILE *unused=stdin) =0;
00186
00188 virtual void mgl_yy_switch_to_buffer(void *buf) =0;
00189
00191 virtual void mgl_yy_init_buffer(void *buf, FILE *unused=stdin) =0;
00193
00194
00198 virtual void from_mstream(mootio::mistream *in=NULL);
00199
00201 virtual void from_filename(const std::string &filename);
00202
00204 virtual void from_file(FILE *in=stdin);
00205
00207 virtual void from_buffer(const char *buf, size_t len);
00208
00210 inline void from_string(const char *s) {
00211 from_buffer(s, strlen(s));
00212 };
00214 inline void from_string(const std::string &s) {
00215 from_buffer(s.data(), s.size());
00216 };
00217
00219 inline void select_streams(FILE *in, FILE *out=stdout, const char *myname=NULL) {
00220 from_file(in);
00221 to_file(out);
00222 if (myname) lexname = myname;
00223 };
00224
00226 inline void select_string(const char *in, FILE *out=stderr, const char *myname=NULL) {
00227 from_string(in);
00228 to_file(out);
00229 if (myname) lexname = myname;
00230 };
00232
00233
00237 virtual void to_mstream(mootio::mostream *out=NULL);
00238
00240 virtual void to_filename(const std::string &filename);
00241
00243 virtual void to_file(FILE *out=stdout);
00245
00246
00247
00251 inline int yyinput(char *buffer, int &result, int max_size)
00252 {
00253 return (result = (mglin ? mglin->read(buffer, max_size) : 0));
00254 };
00256
00257
00261 inline void tokbuf_append(const char *text, size_t len)
00262 {
00263 if (tokbuf_clear) {
00264 tokbuf.assign(text,len);
00265 tokbuf_clear = false;
00266 } else {
00267 tokbuf.append(text,len);
00268 }
00269 };
00271
00272
00276 virtual void yycarp(const char *fmt, ...);
00278 };
00279
00280 };
00281
00282 #endif // _MOOT_GENERIC_LEXER_H