GME  13
DLexerBase.h
Go to the documentation of this file.
00001 /* DLGLexerBase.h
00002  *
00003  * SOFTWARE RIGHTS
00004  *
00005  * We reserve no LEGAL rights to the Purdue Compiler Construction Tool
00006  * Set (PCCTS) -- PCCTS is in the public domain.  An individual or
00007  * company may do whatever they wish with source code distributed with
00008  * PCCTS or the code generated by PCCTS, including the incorporation of
00009  * PCCTS, or its output, into commerical software.
00010  *
00011  * We encourage users to develop software with PCCTS.  However, we do ask
00012  * that credit is given to us for developing PCCTS.  By "credit",
00013  * we mean that if you incorporate our source code into one of your
00014  * programs (commercial product, research project, or otherwise) that you
00015  * acknowledge this fact somewhere in the documentation, research report,
00016  * etc...  If you like PCCTS and have developed a nice tool with the
00017  * output, please mention that you developed it using PCCTS.  In
00018  * addition, we ask that this header remain intact in our source code.
00019  * As long as these guidelines are kept, we expect to continue enhancing
00020  * this system and expect to make other tools available as they are
00021  * completed.
00022  *
00023  * ANTLR 1.33
00024  * Terence Parr
00025  * Parr Research Corporation
00026  * with Purdue University and AHPCRC, University of Minnesota
00027  * 1989-1998
00028  */
00029 
00030 #ifndef DLGX_H
00031 #define DLGX_H
00032 
00033 #include <stdio.h>
00034 #include "config.h"
00035 #include ATOKEN_H
00036 #include ATOKENSTREAM_H
00037 
00038 class ANTLRParser;                                                      // MR1
00039 
00040 /* must define what a char looks like; can make this a class too */
00041 typedef char DLGChar;
00042 
00043 /*  Can have it as a class too: (ack this looks weird; is it right?)
00044 class DLGChar {
00045 private:
00046         int c;
00047 public:
00048         DLGChar(int ch) { c = ch; }
00049         int atom() { return c; }
00050 };
00051 */
00052 
00053 /* user must subclass this */
00054 class DLGInputStream {
00055 public:
00056         virtual int nextChar() = 0;
00057 };
00058 
00059 /* Predefined char stream: Input from FILE */
00060 class DLGFileInput : public DLGInputStream {
00061 private:
00062         int found_eof;
00063         FILE *input;
00064 public:
00065         DLGFileInput(FILE *f) { input = f; found_eof = 0; }
00066         int nextChar() {
00067                         int c;
00068                         if ( found_eof ) return EOF;
00069                         else {
00070                                 c=getc(input);
00071                                 if ( c==EOF ) found_eof = 1;
00072                                 return c;
00073                         }
00074         }
00075     void DLGFileReset(FILE *f) {input=f; found_eof = 0; };              // MR11
00076 };
00077 
00078 // MR9  Suggested by Bruce Guenter (bruceg@qcc.sk.ca)
00079 // MR9  Make DLGStringInput const correct
00080 
00081 /* Predefined char stream: Input from string */
00082 class DLGStringInput : public DLGInputStream {
00083 private:
00084         const DLGChar *input;                                           // MR9
00085         const DLGChar *p;                                               // MR9
00086 public:
00087         DLGStringInput(const DLGChar *s) { input = s; p = &input[0];}   // MR9
00088         int nextChar()
00089                 {
00090                         if (*p) return (int) *p++;
00091                         else return EOF;
00092                 }
00093 
00094     void DLGStringReset(DLGChar *s) {input=s; p= &input[0]; };      // MR11
00095 };
00096 
00097 class DLGState {
00098 public:
00099         DLGInputStream *input;
00100         int interactive;
00101         int track_columns;
00102         int auto_num;
00103         int add_erase;
00104         int lookc;
00105         int char_full;
00106         int begcol, endcol;
00107         int line;
00108         DLGChar *lextext, *begexpr, *endexpr;
00109         int bufsize;
00110         int bufovf;
00111         DLGChar *nextpos;
00112         int     class_num;
00113         int     debugLexerFlag;                                         // MR1
00114         ANTLRParser *parser;                                            // MR1
00115 };
00116 
00117 /* user must subclass this */
00118 class DLGLexerBase : public ANTLRTokenStream {
00119 public:
00120         virtual ANTLRTokenType erraction();
00121 
00122 protected:
00123         DLGInputStream *input;
00124         int interactive;
00125         int track_columns;
00126         DLGChar *_lextext;      /* text of most recently matched token */
00127         DLGChar *_begexpr;      /* beginning of last reg expr recogn. */
00128         DLGChar *_endexpr;      /* beginning of last reg expr recogn. */
00129         int     _bufsize;               /* number of characters in lextext */
00130         int     _begcol;                /* column that first character of token is in*/
00131         int     _endcol;                /* column that last character of token is in */
00132         int     _line;                  /* line current token is on */
00133         int     ch;                             /* character to determine next state */
00134         int     bufovf;                 /* indicates that buffer too small for text */
00135         int     charfull;
00136         DLGChar *nextpos;       /* points to next available position in lextext*/
00137         int     cl;
00138         int automaton;
00139         int     add_erase;
00140         DLGChar ebuf[70];
00141         _ANTLRTokenPtr token_to_fill;
00142 
00143         virtual _ANTLRTokenPtr getToken();
00144         int     debugLexerFlag;                                         // MR1
00145         ANTLRParser     *parser;                                        // MR1
00146 public:
00147         virtual void advance(void) = 0;
00148         void    skip(void);             /* erase lextext, look for antoher token */
00149         void    more(void);             /* keep lextext, look for another token */
00150         void    mode(int k);    /* switch to automaton 'k' */
00151         void    saveState(DLGState *);
00152         void    restoreState(DLGState *);
00153         virtual ANTLRTokenType nextTokenType(void)=0;/* get next token */
00154         void    replchar(DLGChar c);    /* replace last recognized reg. expr. with
00155                                                                          a character */
00156         void    replstr(DLGChar *s);    /* replace last recognized reg. expr. with
00157                                                                          a string */
00158         virtual int err_in();                                           // MR1
00159         virtual void errstd(char *);                                    // MR1
00160         int             line()          { return _line; }
00161         void    set_line(int newValue)  { _line=newValue; };            // MR1
00162         virtual void newline()  { _line++; }
00163         DLGChar *lextext()      { return _lextext; }
00164 
00165         int             begcol()        { return _begcol; }
00166         int             endcol()        { return _endcol; }
00167         void    set_begcol(int a)       { _begcol=a; }
00168         void    set_endcol(int a)       { _endcol=a; }
00169         DLGChar *begexpr()      { return _begexpr; }
00170         DLGChar *endexpr()      { return _endexpr; }
00171         int             bufsize()       { return _bufsize; }
00172 
00173         void    setToken(ANTLRAbstractToken *t) { token_to_fill = t; }
00174 
00175         void    setInputStream(DLGInputStream *);
00176         DLGLexerBase(DLGInputStream *in,
00177                                  unsigned bufsize=2000,
00178                                  int interactive=0,
00179                                  int track_columns=0);
00180         virtual ~DLGLexerBase() { delete [] _lextext; }
00181         virtual void panic(char *msg);                                  // MR1
00182         void    trackColumns() {
00183                                 track_columns = 1;
00184                                 this->_begcol = 0;
00185                                 this->_endcol = 0;
00186                         };
00187         virtual ANTLRParser *setParser(ANTLRParser *p);                 // MR1
00188         virtual ANTLRParser *getParser();                               // MR1
00189         virtual int debugLexer(int value);                              // MR1
00190     int     lexErrCount;                            // MR12
00191 };
00192 
00193 #endif