1 /* DLexer.h (formerly DLexer.cpp) 2 * 3 * This was renamed because the use of the .cpp extension caused problems 4 * with IDEs. 5 * 6 * SOFTWARE RIGHTS 7 * 8 * We reserve no LEGAL rights to the Purdue Compiler Construction Tool 9 * Set (PCCTS) -- PCCTS is in the public domain. An individual or 10 * company may do whatever they wish with source code distributed with 11 * PCCTS or the code generated by PCCTS, including the incorporation of 12 * PCCTS, or its output, into commerical software. 13 * 14 * We encourage users to develop software with PCCTS. However, we do ask 15 * that credit is given to us for developing PCCTS. By "credit", 16 * we mean that if you incorporate our source code into one of your 17 * programs (commercial product, research project, or otherwise) that you 18 * acknowledge this fact somewhere in the documentation, research report, 19 * etc... If you like PCCTS and have developed a nice tool with the 20 * output, please mention that you developed it using PCCTS. In 21 * addition, we ask that this header remain intact in our source code. 22 * As long as these guidelines are kept, we expect to continue enhancing 23 * this system and expect to make other tools available as they are 24 * completed. 25 * 26 * ANTLR 1.33 27 * Terence Parr 28 * Parr Research Corporation 29 * with Purdue University and AHPCRC, University of Minnesota 30 * 1989-2000 31 */ 32 33 #include <assert.h> 34 35 #define ZZINC {if ( track_columns ) (++_endcol);} 36 37 #define ZZGETC {ch = input->nextChar(); cl = ZZSHIFT(ch);} 38 39 #define ZZNEWSTATE (newstate = dfa[state][cl]) 40 41 #ifndef ZZCOPY 42 #define ZZCOPY \ 43 /* Truncate matching buffer to size (not an error) */ \ 44 if (nextpos < lastpos){ \ 45 *(nextpos++) = ch; \ 46 }else{ \ 47 bufovf = 1; \ 48 } 49 #endif 50 51 void DLGLexer:: 52 mode( int m ) 53 { 54 /* points to base of dfa table */ 55 if (m<MAX_MODE){ 56 automaton = m; 57 /* have to redo class since using different compression */ 58 cl = ZZSHIFT(ch); 59 }else{ 60 sprintf((char *)ebuf,"Invalid automaton mode = %d ",m); 61 errstd(ebuf); 62 } 63 } 64 65 ANTLRTokenType DLGLexer:: 66 nextTokenType(void) 67 { 68 register int state, newstate; 69 /* last space reserved for the null char */ 70 register DLGChar *lastpos; 71 ANTLRTokenType tk; 72 73 skip: 74 bufovf = 0; 75 lastpos = &_lextext[_bufsize-1]; 76 nextpos = _lextext; 77 _begcol = _endcol+1; 78 more: 79 _begexpr = nextpos; 80 if ( interactive ) { 81 /* interactive version of automaton */ 82 /* if there is something in ch, process it */ 83 state = newstate = dfa_base[automaton]; 84 if (charfull){ 85 ZZINC; 86 ZZCOPY; 87 ZZNEWSTATE; 88 } 89 while (alternatives[newstate]){ 90 state = newstate; 91 ZZGETC; 92 ZZINC; 93 ZZCOPY; 94 ZZNEWSTATE; 95 } 96 /* figure out if last character really part of token */ 97 if ((state != dfa_base[automaton]) && (newstate == DfaStates)){ 98 charfull = 1; 99 --nextpos; 100 }else{ 101 charfull = 0; 102 state = newstate; 103 } 104 *(nextpos) = '\0'; 105 /* Able to transition out of start state to some non err state?*/ 106 if ( state == dfa_base[automaton] ){ 107 /* make sure doesn't get stuck */ 108 advance(); 109 } 110 } 111 else { /* non-interactive version of automaton */ 112 if (!charfull) 113 advance(); 114 else 115 ZZINC; 116 state = dfa_base[automaton]; 117 while (ZZNEWSTATE != DfaStates) { 118 state = newstate; 119 assert(state <= sizeof(dfa)/sizeof(dfa[0])); 120 ZZCOPY; 121 ZZGETC; 122 ZZINC; 123 } 124 charfull = 1; 125 if ( state == dfa_base[automaton] ){ 126 if (nextpos < lastpos){ 127 *(nextpos++) = ch; 128 }else{ 129 bufovf = 1; 130 } 131 *nextpos = '\0'; 132 /* make sure doesn't get stuck */ 133 advance(); 134 }else{ 135 *nextpos = '\0'; 136 } 137 } 138 if ( track_columns ) _endcol -= charfull; 139 _endexpr = nextpos -1; 140 add_erase = 0; 141 #ifdef OLD 142 tk = (ANTLRTokenType) 143 (*actions[accepts[state]])(this); // must pass this manually 144 // actions is not a [] of pointers 145 // to member functions. 146 #endif 147 tk = (this->*actions[accepts[state]])(); 148 149 // MR1 150 // MR1 11-Apr-97 Help for tracking DLG results 151 // MR1 152 153 #ifdef DEBUG_LEXER 154 155 /* MR1 */ if (debugLexerFlag) { 156 /* MR1 */ if (parser != NULL) { 157 /* MR1 */ /* MR23 */ printMessage(stdout, "\ntoken name=%s",parser->parserTokenName(tk)); 158 /* MR1 */ } else { 159 /* MR1 */ /* MR23 */ printMessage(stdout, "\ntoken nnumber=%d",tk); 160 /* MR1 */ }; 161 /* MR1 */ /* MR23 */ printMessage(stdout, " lextext=(%s) mode=%d", 162 /* MR1 */ (_lextext[0]=='\n' && _lextext[1]==0) ? 163 /* MR1 */ "newline" : _lextext, 164 /* MR1 */ automaton); 165 /* MR1 */ if (interactive && !charfull) { 166 /* MR1 */ /* MR23 */ printMessage(stdout, " char=empty"); 167 /* MR1 */ } else { 168 /* MR1 */ if (ch=='\n') { 169 /* MR1 */ /* MR23 */ printMessage(stdout, " char=newline"); 170 /* MR1 */ } else { 171 /* MR1 */ /* MR23 */ printMessage(stdout, " char=(%c)",ch); 172 /* MR1 */ }; 173 /* MR1 */ }; 174 /* MR1 */ /* MR23 */ printMessage(stdout, " %s\n", 175 /* MR1 */ (add_erase==1 ? "skip()" : 176 /* MR1 */ add_erase==2 ? "more()" : 177 /* MR1 */ "")); 178 /* MR1 */ }; 179 180 #endif 181 182 switch (add_erase) { 183 case 1: goto skip; 184 case 2: goto more; 185 } 186 return tk; 187 } 188 189 void DLGLexer:: 190 advance() 191 { 192 if ( input==NULL ) err_in(); 193 ZZGETC; charfull = 1; ZZINC; 194 } 195