ATokenBuffer.cpp revision 4fd606d1f5abe38e1f42c38de1d2e895166bd0f4
*
* SOFTWARE RIGHTS
*
* We reserve no LEGAL rights to the Purdue Compiler Construction Tool
* Set (PCCTS) -- PCCTS is in the public domain. An individual or
* company may do whatever they wish with source code distributed with
* PCCTS or the code generated by PCCTS, including the incorporation of
* PCCTS, or its output, into commerical software.
*
* We encourage users to develop software with PCCTS. However, we do ask
* that credit is given to us for developing PCCTS. By "credit",
* we mean that if you incorporate our source code into one of your
* programs (commercial product, research project, or otherwise) that you
* acknowledge this fact somewhere in the documentation, research report,
* etc... If you like PCCTS and have developed a nice tool with the
* output, please mention that you developed it using PCCTS. In
* addition, we ask that this header remain intact in our source code.
* As long as these guidelines are kept, we expect to continue enhancing
* this system and expect to make other tools available as they are
* completed.
*
* ANTLR 1.33
* Terence Parr
* Parr Research Corporation
* with Purdue University and AHPCRC, University of Minnesota
* 1989-2000
*/
typedef int ANTLRTokenType; // fool AToken.h into compiling
class ANTLRParser; /* MR1 */
#define ANTLR_SUPPORT_CODE
#include "pcctscfg.h"
#include ATOKENBUFFER_H
#include APARSER_H // MR23
typedef ANTLRAbstractToken *_ANTLRTokenPtr;
#if defined(DBG_TBUF)||defined(DBG_TBUF_MARK_REW)
static unsigned char test[1000];
#endif
#ifdef DBG_REFCOUNTTOKEN
#endif
{
this->k = _k;
buffer = (_ANTLRTokenPtr *)
panic("cannot alloc token buffer");
}
buffer++; // leave the first elem empty so tp-1 is valid ptr
num_markers = 0;
}
static void f() {;}
{
f();
// Delete all remaining tokens (from 0..last inclusive)
if ( _deleteTokens )
{
_ANTLRTokenPtr *z;
{
(*z)->deref();
// z->deref();
#ifdef DBG_REFCOUNTTOKEN
#endif
if ( (*z)->nref()==0 )
{
delete (*z);
}
}
}
}
#if defined(DBG_TBUF)||defined(DBG_TBUF_MARK_REW)
#include "pccts_stdio.h"
#endif
getToken()
{
{
return *tp++; // read buffered lookahead
}
// out of buffered lookahead, get some more "real"
// input from getANTLRToken()
if ( num_markers==0 )
{
{
#ifdef DBG_TBUF
/* MR23 */ printMessage(stderr,"getToken: next > threshold (high water is %d)\n", threshold-buffer);
#endif
makeRoom();
}
}
else {
if ( next > end_of_buffer )
{
#ifdef DBG_TBUF
#endif
extendBuffer();
}
}
*next = getANTLRToken();
next++;
return *tp++;
}
void ANTLRTokenBuffer::
{
#if defined(DBG_TBUF)||defined(DBG_TBUF_MARK_REW)
/* MR23 */ printMessage(stderr, "rewind(%d)[nm=%d,from=%d,%d.n=%d]\n", pos, num_markers, tp-buffer,pos,test[pos]);
#endif
num_markers--;
}
/*
* This function is used to specify that the token pointers read
* by the ANTLRTokenBuffer should be buffered up (to be reused later).
*/
int ANTLRTokenBuffer::
mark()
{
#if defined(DBG_TBUF)||defined(DBG_TBUF_MARK_REW)
/* MR23 */ printMessage(stderr,"mark(%d)[nm=%d,%d.n=%d]\n",tp-buffer,num_markers+1,tp-buffer,test[tp-buffer]);
#endif
num_markers++;
}
/*
* returns the token pointer n positions ahead.
* This implies that bufferedToken(1) gets the NEXT symbol of lookahead.
* This is used in conjunction with the ANTLRParser lookahead buffer.
*
* No markers are set or anything. A bunch of input is buffered--that's all.
* The tp pointer is left alone as the lookahead has not been advanced
* with getToken(). The next call to getToken() will find a token
* in the buffer and won't have to call getANTLRToken().
*
* If this is called before a consume() is done, how_many_more_i_need is
* set to 'n'.
*/
bufferedToken(int n)
{
// int how_many_more_i_need = (last-tp < 0) ? n : n-(last-tp)-1;
// Make sure that at least n tokens are available in the buffer
#ifdef DBG_TBUF
#endif
for (int i=1; i<=how_many_more_i_need; i++)
{
{
extendBuffer();
}
*next = getANTLRToken();
next++;
}
return tp[n - 1];
}
/* If no markers are set, the none of the input needs to be saved (except
* for the lookahead Token pointers). We save only k-1 token pointers as
* we are guaranteed to do a getANTLRToken() right after this because otherwise
* we wouldn't have needed to extend the buffer.
*
* If there are markers in the buffer, we need to save things and so
* extendBuffer() is called.
*/
void ANTLRTokenBuffer::
makeRoom()
{
#ifdef DBG_TBUF
#endif
/*
if ( num_markers == 0 )
{
*/
#ifdef DBG_TBUF
_ANTLRTokenPtr *r;
{
}
"before: tp=%d, last=%d, next=%d, threshold=%d\n",tp-buffer,last-buffer,next-buffer,threshold-buffer);
#endif
// Delete all tokens from 0..last-(k-1) inclusive
if ( _deleteTokens )
{
_ANTLRTokenPtr *z;
{
(*z)->deref();
// z->deref();
#ifdef DBG_REFCOUNTTOKEN
#endif
if ( (*z)->nref()==0 )
{
delete (*z);
}
}
}
// reset the buffer to initial conditions, but move k-1 symbols
// to the beginning of buffer and put new input symbol at k
// ANTLRAbstractToken **p = buffer, **q = end_of_buffer-(k-1)+1;
#ifdef DBG_TBUF
#endif
for (int i=1; i<=(k-1); i++)
{
*p++ = *q++;
#ifdef DBG_TBUF
#endif
}
#ifdef DBG_TBUF
#endif
#ifdef DBG_TBUF
"after: tp=%d, last=%d, next=%d\n",
#endif
/*
}
else {
extendBuffer();
}
*/
}
/* This function extends 'buffer' by chunk_size and returns with all
* pointers at the same relative positions in the buffer (the buffer base
* address could have changed in realloc()) except that 'next' comes
* back set to where the next token should be stored. All other pointers
* are untouched.
*/
void
{
#ifdef DBG_TBUF
#endif
buffer = (_ANTLRTokenPtr *)
panic("cannot alloc token buffer");
}
buffer++; // leave the first elem empty so tp-1 is valid ptr
/*
// zero out new token ptrs so we'll know if something to delete in buffer
ANTLRAbstractToken **p = end_of_buffer-chunk_size+1;
for (; p<=end_of_buffer; p++) *p = NULL;
*/
}
parser=p; // MR1
return old; // MR1
} // MR1
// MR1
getParser() { // MR1
return parser; // MR1
} // MR1
{
if (parser) //MR23
else //MR23
}
//MR23
{
int iRet = 0;
if (parser)
else
return iRet;
}
/* to avoid having to link in another file just for the smart token ptr
* stuff, we include it here. Ugh.
*
* MR23 This causes nothing but problems for IDEs.
* Change from .cpp to .h
*
*/
#include ATOKPTR_IMPL_H