Home | History | Annotate | Download | only in h
      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 #define ZZINC {if ( track_columns ) (++_endcol);}
     34 
     35 #define ZZGETC {ch = input->nextChar(); cl = ZZSHIFT(ch);}
     36 
     37 #define ZZNEWSTATE	(newstate = dfa[state][cl])
     38 
     39 #ifndef ZZCOPY
     40 #define ZZCOPY	\
     41 	/* Truncate matching buffer to size (not an error) */	\
     42 	if (nextpos < lastpos){				\
     43 		*(nextpos++) = ch;			\
     44 	}else{							\
     45 		bufovf = 1;					\
     46 	}
     47 #endif
     48 
     49 void DLGLexer::
     50 mode( int m )
     51 {
     52 	/* points to base of dfa table */
     53 	if (m<MAX_MODE){
     54 		automaton = m;
     55 		/* have to redo class since using different compression */
     56 		cl = ZZSHIFT(ch);
     57 	}else{
     58 		sprintf((char *)ebuf,"Invalid automaton mode = %d ",m);
     59 		errstd(ebuf);
     60 	}
     61 }
     62 
     63 ANTLRTokenType DLGLexer::
     64 nextTokenType(void)
     65 {
     66 	register int state, newstate;
     67 	/* last space reserved for the null char */
     68 	register DLGChar *lastpos;
     69 	ANTLRTokenType tk;
     70 
     71 skip:
     72 	bufovf = 0;
     73 	lastpos = &_lextext[_bufsize-1];
     74 	nextpos = _lextext;
     75 	_begcol = _endcol+1;
     76 more:
     77 	_begexpr = nextpos;
     78 	if ( interactive ) {
     79 		/* interactive version of automaton */
     80 		/* if there is something in ch, process it */
     81 		state = newstate = dfa_base[automaton];
     82 		if (charfull){
     83 			ZZINC;
     84 			ZZCOPY;
     85 			ZZNEWSTATE;
     86 		}
     87 		while (alternatives[newstate]){
     88 			state = newstate;
     89 			ZZGETC;
     90 			ZZINC;
     91 			ZZCOPY;
     92 			ZZNEWSTATE;
     93 		}
     94 		/* figure out if last character really part of token */
     95 		if ((state != dfa_base[automaton]) && (newstate == DfaStates)){
     96 			charfull = 1;
     97 			--nextpos;
     98 		}else{
     99 			charfull = 0;
    100 			state = newstate;
    101 		}
    102 		*(nextpos) = '\0';
    103 		/* Able to transition out of start state to some non err state?*/
    104 		if ( state == dfa_base[automaton] ){
    105 			/* make sure doesn't get stuck */
    106 			advance();
    107 		}
    108 	}
    109 	else { /* non-interactive version of automaton */
    110 		if (!charfull)
    111 			advance();
    112 		else
    113 			ZZINC;
    114 		state = dfa_base[automaton];
    115 		while (ZZNEWSTATE != DfaStates) {
    116 			state = newstate;
    117 			ZZCOPY;
    118 			ZZGETC;
    119 			ZZINC;
    120 		}
    121 		charfull = 1;
    122 		if ( state == dfa_base[automaton] ){
    123 			if (nextpos < lastpos){
    124 				*(nextpos++) = ch;
    125 			}else{
    126 				bufovf = 1;
    127 			}
    128 			*nextpos = '\0';
    129 			/* make sure doesn't get stuck */
    130 			advance();
    131 		}else{
    132 			*nextpos = '\0';
    133 		}
    134 	}
    135 	if ( track_columns ) _endcol -= charfull;
    136 	_endexpr = nextpos -1;
    137 	add_erase = 0;
    138 #ifdef OLD
    139 	tk = (ANTLRTokenType)
    140 		 (*actions[accepts[state]])(this);	// must pass this manually
    141 											// actions is not a [] of pointers
    142 											// to member functions.
    143 #endif
    144 	tk = (this->*actions[accepts[state]])();
    145 
    146 // MR1
    147 // MR1 11-Apr-97  Help for tracking DLG results
    148 // MR1
    149 
    150 #ifdef DEBUG_LEXER
    151 
    152 /* MR1 */        if (debugLexerFlag) {
    153 /* MR1 */	   if (parser != NULL) {
    154 /* MR1 */	     /* MR23 */ printMessage(stdout, "\ntoken name=%s",parser->parserTokenName(tk));
    155 /* MR1 */	   } else {
    156 /* MR1 */	     /* MR23 */ printMessage(stdout, "\ntoken nnumber=%d",tk);
    157 /* MR1 */	   };
    158 /* MR1 */	   /* MR23 */ printMessage(stdout, " lextext=(%s) mode=%d",
    159 /* MR1 */		 (_lextext[0]=='\n' && _lextext[1]==0) ?
    160 /* MR1 */			"newline" : _lextext,
    161 /* MR1 */				automaton);
    162 /* MR1 */          if (interactive && !charfull) {
    163 /* MR1 */	     /* MR23 */ printMessage(stdout, " char=empty");
    164 /* MR1 */          } else {
    165 /* MR1 */	     if (ch=='\n') {
    166 /* MR1 */	       /* MR23 */ printMessage(stdout, " char=newline");
    167 /* MR1 */	     } else {
    168 /* MR1 */	       /* MR23 */ printMessage(stdout, " char=(%c)",ch);
    169 /* MR1 */	     };
    170 /* MR1 */	   };
    171 /* MR1 */	   /* MR23 */ printMessage(stdout, " %s\n",
    172 /* MR1 */		 (add_erase==1 ? "skip()" :
    173 /* MR1 */		  add_erase==2 ? "more()" :
    174 /* MR1 */		  ""));
    175 /* MR1 */        };
    176 
    177 #endif
    178 
    179 	switch (add_erase) {
    180 		case 1: goto skip;
    181 		case 2: goto more;
    182 	}
    183 	return tk;
    184 }
    185 
    186 void DLGLexer::
    187 advance()
    188 {
    189 	if ( input==NULL ) err_in();
    190 	ZZGETC; charfull = 1; ZZINC;
    191 }
    192