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