1
/* ANTLRTokenBuffer.cpp
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
30
typedef int ANTLRTokenType; // fool AToken.h into compiling
32
class ANTLRParser; /* MR1 */
34
#define ANTLR_SUPPORT_CODE
38
#include ATOKENBUFFER_H
39
#include APARSER_H // MR23
41
typedef ANTLRAbstractToken *_ANTLRTokenPtr;
43
#if defined(DBG_TBUF)||defined(DBG_TBUF_MARK_REW)
44
static unsigned char test[1000];
47
#ifdef DBG_REFCOUNTTOKEN
48
int ANTLRRefCountToken::ctor = 0; /* MR23 */
49
int ANTLRRefCountToken::dtor = 0; /* MR23 */
53
ANTLRTokenBuffer(ANTLRTokenStream *_input, int _k, int _chunk_size_formal) /* MR14 */
57
buffer_size = chunk_size = _chunk_size_formal;
58
buffer = (_ANTLRTokenPtr *)
59
calloc(chunk_size+1,sizeof(_ANTLRTokenPtr ));
60
if ( buffer == NULL ) {
61
panic("cannot alloc token buffer");
63
buffer++; // leave the first elem empty so tp-1 is valid ptr
69
end_of_buffer = &buffer[buffer_size-1];
70
threshold = &buffer[(int)(buffer_size/2)]; // MR23 - Used to be 1.0/2.0 !
71
_deleteTokens = 1; // assume we delete tokens
72
parser=NULL; // MR5 - uninitialized reference
80
// Delete all remaining tokens (from 0..last inclusive)
84
for (z=buffer; z<=last; z++)
88
#ifdef DBG_REFCOUNTTOKEN
89
/* MR23 */ printMessage(stderr, "##########dtor: deleting token '%s' (ref %d)\n",
90
((ANTLRCommonToken *)*z)->getText(), (*z)->nref());
92
if ( (*z)->nref()==0 )
99
if ( buffer!=NULL ) free((char *)(buffer-1));
102
#if defined(DBG_TBUF)||defined(DBG_TBUF_MARK_REW)
103
#include "pccts_stdio.h"
107
_ANTLRTokenPtr ANTLRTokenBuffer::
110
if ( tp <= last ) // is there any buffered lookahead still to be read?
112
return *tp++; // read buffered lookahead
114
// out of buffered lookahead, get some more "real"
115
// input from getANTLRToken()
116
if ( num_markers==0 )
118
if( next > threshold )
121
/* MR23 */ printMessage(stderr,"getToken: next > threshold (high water is %d)\n", threshold-buffer);
127
if ( next > end_of_buffer )
130
/* MR23 */ printMessage(stderr,"getToken: next > end_of_buffer (size is %d)\n", buffer_size);
135
*next = getANTLRToken();
136
(*next)->ref(); // say we have a copy of this pointer in buffer
143
void ANTLRTokenBuffer::
146
#if defined(DBG_TBUF)||defined(DBG_TBUF_MARK_REW)
147
/* MR23 */ printMessage(stderr, "rewind(%d)[nm=%d,from=%d,%d.n=%d]\n", pos, num_markers, tp-buffer,pos,test[pos]);
155
* This function is used to specify that the token pointers read
156
* by the ANTLRTokenBuffer should be buffered up (to be reused later).
158
int ANTLRTokenBuffer::
161
#if defined(DBG_TBUF)||defined(DBG_TBUF_MARK_REW)
163
/* MR23 */ printMessage(stderr,"mark(%d)[nm=%d,%d.n=%d]\n",tp-buffer,num_markers+1,tp-buffer,test[tp-buffer]);
170
* returns the token pointer n positions ahead.
171
* This implies that bufferedToken(1) gets the NEXT symbol of lookahead.
172
* This is used in conjunction with the ANTLRParser lookahead buffer.
174
* No markers are set or anything. A bunch of input is buffered--that's all.
175
* The tp pointer is left alone as the lookahead has not been advanced
176
* with getToken(). The next call to getToken() will find a token
177
* in the buffer and won't have to call getANTLRToken().
179
* If this is called before a consume() is done, how_many_more_i_need is
182
_ANTLRTokenPtr ANTLRTokenBuffer::
185
// int how_many_more_i_need = (last-tp < 0) ? n : n-(last-tp)-1;
186
int how_many_more_i_need = (tp > last) ? n : n-(last-tp)-1;
187
// Make sure that at least n tokens are available in the buffer
189
/* MR23 */ printMessage(stderr, "bufferedToken(%d)\n", n);
191
for (int i=1; i<=how_many_more_i_need; i++)
193
if ( next > end_of_buffer ) // buffer overflow?
197
*next = getANTLRToken();
198
(*next)->ref(); // say we have a copy of this pointer in buffer
205
/* If no markers are set, the none of the input needs to be saved (except
206
* for the lookahead Token pointers). We save only k-1 token pointers as
207
* we are guaranteed to do a getANTLRToken() right after this because otherwise
208
* we wouldn't have needed to extend the buffer.
210
* If there are markers in the buffer, we need to save things and so
211
* extendBuffer() is called.
213
void ANTLRTokenBuffer::
217
/* MR23 */ printMessage(stderr, "in makeRoom.................\n");
218
/* MR23 */ printMessage(stderr, "num_markers==%d\n", num_markers);
221
if ( num_markers == 0 )
225
/* MR23 */ printMessage(stderr, "moving lookahead and resetting next\n");
228
/* MR23 */ printMessage(stderr, "tbuf = [");
229
for (r=buffer; r<=last; r++)
231
if ( *r==NULL ) /* MR23 */ printMessage(stderr, " xxx");
232
else /* MR23 */ printMessage(stderr, " '%s'", ((ANTLRCommonToken *)*r)->getText());
234
/* MR23 */ printMessage(stderr, " ]\n");
236
/* MR23 */ printMessage(stderr,
237
"before: tp=%d, last=%d, next=%d, threshold=%d\n",tp-buffer,last-buffer,next-buffer,threshold-buffer);
240
// Delete all tokens from 0..last-(k-1) inclusive
244
for (z=buffer; z<=last-(k-1); z++)
248
#ifdef DBG_REFCOUNTTOKEN
249
/* MR23 */ printMessage(stderr, "##########makeRoom: deleting token '%s' (ref %d)\n",
250
((ANTLRCommonToken *)*z)->getText(), (*z)->nref());
252
if ( (*z)->nref()==0 )
259
// reset the buffer to initial conditions, but move k-1 symbols
260
// to the beginning of buffer and put new input symbol at k
261
_ANTLRTokenPtr *p = buffer, *q = last-(k-1)+1;
262
// ANTLRAbstractToken **p = buffer, **q = end_of_buffer-(k-1)+1;
264
/* MR23 */ printMessage(stderr, "lookahead buffer = [");
266
for (int i=1; i<=(k-1); i++)
270
/* MR23 */ printMessage(stderr,
271
" '%s'", ((ANTLRCommonToken *)buffer[i-1])->getText());
275
/* MR23 */ printMessage(stderr, " ]\n");
278
tp = &buffer[k-1]; // tp points to what will be filled in next
281
/* MR23 */ printMessage(stderr,
282
"after: tp=%d, last=%d, next=%d\n",
283
tp-buffer, last-buffer, next-buffer);
293
/* This function extends 'buffer' by chunk_size and returns with all
294
* pointers at the same relative positions in the buffer (the buffer base
295
* address could have changed in realloc()) except that 'next' comes
296
* back set to where the next token should be stored. All other pointers
303
int save_last = last-buffer, save_tp = tp-buffer, save_next = next-buffer;
305
/* MR23 */ printMessage(stderr, "extending physical buffer\n");
307
buffer_size += chunk_size;
308
buffer = (_ANTLRTokenPtr *)
309
realloc((char *)(buffer-1),
310
(buffer_size+1)*sizeof(_ANTLRTokenPtr ));
311
if ( buffer == NULL ) {
312
panic("cannot alloc token buffer");
314
buffer++; // leave the first elem empty so tp-1 is valid ptr
316
tp = buffer + save_tp; // put the pointers back to same relative position
317
last = buffer + save_last;
318
next = buffer + save_next;
319
end_of_buffer = &buffer[buffer_size-1];
320
threshold = &buffer[(int)(buffer_size*(1.0/2.0))];
323
// zero out new token ptrs so we'll know if something to delete in buffer
324
ANTLRAbstractToken **p = end_of_buffer-chunk_size+1;
325
for (; p<=end_of_buffer; p++) *p = NULL;
329
ANTLRParser * ANTLRTokenBuffer:: // MR1
330
setParser(ANTLRParser *p) { // MR1
331
ANTLRParser *old=parser; // MR1
333
input->setParser(p); // MR1
337
ANTLRParser * ANTLRTokenBuffer:: // MR1
339
return parser; // MR1
342
void ANTLRTokenBuffer::panic(const char *msg) // MR23
345
parser->panic(msg); //MR23
347
exit(PCCTS_EXIT_FAILURE);
351
int ANTLRTokenBuffer::printMessage(FILE* pFile, const char* pFormat, ...)
354
va_start( marker, pFormat );
358
parser->printMessageV(pFile, pFormat, marker);
360
iRet = vfprintf(pFile, pFormat, marker);
366
/* to avoid having to link in another file just for the smart token ptr
367
* stuff, we include it here. Ugh.
369
* MR23 This causes nothing but problems for IDEs.
370
* Change from .cpp to .h
374
#include ATOKPTR_IMPL_H