]> pd.if.org Git - pccts/blob - h/AToken.h
auto commit for import
[pccts] / h / AToken.h
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-1995
28  */
29
30 #ifndef ATOKEN_H_GATE
31 #define ATOKEN_H_GATE
32
33 #include <string.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36
37 #ifndef ANTLRCommonTokenTEXTSIZE
38 #define ANTLRCommonTokenTEXTSIZE                100
39 #endif
40
41 #ifdef DBG_REFCOUNTTOKEN
42 #include <stdio.h>
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 #include "config.h"
50 //#include ATOKPTR_H   not tested yet, leave out
51 class ANTLRAbstractToken;
52 typedef ANTLRAbstractToken *_ANTLRTokenPtr;
53
54 class ANTLRAbstractToken {
55 public:
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;
63
64     /* This function will disappear when I can use templates */
65         virtual ANTLRAbstractToken *makeToken(ANTLRTokenType tt,
66                                                                                   ANTLRChar *text,
67                                                                                   int line) = 0;
68
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.
75     */
76         virtual unsigned nref() { return 1; }
77         virtual void ref() {;}
78         virtual void deref() {;}
79
80         virtual void panic(char *msg)
81                 {
82                         fprintf(stderr, "ANTLRAbstractToken panic: %s\n", msg);
83                         exit(PCCTS_EXIT_FAILURE);
84                 }
85 };
86
87 /* This class should be subclassed.  It cannot store token type or text */
88
89 class ANTLRRefCountToken : public ANTLRAbstractToken {
90 public:
91 #ifdef DBG_REFCOUNTTOKEN
92         static int ctor;
93         static int dtor;
94 #endif
95 protected:
96     unsigned refcnt_;
97 #ifdef DBG_REFCOUNTTOKEN
98         char object[200];
99 #endif
100
101 public:
102         ANTLRRefCountToken(ANTLRTokenType t, ANTLRChar *s)
103 #ifndef DBG_REFCOUNTTOKEN
104                 {
105                         refcnt_ = 0;
106                 }
107 #else
108         {
109                 ctor++;
110                 refcnt_ = 0;
111                 if ( t==1 ) sprintf(object,"tok_EOF");
112                 else sprintf(object,"tok_%s",s);
113                 fprintf(stderr, "ctor %s #%d\n",object,ctor);
114         }
115 #endif
116         ANTLRRefCountToken()
117 #ifndef DBG_REFCOUNTTOKEN
118                 { refcnt_ = 0; }
119 #else
120                 {
121                         ctor++;
122                         refcnt_ = 0;
123                         sprintf(object,"tok_blank");
124                         fprintf(stderr, "ctor %s #%d\n",object,ctor);
125                 }
126         virtual ~ANTLRRefCountToken()
127                 {
128                         dtor++;
129                         if ( dtor>ctor ) fprintf(stderr, "WARNING: dtor>ctor\n");
130                         fprintf(stderr, "dtor %s #%d\n", object, dtor);
131                         object[0]='\0';
132                 }
133 #endif
134
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_; }
141
142         virtual ANTLRAbstractToken *makeToken(ANTLRTokenType tt,
143                                                                                   ANTLRChar *txt,
144                                                                                   int line)
145         {
146                 panic("call to ANTLRRefCountToken::makeToken()\n");
147                 return NULL;
148         }
149 };
150
151 class ANTLRCommonNoRefCountToken : public ANTLRAbstractToken {
152 protected:
153         ANTLRTokenType _type;
154         int _line;
155         ANTLRChar _text[ANTLRCommonTokenTEXTSIZE+1];
156
157 public:
158         ANTLRCommonNoRefCountToken(ANTLRTokenType t, ANTLRChar *s)
159                 { setType(t); _line = 0; setText(s); }
160         ANTLRCommonNoRefCountToken()
161                 { setType((ANTLRTokenType)0); _line = 0; setText(""); }
162
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,
171                                                                                   ANTLRChar *txt,
172                                                                                   int line)
173                 {
174                         ANTLRAbstractToken *t = new ANTLRCommonNoRefCountToken;
175                         t->setType(tt); t->setText(txt); t->setLine(line);
176                         return t;
177                 }
178 };
179
180 class ANTLRCommonToken : public ANTLRRefCountToken {
181 protected:
182         ANTLRTokenType _type;
183         int _line;
184         ANTLRChar _text[ANTLRCommonTokenTEXTSIZE+1];
185
186 public:
187         ANTLRCommonToken(ANTLRTokenType t, ANTLRChar *s) : ANTLRRefCountToken(t,s)
188                 { setType(t); _line = 0; setText(s); }
189         ANTLRCommonToken()
190                 { setType((ANTLRTokenType)0); _line = 0; setText(""); }
191         virtual ~ANTLRCommonToken() {;}
192
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,
201                                                                                   ANTLRChar *txt,
202                                                                                   int line)
203                 {
204                         ANTLRAbstractToken *t = new ANTLRCommonToken(tt,txt);
205                         t->setLine(line);
206                         return t;
207                 }
208 };
209
210 // used for backward compatibility
211 typedef ANTLRCommonToken ANTLRCommonBacktrackingToken;
212
213 #endif