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