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"
41 // MR9 RJV (JVincent@novell.com) Not needed for variable length strings
43 //// MR9 #ifndef ANTLRCommonTokenTEXTSIZE
44 //// MR9 #define ANTLRCommonTokenTEXTSIZE 100
48 /* must define what a char looks like; can make this a class too */
49 typedef char ANTLRChar
;
51 /* D E F I N E S M A R T P O I N T E R S */
53 //#include ATOKPTR_H not tested yet, leave out
54 class ANTLRAbstractToken
;
55 typedef ANTLRAbstractToken
*_ANTLRTokenPtr
;
57 class ANTLRAbstractToken
{
59 virtual ~ANTLRAbstractToken() {;}
60 virtual ANTLRTokenType
getType() const = 0;
61 virtual void setType(ANTLRTokenType t
) = 0;
62 virtual int getLine() const = 0;
63 virtual void setLine(int line
) = 0;
64 virtual ANTLRChar
*getText() const = 0;
65 virtual void setText(const ANTLRChar
*) = 0;
67 /* This function will disappear when I can use templates */
68 virtual ANTLRAbstractToken
*makeToken(ANTLRTokenType tt
,
72 /* define to satisfy ANTLRTokenBuffer's need to determine whether or
73 not a token object can be destroyed. If nref()==0, no one has
74 a reference, and the object may be destroyed. This function defaults
75 to 1, hence, if you use deleteTokens() message with a token object
76 not derived from ANTLRCommonRefCountToken, the parser will compile
77 but will not delete objects after they leave the token buffer.
80 virtual unsigned nref() const { return 1; } // MR11
81 virtual void ref() {;}
82 virtual void deref() {;}
84 virtual void panic(const char *msg
) // MR20 const
86 fprintf(stderr
, "ANTLRAbstractToken panic: %s\n", msg
);
87 exit(PCCTS_EXIT_FAILURE
);
91 /* This class should be subclassed. It cannot store token type or text */
93 class ANTLRRefCountToken
: public ANTLRAbstractToken
{
95 #ifdef DBG_REFCOUNTTOKEN
101 #ifdef DBG_REFCOUNTTOKEN
106 ANTLRRefCountToken(ANTLRTokenType t
, const ANTLRChar
*s
)
107 #ifndef DBG_REFCOUNTTOKEN
115 if ( t
==1 ) sprintf(object
,"tok_EOF");
116 else sprintf(object
,"tok_%s",s
);
117 fprintf(stderr
, "ctor %s #%d\n",object
,ctor
);
121 #ifndef DBG_REFCOUNTTOKEN
127 sprintf(object
,"tok_blank");
128 fprintf(stderr
, "ctor %s #%d\n",object
,ctor
);
130 virtual ~ANTLRRefCountToken()
133 if ( dtor
>ctor
) fprintf(stderr
, "WARNING: dtor>ctor\n");
134 fprintf(stderr
, "dtor %s #%d\n", object
, dtor
);
139 // reference counting stuff needed by ANTLRTokenPtr.
140 // User should not access these; for C++ language reasons, we had
141 // to make these public. Yuck.
143 void ref() { refcnt_
++; }
144 void deref() { refcnt_
--; }
145 unsigned nref() const { return refcnt_
; } // MR11
147 virtual ANTLRAbstractToken
*makeToken(ANTLRTokenType tt
,
151 panic("call to ANTLRRefCountToken::makeToken()\n");
156 class ANTLRCommonNoRefCountToken
: public ANTLRAbstractToken
{
158 ANTLRTokenType _type
;
160 ANTLRChar
*_text
; // MR9 RJV
163 ANTLRCommonNoRefCountToken(ANTLRTokenType t
, const ANTLRChar
*s
)
164 { setType(t
); _line
= 0; _text
= NULL
; setText(s
); }
165 ANTLRCommonNoRefCountToken()
166 { setType((ANTLRTokenType
)0); _line
= 0; _text
= NULL
; setText(""); }
168 ~ANTLRCommonNoRefCountToken() { if (_text
) delete [] _text
; } // MR9 RJV: Added Destructor to remove string
170 ANTLRTokenType
getType() const { return _type
; }
171 void setType(ANTLRTokenType t
) { _type
= t
; }
172 virtual int getLine() const { return _line
; }
173 void setLine(int line
) { _line
= line
; }
174 ANTLRChar
*getText() const { return _text
; }
175 int getLength() const { return strlen(getText()); } // MR11
177 // MR9 RJV: Added code for variable length strings to setText()
179 void setText(const ANTLRChar
*s
)
181 if (_text
) delete [] _text
;
183 _text
= new ANTLRChar
[strlen(s
)+1];
184 if (_text
== NULL
) panic("ANTLRCommonNoRefCountToken::setText new failed");
187 _text
= new ANTLRChar
[1];
188 if (_text
== NULL
) panic("ANTLRCommonNoRefCountToken::setText new failed");
194 virtual ANTLRAbstractToken
*makeToken(ANTLRTokenType tt
,
198 ANTLRAbstractToken
*t
= new ANTLRCommonNoRefCountToken
;
199 t
->setType(tt
); t
->setText(txt
); t
->setLine(line
);
203 // MR9 THM Copy constructor required when heap allocated string is used with copy semantics
205 ANTLRCommonNoRefCountToken (const ANTLRCommonNoRefCountToken
& from
) :
206 ANTLRAbstractToken(from
) {
213 // MR9 THM operator =() required when heap allocated string is used with copy semantics
215 virtual ANTLRCommonNoRefCountToken
& operator =(const ANTLRCommonNoRefCountToken
& rhs
) {
217 ////// MR15 WatCom can't hack use of operator =()
218 ////// Use this: *( (ANTRLAbstractToken *) this)=rhs;
220 *( (ANTLRAbstractToken
*) this ) = rhs
;
229 class ANTLRCommonToken
: public ANTLRRefCountToken
{
231 ANTLRTokenType _type
;
233 ANTLRChar
*_text
; // MR9 RJV:Added
236 ANTLRCommonToken(ANTLRTokenType t
, const ANTLRChar
*s
) : ANTLRRefCountToken(t
,s
)
237 { setType(t
); _line
= 0; _text
= NULL
; setText(s
); } // MR9
239 { setType((ANTLRTokenType
)0); _line
= 0; _text
= NULL
; setText(""); } // MR9
241 virtual ~ANTLRCommonToken() { if (_text
) delete [] _text
; } // MR9 RJV: Added Destructor to remove string
243 ANTLRTokenType
getType() const { return _type
; }
244 void setType(ANTLRTokenType t
) { _type
= t
; }
245 virtual int getLine() const { return _line
; }
246 void setLine(int line
) { _line
= line
; }
247 ANTLRChar
*getText() const { return _text
; }
248 int getLength() const { return strlen(getText()); } // MR11
250 // MR9 RJV: Added code for variable length strings to setText()
252 void setText(const ANTLRChar
*s
)
254 if (_text
) delete [] _text
;
256 _text
= new ANTLRChar
[strlen(s
)+1];
257 if (_text
== NULL
) panic("ANTLRCommonToken::setText new failed");
260 _text
= new ANTLRChar
[1];
261 if (_text
== NULL
) panic("ANTLRCommonToken::setText new failed");
267 virtual ANTLRAbstractToken
*makeToken(ANTLRTokenType tt
,
271 ANTLRAbstractToken
*t
= new ANTLRCommonToken(tt
,txt
);
276 // MR9 THM Copy constructor required when heap allocated string is used with copy semantics
278 ANTLRCommonToken (const ANTLRCommonToken
& from
) :
279 ANTLRRefCountToken(from
) {
286 // MR9 THM operator =() required when heap allocated string is used with copy semantics
288 virtual ANTLRCommonToken
& operator =(const ANTLRCommonToken
& rhs
) {
290 ////// MR15 WatCom can't hack use of operator =()
291 ////// Use this instead: *( (ANTRLRRefCountToken *) this)=rhs;
293 *( (ANTLRRefCountToken
*) this) = rhs
;
302 // used for backward compatibility
303 typedef ANTLRCommonToken ANTLRCommonBacktrackingToken
;