EDK2 doxygen online documents - Firmware Encoding Index 1
EDK2 doxygen online documents - Firmware Encoding Index

BaseTools/Source/C/VfrCompile/Pccts/h/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-2000
00028  */
00029 
00030 #ifndef ATOKEN_H_GATE
00031 #define ATOKEN_H_GATE
00032 
00033 #include "pcctscfg.h"
00034 
00035 #include "pccts_string.h"
00036 #include "pccts_stdio.h"
00037 #include "pccts_stdlib.h"
00038 #include "pccts_stdarg.h" // MR23
00039 
00040 PCCTS_NAMESPACE_STD
00041 
00042 // MR9      RJV (JVincent@novell.com) Not needed for variable length strings
00043 
00047 
00048 
00049 /* must define what a char looks like; can make this a class too */
00050 typedef char ANTLRChar;
00051 
00052 /* D E F I N E  S M A R T  P O I N T E R S */
00053 
00054 //#include ATOKPTR_H   not tested yet, leave out
00055 class ANTLRAbstractToken;
00056 typedef ANTLRAbstractToken *_ANTLRTokenPtr;
00057 
00058 class ANTLRAbstractToken {
00059 public:
00060     virtual ~ANTLRAbstractToken() {;}
00061     virtual ANTLRTokenType getType() const = 0;
00062     virtual void setType(ANTLRTokenType t) = 0;
00063     virtual int getLine() const = 0;
00064     virtual void setLine(int line) = 0;
00065     virtual ANTLRChar *getText() const = 0;
00066     virtual void setText(const ANTLRChar *) = 0;
00067 
00068     /* This function will disappear when I can use templates */
00069         virtual ANTLRAbstractToken *makeToken(ANTLRTokenType tt,
00070                                                                                   ANTLRChar *text,
00071                                                                                   int line) = 0;
00072 
00073         /* define to satisfy ANTLRTokenBuffer's need to determine whether or
00074            not a token object can be destroyed.  If nref()==0, no one has
00075            a reference, and the object may be destroyed.  This function defaults
00076            to 1, hence, if you use deleteTokens() message with a token object
00077            not derived from ANTLRCommonRefCountToken, the parser will compile
00078            but will not delete objects after they leave the token buffer.
00079     */
00080 
00081         virtual unsigned nref() const { return 1; }     // MR11
00082         virtual void ref() {;}
00083         virtual void deref() {;}
00084 
00085         virtual void panic(const char *msg)             // MR20 const
00086                 {
00087                         /* MR23 */ printMessage(stderr, "ANTLRAbstractToken panic: %s\n", msg);
00088                         exit(PCCTS_EXIT_FAILURE);
00089                 }
00090 
00091         virtual int printMessage(FILE* pFile, const char* pFormat, ...) // MR23
00092                 {
00093                         va_list marker;
00094                         va_start( marker, pFormat );
00095                         int iRet = vfprintf(pFile, pFormat, marker);
00096                         va_end( marker );
00097                         return iRet;
00098                 }
00099 };
00100 
00101 /* This class should be subclassed.  It cannot store token type or text */
00102 
00103 class ANTLRRefCountToken : public ANTLRAbstractToken {
00104 public:
00105 #ifdef DBG_REFCOUNTTOKEN
00106         static int ctor;
00107         static int dtor;
00108 #endif
00109 protected:
00110     unsigned refcnt_;
00111 #ifdef DBG_REFCOUNTTOKEN
00112         char object[200];
00113 #endif
00114 
00115 public:
00116 
00117         // MR23 - No matter what you do, you're hammered.
00118         //        Don't give names to formals something breaks.
00119         //                Give names to formals and don't use them it breaks.
00120 
00121 #ifndef DBG_REFCOUNTTOKEN
00122         ANTLRRefCountToken(ANTLRTokenType /* t MR23 */, const ANTLRChar * /* s MR23 */)
00123 #else
00124         ANTLRRefCountToken(ANTLRTokenType t, const ANTLRChar * s)
00125 #endif
00126 
00127 #ifndef DBG_REFCOUNTTOKEN
00128                 {
00129                         refcnt_ = 0;
00130                 }
00131 #else
00132         {
00133                 ctor++;
00134                 refcnt_ = 0;
00135                 if ( t==1 ) sprintf(object,"tok_EOF");
00136                 else sprintf(object,"tok_%s",s);
00137                 /* MR23 */ printMessage(stderr, "ctor %s #%d\n",object,ctor);
00138         }
00139 #endif
00140         ANTLRRefCountToken()
00141 #ifndef DBG_REFCOUNTTOKEN
00142                 { refcnt_ = 0; }
00143 #else
00144                 {
00145                         ctor++;
00146                         refcnt_ = 0;
00147                         sprintf(object,"tok_blank");
00148                         /* MR23 */ printMessage(stderr, "ctor %s #%d\n",object,ctor);
00149                 }
00150         virtual ~ANTLRRefCountToken()
00151                 {
00152                         dtor++;
00153                         if ( dtor>ctor ) /* MR23 */ printMessage(stderr, "WARNING: dtor>ctor\n");
00154                         /* MR23 */ printMessage(stderr, "dtor %s #%d\n", object, dtor);
00155                         object[0]='\0';
00156                 }
00157 #endif
00158 
00159         // reference counting stuff needed by ANTLRTokenPtr.
00160         // User should not access these; for C++ language reasons, we had
00161         // to make these public.  Yuck.
00162 
00163         void ref()                    { refcnt_++; }
00164         void deref()          { refcnt_--; }
00165         unsigned nref() const { return refcnt_; }   // MR11
00166 
00167         virtual ANTLRAbstractToken *makeToken(ANTLRTokenType /*tt MR23*/,
00168                                                                                   ANTLRChar * /*txt MR23*/,
00169                                                                                   int /*line MR23*/)
00170         {
00171                 panic("call to ANTLRRefCountToken::makeToken()\n");
00172                 return NULL;
00173         }
00174 };
00175 
00176 class ANTLRCommonNoRefCountToken : public ANTLRAbstractToken {
00177 protected:
00178         ANTLRTokenType _type;
00179         int _line;
00180         ANTLRChar *_text;               // MR9 RJV
00181 
00182 public:
00183         ANTLRCommonNoRefCountToken(ANTLRTokenType t, const ANTLRChar *s)
00184         { setType(t); _line = 0; _text = NULL; setText(s); }
00185         ANTLRCommonNoRefCountToken()
00186         { setType((ANTLRTokenType)0); _line = 0; _text = NULL; setText(""); }
00187 
00188         ~ANTLRCommonNoRefCountToken() { if (_text) delete [] _text; }  // MR9 RJV: Added Destructor to remove string
00189 
00190         ANTLRTokenType getType() const  { return _type; }
00191         void setType(ANTLRTokenType t)  { _type = t; }
00192         virtual int getLine() const             { return _line; }
00193         void setLine(int line)          { _line = line; }
00194         ANTLRChar *getText() const      { return _text; }
00195     int getLength() const           { return strlen(getText()); }       // MR11
00196 
00197 // MR9 RJV: Added code for variable length strings to setText()
00198 
00199         void setText(const ANTLRChar *s)
00200         {       if (s != _text) {
00201           if (_text) delete [] _text;
00202           if (s != NULL) {
00203                 _text = new ANTLRChar[strlen(s)+1];
00204             if (_text == NULL) panic("ANTLRCommonNoRefCountToken::setText new failed");
00205             strcpy(_text,s);
00206           } else {
00207             _text = new ANTLRChar[1];
00208             if (_text == NULL) panic("ANTLRCommonNoRefCountToken::setText new failed");
00209             strcpy(_text,"");
00210           };
00211         };
00212         }
00213 
00214         virtual ANTLRAbstractToken *makeToken(ANTLRTokenType tt,
00215                                                                                   ANTLRChar *txt,
00216                                                                                   int line)
00217                 {
00218                         ANTLRAbstractToken *t = new ANTLRCommonNoRefCountToken;
00219                         t->setType(tt); t->setText(txt); t->setLine(line);
00220                         return t;
00221                 }
00222 
00223 // MR9 THM Copy constructor required when heap allocated string is used with copy semantics
00224 
00225    ANTLRCommonNoRefCountToken (const ANTLRCommonNoRefCountToken& from) :
00226          ANTLRAbstractToken(from) {
00227          setType(from._type);
00228          setLine(from._line);
00229      _text=NULL;
00230      setText(from._text);
00231   };
00232 
00233 // MR9 THM operator =() required when heap allocated string is used with copy semantics
00234 
00235    virtual ANTLRCommonNoRefCountToken& operator =(const ANTLRCommonNoRefCountToken& rhs) {
00236 
00239 
00240      *( (ANTLRAbstractToken *) this ) = rhs;
00241 
00242          setType(rhs._type);
00243          setLine(rhs._line);
00244      setText(rhs._text);
00245      return *this;
00246    };
00247 };
00248 
00249 class ANTLRCommonToken : public ANTLRRefCountToken {
00250 protected:
00251         ANTLRTokenType       _type;
00252         int                  _line;
00253         ANTLRChar           *_text;               // MR9 RJV:Added
00254 
00255 public:
00256         ANTLRCommonToken(ANTLRTokenType t, const ANTLRChar *s) : ANTLRRefCountToken(t,s)
00257                 { setType(t); _line = 0; _text = NULL; setText(s); }                    // MR9
00258         ANTLRCommonToken()
00259                 { setType((ANTLRTokenType)0); _line = 0; _text = NULL; setText(""); }   // MR9
00260 
00261         virtual ~ANTLRCommonToken() { if (_text) delete [] _text; } // MR9 RJV: Added Destructor to remove string
00262 
00263         ANTLRTokenType getType() const  { return _type; }
00264         void setType(ANTLRTokenType t)  { _type = t; }
00265         virtual int getLine() const             { return _line; }
00266         void setLine(int line)          { _line = line; }
00267         ANTLRChar *getText() const              { return _text; }
00268     int getLength() const           { return strlen(getText()); }       // MR11
00269 
00270 // MR9 RJV: Added code for variable length strings to setText()
00271 
00272         void setText(const ANTLRChar *s)
00273         {       if (s != _text) {
00274           if (_text) delete [] _text;
00275           if (s != NULL) {
00276                 _text = new ANTLRChar[strlen(s)+1];
00277             if (_text == NULL) panic("ANTLRCommonToken::setText new failed");
00278             strcpy(_text,s);
00279           } else {
00280             _text = new ANTLRChar[1];
00281             if (_text == NULL) panic("ANTLRCommonToken::setText new failed");
00282             strcpy(_text,"");
00283           };
00284         };
00285         }
00286 
00287         virtual ANTLRAbstractToken *makeToken(ANTLRTokenType tt,
00288                                                                                   ANTLRChar *txt,
00289                                                                                   int line)
00290         {
00291                 ANTLRAbstractToken *t = new ANTLRCommonToken(tt,txt);
00292                 t->setLine(line);
00293                 return t;
00294         }
00295 
00296 // MR9 THM Copy constructor required when heap allocated string is used with copy semantics
00297 
00298    ANTLRCommonToken (const ANTLRCommonToken& from) :
00299          ANTLRRefCountToken(from) {
00300          setType(from._type);
00301          setLine(from._line);
00302      _text=NULL;
00303      setText(from._text);
00304   };
00305 
00306 // MR9 THM operator =() required when heap allocated string is used with copy semantics
00307 
00308    virtual ANTLRCommonToken& operator =(const ANTLRCommonToken& rhs) {
00309 
00312 
00313      *( (ANTLRRefCountToken *) this) = rhs;
00314 
00315          setType(rhs._type);
00316          setLine(rhs._line);
00317      setText(rhs._text);
00318      return *this;
00319    };
00320 };
00321 
00322 // used for backward compatibility
00323 typedef ANTLRCommonToken ANTLRCommonBacktrackingToken;
00324 
00325 #endif
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Defines