summaryrefslogtreecommitdiff
path: root/BaseTools/Source/C/VfrCompile/Pccts/h/AToken.h
blob: 6167c21ef5394d55529b9f32af7e0d75e25f54ce (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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/* ANTLRToken.h
 *
 * 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
 */

#ifndef ATOKEN_H_GATE
#define ATOKEN_H_GATE

#include "pcctscfg.h"

#include "pccts_string.h"
#include "pccts_stdio.h"
#include "pccts_stdlib.h"
#include "pccts_stdarg.h" // MR23

PCCTS_NAMESPACE_STD

// MR9      RJV (JVincent@novell.com) Not needed for variable length strings

//// MR9 #ifndef ANTLRCommonTokenTEXTSIZE
//// MR9 #define ANTLRCommonTokenTEXTSIZE        	100
//// MR9 #endif


/* must define what a char looks like; can make this a class too */
typedef char ANTLRChar;

/* D E F I N E  S M A R T  P O I N T E R S */

//#include ATOKPTR_H   not tested yet, leave out
class ANTLRAbstractToken;
typedef ANTLRAbstractToken *_ANTLRTokenPtr;

class ANTLRAbstractToken {
public:
    virtual ~ANTLRAbstractToken() {;}
    virtual ANTLRTokenType getType() const = 0;
    virtual void setType(ANTLRTokenType t) = 0;
    virtual int getLine() const = 0;
    virtual void setLine(int line) = 0;
    virtual ANTLRChar *getText() const = 0;
    virtual void setText(const ANTLRChar *) = 0;

    /* This function will disappear when I can use templates */
	virtual ANTLRAbstractToken *makeToken(ANTLRTokenType tt,
										  ANTLRChar *text,
										  int line) = 0;

	/* define to satisfy ANTLRTokenBuffer's need to determine whether or
	   not a token object can be destroyed.  If nref()==0, no one has
	   a reference, and the object may be destroyed.  This function defaults
	   to 1, hence, if you use deleteTokens() message with a token object
	   not derived from ANTLRCommonRefCountToken, the parser will compile
	   but will not delete objects after they leave the token buffer.
    */

	virtual unsigned nref() const { return 1; }     // MR11
	virtual void ref() {;}
	virtual void deref() {;}

	virtual void panic(const char *msg)             // MR20 const
		{
			/* MR23 */ printMessage(stderr, "ANTLRAbstractToken panic: %s\n", msg);
			exit(PCCTS_EXIT_FAILURE);
		}

	virtual int printMessage(FILE* pFile, const char* pFormat, ...) // MR23
		{
			va_list marker;
			va_start( marker, pFormat );
  			int iRet = vfprintf(pFile, pFormat, marker);
			va_end( marker );
			return iRet;
		}
};

/* This class should be subclassed.  It cannot store token type or text */

class ANTLRRefCountToken : public ANTLRAbstractToken {
public:
#ifdef DBG_REFCOUNTTOKEN
	static int ctor;
	static int dtor;
#endif
protected:
    unsigned refcnt_;
#ifdef DBG_REFCOUNTTOKEN
	char object[200];
#endif

public:

	// MR23 - No matter what you do, you're hammered.
	//        Don't give names to formals something breaks.
	//		  Give names to formals and don't use them it breaks.

#ifndef DBG_REFCOUNTTOKEN
	ANTLRRefCountToken(ANTLRTokenType /* t MR23 */, const ANTLRChar * /* s MR23 */)
#else
	ANTLRRefCountToken(ANTLRTokenType t, const ANTLRChar * s)
#endif

#ifndef DBG_REFCOUNTTOKEN
		{
			refcnt_ = 0;
		}
#else
	{
		ctor++;
		refcnt_ = 0;
		if ( t==1 ) sprintf(object,"tok_EOF");
		else sprintf(object,"tok_%s",s);
		/* MR23 */ printMessage(stderr, "ctor %s #%d\n",object,ctor);
	}
#endif
	ANTLRRefCountToken()
#ifndef DBG_REFCOUNTTOKEN
		{ refcnt_ = 0; }
#else
		{
			ctor++;
			refcnt_ = 0;
			sprintf(object,"tok_blank");
			/* MR23 */ printMessage(stderr, "ctor %s #%d\n",object,ctor);
		}
	virtual ~ANTLRRefCountToken()
		{
			dtor++;
			if ( dtor>ctor ) /* MR23 */ printMessage(stderr, "WARNING: dtor>ctor\n");
			/* MR23 */ printMessage(stderr, "dtor %s #%d\n", object, dtor);
			object[0]='\0';
		}
#endif

	// reference counting stuff needed by ANTLRTokenPtr.
	// User should not access these; for C++ language reasons, we had
	// to make these public.  Yuck.

	void ref()		      { refcnt_++; }
	void deref()	      { refcnt_--; }
	unsigned nref()	const { return refcnt_; }   // MR11

	virtual ANTLRAbstractToken *makeToken(ANTLRTokenType /*tt MR23*/,
										  ANTLRChar * /*txt MR23*/,
										  int /*line MR23*/)
	{
		panic("call to ANTLRRefCountToken::makeToken()\n");
		return NULL;
	}
};

class ANTLRCommonNoRefCountToken : public ANTLRAbstractToken {
protected:
	ANTLRTokenType _type;
	int _line;
	ANTLRChar *_text;               // MR9 RJV

public:
	ANTLRCommonNoRefCountToken(ANTLRTokenType t, const ANTLRChar *s)
	{ setType(t); _line = 0; _text = NULL; setText(s); }
	ANTLRCommonNoRefCountToken()
	{ setType((ANTLRTokenType)0); _line = 0; _text = NULL; setText(""); }

	~ANTLRCommonNoRefCountToken() { if (_text) delete [] _text; }  // MR9 RJV: Added Destructor to remove string

	ANTLRTokenType getType() const 	{ return _type; }
	void setType(ANTLRTokenType t)	{ _type = t; }
	virtual int getLine() const		{ return _line; }
	void setLine(int line)	    	{ _line = line; }
	ANTLRChar *getText() const   	{ return _text; }
    int getLength() const           { return strlen(getText()); }       // MR11

// MR9 RJV: Added code for variable length strings to setText()

	void setText(const ANTLRChar *s)
	{	if (s != _text) {
          if (_text) delete [] _text;
          if (s != NULL) {
         	_text = new ANTLRChar[strlen(s)+1];
            if (_text == NULL) panic("ANTLRCommonNoRefCountToken::setText new failed");
            strcpy(_text,s);
    	  } else {
            _text = new ANTLRChar[1];
            if (_text == NULL) panic("ANTLRCommonNoRefCountToken::setText new failed");
            strcpy(_text,"");
          };
        };
	}

	virtual ANTLRAbstractToken *makeToken(ANTLRTokenType tt,
										  ANTLRChar *txt,
										  int line)
		{
			ANTLRAbstractToken *t = new ANTLRCommonNoRefCountToken;
			t->setType(tt); t->setText(txt); t->setLine(line);
			return t;
		}

// MR9 THM Copy constructor required when heap allocated string is used with copy semantics

   ANTLRCommonNoRefCountToken (const ANTLRCommonNoRefCountToken& from) :
         ANTLRAbstractToken(from) {
 	 setType(from._type);
	 setLine(from._line);
     _text=NULL;
     setText(from._text);
  };

// MR9 THM operator =() required when heap allocated string is used with copy semantics

   virtual ANTLRCommonNoRefCountToken& operator =(const ANTLRCommonNoRefCountToken& rhs) {

//////  MR15 WatCom can't hack use of operator =()
//////  Use this:  *( (ANTRLAbstractToken *) this)=rhs;

     *( (ANTLRAbstractToken *) this ) = rhs;

  	 setType(rhs._type);
 	 setLine(rhs._line);
     setText(rhs._text);
     return *this;
   };
};

class ANTLRCommonToken : public ANTLRRefCountToken {
protected:
	ANTLRTokenType       _type;
	int                  _line;
	ANTLRChar           *_text;               // MR9 RJV:Added

public:
	ANTLRCommonToken(ANTLRTokenType t, const ANTLRChar *s) : ANTLRRefCountToken(t,s)
		{ setType(t); _line = 0; _text = NULL; setText(s); }                    // MR9
	ANTLRCommonToken()
		{ setType((ANTLRTokenType)0); _line = 0; _text = NULL; setText(""); }   // MR9

	virtual ~ANTLRCommonToken() { if (_text) delete [] _text; } // MR9 RJV: Added Destructor to remove string

	ANTLRTokenType getType() const 	{ return _type; }
	void setType(ANTLRTokenType t)	{ _type = t; }
	virtual int getLine() const		{ return _line; }
	void setLine(int line)	    	{ _line = line; }
	ANTLRChar *getText() const		{ return _text; }
    int getLength() const           { return strlen(getText()); }       // MR11

// MR9 RJV: Added code for variable length strings to setText()

	void setText(const ANTLRChar *s)
	{	if (s != _text) {
          if (_text) delete [] _text;
          if (s != NULL) {
         	_text = new ANTLRChar[strlen(s)+1];
            if (_text == NULL) panic("ANTLRCommonToken::setText new failed");
            strcpy(_text,s);
    	  } else {
            _text = new ANTLRChar[1];
            if (_text == NULL) panic("ANTLRCommonToken::setText new failed");
            strcpy(_text,"");
          };
        };
	}

	virtual ANTLRAbstractToken *makeToken(ANTLRTokenType tt,
										  ANTLRChar *txt,
										  int line)
	{
		ANTLRAbstractToken *t = new ANTLRCommonToken(tt,txt);
		t->setLine(line);
		return t;
	}

// MR9 THM Copy constructor required when heap allocated string is used with copy semantics

   ANTLRCommonToken (const ANTLRCommonToken& from) :
         ANTLRRefCountToken(from) {
 	 setType(from._type);
	 setLine(from._line);
     _text=NULL;
     setText(from._text);
  };

// MR9 THM operator =() required when heap allocated string is used with copy semantics

   virtual ANTLRCommonToken& operator =(const ANTLRCommonToken& rhs) {

//////  MR15 WatCom can't hack use of operator =()
//////  Use this instead:   *( (ANTRLRRefCountToken *) this)=rhs;

     *( (ANTLRRefCountToken *) this) = rhs;

  	 setType(rhs._type);
 	 setLine(rhs._line);
     setText(rhs._text);
     return *this;
   };
};

// used for backward compatibility
typedef ANTLRCommonToken ANTLRCommonBacktrackingToken;

#endif