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
35 #include "pccts_string.h"
36 #include "pccts_stdio.h"
37 #include "pccts_stdlib.h"
41 #ifndef ANTLRCommonTokenTEXTSIZE
42 #define ANTLRCommonTokenTEXTSIZE 100
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 */
52 //#include ATOKPTR_H not tested yet, leave out
53 class ANTLRAbstractToken
;
54 typedef ANTLRAbstractToken
*_ANTLRTokenPtr
;
56 class DllExportPCCTS ANTLRAbstractToken
{
58 virtual ~ANTLRAbstractToken() {;}
59 virtual ANTLRTokenType
getType() = 0;
60 virtual void setType(ANTLRTokenType t
) = 0;
61 virtual int getLine() = 0;
62 virtual void setLine(int line
) = 0;
63 virtual ANTLRChar
*getText() = 0;
64 virtual void setText(ANTLRChar
*) = 0;
66 /* This function will disappear when I can use templates */
67 virtual ANTLRAbstractToken
*makeToken(ANTLRTokenType tt
,
71 /* define to satisfy ANTLRTokenBuffer's need to determine whether or
72 not a token object can be destroyed. If nref()==0, no one has
73 a reference, and the object may be destroyed. This function defaults
74 to 1, hence, if you use deleteTokens() message with a token object
75 not derived from ANTLRCommonRefCountToken, the parser will compile
76 but will not delete objects after they leave the token buffer.
78 virtual unsigned nref() { return 1; }
79 virtual void ref() {;}
80 virtual void deref() {;}
82 virtual void panic(char *msg
)
84 fprintf(stderr
, "ANTLRAbstractToken panic: %s\n", msg
);
85 exit(PCCTS_EXIT_FAILURE
);
89 /* This class should be subclassed. It cannot store token type or text */
91 class DllExportPCCTS ANTLRRefCountToken
: public ANTLRAbstractToken
{
93 #ifdef DBG_REFCOUNTTOKEN
99 #ifdef DBG_REFCOUNTTOKEN
104 ANTLRRefCountToken(ANTLRTokenType t
, ANTLRChar
*s
)
105 #ifndef DBG_REFCOUNTTOKEN
113 if ( t
==1 ) sprintf(object
,"tok_EOF");
114 else sprintf(object
,"tok_%s",s
);
115 fprintf(stderr
, "ctor %s #%d\n",object
,ctor
);
119 #ifndef DBG_REFCOUNTTOKEN
125 sprintf(object
,"tok_blank");
126 fprintf(stderr
, "ctor %s #%d\n",object
,ctor
);
128 virtual ~ANTLRRefCountToken()
131 if ( dtor
>ctor
) fprintf(stderr
, "WARNING: dtor>ctor\n");
132 fprintf(stderr
, "dtor %s #%d\n", object
, dtor
);
137 // reference counting stuff needed by ANTLRTokenPtr.
138 // User should not access these; for C++ language reasons, we had
139 // to make these public. Yuck.
140 void ref() { refcnt_
++; }
141 void deref() { refcnt_
--; }
142 unsigned nref() { return refcnt_
; }
144 virtual ANTLRAbstractToken
*makeToken(ANTLRTokenType tt
,
148 panic("call to ANTLRRefCountToken::makeToken()\n");
153 class DllExportPCCTS ANTLRCommonNoRefCountToken
: public ANTLRAbstractToken
{
155 ANTLRTokenType _type
;
157 ANTLRChar _text
[ANTLRCommonTokenTEXTSIZE
+1];
160 ANTLRCommonNoRefCountToken(ANTLRTokenType t
, ANTLRChar
*s
)
161 { setType(t
); _line
= 0; setText(s
); }
162 ANTLRCommonNoRefCountToken()
163 { setType((ANTLRTokenType
)0); _line
= 0; setText(""); }
165 ANTLRTokenType
getType() { return _type
; }
166 void setType(ANTLRTokenType t
) { _type
= t
; }
167 virtual int getLine() { return _line
; }
168 void setLine(int line
) { _line
= line
; }
169 ANTLRChar
*getText() { return _text
; }
170 void setText(ANTLRChar
*s
)
171 { strncpy((char *)_text
, (char *)s
, ANTLRCommonTokenTEXTSIZE
); }
172 virtual ANTLRAbstractToken
*makeToken(ANTLRTokenType tt
,
176 ANTLRAbstractToken
*t
= new ANTLRCommonNoRefCountToken
;
177 t
->setType(tt
); t
->setText(txt
); t
->setLine(line
);
182 class DllExportPCCTS ANTLRCommonToken
: public ANTLRRefCountToken
{
184 ANTLRTokenType _type
;
186 ANTLRChar _text
[ANTLRCommonTokenTEXTSIZE
+1];
189 ANTLRCommonToken(ANTLRTokenType t
, ANTLRChar
*s
) : ANTLRRefCountToken(t
,s
)
190 { setType(t
); _line
= 0; setText(s
); }
192 { setType((ANTLRTokenType
)0); _line
= 0; setText(""); }
193 virtual ~ANTLRCommonToken() {;}
195 ANTLRTokenType
getType() { return _type
; }
196 void setType(ANTLRTokenType t
) { _type
= t
; }
197 virtual int getLine() { return _line
; }
198 void setLine(int line
) { _line
= line
; }
199 ANTLRChar
*getText() { return _text
; }
200 void setText(ANTLRChar
*s
)
201 { strncpy((char *)_text
, (char *)s
, ANTLRCommonTokenTEXTSIZE
); }
202 virtual ANTLRAbstractToken
*makeToken(ANTLRTokenType tt
,
206 ANTLRAbstractToken
*t
= new ANTLRCommonToken(tt
,txt
);
212 // used for backward compatibility
213 typedef ANTLRCommonToken ANTLRCommonBacktrackingToken
;