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