1/* ANTLRToken.h
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#ifndef ATOKEN_H_GATE
31#define ATOKEN_H_GATE
32
33#include "pcctscfg.h"
34
35#include "pccts_string.h"
36#include "pccts_stdio.h"
37#include "pccts_stdlib.h"
38
39PCCTS_NAMESPACE_STD
40
41// MR9      RJV (JVincent@novell.com) Not needed for variable length strings
42
43//// MR9 #ifndef ANTLRCommonTokenTEXTSIZE
44//// MR9 #define ANTLRCommonTokenTEXTSIZE          100
45//// MR9 #endif
46
47
48/* must define what a char looks like; can make this a class too */
49typedef char ANTLRChar;
50
51/* D E F I N E  S M A R T  P O I N T E R S */
52
53//#include ATOKPTR_H   not tested yet, leave out
54class ANTLRAbstractToken;
55typedef ANTLRAbstractToken *_ANTLRTokenPtr;
56
57class ANTLRAbstractToken {
58public:
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;
66
67    /* This function will disappear when I can use templates */
68  virtual ANTLRAbstractToken *makeToken(ANTLRTokenType tt,
69                      ANTLRChar *text,
70                      int line) = 0;
71
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.
78    */
79
80  virtual unsigned nref() const { return 1; }     // MR11
81  virtual void ref() {;}
82  virtual void deref() {;}
83
84  virtual void panic(const char *msg)             // MR20 const
85    {
86      fprintf(stderr, "ANTLRAbstractToken panic: %s\n", msg);
87      exit(PCCTS_EXIT_FAILURE);
88    }
89};
90
91/* This class should be subclassed.  It cannot store token type or text */
92
93class ANTLRRefCountToken : public ANTLRAbstractToken {
94public:
95#ifdef DBG_REFCOUNTTOKEN
96  static int ctor;
97  static int dtor;
98#endif
99protected:
100    unsigned refcnt_;
101#ifdef DBG_REFCOUNTTOKEN
102  char object[200];
103#endif
104
105public:
106  ANTLRRefCountToken(ANTLRTokenType t, const ANTLRChar *s)
107#ifndef DBG_REFCOUNTTOKEN
108    {
109      refcnt_ = 0;
110    }
111#else
112  {
113    ctor++;
114    refcnt_ = 0;
115    if ( t==1 ) sprintf(object,"tok_EOF");
116    else sprintf(object,"tok_%s",s);
117    fprintf(stderr, "ctor %s #%d\n",object,ctor);
118  }
119#endif
120  ANTLRRefCountToken()
121#ifndef DBG_REFCOUNTTOKEN
122    { refcnt_ = 0; }
123#else
124    {
125      ctor++;
126      refcnt_ = 0;
127      sprintf(object,"tok_blank");
128      fprintf(stderr, "ctor %s #%d\n",object,ctor);
129    }
130  virtual ~ANTLRRefCountToken()
131    {
132      dtor++;
133      if ( dtor>ctor ) fprintf(stderr, "WARNING: dtor>ctor\n");
134      fprintf(stderr, "dtor %s #%d\n", object, dtor);
135      object[0]='\0';
136    }
137#endif
138
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.
142
143  void ref()          { refcnt_++; }
144  void deref()        { refcnt_--; }
145  unsigned nref()  const { return refcnt_; }   // MR11
146
147  virtual ANTLRAbstractToken *makeToken(ANTLRTokenType tt,
148                      ANTLRChar *txt,
149                      int line)
150  {
151    panic("call to ANTLRRefCountToken::makeToken()\n");
152    return NULL;
153  }
154};
155
156class ANTLRCommonNoRefCountToken : public ANTLRAbstractToken {
157protected:
158  ANTLRTokenType _type;
159  int _line;
160  ANTLRChar *_text;               // MR9 RJV
161
162public:
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(""); }
167
168  ~ANTLRCommonNoRefCountToken() { if (_text) delete [] _text; }  // MR9 RJV: Added Destructor to remove string
169
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
176
177// MR9 RJV: Added code for variable length strings to setText()
178
179  void setText(const ANTLRChar *s)
180  {  if (s != _text) {
181          if (_text) delete [] _text;
182          if (s != NULL) {
183           _text = new ANTLRChar[strlen(s)+1];
184            if (_text == NULL) panic("ANTLRCommonNoRefCountToken::setText new failed");
185            strcpy(_text,s);
186        } else {
187            _text = new ANTLRChar[1];
188            if (_text == NULL) panic("ANTLRCommonNoRefCountToken::setText new failed");
189            strcpy(_text,"");
190          };
191        };
192  }
193
194  virtual ANTLRAbstractToken *makeToken(ANTLRTokenType tt,
195                      ANTLRChar *txt,
196                      int line)
197    {
198      ANTLRAbstractToken *t = new ANTLRCommonNoRefCountToken;
199      t->setType(tt); t->setText(txt); t->setLine(line);
200      return t;
201    }
202
203// MR9 THM Copy constructor required when heap allocated string is used with copy semantics
204
205   ANTLRCommonNoRefCountToken (const ANTLRCommonNoRefCountToken& from) :
206         ANTLRAbstractToken(from) {
207    setType(from._type);
208   setLine(from._line);
209     _text=NULL;
210     setText(from._text);
211  };
212
213// MR9 THM operator =() required when heap allocated string is used with copy semantics
214
215   virtual ANTLRCommonNoRefCountToken& operator =(const ANTLRCommonNoRefCountToken& rhs) {
216
217//////  MR15 WatCom can't hack use of operator =()
218//////  Use this:  *( (ANTRLAbstractToken *) this)=rhs;
219
220     *( (ANTLRAbstractToken *) this ) = rhs;
221
222     setType(rhs._type);
223    setLine(rhs._line);
224     setText(rhs._text);
225     return *this;
226   };
227};
228
229class ANTLRCommonToken : public ANTLRRefCountToken {
230protected:
231  ANTLRTokenType       _type;
232  int                  _line;
233  ANTLRChar           *_text;               // MR9 RJV:Added
234
235public:
236  ANTLRCommonToken(ANTLRTokenType t, const ANTLRChar *s) : ANTLRRefCountToken(t,s)
237    { setType(t); _line = 0; _text = NULL; setText(s); }                    // MR9
238  ANTLRCommonToken()
239    { setType((ANTLRTokenType)0); _line = 0; _text = NULL; setText(""); }   // MR9
240
241  virtual ~ANTLRCommonToken() { if (_text) delete [] _text; } // MR9 RJV: Added Destructor to remove string
242
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
249
250// MR9 RJV: Added code for variable length strings to setText()
251
252  void setText(const ANTLRChar *s)
253  {  if (s != _text) {
254          if (_text) delete [] _text;
255          if (s != NULL) {
256           _text = new ANTLRChar[strlen(s)+1];
257            if (_text == NULL) panic("ANTLRCommonToken::setText new failed");
258            strcpy(_text,s);
259        } else {
260            _text = new ANTLRChar[1];
261            if (_text == NULL) panic("ANTLRCommonToken::setText new failed");
262            strcpy(_text,"");
263          };
264        };
265  }
266
267  virtual ANTLRAbstractToken *makeToken(ANTLRTokenType tt,
268                      ANTLRChar *txt,
269                      int line)
270  {
271    ANTLRAbstractToken *t = new ANTLRCommonToken(tt,txt);
272    t->setLine(line);
273    return t;
274  }
275
276// MR9 THM Copy constructor required when heap allocated string is used with copy semantics
277
278   ANTLRCommonToken (const ANTLRCommonToken& from) :
279         ANTLRRefCountToken(from) {
280    setType(from._type);
281   setLine(from._line);
282     _text=NULL;
283     setText(from._text);
284  };
285
286// MR9 THM operator =() required when heap allocated string is used with copy semantics
287
288   virtual ANTLRCommonToken& operator =(const ANTLRCommonToken& rhs) {
289
290//////  MR15 WatCom can't hack use of operator =()
291//////  Use this instead:   *( (ANTRLRRefCountToken *) this)=rhs;
292
293     *( (ANTLRRefCountToken *) this) = rhs;
294
295     setType(rhs._type);
296    setLine(rhs._line);
297     setText(rhs._text);
298     return *this;
299   };
300};
301
302// used for backward compatibility
303typedef ANTLRCommonToken ANTLRCommonBacktrackingToken;
304
305#endif
306