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