Home | History | Annotate | Download | only in h
      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-2000
     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 #include "pccts_stdarg.h" // MR23
     39 
     40 PCCTS_NAMESPACE_STD
     41 
     42 // MR9      RJV (JVincent (at) novell.com) Not needed for variable length strings
     43 
     44 //// MR9 #ifndef ANTLRCommonTokenTEXTSIZE
     45 //// MR9 #define ANTLRCommonTokenTEXTSIZE        	100
     46 //// MR9 #endif
     47 
     48 
     49 /* must define what a char looks like; can make this a class too */
     50 typedef char ANTLRChar;
     51 
     52 /* D E F I N E  S M A R T  P O I N T E R S */
     53 
     54 //#include ATOKPTR_H   not tested yet, leave out
     55 class ANTLRAbstractToken;
     56 typedef ANTLRAbstractToken *_ANTLRTokenPtr;
     57 
     58 class ANTLRAbstractToken {
     59 public:
     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;
     67 
     68     /* This function will disappear when I can use templates */
     69 	virtual ANTLRAbstractToken *makeToken(ANTLRTokenType tt,
     70 										  ANTLRChar *text,
     71 										  int line) = 0;
     72 
     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.
     79     */
     80 
     81 	virtual unsigned nref() const { return 1; }     // MR11
     82 	virtual void ref() {;}
     83 	virtual void deref() {;}
     84 
     85 	virtual void panic(const char *msg)             // MR20 const
     86 		{
     87 			/* MR23 */ printMessage(stderr, "ANTLRAbstractToken panic: %s\n", msg);
     88 			exit(PCCTS_EXIT_FAILURE);
     89 		}
     90 
     91 	virtual int printMessage(FILE* pFile, const char* pFormat, ...) // MR23
     92 		{
     93 			va_list marker;
     94 			va_start( marker, pFormat );
     95   			int iRet = vfprintf(pFile, pFormat, marker);
     96 			va_end( marker );
     97 			return iRet;
     98 		}
     99 };
    100 
    101 /* This class should be subclassed.  It cannot store token type or text */
    102 
    103 class ANTLRRefCountToken : public ANTLRAbstractToken {
    104 public:
    105 #ifdef DBG_REFCOUNTTOKEN
    106 	static int ctor;
    107 	static int dtor;
    108 #endif
    109 protected:
    110     unsigned refcnt_;
    111 #ifdef DBG_REFCOUNTTOKEN
    112 	char object[200];
    113 #endif
    114 
    115 public:
    116 
    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.
    120 
    121 #ifndef DBG_REFCOUNTTOKEN
    122 	ANTLRRefCountToken(ANTLRTokenType /* t MR23 */, const ANTLRChar * /* s MR23 */)
    123 #else
    124 	ANTLRRefCountToken(ANTLRTokenType t, const ANTLRChar * s)
    125 #endif
    126 
    127 #ifndef DBG_REFCOUNTTOKEN
    128 		{
    129 			refcnt_ = 0;
    130 		}
    131 #else
    132 	{
    133 		ctor++;
    134 		refcnt_ = 0;
    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);
    138 	}
    139 #endif
    140 	ANTLRRefCountToken()
    141 #ifndef DBG_REFCOUNTTOKEN
    142 		{ refcnt_ = 0; }
    143 #else
    144 		{
    145 			ctor++;
    146 			refcnt_ = 0;
    147 			sprintf(object,"tok_blank");
    148 			/* MR23 */ printMessage(stderr, "ctor %s #%d\n",object,ctor);
    149 		}
    150 	virtual ~ANTLRRefCountToken()
    151 		{
    152 			dtor++;
    153 			if ( dtor>ctor ) /* MR23 */ printMessage(stderr, "WARNING: dtor>ctor\n");
    154 			/* MR23 */ printMessage(stderr, "dtor %s #%d\n", object, dtor);
    155 			object[0]='\0';
    156 		}
    157 #endif
    158 
    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.
    162 
    163 	void ref()		      { refcnt_++; }
    164 	void deref()	      { refcnt_--; }
    165 	unsigned nref()	const { return refcnt_; }   // MR11
    166 
    167 	virtual ANTLRAbstractToken *makeToken(ANTLRTokenType /*tt MR23*/,
    168 										  ANTLRChar * /*txt MR23*/,
    169 										  int /*line MR23*/)
    170 	{
    171 		panic("call to ANTLRRefCountToken::makeToken()\n");
    172 		return NULL;
    173 	}
    174 };
    175 
    176 class ANTLRCommonNoRefCountToken : public ANTLRAbstractToken {
    177 protected:
    178 	ANTLRTokenType _type;
    179 	int _line;
    180 	ANTLRChar *_text;               // MR9 RJV
    181 
    182 public:
    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(""); }
    187 
    188 	~ANTLRCommonNoRefCountToken() { if (_text) delete [] _text; }  // MR9 RJV: Added Destructor to remove string
    189 
    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
    196 
    197 // MR9 RJV: Added code for variable length strings to setText()
    198 
    199 	void setText(const ANTLRChar *s)
    200 	{	if (s != _text) {
    201           if (_text) delete [] _text;
    202           if (s != NULL) {
    203          	_text = new ANTLRChar[strlen(s)+1];
    204             if (_text == NULL) panic("ANTLRCommonNoRefCountToken::setText new failed");
    205             strcpy(_text,s);
    206     	  } else {
    207             _text = new ANTLRChar[1];
    208             if (_text == NULL) panic("ANTLRCommonNoRefCountToken::setText new failed");
    209             strcpy(_text,"");
    210           };
    211         };
    212 	}
    213 
    214 	virtual ANTLRAbstractToken *makeToken(ANTLRTokenType tt,
    215 										  ANTLRChar *txt,
    216 										  int line)
    217 		{
    218 			ANTLRAbstractToken *t = new ANTLRCommonNoRefCountToken;
    219 			t->setType(tt); t->setText(txt); t->setLine(line);
    220 			return t;
    221 		}
    222 
    223 // MR9 THM Copy constructor required when heap allocated string is used with copy semantics
    224 
    225    ANTLRCommonNoRefCountToken (const ANTLRCommonNoRefCountToken& from) :
    226          ANTLRAbstractToken(from) {
    227  	 setType(from._type);
    228 	 setLine(from._line);
    229      _text=NULL;
    230      setText(from._text);
    231   };
    232 
    233 // MR9 THM operator =() required when heap allocated string is used with copy semantics
    234 
    235    virtual ANTLRCommonNoRefCountToken& operator =(const ANTLRCommonNoRefCountToken& rhs) {
    236 
    237 //////  MR15 WatCom can't hack use of operator =()
    238 //////  Use this:  *( (ANTRLAbstractToken *) this)=rhs;
    239 
    240      *( (ANTLRAbstractToken *) this ) = rhs;
    241 
    242   	 setType(rhs._type);
    243  	 setLine(rhs._line);
    244      setText(rhs._text);
    245      return *this;
    246    };
    247 };
    248 
    249 class ANTLRCommonToken : public ANTLRRefCountToken {
    250 protected:
    251 	ANTLRTokenType       _type;
    252 	int                  _line;
    253 	ANTLRChar           *_text;               // MR9 RJV:Added
    254 
    255 public:
    256 	ANTLRCommonToken(ANTLRTokenType t, const ANTLRChar *s) : ANTLRRefCountToken(t,s)
    257 		{ setType(t); _line = 0; _text = NULL; setText(s); }                    // MR9
    258 	ANTLRCommonToken()
    259 		{ setType((ANTLRTokenType)0); _line = 0; _text = NULL; setText(""); }   // MR9
    260 
    261 	virtual ~ANTLRCommonToken() { if (_text) delete [] _text; } // MR9 RJV: Added Destructor to remove string
    262 
    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
    269 
    270 // MR9 RJV: Added code for variable length strings to setText()
    271 
    272 	void setText(const ANTLRChar *s)
    273 	{	if (s != _text) {
    274           if (_text) delete [] _text;
    275           if (s != NULL) {
    276          	_text = new ANTLRChar[strlen(s)+1];
    277             if (_text == NULL) panic("ANTLRCommonToken::setText new failed");
    278             strcpy(_text,s);
    279     	  } else {
    280             _text = new ANTLRChar[1];
    281             if (_text == NULL) panic("ANTLRCommonToken::setText new failed");
    282             strcpy(_text,"");
    283           };
    284         };
    285 	}
    286 
    287 	virtual ANTLRAbstractToken *makeToken(ANTLRTokenType tt,
    288 										  ANTLRChar *txt,
    289 										  int line)
    290 	{
    291 		ANTLRAbstractToken *t = new ANTLRCommonToken(tt,txt);
    292 		t->setLine(line);
    293 		return t;
    294 	}
    295 
    296 // MR9 THM Copy constructor required when heap allocated string is used with copy semantics
    297 
    298    ANTLRCommonToken (const ANTLRCommonToken& from) :
    299          ANTLRRefCountToken(from) {
    300  	 setType(from._type);
    301 	 setLine(from._line);
    302      _text=NULL;
    303      setText(from._text);
    304   };
    305 
    306 // MR9 THM operator =() required when heap allocated string is used with copy semantics
    307 
    308    virtual ANTLRCommonToken& operator =(const ANTLRCommonToken& rhs) {
    309 
    310 //////  MR15 WatCom can't hack use of operator =()
    311 //////  Use this instead:   *( (ANTRLRRefCountToken *) this)=rhs;
    312 
    313      *( (ANTLRRefCountToken *) this) = rhs;
    314 
    315   	 setType(rhs._type);
    316  	 setLine(rhs._line);
    317      setText(rhs._text);
    318      return *this;
    319    };
    320 };
    321 
    322 // used for backward compatibility
    323 typedef ANTLRCommonToken ANTLRCommonBacktrackingToken;
    324 
    325 #endif
    326