1 /* DLGLexerBase.c 2 * 3 * SOFTWARE RIGHTS 4 * 5 * We reserve no LEGAL rights to the Purdue Compiler Construction Tool 6 * Set (PCCTS) -- PCCTS is in the public domain. An individual or 7 * company may do whatever they wish with source code distributed with 8 * PCCTS or the code generated by PCCTS, including the incorporation of 9 * PCCTS, or its output, into commerical software. 10 * 11 * We encourage users to develop software with PCCTS. However, we do ask 12 * that credit is given to us for developing PCCTS. By "credit", 13 * we mean that if you incorporate our source code into one of your 14 * programs (commercial product, research project, or otherwise) that you 15 * acknowledge this fact somewhere in the documentation, research report, 16 * etc... If you like PCCTS and have developed a nice tool with the 17 * output, please mention that you developed it using PCCTS. In 18 * addition, we ask that this header remain intact in our source code. 19 * As long as these guidelines are kept, we expect to continue enhancing 20 * this system and expect to make other tools available as they are 21 * completed. 22 * 23 * ANTLR 1.33 24 * Terence Parr 25 * Parr Research Corporation 26 * with Purdue University and AHPCRC, University of Minnesota 27 * 1989-1998 28 */ 29 30 #include "pcctscfg.h" 31 32 #include "pccts_stdio.h" 33 #include "pccts_stdlib.h" 34 35 PCCTS_NAMESPACE_STD 36 37 /* I have to put this here due to C++ limitation 38 * that you can't have a 'forward' decl for enums. 39 * I hate C++!!!!!!!!!!!!!!! 40 */ 41 42 // MR1 43 // MR1 10-Apr-97 133MR1 Prevent use of varying sizes for the 44 // MR1 ANTLRTokenType enum 45 // MR1 46 47 enum ANTLRTokenType { TER_HATES_CPP=0, ITS_UTTER_GARBAGE, // MR1 48 WITH_SOME_GOOD_IDEAS=9999}; // MR1 49 50 #define ANTLR_SUPPORT_CODE 51 52 #include "pcctscfg.h" 53 #include DLEXERBASE_H 54 55 DLGLexerBase:: 56 DLGLexerBase(DLGInputStream *in, 57 unsigned bufsize, 58 int _interactive, 59 int _track_columns) 60 { 61 this->_bufsize = bufsize; 62 this->_lextext = new DLGChar[_bufsize]; 63 if ( this->_lextext==NULL ) { 64 panic("text buffer is NULL"); 65 } 66 this->_begexpr = this->_endexpr = NULL; 67 this->ch = this->bufovf = 0; 68 this->nextpos = NULL; 69 this->cl = 0; 70 this->add_erase = 0; 71 this->input = in; 72 this->_begcol = 0; 73 this->_endcol = 0; 74 this->_line = 1; 75 this->charfull = 0; 76 this->automaton = 0; 77 this->token_to_fill = NULL; 78 this->interactive = _interactive; 79 this->track_columns = _track_columns; 80 this->debugLexerFlag = 0; // MR1 81 this->parser = NULL; // MR1 82 this->lexErrCount=0; // MR11 83 } 84 85 // MR19 THM 86 87 void DLGLexerBase::reset() 88 { 89 this->charfull = 0; 90 this->_begcol = 0; 91 this->_endcol = 0; 92 this->automaton = 0; 93 this->_line=1; 94 this->lexErrCount=0; 95 } 96 97 void DLGLexerBase:: 98 setInputStream( DLGInputStream *in ) 99 { 100 this->input = in; 101 _line = 1; 102 charfull = 0; 103 } 104 105 /* saves dlg state, but not what feeds dlg (such as file position) */ 106 void DLGLexerBase:: 107 saveState(DLGState *state) 108 { 109 state->input = input; 110 state->interactive = interactive; 111 state->track_columns = track_columns; 112 state->auto_num = automaton; 113 state->add_erase = add_erase; 114 state->lookc = ch; 115 state->char_full = charfull; 116 state->begcol = _begcol; 117 state->endcol = _endcol; 118 state->line = _line; 119 state->lextext = _lextext; 120 state->begexpr = _begexpr; 121 state->endexpr = _endexpr; 122 state->bufsize = _bufsize; 123 state->bufovf = bufovf; 124 state->nextpos = nextpos; 125 state->class_num = cl; 126 state->debugLexerFlag = debugLexerFlag; // MR1 127 state->parser = parser; // MR1 128 } 129 130 void DLGLexerBase:: 131 restoreState(DLGState *state) 132 { 133 input = state->input; 134 interactive = state->interactive; 135 track_columns = state->track_columns; 136 automaton = state->auto_num; 137 add_erase = state->add_erase; 138 ch = state->lookc; 139 charfull = state->char_full; 140 _begcol = state->begcol; 141 _endcol = state->endcol; 142 _line = state->line; 143 _lextext = state->lextext; 144 _begexpr = state->begexpr; 145 _endexpr = state->endexpr; 146 _bufsize = state->bufsize; 147 bufovf = state->bufovf; 148 nextpos = state->nextpos; 149 cl = state->class_num; 150 debugLexerFlag = state->debugLexerFlag; // MR1 151 parser = state->parser; // MR1 152 } 153 154 /* erase what is currently in the buffer, and get a new reg. expr */ 155 void DLGLexerBase:: 156 skip() 157 { 158 add_erase = 1; 159 } 160 161 /* don't erase what is in the lextext buffer, add on to it */ 162 void DLGLexerBase:: 163 more() 164 { 165 add_erase = 2; 166 } 167 168 /* substitute c for the reg. expr last matched and is in the buffer */ 169 void DLGLexerBase:: 170 replchar(DLGChar c) 171 { 172 /* can't allow overwriting null at end of string */ 173 if (_begexpr < &_lextext[_bufsize-1]){ 174 *_begexpr = c; 175 *(_begexpr+1) = '\0'; 176 } 177 _endexpr = _begexpr; 178 nextpos = _begexpr + 1; 179 } 180 181 /* replace the string s for the reg. expr last matched and in the buffer */ 182 183 void DLGLexerBase:: 184 replstr(const DLGChar *s) /* MR20 const */ 185 { 186 register DLGChar *l= &_lextext[_bufsize -1]; 187 188 nextpos = _begexpr; 189 if (s){ 190 while ((nextpos <= l) && (*(nextpos++) = *(s++))){ 191 /* empty */ 192 } 193 /* correct for NULL at end of string */ 194 nextpos--; 195 } 196 if ((nextpos <= l) && (*(--s) == 0)){ 197 bufovf = 0; 198 }else{ 199 bufovf = 1; 200 } 201 *(nextpos) = '\0'; 202 _endexpr = nextpos - 1; 203 } 204 205 void DLGLexerBase:: 206 errstd(const char *s) /* MR20 const */ 207 { 208 lexErrCount++; /* MR11 */ 209 fprintf(stderr, 210 "%s near line %d (text was '%s')\n", 211 ((s == NULL) ? "Lexical error" : s), 212 _line,_lextext); 213 } 214 215 int DLGLexerBase:: 216 err_in() 217 { 218 fprintf(stderr,"No input stream, function, or string\n"); 219 /* return eof to get out gracefully */ 220 return EOF; 221 } 222 223 ANTLRTokenType DLGLexerBase:: 224 erraction() 225 { 226 errstd("invalid token"); 227 advance(); 228 skip(); 229 return (ANTLRTokenType) 0; // bogus, but satisfies compiler 230 } 231 232 _ANTLRTokenPtr DLGLexerBase:: 233 getToken() 234 { 235 if ( token_to_fill==NULL ) panic("NULL token_to_fill"); 236 ANTLRTokenType tt = nextTokenType(); 237 _ANTLRTokenPtr tk = token_to_fill->makeToken(tt, _lextext,_line); 238 return tk; 239 } 240 241 void DLGLexerBase:: 242 panic(const char *msg) /* MR20 const */ 243 { 244 fprintf(stderr, "DLG panic: %s\n", msg); 245 // 246 // 7-Apr-97 133MR1 247 // 248 exit(PCCTS_EXIT_FAILURE); // MR1 249 } 250 251 ANTLRParser * DLGLexerBase:: // MR1 252 setParser(ANTLRParser *p) { // MR1 253 ANTLRParser *oldValue=parser; // MR1 254 parser=p; // MR1 255 return oldValue; // MR1 256 } // MR1 257 // MR1 258 ANTLRParser * DLGLexerBase:: // MR1 259 getParser() { // MR1 260 return parser; // MR1 261 } // MR1 262 // MR1 263 int DLGLexerBase:: // MR1 264 debugLexer(int newValue) { // MR1 265 int oldValue=debugLexerFlag; // MR1 266 debugLexerFlag=newValue; // MR1 267 return oldValue; // MR1 268 } // MR1 269