GME  13
DLexerBase.cpp
Go to the documentation of this file.
00001 /* DLGLexerBase.c
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 // See http://support.microsoft.com/kb/148652
00030 //#include "forcelib.h"
00031 #include "stdafx.h"
00032 #include <stdio.h>
00033 #include <stdlib.h>
00034 
00035 /* I have to put this here due to C++ limitation
00036  * that you can't have a 'forward' decl for enums.
00037  * I hate C++!!!!!!!!!!!!!!!
00038  */
00039 
00040 // MR1
00041 // MR1  10-Apr-97  133MR1  Prevent use of varying sizes for the
00042 // MR1                          ANTLRTokenType enum
00043 // MR1
00044 
00045 enum ANTLRTokenType { TER_HATES_CPP=0, ITS_UTTER_GARBAGE,               // MR1
00046                                          WITH_SOME_GOOD_IDEAS=9999};    // MR1
00047 
00048 #define ANTLR_SUPPORT_CODE
00049 
00050 #include "config.h"
00051 #include DLEXERBASE_H
00052 
00053 DLGLexerBase::
00054 DLGLexerBase(DLGInputStream *in,
00055              unsigned bufsize,
00056              int _interactive,
00057              int _track_columns)
00058 {
00059         this->_bufsize = bufsize;
00060         this->_lextext = new DLGChar[_bufsize];
00061         if ( this->_lextext==NULL ) {
00062             panic("text buffer is NULL");
00063         }
00064         this->_begexpr = this->_endexpr = NULL;
00065         this->ch = this->bufovf = 0;
00066         this->nextpos = NULL;
00067         this->cl = 0;
00068         this->add_erase = 0;
00069         this->input = in;
00070         this->_begcol = 0;
00071         this->_endcol = 0;
00072         this->_line = 1;
00073         this->charfull = 0;
00074         this->automaton = 0;
00075         this->token_to_fill = NULL;
00076         this->interactive = _interactive;
00077         this->track_columns = _track_columns;
00078         this->debugLexerFlag = 0;                                       // MR1
00079         this->parser = NULL;                                            // MR1
00080     this->lexErrCount=0;                        // MR11
00081 }
00082 
00083 void DLGLexerBase::
00084 setInputStream( DLGInputStream *in )
00085 {
00086         this->input = in;
00087         _line = 1;
00088         charfull = 0;
00089 }
00090 
00091 /* saves dlg state, but not what feeds dlg (such as file position) */
00092 void DLGLexerBase::
00093 saveState(DLGState *state)
00094 {
00095         state->input = input;
00096         state->interactive = interactive;
00097         state->track_columns = track_columns;
00098         state->auto_num = automaton;
00099         state->add_erase = add_erase;
00100         state->lookc = ch;
00101         state->char_full = charfull;
00102         state->begcol = _begcol;
00103         state->endcol = _endcol;
00104         state->line = _line;
00105         state->lextext = _lextext;
00106         state->begexpr = _begexpr;
00107         state->endexpr = _endexpr;
00108         state->bufsize = _bufsize;
00109         state->bufovf = bufovf;
00110         state->nextpos = nextpos;
00111         state->class_num = cl;
00112         state->debugLexerFlag = debugLexerFlag;                         // MR1
00113         state->parser = parser;                                         // MR1
00114 }
00115 
00116 void DLGLexerBase::
00117 restoreState(DLGState *state)
00118 {
00119         input = state->input;
00120         interactive = state->interactive;
00121         track_columns = state->track_columns;
00122         automaton = state->auto_num;
00123         add_erase = state->add_erase;
00124         ch = state->lookc;
00125         charfull = state->char_full;
00126         _begcol = state->begcol;
00127         _endcol = state->endcol;
00128         _line = state->line;
00129         _lextext = state->lextext;
00130         _begexpr = state->begexpr;
00131         _endexpr = state->endexpr;
00132         _bufsize = state->bufsize;
00133         bufovf = state->bufovf;
00134         nextpos = state->nextpos;
00135         cl = state->class_num;
00136         debugLexerFlag = state->debugLexerFlag;                         // MR1
00137         parser = state->parser;                                         // MR1
00138 }
00139 
00140 /* erase what is currently in the buffer, and get a new reg. expr */
00141 void DLGLexerBase::
00142 skip()
00143 {
00144         add_erase = 1;
00145 }
00146 
00147 /* don't erase what is in the lextext buffer, add on to it */
00148 void DLGLexerBase::
00149 more()
00150 {
00151         add_erase = 2;
00152 }
00153 
00154 /* substitute c for the reg. expr last matched and is in the buffer */
00155 void DLGLexerBase::
00156 replchar(DLGChar c)
00157 {
00158         /* can't allow overwriting null at end of string */
00159         if (_begexpr < &_lextext[_bufsize-1]){
00160                 *_begexpr = c;
00161                 *(_begexpr+1) = '\0';
00162         }
00163         _endexpr = _begexpr;
00164         nextpos = _begexpr + 1;
00165 }
00166 
00167 /* replace the string s for the reg. expr last matched and in the buffer */
00168 void DLGLexerBase::
00169 replstr(register DLGChar *s)
00170 {
00171         register DLGChar *l= &_lextext[_bufsize -1];
00172 
00173         nextpos = _begexpr;
00174         if (s){
00175                 while ((nextpos <= l) && (*(nextpos++) = *(s++))){
00176                         /* empty */
00177                 }
00178                 /* correct for NULL at end of string */
00179                 nextpos--;
00180         }
00181         if ((nextpos <= l) && (*(--s) == 0)){
00182                 bufovf = 0;
00183         }else{
00184                 bufovf = 1;
00185         }
00186         *(nextpos) = '\0';
00187         _endexpr = nextpos - 1;
00188 }
00189 
00190 void DLGLexerBase::
00191 errstd(char *s)
00192 {
00193         lexErrCount++;                              /* MR11 */
00194         fprintf(stderr,
00195                 "%s near line %d (text was '%s')\n",
00196                 ((s == NULL) ? "Lexical error" : s),
00197                 _line,_lextext);
00198 }
00199 
00200 int DLGLexerBase::
00201 err_in()
00202 {
00203         fprintf(stderr,"No input stream, function, or string\n");
00204         /* return eof to get out gracefully */
00205         return EOF;
00206 }
00207 
00208 ANTLRTokenType DLGLexerBase::
00209 erraction()
00210 {
00211         errstd("invalid token");
00212         advance();
00213         skip();
00214         return (ANTLRTokenType) 0;      // bogus, but satisfies compiler
00215 }
00216 
00217 _ANTLRTokenPtr DLGLexerBase::
00218 getToken()
00219 {
00220         if ( token_to_fill==NULL ) panic("NULL token_to_fill");
00221         ANTLRTokenType tt = nextTokenType();
00222         _ANTLRTokenPtr tk = token_to_fill->makeToken(tt, _lextext,_line);
00223         return tk;
00224 }
00225 
00226 void DLGLexerBase::
00227 panic(char *msg)
00228 {
00229         fprintf(stderr, "DLG panic: %s\n", msg);
00230 //
00231 //  7-Apr-97 133MR1
00232 //
00233         exit(PCCTS_EXIT_FAILURE);                                       // MR1
00234 }
00235 
00236 ANTLRParser * DLGLexerBase::                                            // MR1
00237 setParser(ANTLRParser *p) {                                             // MR1
00238   ANTLRParser   *oldValue=parser;                                       // MR1
00239   parser=p;                                                             // MR1
00240   return oldValue;                                                      // MR1
00241 }                                                                       // MR1
00242                                                                         // MR1
00243 ANTLRParser * DLGLexerBase::                                            // MR1
00244 getParser() {                                                           // MR1
00245   return parser;                                                        // MR1
00246 }                                                                       // MR1
00247                                                                         // MR1
00248 int DLGLexerBase::                                                      // MR1
00249 debugLexer(int newValue) {                                              // MR1
00250   int   oldValue=debugLexerFlag;                                        // MR1
00251   debugLexerFlag=newValue;                                              // MR1
00252   return oldValue;                                                      // MR1
00253 }                                                                       // MR1