|
// CPreInputStack.cpp: Implementation of a preprocessor-specific token buffer
// that supports nesting of included files.
#include "CPreInputStack.h"
#include "tokens.h"
#include <deque>
#include <stack>
#include "CPreParser.h"
#include "ATokenStream.h"
CPreInputStack::CPreInputStack(
ANTLRTokenStream *in
) :
parser(0)
{
#ifndef NDEBUG
magic = Magic;
#endif
streamStack = new StreamStack;
PushStream(in);
// initialize LT1
LT1 = GetToken();
}
CPreInputStack::~CPreInputStack()
{
delete streamStack;
}
// discard current LT1 and get another token
void
CPreInputStack::consume()
{
LT1 = GetToken();
}
// Push a new stream on the stream input stack
// Note that this does not change the lookahead LT1.
// It is assumed that the LT1 will be consumed after the
// installation of the new stack (and not before!).
void
CPreInputStack::PushStream(
ANTLRTokenStream *in
)
{
validate();
// Put the stream on the stack
streamStack->push(in);
}
// Get a token from the topmost stream. If that stream
// returns TOK_Eof, and there is more than
// one stream on the stack, then pop the stack and try again.
// If that fails, return TOK_Eof.
ANTLRTokenPtr
CPreInputStack::GetToken()
{
validate();
while (true)
{
// Try a token from the stream on the top of the stack
ANTLRTokenPtr tokptr = streamStack->top()->getToken();
if (tokptr->getType() != TOK_Eof)
{
// Success -- return token from input stream
return tokptr;
}
// Failing that, see if we can pop the stack and try again
if (streamStack->size() > 1)
{
PopStream();
// ... and keep trying
} else {
// Return TOK_Eof. There is no more input from any source.
assert(tokptr->getType() == TOK_Eof);
return tokptr;
}
}
// Should not get here
assert(false);
return NULL;
}
// Pop an input token stream from the stack.
void
CPreInputStack::PopStream()
{
validate();
OnIncludeEof(); // allows for cleanup of popped input stream
streamStack->pop();
}
void
CPreInputStack::OnIncludeEof()
{
if (parser != 0) {
parser->OnIncludeEof();
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.
A list of licenses authors might use can be found here
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.