summaryrefslogtreecommitdiffstats
path: root/BaseTools/Source/C/VfrCompile/Pccts/h/DLexerBase.cpp
blob: b218afc0381145302694509e681e09b5fd53d9ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/* DLGLexerBase.c
 *
 * SOFTWARE RIGHTS
 *
 * We reserve no LEGAL rights to the Purdue Compiler Construction Tool
 * Set (PCCTS) -- PCCTS is in the public domain.  An individual or
 * company may do whatever they wish with source code distributed with
 * PCCTS or the code generated by PCCTS, including the incorporation of
 * PCCTS, or its output, into commerical software.
 *
 * We encourage users to develop software with PCCTS.  However, we do ask
 * that credit is given to us for developing PCCTS.  By "credit",
 * we mean that if you incorporate our source code into one of your
 * programs (commercial product, research project, or otherwise) that you
 * acknowledge this fact somewhere in the documentation, research report,
 * etc...  If you like PCCTS and have developed a nice tool with the
 * output, please mention that you developed it using PCCTS.  In
 * addition, we ask that this header remain intact in our source code.
 * As long as these guidelines are kept, we expect to continue enhancing
 * this system and expect to make other tools available as they are
 * completed.
 *
 * ANTLR 1.33
 * Terence Parr
 * Parr Research Corporation
 * with Purdue University and AHPCRC, University of Minnesota
 * 1989-2000
 */

#include "pcctscfg.h"

#include "pccts_stdio.h"
#include "pccts_stdlib.h"

PCCTS_NAMESPACE_STD

/* I have to put this here due to C++ limitation
 * that you can't have a 'forward' decl for enums.
 * I hate C++!!!!!!!!!!!!!!!
 */

// MR1
// MR1  10-Apr-97  133MR1  Prevent use of varying sizes for the
// MR1  			ANTLRTokenType enum
// MR1

enum ANTLRTokenType { TER_HATES_CPP=0, ITS_UTTER_GARBAGE,		// MR1
					 WITH_SOME_GOOD_IDEAS=9999};	// MR1

#define ANTLR_SUPPORT_CODE

#include "pcctscfg.h"
#include DLEXERBASE_H
#include APARSER_H		// MR23

DLGLexerBase::
DLGLexerBase(DLGInputStream *in,
	     unsigned bufsize,
	     int _interactive,
	     int _track_columns)
{
	this->_bufsize = bufsize;
	this->_lextext = new DLGChar[_bufsize];
	if ( this->_lextext==NULL ) {
	    panic("text buffer is NULL");
	}
	this->_begexpr = this->_endexpr = NULL;
	this->ch = this->bufovf = 0;
	this->nextpos = NULL;
	this->cl = 0;
	this->add_erase = 0;
	this->input = in;
	this->_begcol = 0;
	this->_endcol = 0;
	this->_line = 1;
	this->charfull = 0;
	this->automaton = 0;
	this->token_to_fill = NULL;
	this->interactive = _interactive;
	this->track_columns = _track_columns;
	this->debugLexerFlag = 0;					// MR1
	this->parser = NULL;						// MR1
    this->lexErrCount=0;                        // MR11
}

// MR19  THM 

void DLGLexerBase::reset()
{
	this->charfull = 0;
	this->_begcol = 0;
	this->_endcol = 0;
	this->automaton = 0;
	this->_line=1;
	this->lexErrCount=0;
}

void DLGLexerBase::
setInputStream( DLGInputStream *in )
{
	this->input = in;
	_line = 1;
	charfull = 0;
}

/* saves dlg state, but not what feeds dlg (such as file position) */
void DLGLexerBase::
saveState(DLGState *state)
{
	state->input = input;
	state->interactive = interactive;
	state->track_columns = track_columns;
	state->auto_num = automaton;
	state->add_erase = add_erase;
	state->lookc = ch;
	state->char_full = charfull;
	state->begcol = _begcol;
	state->endcol = _endcol;
	state->line = _line;
	state->lextext = _lextext;
	state->begexpr = _begexpr;
	state->endexpr = _endexpr;
	state->bufsize = _bufsize;
	state->bufovf = bufovf;
	state->nextpos = nextpos;
	state->class_num = cl;
	state->debugLexerFlag = debugLexerFlag;				// MR1
	state->parser = parser;						// MR1
}

void DLGLexerBase::
restoreState(DLGState *state)
{
	input = state->input;
	interactive = state->interactive;
	track_columns = state->track_columns;
	automaton = state->auto_num;
	add_erase = state->add_erase;
	ch = state->lookc;
	charfull = state->char_full;
	_begcol = state->begcol;
	_endcol = state->endcol;
	_line = state->line;
	_lextext = state->lextext;
	_begexpr = state->begexpr;
	_endexpr = state->endexpr;
	_bufsize = state->bufsize;
	bufovf = state->bufovf;
	nextpos = state->nextpos;
	cl = state->class_num;
	debugLexerFlag = state->debugLexerFlag;				// MR1
	parser = state->parser;						// MR1
}

/* erase what is currently in the buffer, and get a new reg. expr */
void DLGLexerBase::
skip()
{
	add_erase = 1;
}

/* don't erase what is in the lextext buffer, add on to it */
void DLGLexerBase::
more()
{
	add_erase = 2;
}

/* substitute c for the reg. expr last matched and is in the buffer */
void DLGLexerBase::
replchar(DLGChar c)
{
	/* can't allow overwriting null at end of string */
	if (_begexpr < &_lextext[_bufsize-1]){
		*_begexpr = c;
		*(_begexpr+1) = '\0';
	}
	_endexpr = _begexpr;
	if (c != '\0') {
		nextpos = _begexpr + 1;
	}
	else {
		nextpos = _begexpr;	/* MR30 Zero terminates string. */
	}
}

/* replace the string s for the reg. expr last matched and in the buffer */

#ifdef _MSC_VER  // MR23
//Turn off "assignment within conditional expression" warning
#pragma warning(disable : 4706)
#endif
void DLGLexerBase::
replstr(const DLGChar *s) /* MR20 const */
{
	register DLGChar *l= &_lextext[_bufsize -1];

	nextpos = _begexpr;
	if (s){
		while ((nextpos <= l) && (*(nextpos++) = *(s++))){
			/* empty */
		}
		/* correct for NULL at end of string */
		nextpos--;
	}
	if ((nextpos <= l) && (*(--s) == 0)){
		bufovf = 0;
	}else{
		bufovf = 1;
	}
	*(nextpos) = '\0';
	_endexpr = nextpos - 1;
}
#ifdef _MSC_VER  // MR23
#pragma warning(default: 4706)
#endif

void DLGLexerBase::
errstd(const char *s)                               /* MR20 const */
{
        lexErrCount++;                              /* MR11 */
        /* MR23 */ printMessage(stderr,
                "%s near line %d (text was '%s')\n",
                ((s == NULL) ? "Lexical error" : s),
                _line,_lextext);
}

int DLGLexerBase::
err_in()
{
	/* MR23 */ printMessage(stderr,"No input stream, function, or string\n");
	/* return eof to get out gracefully */
	return EOF;
}

ANTLRTokenType DLGLexerBase::
erraction()
{
	errstd("invalid token");
	advance();
	skip();
	return (ANTLRTokenType) 0;	// bogus, but satisfies compiler
}

_ANTLRTokenPtr DLGLexerBase::
getToken()
{
	if ( token_to_fill==NULL ) panic("NULL token_to_fill");
	ANTLRTokenType tt = nextTokenType();
	_ANTLRTokenPtr tk = token_to_fill->makeToken(tt, _lextext,_line);
	return tk;
}

void DLGLexerBase::
panic(const char *msg)      /* MR20 const */
{
	if (parser)				//MR23
		parser->panic(msg);	//MR23
	else					//MR23
	{
		/* MR23 */ printMessage(stderr, "DLG panic: %s\n", msg);
	//
	//  7-Apr-97 133MR1
	//
		exit(PCCTS_EXIT_FAILURE);					// MR1
	}
}

ANTLRParser * DLGLexerBase::						// MR1
setParser(ANTLRParser *p) {						// MR1
  ANTLRParser	*oldValue=parser;					// MR1
  parser=p;								// MR1
  return oldValue;							// MR1
}									// MR1
									// MR1
ANTLRParser * DLGLexerBase::						// MR1
getParser() {								// MR1
  return parser;							// MR1
}									// MR1
									// MR1
int DLGLexerBase::							// MR1
debugLexer(int newValue) {						// MR1
  int	oldValue=debugLexerFlag;					// MR1
  debugLexerFlag=newValue;						// MR1
  return oldValue;							// MR1
}									// MR1

//MR23
int DLGLexerBase::printMessage(FILE* pFile, const char* pFormat, ...)
{
	va_list marker;
	va_start( marker, pFormat );

	int iRet = 0;
	if (parser)
		parser->printMessageV(pFile, pFormat, marker);
	else
  		iRet = vfprintf(pFile, pFormat, marker);

	va_end( marker );
	return iRet;
}