GME  13
AToken.h
Go to the documentation of this file.
00001 /* ANTLRToken.h
00002  *
00003  * SOFTWARE RIGHTS
00004  *
00005  * We reserve no LEGAL rights to the Purdue Compiler Construction Tool
00006  * Set (PCCTS) -- PCCTS is in the public domain.  An individual or
00007  * company may do whatever they wish with source code distributed with
00008  * PCCTS or the code generated by PCCTS, including the incorporation of
00009  * PCCTS, or its output, into commerical software.
00010  *
00011  * We encourage users to develop software with PCCTS.  However, we do ask
00012  * that credit is given to us for developing PCCTS.  By "credit",
00013  * we mean that if you incorporate our source code into one of your
00014  * programs (commercial product, research project, or otherwise) that you
00015  * acknowledge this fact somewhere in the documentation, research report,
00016  * etc...  If you like PCCTS and have developed a nice tool with the
00017  * output, please mention that you developed it using PCCTS.  In
00018  * addition, we ask that this header remain intact in our source code.
00019  * As long as these guidelines are kept, we expect to continue enhancing
00020  * this system and expect to make other tools available as they are
00021  * completed.
00022  *
00023  * ANTLR 1.33
00024  * Terence Parr
00025  * Parr Research Corporation
00026  * with Purdue University and AHPCRC, University of Minnesota
00027  * 1989-1998
00028  */
00029 
00030 #ifndef ATOKEN_H_GATE
00031 #define ATOKEN_H_GATE
00032 
00033 #include <string.h>
00034 #include <stdio.h>
00035 #include <stdlib.h>
00036 
00037 // MR9      RJV (JVincent@novell.com) Not needed for variable length strings
00038 
00042 
00043 #ifdef DBG_REFCOUNTTOKEN
00044 #include <stdio.h>
00045 #endif
00046 
00047 /* must define what a char looks like; can make this a class too */
00048 typedef char ANTLRChar;
00049 
00050 /* D E F I N E  S M A R T  P O I N T E R S */
00051 #include "config.h"
00052 //#include ATOKPTR_H   not tested yet, leave out
00053 class ANTLRAbstractToken;
00054 typedef ANTLRAbstractToken *_ANTLRTokenPtr;
00055 
00056 class ANTLRAbstractToken {
00057 public:
00058     virtual ~ANTLRAbstractToken() {;}
00059     virtual ANTLRTokenType getType() const = 0;
00060     virtual void setType(ANTLRTokenType t) = 0;
00061     virtual int getLine() const = 0;
00062     virtual void setLine(int line) = 0;
00063     virtual ANTLRChar *getText() const = 0;
00064     virtual void setText(ANTLRChar *) = 0;
00065 
00066     /* This function will disappear when I can use templates */
00067         virtual ANTLRAbstractToken *makeToken(ANTLRTokenType tt,
00068                                                                                   ANTLRChar *text,
00069                                                                                   int line) = 0;
00070 
00071         /* define to satisfy ANTLRTokenBuffer's need to determine whether or
00072            not a token object can be destroyed.  If nref()==0, no one has
00073            a reference, and the object may be destroyed.  This function defaults
00074            to 1, hence, if you use deleteTokens() message with a token object
00075            not derived from ANTLRCommonRefCountToken, the parser will compile
00076            but will not delete objects after they leave the token buffer.
00077     */
00078 
00079         virtual unsigned nref() const { return 1; }     // MR11
00080         virtual void ref() {;}
00081         virtual void deref() {;}
00082 
00083         virtual void panic(char *msg)
00084                 {
00085                         fprintf(stderr, "ANTLRAbstractToken panic: %s\n", msg);
00086                         exit(PCCTS_EXIT_FAILURE);
00087                 }
00088 };
00089 
00090 /* This class should be subclassed.  It cannot store token type or text */
00091 
00092 class ANTLRRefCountToken : public ANTLRAbstractToken {
00093 public:
00094 #ifdef DBG_REFCOUNTTOKEN
00095         static int ctor;
00096         static int dtor;
00097 #endif
00098 protected:
00099     unsigned refcnt_;
00100 #ifdef DBG_REFCOUNTTOKEN
00101         char object[200];
00102 #endif
00103 
00104 public:
00105         ANTLRRefCountToken(ANTLRTokenType t, ANTLRChar *s)
00106 #ifndef DBG_REFCOUNTTOKEN
00107                 {
00108                         refcnt_ = 0;
00109                 }
00110 #else
00111         {
00112                 ctor++;
00113                 refcnt_ = 0;
00114                 if ( t==1 ) sprintf(object,"tok_EOF");
00115                 else sprintf(object,"tok_%s",s);
00116                 fprintf(stderr, "ctor %s #%d\n",object,ctor);
00117         }
00118 #endif
00119         ANTLRRefCountToken()
00120 #ifndef DBG_REFCOUNTTOKEN
00121                 { refcnt_ = 0; }
00122 #else
00123                 {
00124                         ctor++;
00125                         refcnt_ = 0;
00126                         sprintf(object,"tok_blank");
00127                         fprintf(stderr, "ctor %s #%d\n",object,ctor);
00128                 }
00129         virtual ~ANTLRRefCountToken()
00130                 {
00131                         dtor++;
00132                         if ( dtor>ctor ) fprintf(stderr, "WARNING: dtor>ctor\n");
00133                         fprintf(stderr, "dtor %s #%d\n", object, dtor);
00134                         object[0]='\0';
00135                 }
00136 #endif
00137 
00138         // reference counting stuff needed by ANTLRTokenPtr.
00139         // User should not access these; for C++ language reasons, we had
00140         // to make these public.  Yuck.
00141 
00142         void ref()                    { refcnt_++; }
00143         void deref()          { refcnt_--; }
00144         unsigned nref() const { return refcnt_; }   // MR11
00145 
00146         virtual ANTLRAbstractToken *makeToken(ANTLRTokenType tt,
00147                                                                                   ANTLRChar *txt,
00148                                                                                   int line)
00149         {
00150                 panic("call to ANTLRRefCountToken::makeToken()\n");
00151                 return NULL;
00152         }
00153 };
00154 
00155 class ANTLRCommonNoRefCountToken : public ANTLRAbstractToken {
00156 protected:
00157         ANTLRTokenType _type;
00158         int _line;
00159         ANTLRChar *_text;               // MR9 RJV
00160 
00161 public:
00162         ANTLRCommonNoRefCountToken(ANTLRTokenType t, ANTLRChar *s)
00163         { setType(t); _line = 0; _text = NULL; setText(s); }
00164         ANTLRCommonNoRefCountToken()
00165         { setType((ANTLRTokenType)0); _line = 0; _text = NULL; setText(""); }
00166 
00167         ~ANTLRCommonNoRefCountToken() { if (_text) delete [] _text; }  // MR9 RJV: Added Destructor to remove string
00168 
00169         ANTLRTokenType getType() const  { return _type; }
00170         void setType(ANTLRTokenType t)  { _type = t; }
00171         virtual int getLine() const             { return _line; }
00172         void setLine(int line)          { _line = line; }
00173         ANTLRChar *getText() const              { return _text; }
00174     int getLength() const           { return strlen(getText()); }       // MR11
00175 
00176 // MR9 RJV: Added code for variable length strings to setText()
00177 
00178         void setText(ANTLRChar *s)
00179         {       if (s != _text) {
00180           if (_text) delete [] _text;
00181           if (s != NULL) {
00182                 _text = new ANTLRChar[strlen(s)+1];
00183             if (_text == NULL) panic("ANTLRCommonNoRefCountToken::setText new failed");
00184             strcpy(_text,s);
00185           } else {
00186             _text = new ANTLRChar[1];
00187             if (_text == NULL) panic("ANTLRCommonNoRefCountToken::setText new failed");
00188             strcpy(_text,"");
00189           };
00190         };
00191         }
00192 
00193         virtual ANTLRAbstractToken *makeToken(ANTLRTokenType tt,
00194                                                                                   ANTLRChar *txt,
00195                                                                                   int line)
00196                 {
00197                         ANTLRAbstractToken *t = new ANTLRCommonNoRefCountToken;
00198                         t->setType(tt); t->setText(txt); t->setLine(line);
00199                         return t;
00200                 }
00201 
00202 // MR9 THM Copy constructor required when heap allocated string is used with copy semantics
00203 
00204    ANTLRCommonNoRefCountToken (const ANTLRCommonNoRefCountToken& from) :
00205          ANTLRAbstractToken(from) {
00206          setType(from._type);
00207          setLine(from._line);
00208      _text=NULL;
00209      setText(from._text);
00210   };
00211 
00212 // MR9 THM operator =() required when heap allocated string is used with copy semantics
00213 
00214    virtual ANTLRCommonNoRefCountToken& operator =(const ANTLRCommonNoRefCountToken& rhs) {
00215      this->ANTLRAbstractToken::operator=(rhs);
00216          setType(rhs._type);
00217          setLine(rhs._line);
00218      setText(rhs._text);
00219      return *this;
00220    };
00221 };
00222 
00223 class ANTLRCommonToken : public ANTLRRefCountToken {
00224 protected:
00225         ANTLRTokenType _type;
00226         int            _line;
00227         ANTLRChar      *_text;               // MR9 RJV:Added
00228 
00229 public:
00230         ANTLRCommonToken(ANTLRTokenType t, ANTLRChar *s) : ANTLRRefCountToken(t,s)
00231                 { setType(t); _line = 0; _text = NULL; setText(s); }                    // MR9
00232         ANTLRCommonToken()
00233                 { setType((ANTLRTokenType)0); _line = 0; _text = NULL; setText(""); }   // MR9
00234 
00235         virtual ~ANTLRCommonToken() { if (_text) delete [] _text; } // MR9 RJV: Added Destructor to remove string
00236 
00237         ANTLRTokenType getType() const  { return _type; }
00238         void setType(ANTLRTokenType t)  { _type = t; }
00239         virtual int getLine() const             { return _line; }
00240         void setLine(int line)          { _line = line; }
00241         ANTLRChar *getText() const              { return _text; }
00242     int getLength() const           { return strlen(getText()); }       // MR11
00243 
00244 // MR9 RJV: Added code for variable length strings to setText()
00245 
00246         void setText(ANTLRChar *s)
00247         {       if (s != _text) {
00248           if (_text) delete [] _text;
00249           if (s != NULL) {
00250                 _text = new ANTLRChar[strlen(s)+1];
00251             if (_text == NULL) panic("ANTLRCommonToken::setText new failed");
00252             strcpy(_text,s);
00253           } else {
00254             _text = new ANTLRChar[1];
00255             if (_text == NULL) panic("ANTLRCommonToken::setText new failed");
00256             strcpy(_text,"");
00257           };
00258         };
00259         }
00260 
00261         virtual ANTLRAbstractToken *makeToken(ANTLRTokenType tt,
00262                                                                                   ANTLRChar *txt,
00263                                                                                   int line)
00264         {
00265                 ANTLRAbstractToken *t = new ANTLRCommonToken(tt,txt);
00266                 t->setLine(line);
00267                 return t;
00268         }
00269 
00270 // MR9 THM Copy constructor required when heap allocated string is used with copy semantics
00271 
00272    ANTLRCommonToken (const ANTLRCommonToken& from) :
00273          ANTLRRefCountToken(from) {
00274          setType(from._type);
00275          setLine(from._line);
00276      _text=NULL;
00277      setText(from._text);
00278   };
00279 
00280 // MR9 THM operator =() required when heap allocated string is used with copy semantics
00281 
00282    virtual ANTLRCommonToken& operator =(const ANTLRCommonToken& rhs) {
00283      this->ANTLRRefCountToken::operator=(rhs);
00284          setType(rhs._type);
00285          setLine(rhs._line);
00286      setText(rhs._text);
00287      return *this;
00288    };
00289 };
00290 
00291 // used for backward compatibility
00292 typedef ANTLRCommonToken ANTLRCommonBacktrackingToken;
00293 
00294 #endif