]>
Commit | Line | Data |
---|---|---|
30fdf114 LG |
1 | /* ANTLRToken.h\r |
2 | *\r | |
3 | * SOFTWARE RIGHTS\r | |
4 | *\r | |
5 | * We reserve no LEGAL rights to the Purdue Compiler Construction Tool\r | |
6 | * Set (PCCTS) -- PCCTS is in the public domain. An individual or\r | |
7 | * company may do whatever they wish with source code distributed with\r | |
8 | * PCCTS or the code generated by PCCTS, including the incorporation of\r | |
9 | * PCCTS, or its output, into commerical software.\r | |
10 | *\r | |
11 | * We encourage users to develop software with PCCTS. However, we do ask\r | |
12 | * that credit is given to us for developing PCCTS. By "credit",\r | |
13 | * we mean that if you incorporate our source code into one of your\r | |
14 | * programs (commercial product, research project, or otherwise) that you\r | |
15 | * acknowledge this fact somewhere in the documentation, research report,\r | |
16 | * etc... If you like PCCTS and have developed a nice tool with the\r | |
17 | * output, please mention that you developed it using PCCTS. In\r | |
18 | * addition, we ask that this header remain intact in our source code.\r | |
19 | * As long as these guidelines are kept, we expect to continue enhancing\r | |
20 | * this system and expect to make other tools available as they are\r | |
21 | * completed.\r | |
22 | *\r | |
23 | * ANTLR 1.33\r | |
24 | * Terence Parr\r | |
25 | * Parr Research Corporation\r | |
26 | * with Purdue University and AHPCRC, University of Minnesota\r | |
27 | * 1989-2000\r | |
28 | */\r | |
29 | \r | |
30 | #ifndef ATOKEN_H_GATE\r | |
31 | #define ATOKEN_H_GATE\r | |
32 | \r | |
33 | #include "pcctscfg.h"\r | |
34 | \r | |
35 | #include "pccts_string.h"\r | |
36 | #include "pccts_stdio.h"\r | |
37 | #include "pccts_stdlib.h"\r | |
38 | #include "pccts_stdarg.h" // MR23\r | |
39 | \r | |
40 | PCCTS_NAMESPACE_STD\r | |
41 | \r | |
42 | // MR9 RJV (JVincent@novell.com) Not needed for variable length strings\r | |
43 | \r | |
44 | //// MR9 #ifndef ANTLRCommonTokenTEXTSIZE\r | |
45 | //// MR9 #define ANTLRCommonTokenTEXTSIZE 100\r | |
46 | //// MR9 #endif\r | |
47 | \r | |
48 | \r | |
49 | /* must define what a char looks like; can make this a class too */\r | |
50 | typedef char ANTLRChar;\r | |
51 | \r | |
52 | /* D E F I N E S M A R T P O I N T E R S */\r | |
53 | \r | |
54 | //#include ATOKPTR_H not tested yet, leave out\r | |
55 | class ANTLRAbstractToken;\r | |
56 | typedef ANTLRAbstractToken *_ANTLRTokenPtr;\r | |
57 | \r | |
58 | class ANTLRAbstractToken {\r | |
59 | public:\r | |
60 | virtual ~ANTLRAbstractToken() {;}\r | |
61 | virtual ANTLRTokenType getType() const = 0;\r | |
62 | virtual void setType(ANTLRTokenType t) = 0;\r | |
63 | virtual int getLine() const = 0;\r | |
64 | virtual void setLine(int line) = 0;\r | |
65 | virtual ANTLRChar *getText() const = 0;\r | |
66 | virtual void setText(const ANTLRChar *) = 0;\r | |
67 | \r | |
68 | /* This function will disappear when I can use templates */\r | |
69 | virtual ANTLRAbstractToken *makeToken(ANTLRTokenType tt,\r | |
70 | ANTLRChar *text,\r | |
71 | int line) = 0;\r | |
72 | \r | |
73 | /* define to satisfy ANTLRTokenBuffer's need to determine whether or\r | |
74 | not a token object can be destroyed. If nref()==0, no one has\r | |
75 | a reference, and the object may be destroyed. This function defaults\r | |
76 | to 1, hence, if you use deleteTokens() message with a token object\r | |
77 | not derived from ANTLRCommonRefCountToken, the parser will compile\r | |
78 | but will not delete objects after they leave the token buffer.\r | |
79 | */\r | |
80 | \r | |
81 | virtual unsigned nref() const { return 1; } // MR11\r | |
82 | virtual void ref() {;}\r | |
83 | virtual void deref() {;}\r | |
84 | \r | |
85 | virtual void panic(const char *msg) // MR20 const\r | |
86 | {\r | |
87 | /* MR23 */ printMessage(stderr, "ANTLRAbstractToken panic: %s\n", msg);\r | |
88 | exit(PCCTS_EXIT_FAILURE);\r | |
89 | }\r | |
90 | \r | |
91 | virtual int printMessage(FILE* pFile, const char* pFormat, ...) // MR23\r | |
92 | {\r | |
93 | va_list marker;\r | |
94 | va_start( marker, pFormat );\r | |
95 | int iRet = vfprintf(pFile, pFormat, marker);\r | |
96 | va_end( marker );\r | |
97 | return iRet;\r | |
98 | }\r | |
99 | };\r | |
100 | \r | |
101 | /* This class should be subclassed. It cannot store token type or text */\r | |
102 | \r | |
103 | class ANTLRRefCountToken : public ANTLRAbstractToken {\r | |
104 | public:\r | |
105 | #ifdef DBG_REFCOUNTTOKEN\r | |
106 | static int ctor;\r | |
107 | static int dtor;\r | |
108 | #endif\r | |
109 | protected:\r | |
110 | unsigned refcnt_;\r | |
111 | #ifdef DBG_REFCOUNTTOKEN\r | |
112 | char object[200];\r | |
113 | #endif\r | |
114 | \r | |
115 | public:\r | |
116 | \r | |
117 | // MR23 - No matter what you do, you're hammered.\r | |
118 | // Don't give names to formals something breaks.\r | |
119 | // Give names to formals and don't use them it breaks.\r | |
120 | \r | |
121 | #ifndef DBG_REFCOUNTTOKEN\r | |
122 | ANTLRRefCountToken(ANTLRTokenType /* t MR23 */, const ANTLRChar * /* s MR23 */)\r | |
123 | #else\r | |
124 | ANTLRRefCountToken(ANTLRTokenType t, const ANTLRChar * s)\r | |
125 | #endif\r | |
126 | \r | |
127 | #ifndef DBG_REFCOUNTTOKEN\r | |
128 | {\r | |
129 | refcnt_ = 0;\r | |
130 | }\r | |
131 | #else\r | |
132 | {\r | |
133 | ctor++;\r | |
134 | refcnt_ = 0;\r | |
135 | if ( t==1 ) sprintf(object,"tok_EOF");\r | |
136 | else sprintf(object,"tok_%s",s);\r | |
137 | /* MR23 */ printMessage(stderr, "ctor %s #%d\n",object,ctor);\r | |
138 | }\r | |
139 | #endif\r | |
140 | ANTLRRefCountToken()\r | |
141 | #ifndef DBG_REFCOUNTTOKEN\r | |
142 | { refcnt_ = 0; }\r | |
143 | #else\r | |
144 | {\r | |
145 | ctor++;\r | |
146 | refcnt_ = 0;\r | |
147 | sprintf(object,"tok_blank");\r | |
148 | /* MR23 */ printMessage(stderr, "ctor %s #%d\n",object,ctor);\r | |
149 | }\r | |
150 | virtual ~ANTLRRefCountToken()\r | |
151 | {\r | |
152 | dtor++;\r | |
153 | if ( dtor>ctor ) /* MR23 */ printMessage(stderr, "WARNING: dtor>ctor\n");\r | |
154 | /* MR23 */ printMessage(stderr, "dtor %s #%d\n", object, dtor);\r | |
155 | object[0]='\0';\r | |
156 | }\r | |
157 | #endif\r | |
158 | \r | |
159 | // reference counting stuff needed by ANTLRTokenPtr.\r | |
160 | // User should not access these; for C++ language reasons, we had\r | |
161 | // to make these public. Yuck.\r | |
162 | \r | |
163 | void ref() { refcnt_++; }\r | |
164 | void deref() { refcnt_--; }\r | |
165 | unsigned nref() const { return refcnt_; } // MR11\r | |
166 | \r | |
167 | virtual ANTLRAbstractToken *makeToken(ANTLRTokenType /*tt MR23*/,\r | |
168 | ANTLRChar * /*txt MR23*/,\r | |
169 | int /*line MR23*/)\r | |
170 | {\r | |
171 | panic("call to ANTLRRefCountToken::makeToken()\n");\r | |
172 | return NULL;\r | |
173 | }\r | |
174 | };\r | |
175 | \r | |
176 | class ANTLRCommonNoRefCountToken : public ANTLRAbstractToken {\r | |
177 | protected:\r | |
178 | ANTLRTokenType _type;\r | |
179 | int _line;\r | |
180 | ANTLRChar *_text; // MR9 RJV\r | |
181 | \r | |
182 | public:\r | |
183 | ANTLRCommonNoRefCountToken(ANTLRTokenType t, const ANTLRChar *s)\r | |
184 | { setType(t); _line = 0; _text = NULL; setText(s); }\r | |
185 | ANTLRCommonNoRefCountToken()\r | |
186 | { setType((ANTLRTokenType)0); _line = 0; _text = NULL; setText(""); }\r | |
187 | \r | |
188 | ~ANTLRCommonNoRefCountToken() { if (_text) delete [] _text; } // MR9 RJV: Added Destructor to remove string\r | |
189 | \r | |
190 | ANTLRTokenType getType() const { return _type; }\r | |
191 | void setType(ANTLRTokenType t) { _type = t; }\r | |
192 | virtual int getLine() const { return _line; }\r | |
193 | void setLine(int line) { _line = line; }\r | |
194 | ANTLRChar *getText() const { return _text; }\r | |
195 | int getLength() const { return strlen(getText()); } // MR11\r | |
196 | \r | |
197 | // MR9 RJV: Added code for variable length strings to setText()\r | |
198 | \r | |
199 | void setText(const ANTLRChar *s)\r | |
200 | { if (s != _text) {\r | |
201 | if (_text) delete [] _text;\r | |
202 | if (s != NULL) {\r | |
203 | _text = new ANTLRChar[strlen(s)+1];\r | |
204 | if (_text == NULL) panic("ANTLRCommonNoRefCountToken::setText new failed");\r | |
205 | strcpy(_text,s);\r | |
206 | } else {\r | |
207 | _text = new ANTLRChar[1];\r | |
208 | if (_text == NULL) panic("ANTLRCommonNoRefCountToken::setText new failed");\r | |
209 | strcpy(_text,"");\r | |
210 | };\r | |
211 | };\r | |
212 | }\r | |
213 | \r | |
214 | virtual ANTLRAbstractToken *makeToken(ANTLRTokenType tt,\r | |
215 | ANTLRChar *txt,\r | |
216 | int line)\r | |
217 | {\r | |
218 | ANTLRAbstractToken *t = new ANTLRCommonNoRefCountToken;\r | |
219 | t->setType(tt); t->setText(txt); t->setLine(line);\r | |
220 | return t;\r | |
221 | }\r | |
222 | \r | |
223 | // MR9 THM Copy constructor required when heap allocated string is used with copy semantics\r | |
224 | \r | |
225 | ANTLRCommonNoRefCountToken (const ANTLRCommonNoRefCountToken& from) :\r | |
226 | ANTLRAbstractToken(from) {\r | |
227 | setType(from._type);\r | |
228 | setLine(from._line);\r | |
229 | _text=NULL;\r | |
230 | setText(from._text);\r | |
231 | };\r | |
232 | \r | |
233 | // MR9 THM operator =() required when heap allocated string is used with copy semantics\r | |
234 | \r | |
235 | virtual ANTLRCommonNoRefCountToken& operator =(const ANTLRCommonNoRefCountToken& rhs) {\r | |
236 | \r | |
237 | ////// MR15 WatCom can't hack use of operator =()\r | |
238 | ////// Use this: *( (ANTRLAbstractToken *) this)=rhs;\r | |
239 | \r | |
240 | *( (ANTLRAbstractToken *) this ) = rhs;\r | |
241 | \r | |
242 | setType(rhs._type);\r | |
243 | setLine(rhs._line);\r | |
244 | setText(rhs._text);\r | |
245 | return *this;\r | |
246 | };\r | |
247 | };\r | |
248 | \r | |
249 | class ANTLRCommonToken : public ANTLRRefCountToken {\r | |
250 | protected:\r | |
251 | ANTLRTokenType _type;\r | |
252 | int _line;\r | |
253 | ANTLRChar *_text; // MR9 RJV:Added\r | |
254 | \r | |
255 | public:\r | |
256 | ANTLRCommonToken(ANTLRTokenType t, const ANTLRChar *s) : ANTLRRefCountToken(t,s)\r | |
257 | { setType(t); _line = 0; _text = NULL; setText(s); } // MR9\r | |
258 | ANTLRCommonToken()\r | |
259 | { setType((ANTLRTokenType)0); _line = 0; _text = NULL; setText(""); } // MR9\r | |
260 | \r | |
261 | virtual ~ANTLRCommonToken() { if (_text) delete [] _text; } // MR9 RJV: Added Destructor to remove string\r | |
262 | \r | |
263 | ANTLRTokenType getType() const { return _type; }\r | |
264 | void setType(ANTLRTokenType t) { _type = t; }\r | |
265 | virtual int getLine() const { return _line; }\r | |
266 | void setLine(int line) { _line = line; }\r | |
267 | ANTLRChar *getText() const { return _text; }\r | |
268 | int getLength() const { return strlen(getText()); } // MR11\r | |
269 | \r | |
270 | // MR9 RJV: Added code for variable length strings to setText()\r | |
271 | \r | |
272 | void setText(const ANTLRChar *s)\r | |
273 | { if (s != _text) {\r | |
274 | if (_text) delete [] _text;\r | |
275 | if (s != NULL) {\r | |
276 | _text = new ANTLRChar[strlen(s)+1];\r | |
277 | if (_text == NULL) panic("ANTLRCommonToken::setText new failed");\r | |
278 | strcpy(_text,s);\r | |
279 | } else {\r | |
280 | _text = new ANTLRChar[1];\r | |
281 | if (_text == NULL) panic("ANTLRCommonToken::setText new failed");\r | |
282 | strcpy(_text,"");\r | |
283 | };\r | |
284 | };\r | |
285 | }\r | |
286 | \r | |
287 | virtual ANTLRAbstractToken *makeToken(ANTLRTokenType tt,\r | |
288 | ANTLRChar *txt,\r | |
289 | int line)\r | |
290 | {\r | |
291 | ANTLRAbstractToken *t = new ANTLRCommonToken(tt,txt);\r | |
292 | t->setLine(line);\r | |
293 | return t;\r | |
294 | }\r | |
295 | \r | |
296 | // MR9 THM Copy constructor required when heap allocated string is used with copy semantics\r | |
297 | \r | |
298 | ANTLRCommonToken (const ANTLRCommonToken& from) :\r | |
299 | ANTLRRefCountToken(from) {\r | |
300 | setType(from._type);\r | |
301 | setLine(from._line);\r | |
302 | _text=NULL;\r | |
303 | setText(from._text);\r | |
304 | };\r | |
305 | \r | |
306 | // MR9 THM operator =() required when heap allocated string is used with copy semantics\r | |
307 | \r | |
308 | virtual ANTLRCommonToken& operator =(const ANTLRCommonToken& rhs) {\r | |
309 | \r | |
310 | ////// MR15 WatCom can't hack use of operator =()\r | |
311 | ////// Use this instead: *( (ANTRLRRefCountToken *) this)=rhs;\r | |
312 | \r | |
313 | *( (ANTLRRefCountToken *) this) = rhs;\r | |
314 | \r | |
315 | setType(rhs._type);\r | |
316 | setLine(rhs._line);\r | |
317 | setText(rhs._text);\r | |
318 | return *this;\r | |
319 | };\r | |
320 | };\r | |
321 | \r | |
322 | // used for backward compatibility\r | |
323 | typedef ANTLRCommonToken ANTLRCommonBacktrackingToken;\r | |
324 | \r | |
325 | #endif\r |