AToken.h
上传用户:itx_2006
上传日期:2007-01-06
资源大小:493k
文件大小:9k
源码类别:

编译器/解释器

开发平台:

Others

  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. #ifndef ATOKEN_H_GATE
  30. #define ATOKEN_H_GATE
  31. #include "pcctscfg.h"
  32. #include PCCTS_STRING_H
  33. #include PCCTS_STDIO_H
  34. #include PCCTS_STDLIB_H
  35. PCCTS_NAMESPACE_STD
  36. // MR9      RJV (JVincent@novell.com) Not needed for variable length strings
  37. //// MR9 #ifndef ANTLRCommonTokenTEXTSIZE
  38. //// MR9 #define ANTLRCommonTokenTEXTSIZE         100
  39. //// MR9 #endif
  40. /* must define what a char looks like; can make this a class too */
  41. typedef char ANTLRChar;
  42. /* D E F I N E  S M A R T  P O I N T E R S */
  43. //#include ATOKPTR_H   not tested yet, leave out
  44. class ANTLRAbstractToken;
  45. typedef ANTLRAbstractToken *_ANTLRTokenPtr;
  46. class ANTLRAbstractToken {
  47. public:
  48.     virtual ~ANTLRAbstractToken() {;}
  49.     virtual ANTLRTokenType getType() const = 0;
  50.     virtual void setType(ANTLRTokenType t) = 0;
  51.     virtual int getLine() const = 0;
  52.     virtual void setLine(int line) = 0;
  53.     virtual ANTLRChar *getText() const = 0;
  54.     virtual void setText(const ANTLRChar *) = 0;
  55.     /* This function will disappear when I can use templates */
  56. virtual ANTLRAbstractToken *makeToken(ANTLRTokenType tt,
  57.   ANTLRChar *text,
  58.   int line) = 0;
  59. /* define to satisfy ANTLRTokenBuffer's need to determine whether or
  60.    not a token object can be destroyed.  If nref()==0, no one has
  61.    a reference, and the object may be destroyed.  This function defaults
  62.    to 1, hence, if you use deleteTokens() message with a token object
  63.    not derived from ANTLRCommonRefCountToken, the parser will compile
  64.    but will not delete objects after they leave the token buffer.
  65.     */
  66. virtual unsigned nref() const { return 1; }     // MR11
  67. virtual void ref() {;}
  68. virtual void deref() {;}
  69. virtual void panic(char *msg)
  70. {
  71. fprintf(stderr, "ANTLRAbstractToken panic: %sn", msg);
  72. exit(PCCTS_EXIT_FAILURE);
  73. }
  74. };
  75. /* This class should be subclassed.  It cannot store token type or text */
  76. class ANTLRRefCountToken : public ANTLRAbstractToken {
  77. public:
  78. #ifdef DBG_REFCOUNTTOKEN
  79. static int ctor;
  80. static int dtor;
  81. #endif
  82. protected:
  83.     unsigned refcnt_;
  84. #ifdef DBG_REFCOUNTTOKEN
  85. char object[200];
  86. #endif
  87. public:
  88. ANTLRRefCountToken(ANTLRTokenType t, const ANTLRChar *s)
  89. #ifndef DBG_REFCOUNTTOKEN
  90. {
  91. refcnt_ = 0;
  92. }
  93. #else
  94. {
  95. ctor++;
  96. refcnt_ = 0;
  97. if ( t==1 ) sprintf(object,"tok_EOF");
  98. else sprintf(object,"tok_%s",s);
  99. fprintf(stderr, "ctor %s #%dn",object,ctor);
  100. }
  101. #endif
  102. ANTLRRefCountToken()
  103. #ifndef DBG_REFCOUNTTOKEN
  104. { refcnt_ = 0; }
  105. #else
  106. {
  107. ctor++;
  108. refcnt_ = 0;
  109. sprintf(object,"tok_blank");
  110. fprintf(stderr, "ctor %s #%dn",object,ctor);
  111. }
  112. virtual ~ANTLRRefCountToken()
  113. {
  114. dtor++;
  115. if ( dtor>ctor ) fprintf(stderr, "WARNING: dtor>ctorn");
  116. fprintf(stderr, "dtor %s #%dn", object, dtor);
  117. object[0]='';
  118. }
  119. #endif
  120. // reference counting stuff needed by ANTLRTokenPtr.
  121. // User should not access these; for C++ language reasons, we had
  122. // to make these public.  Yuck.
  123. void ref()       { refcnt_++; }
  124. void deref()       { refcnt_--; }
  125. unsigned nref() const { return refcnt_; }   // MR11
  126. virtual ANTLRAbstractToken *makeToken(ANTLRTokenType tt,
  127.   ANTLRChar *txt,
  128.   int line)
  129. {
  130. panic("call to ANTLRRefCountToken::makeToken()n");
  131. return NULL;
  132. }
  133. };
  134. class ANTLRCommonNoRefCountToken : public ANTLRAbstractToken {
  135. protected:
  136. ANTLRTokenType _type;
  137. int _line;
  138. ANTLRChar *_text;               // MR9 RJV
  139. public:
  140. ANTLRCommonNoRefCountToken(ANTLRTokenType t, const ANTLRChar *s)
  141. { setType(t); _line = 0; _text = NULL; setText(s); }
  142. ANTLRCommonNoRefCountToken()
  143. { setType((ANTLRTokenType)0); _line = 0; _text = NULL; setText(""); }
  144. ~ANTLRCommonNoRefCountToken() { if (_text) delete [] _text; }  // MR9 RJV: Added Destructor to remove string
  145. ANTLRTokenType getType() const  { return _type; }
  146. void setType(ANTLRTokenType t) { _type = t; }
  147. virtual int getLine() const { return _line; }
  148. void setLine(int line)      { _line = line; }
  149. ANTLRChar *getText() const    { return _text; }
  150.     int getLength() const           { return strlen(getText()); }       // MR11
  151. // MR9 RJV: Added code for variable length strings to setText()
  152. void setText(const ANTLRChar *s)
  153. { if (s != _text) {
  154.           if (_text) delete [] _text;
  155.           if (s != NULL) {
  156.           _text = new ANTLRChar[strlen(s)+1];
  157.             if (_text == NULL) panic("ANTLRCommonNoRefCountToken::setText new failed");
  158.             strcpy(_text,s);
  159.        } else {
  160.             _text = new ANTLRChar[1];
  161.             if (_text == NULL) panic("ANTLRCommonNoRefCountToken::setText new failed");
  162.             strcpy(_text,"");
  163.           };
  164.         };
  165. }
  166. virtual ANTLRAbstractToken *makeToken(ANTLRTokenType tt,
  167.   ANTLRChar *txt,
  168.   int line)
  169. {
  170. ANTLRAbstractToken *t = new ANTLRCommonNoRefCountToken;
  171. t->setType(tt); t->setText(txt); t->setLine(line);
  172. return t;
  173. }
  174. // MR9 THM Copy constructor required when heap allocated string is used with copy semantics
  175.    ANTLRCommonNoRefCountToken (const ANTLRCommonNoRefCountToken& from) :
  176.          ANTLRAbstractToken(from) {
  177.    setType(from._type);
  178.  setLine(from._line);
  179.      _text=NULL;
  180.      setText(from._text);
  181.   };
  182. // MR9 THM operator =() required when heap allocated string is used with copy semantics
  183.    virtual ANTLRCommonNoRefCountToken& operator =(const ANTLRCommonNoRefCountToken& rhs) {
  184.      this->ANTLRAbstractToken::operator=(rhs);
  185.     setType(rhs._type);
  186.    setLine(rhs._line);
  187.      setText(rhs._text);
  188.      return *this;
  189.    };
  190. };
  191. class ANTLRCommonToken : public ANTLRRefCountToken {
  192. protected:
  193. ANTLRTokenType       _type;
  194. int                  _line;
  195. ANTLRChar           *_text;               // MR9 RJV:Added
  196. public:
  197. ANTLRCommonToken(ANTLRTokenType t, const ANTLRChar *s) : ANTLRRefCountToken(t,s)
  198. { setType(t); _line = 0; _text = NULL; setText(s); }                    // MR9
  199. ANTLRCommonToken()
  200. { setType((ANTLRTokenType)0); _line = 0; _text = NULL; setText(""); }   // MR9
  201. virtual ~ANTLRCommonToken() { if (_text) delete [] _text; } // MR9 RJV: Added Destructor to remove string
  202. ANTLRTokenType getType() const  { return _type; }
  203. void setType(ANTLRTokenType t) { _type = t; }
  204. virtual int getLine() const { return _line; }
  205. void setLine(int line)      { _line = line; }
  206. ANTLRChar *getText() const { return _text; }
  207.     int getLength() const           { return strlen(getText()); }       // MR11
  208. // MR9 RJV: Added code for variable length strings to setText()
  209. void setText(const ANTLRChar *s)
  210. { if (s != _text) {
  211.           if (_text) delete [] _text;
  212.           if (s != NULL) {
  213.           _text = new ANTLRChar[strlen(s)+1];
  214.             if (_text == NULL) panic("ANTLRCommonToken::setText new failed");
  215.             strcpy(_text,s);
  216.        } else {
  217.             _text = new ANTLRChar[1];
  218.             if (_text == NULL) panic("ANTLRCommonToken::setText new failed");
  219.             strcpy(_text,"");
  220.           };
  221.         };
  222. }
  223. virtual ANTLRAbstractToken *makeToken(ANTLRTokenType tt,
  224.   ANTLRChar *txt,
  225.   int line)
  226. {
  227. ANTLRAbstractToken *t = new ANTLRCommonToken(tt,txt);
  228. t->setLine(line);
  229. return t;
  230. }
  231. // MR9 THM Copy constructor required when heap allocated string is used with copy semantics
  232.    ANTLRCommonToken (const ANTLRCommonToken& from) :
  233.          ANTLRRefCountToken(from) {
  234.    setType(from._type);
  235.  setLine(from._line);
  236.      _text=NULL;
  237.      setText(from._text);
  238.   };
  239. // MR9 THM operator =() required when heap allocated string is used with copy semantics
  240.    virtual ANTLRCommonToken& operator =(const ANTLRCommonToken& rhs) {
  241.      this->ANTLRRefCountToken::operator=(rhs);
  242.     setType(rhs._type);
  243.    setLine(rhs._line);
  244.      setText(rhs._text);
  245.      return *this;
  246.    };
  247. };
  248. // used for backward compatibility
  249. typedef ANTLRCommonToken ANTLRCommonBacktrackingToken;
  250. #endif