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.
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
25 * Parr Research Corporation
26 * with Purdue University and AHPCRC, University of Minnesota
37 #ifndef ANTLRCommonTokenTEXTSIZE
38 #define ANTLRCommonTokenTEXTSIZE 100
41 #ifdef DBG_REFCOUNTTOKEN
45 /* must define what a char looks like; can make this a class too */
46 typedef char ANTLRChar;
48 /* D E F I N E S M A R T P O I N T E R S */
50 //#include ATOKPTR_H not tested yet, leave out
51 class ANTLRAbstractToken;
52 typedef ANTLRAbstractToken *_ANTLRTokenPtr;
54 class ANTLRAbstractToken {
56 virtual ~ANTLRAbstractToken() {;}
57 virtual ANTLRTokenType getType() = 0;
58 virtual void setType(ANTLRTokenType t) = 0;
59 virtual int getLine() = 0;
60 virtual void setLine(int line) = 0;
61 virtual ANTLRChar *getText() = 0;
62 virtual void setText(ANTLRChar *) = 0;
64 /* This function will disappear when I can use templates */
65 virtual ANTLRAbstractToken *makeToken(ANTLRTokenType tt,
69 /* define to satisfy ANTLRTokenBuffer's need to determine whether or
70 not a token object can be destroyed. If nref()==0, no one has
71 a reference, and the object may be destroyed. This function defaults
72 to 1, hence, if you use deleteTokens() message with a token object
73 not derived from ANTLRCommonRefCountToken, the parser will compile
74 but will not delete objects after they leave the token buffer.
76 virtual unsigned nref() { return 1; }
77 virtual void ref() {;}
78 virtual void deref() {;}
80 virtual void panic(char *msg)
82 fprintf(stderr, "ANTLRAbstractToken panic: %s\n", msg);
83 exit(PCCTS_EXIT_FAILURE);
87 /* This class should be subclassed. It cannot store token type or text */
89 class ANTLRRefCountToken : public ANTLRAbstractToken {
91 #ifdef DBG_REFCOUNTTOKEN
97 #ifdef DBG_REFCOUNTTOKEN
102 ANTLRRefCountToken(ANTLRTokenType t, ANTLRChar *s)
103 #ifndef DBG_REFCOUNTTOKEN
111 if ( t==1 ) sprintf(object,"tok_EOF");
112 else sprintf(object,"tok_%s",s);
113 fprintf(stderr, "ctor %s #%d\n",object,ctor);
117 #ifndef DBG_REFCOUNTTOKEN
123 sprintf(object,"tok_blank");
124 fprintf(stderr, "ctor %s #%d\n",object,ctor);
126 virtual ~ANTLRRefCountToken()
129 if ( dtor>ctor ) fprintf(stderr, "WARNING: dtor>ctor\n");
130 fprintf(stderr, "dtor %s #%d\n", object, dtor);
135 // reference counting stuff needed by ANTLRTokenPtr.
136 // User should not access these; for C++ language reasons, we had
137 // to make these public. Yuck.
138 void ref() { refcnt_++; }
139 void deref() { refcnt_--; }
140 unsigned nref() { return refcnt_; }
142 virtual ANTLRAbstractToken *makeToken(ANTLRTokenType tt,
146 panic("call to ANTLRRefCountToken::makeToken()\n");
151 class ANTLRCommonNoRefCountToken : public ANTLRAbstractToken {
153 ANTLRTokenType _type;
155 ANTLRChar _text[ANTLRCommonTokenTEXTSIZE+1];
158 ANTLRCommonNoRefCountToken(ANTLRTokenType t, ANTLRChar *s)
159 { setType(t); _line = 0; setText(s); }
160 ANTLRCommonNoRefCountToken()
161 { setType((ANTLRTokenType)0); _line = 0; setText(""); }
163 ANTLRTokenType getType() { return _type; }
164 void setType(ANTLRTokenType t) { _type = t; }
165 virtual int getLine() { return _line; }
166 void setLine(int line) { _line = line; }
167 ANTLRChar *getText() { return _text; }
168 void setText(ANTLRChar *s)
169 { strncpy((char *)_text, (char *)s, ANTLRCommonTokenTEXTSIZE); }
170 virtual ANTLRAbstractToken *makeToken(ANTLRTokenType tt,
174 ANTLRAbstractToken *t = new ANTLRCommonNoRefCountToken;
175 t->setType(tt); t->setText(txt); t->setLine(line);
180 class ANTLRCommonToken : public ANTLRRefCountToken {
182 ANTLRTokenType _type;
184 ANTLRChar _text[ANTLRCommonTokenTEXTSIZE+1];
187 ANTLRCommonToken(ANTLRTokenType t, ANTLRChar *s) : ANTLRRefCountToken(t,s)
188 { setType(t); _line = 0; setText(s); }
190 { setType((ANTLRTokenType)0); _line = 0; setText(""); }
191 virtual ~ANTLRCommonToken() {;}
193 ANTLRTokenType getType() { return _type; }
194 void setType(ANTLRTokenType t) { _type = t; }
195 virtual int getLine() { return _line; }
196 void setLine(int line) { _line = line; }
197 ANTLRChar *getText() { return _text; }
198 void setText(ANTLRChar *s)
199 { strncpy((char *)_text, (char *)s, ANTLRCommonTokenTEXTSIZE); }
200 virtual ANTLRAbstractToken *makeToken(ANTLRTokenType tt,
204 ANTLRAbstractToken *t = new ANTLRCommonToken(tt,txt);
210 // used for backward compatibility
211 typedef ANTLRCommonToken ANTLRCommonBacktrackingToken;