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.
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
25 * Parr Research Corporation
26 * with Purdue University and AHPCRC, University of Minnesota
35 #include "pccts_string.h"
36 #include "pccts_stdio.h"
37 #include "pccts_stdlib.h"
38 #include "pccts_stdarg.h" // MR23
42 // MR9 RJV (JVincent@novell.com) Not needed for variable length strings
44 //// MR9 #ifndef ANTLRCommonTokenTEXTSIZE
45 //// MR9 #define ANTLRCommonTokenTEXTSIZE 100
49 /* must define what a char looks like; can make this a class too */
50 typedef char ANTLRChar
;
52 /* D E F I N E S M A R T P O I N T E R S */
54 //#include ATOKPTR_H not tested yet, leave out
55 class ANTLRAbstractToken
;
56 typedef ANTLRAbstractToken
*_ANTLRTokenPtr
;
58 class ANTLRAbstractToken
{
60 virtual ~ANTLRAbstractToken() {;}
61 virtual ANTLRTokenType
getType() const = 0;
62 virtual void setType(ANTLRTokenType t
) = 0;
63 virtual int getLine() const = 0;
64 virtual void setLine(int line
) = 0;
65 virtual ANTLRChar
*getText() const = 0;
66 virtual void setText(const ANTLRChar
*) = 0;
68 /* This function will disappear when I can use templates */
69 virtual ANTLRAbstractToken
*makeToken(ANTLRTokenType tt
,
73 /* define to satisfy ANTLRTokenBuffer's need to determine whether or
74 not a token object can be destroyed. If nref()==0, no one has
75 a reference, and the object may be destroyed. This function defaults
76 to 1, hence, if you use deleteTokens() message with a token object
77 not derived from ANTLRCommonRefCountToken, the parser will compile
78 but will not delete objects after they leave the token buffer.
81 virtual unsigned nref() const { return 1; } // MR11
82 virtual void ref() {;}
83 virtual void deref() {;}
85 virtual void panic(const char *msg
) // MR20 const
87 /* MR23 */ printMessage(stderr
, "ANTLRAbstractToken panic: %s\n", msg
);
88 exit(PCCTS_EXIT_FAILURE
);
91 virtual int printMessage(FILE* pFile
, const char* pFormat
, ...) // MR23
94 va_start( marker
, pFormat
);
95 int iRet
= vfprintf(pFile
, pFormat
, marker
);
101 /* This class should be subclassed. It cannot store token type or text */
103 class ANTLRRefCountToken
: public ANTLRAbstractToken
{
105 #ifdef DBG_REFCOUNTTOKEN
111 #ifdef DBG_REFCOUNTTOKEN
117 // MR23 - No matter what you do, you're hammered.
118 // Don't give names to formals something breaks.
119 // Give names to formals and don't use them it breaks.
121 #ifndef DBG_REFCOUNTTOKEN
122 ANTLRRefCountToken(ANTLRTokenType
/* t MR23 */, const ANTLRChar
* /* s MR23 */)
124 ANTLRRefCountToken(ANTLRTokenType t
, const ANTLRChar
* s
)
127 #ifndef DBG_REFCOUNTTOKEN
135 if ( t
==1 ) sprintf(object
,"tok_EOF");
136 else sprintf(object
,"tok_%s",s
);
137 /* MR23 */ printMessage(stderr
, "ctor %s #%d\n",object
,ctor
);
141 #ifndef DBG_REFCOUNTTOKEN
147 sprintf(object
,"tok_blank");
148 /* MR23 */ printMessage(stderr
, "ctor %s #%d\n",object
,ctor
);
150 virtual ~ANTLRRefCountToken()
153 if ( dtor
>ctor
) /* MR23 */ printMessage(stderr
, "WARNING: dtor>ctor\n");
154 /* MR23 */ printMessage(stderr
, "dtor %s #%d\n", object
, dtor
);
159 // reference counting stuff needed by ANTLRTokenPtr.
160 // User should not access these; for C++ language reasons, we had
161 // to make these public. Yuck.
163 void ref() { refcnt_
++; }
164 void deref() { refcnt_
--; }
165 unsigned nref() const { return refcnt_
; } // MR11
167 virtual ANTLRAbstractToken
*makeToken(ANTLRTokenType
/*tt MR23*/,
168 ANTLRChar
* /*txt MR23*/,
171 panic("call to ANTLRRefCountToken::makeToken()\n");
176 class ANTLRCommonNoRefCountToken
: public ANTLRAbstractToken
{
178 ANTLRTokenType _type
;
180 ANTLRChar
*_text
; // MR9 RJV
183 ANTLRCommonNoRefCountToken(ANTLRTokenType t
, const ANTLRChar
*s
)
184 { setType(t
); _line
= 0; _text
= NULL
; setText(s
); }
185 ANTLRCommonNoRefCountToken()
186 { setType((ANTLRTokenType
)0); _line
= 0; _text
= NULL
; setText(""); }
188 ~ANTLRCommonNoRefCountToken() { if (_text
) delete [] _text
; } // MR9 RJV: Added Destructor to remove string
190 ANTLRTokenType
getType() const { return _type
; }
191 void setType(ANTLRTokenType t
) { _type
= t
; }
192 virtual int getLine() const { return _line
; }
193 void setLine(int line
) { _line
= line
; }
194 ANTLRChar
*getText() const { return _text
; }
195 int getLength() const { return strlen(getText()); } // MR11
197 // MR9 RJV: Added code for variable length strings to setText()
199 void setText(const ANTLRChar
*s
)
201 if (_text
) delete [] _text
;
203 _text
= new ANTLRChar
[strlen(s
)+1];
204 if (_text
== NULL
) panic("ANTLRCommonNoRefCountToken::setText new failed");
207 _text
= new ANTLRChar
[1];
208 if (_text
== NULL
) panic("ANTLRCommonNoRefCountToken::setText new failed");
214 virtual ANTLRAbstractToken
*makeToken(ANTLRTokenType tt
,
218 ANTLRAbstractToken
*t
= new ANTLRCommonNoRefCountToken
;
219 t
->setType(tt
); t
->setText(txt
); t
->setLine(line
);
223 // MR9 THM Copy constructor required when heap allocated string is used with copy semantics
225 ANTLRCommonNoRefCountToken (const ANTLRCommonNoRefCountToken
& from
) :
226 ANTLRAbstractToken(from
) {
233 // MR9 THM operator =() required when heap allocated string is used with copy semantics
235 virtual ANTLRCommonNoRefCountToken
& operator =(const ANTLRCommonNoRefCountToken
& rhs
) {
237 ////// MR15 WatCom can't hack use of operator =()
238 ////// Use this: *( (ANTRLAbstractToken *) this)=rhs;
240 *( (ANTLRAbstractToken
*) this ) = rhs
;
249 class ANTLRCommonToken
: public ANTLRRefCountToken
{
251 ANTLRTokenType _type
;
253 ANTLRChar
*_text
; // MR9 RJV:Added
256 ANTLRCommonToken(ANTLRTokenType t
, const ANTLRChar
*s
) : ANTLRRefCountToken(t
,s
)
257 { setType(t
); _line
= 0; _text
= NULL
; setText(s
); } // MR9
259 { setType((ANTLRTokenType
)0); _line
= 0; _text
= NULL
; setText(""); } // MR9
261 virtual ~ANTLRCommonToken() { if (_text
) delete [] _text
; } // MR9 RJV: Added Destructor to remove string
263 ANTLRTokenType
getType() const { return _type
; }
264 void setType(ANTLRTokenType t
) { _type
= t
; }
265 virtual int getLine() const { return _line
; }
266 void setLine(int line
) { _line
= line
; }
267 ANTLRChar
*getText() const { return _text
; }
268 int getLength() const { return strlen(getText()); } // MR11
270 // MR9 RJV: Added code for variable length strings to setText()
272 void setText(const ANTLRChar
*s
)
274 if (_text
) delete [] _text
;
276 _text
= new ANTLRChar
[strlen(s
)+1];
277 if (_text
== NULL
) panic("ANTLRCommonToken::setText new failed");
280 _text
= new ANTLRChar
[1];
281 if (_text
== NULL
) panic("ANTLRCommonToken::setText new failed");
287 virtual ANTLRAbstractToken
*makeToken(ANTLRTokenType tt
,
291 ANTLRAbstractToken
*t
= new ANTLRCommonToken(tt
,txt
);
296 // MR9 THM Copy constructor required when heap allocated string is used with copy semantics
298 ANTLRCommonToken (const ANTLRCommonToken
& from
) :
299 ANTLRRefCountToken(from
) {
306 // MR9 THM operator =() required when heap allocated string is used with copy semantics
308 virtual ANTLRCommonToken
& operator =(const ANTLRCommonToken
& rhs
) {
310 ////// MR15 WatCom can't hack use of operator =()
311 ////// Use this instead: *( (ANTRLRRefCountToken *) this)=rhs;
313 *( (ANTLRRefCountToken
*) this) = rhs
;
322 // used for backward compatibility
323 typedef ANTLRCommonToken ANTLRCommonBacktrackingToken
;