Example #1
0
/*!
 * \brief
 * Change to a new input stream, remembering the old one.
 * 
 * \param lexer
 * Pointer to the lexer instance to switch input streams for.
 * 
 * \param input
 * New input stream to install as the current one.
 * 
 * Switches the current character input stream to 
 * a new one, saving the old one, which we will revert to at the end of this 
 * new one.
 */
static void
pushCharStream  (pANTLR3_LEXER lexer,  pANTLR3_INPUT_STREAM input)
{
	// Do we need a new input stream stack?
	//
	if	(lexer->rec->state->streams == NULL)
	{
		// This is the first call to stack a new
		// stream and so we must create the stack first.
		//
		lexer->rec->state->streams = antlr3StackNew(0);

		if  (lexer->rec->state->streams == NULL)
		{
			// Could not do this, we just fail to push it.
			// TODO: Consider if this is what we want to do, but then
			//       any programmer can override this method to do something else.
			return;
		}
	}

	// We have a stack, so we can save the current input stream
	// into it.
	//
	lexer->input->istream->mark(lexer->input->istream);
	lexer->rec->state->streams->push(lexer->rec->state->streams, lexer->input, NULL);

	// And now we can install this new one
	//
	lexer->setCharStream(lexer, input);
}
Example #2
0
ANTLR3_API pANTLR3_ARBORETUM
antlr3ArboretumNew(pANTLR3_STRING_FACTORY strFactory)
{
    pANTLR3_ARBORETUM   factory;

    // Allocate memory
    //
    factory	= (pANTLR3_ARBORETUM) ANTLR3_MALLOC((size_t)sizeof(ANTLR3_ARBORETUM));
    if	(factory == NULL)
    {
		return	NULL;
    }

	// Install a vector factory to create, track and free() any child
	// node lists.
	//
	factory->vFactory					= antlr3VectorFactoryNew(0);
	if	(factory->vFactory == NULL)
	{
		free(factory);
		return	NULL;
	}

    // We also keep a reclaim stack, so that any Nil nodes that are
    // orphaned are not just left in the pool but are reused, other wise
    // we create 6 times as many nilNodes as ordinary nodes and use loads of
    // memory. Perhaps at some point, the analysis phase will generate better
    // code and we won't need to do this here.
    //
    factory->nilStack       =  antlr3StackNew(0);

    // Install factory API
    //
    factory->newTree	    =  newPoolTree;
    factory->newFromTree    =  newFromTree;
    factory->newFromToken   =  newFromToken;
    factory->close			=  factoryClose;

    // Allocate the initial pool
    //
    factory->thisPool	= -1;
    factory->pools		= NULL;
    newPool(factory);

    // Factory space is good, we now want to initialize our cheating token
    // which one it is initialized is the model for all tokens we manufacture
    //
    antlr3SetCTAPI(&factory->unTruc);

    // Set some initial variables for future copying, including a string factory
    // that we can use later for converting trees to strings.
    //
	factory->unTruc.factory				= factory;
    factory->unTruc.baseTree.strFactory	= strFactory;

    return  factory;

}
Example #3
0
static void
reset(pANTLR3_BASE_RECOGNIZER recognizer)
{
    if	(recognizer->following != NULL)
    {
	recognizer->following->free(recognizer->following);
    }

    /* Install a new following set
     */
    recognizer->following   = antlr3StackNew(64);
}
Example #4
0
void  pythonbriefLexer_initLexer( ppythonbriefLexer  ctx )
{
    ctx->implicitLineJoiningLevel = 0;
    ctx->startPos = -1;

    ctx->tokens = antlr3VectorNew( 16384 );
    ctx->identStack = antlr3StackNew( ANTLR3_LIST_SIZE_HINT );
    stackPush( ctx->identStack, (void *)FIRST_CHAR_POSITION, NULL );

    // Override nextToken implementation by Python specific
    ctx->origNextToken = ctx->pLexer->rec->state->tokSource->nextToken;
    ctx->pLexer->rec->state->tokSource->nextToken = pythonbriefLexer_nextTokenImpl;

    ctx->origFree = ctx->free;
    ctx->free = pythonbriefLexer_FreeImpl;

    ctx->onEncoding = NULL;
    return;
}
/// Reset the input stream to the start of the input nodes.
///
static	void		
reset	    (pANTLR3_COMMON_TREE_NODE_STREAM ctns)
{
	ctns->p									= -1;
	ctns->tnstream->istream->lastMarker		= 0;


	// Free and reset the node stack only if this is not
	// a rewriter, which is going to reuse the originating
	// node streams node stack
	//
	if  (ctns->isRewriter != ANTLR3_TRUE)
    {
		if	(ctns->nodeStack != NULL)
		{
			ctns->nodeStack->free(ctns->nodeStack);
			ctns->nodeStack = antlr3StackNew(INITIAL_CALL_STACK_SIZE);
		}
	}
}
ANTLR3_API pANTLR3_COMMON_TREE_NODE_STREAM
antlr3CommonTreeNodeStreamNew(pANTLR3_STRING_FACTORY strFactory, ANTLR3_UINT32 hint)
{
        pANTLR3_COMMON_TREE_NODE_STREAM stream;
        pANTLR3_COMMON_TOKEN                    token;

        // Memory for the interface structure
        //
        stream  = (pANTLR3_COMMON_TREE_NODE_STREAM) ANTLR3_CALLOC(1, sizeof(ANTLR3_COMMON_TREE_NODE_STREAM));

        if      (stream == NULL)
        {
                return  NULL;
        }

        // String factory for tree walker
        //
        stream->stringFactory           = strFactory;

        // Create an adaptor for the common tree node stream
        //
        stream->adaptor                         = ANTLR3_TREE_ADAPTORNew(strFactory);

        if      (stream->adaptor == NULL)
        {
                stream->free(stream);
                return  NULL;
        }

        // Create space for the tree node stream interface
        //
        stream->tnstream            = antlr3TreeNodeStreamNew();

        if      (stream->tnstream == NULL)
        {
                stream->adaptor->free           (stream->adaptor);
                stream->free                            (stream);

                return  NULL;
        }

        // Create space for the INT_STREAM interface
        //
        stream->tnstream->istream                   =  antlr3IntStreamNew();

        if      (stream->tnstream->istream == NULL)
        {
                stream->adaptor->free           (stream->adaptor);
                stream->tnstream->free          (stream->tnstream);
                stream->free                            (stream);

                return  NULL;
        }

        // Install the common tree node stream API
        //
        stream->addNavigationNode                   =  addNavigationNode;
        stream->hasUniqueNavigationNodes    =  hasUniqueNavigationNodes;
        stream->newDownNode                                     =  newDownNode;
        stream->newUpNode                                       =  newUpNode;
        stream->reset                                           =  reset;
        stream->push                                            =  push;
        stream->pop                                                     =  pop;

        stream->free                        =  antlr3CommonTreeNodeStreamFree;

        // Install the tree node stream API
        //
        stream->tnstream->getTreeAdaptor                        =  getTreeAdaptor;
        stream->tnstream->getTreeSource                         =  getTreeSource;
        stream->tnstream->_LT                                           =  _LT;
        stream->tnstream->setUniqueNavigationNodes      =  setUniqueNavigationNodes;
        stream->tnstream->toString                                      =  toString;
        stream->tnstream->toStringSS                            =  toStringSS;
        stream->tnstream->toStringWork                          =  toStringWork;
        stream->tnstream->get                                           =  get;

        // Install INT_STREAM interface
        //
        stream->tnstream->istream->consume          =  consume;
        stream->tnstream->istream->index            =  tindex;
        stream->tnstream->istream->_LA                  =  _LA;
        stream->tnstream->istream->mark                 =  mark;
        stream->tnstream->istream->release          =  release;
        stream->tnstream->istream->rewind           =  rewindMark;
        stream->tnstream->istream->rewindLast   =  rewindLast;
        stream->tnstream->istream->seek                 =  seek;
        stream->tnstream->istream->size                 =  size;

        // Initialize data elements of INT stream
        //
        stream->tnstream->istream->type                 = ANTLR3_COMMONTREENODE;
        stream->tnstream->istream->super            =  (stream->tnstream);

        // Initialize data elements of TREE stream
        //
        stream->tnstream->ctns =  stream;

        // Initialize data elements of the COMMON TREE NODE stream
        //
        stream->super                                   = NULL;
        stream->uniqueNavigationNodes   = ANTLR3_FALSE;
        stream->markers                                 = NULL;
        stream->nodeStack                               = antlr3StackNew(INITIAL_CALL_STACK_SIZE);

        // Create the node list map
        //
        if      (hint == 0)
        {
                hint = DEFAULT_INITIAL_BUFFER_SIZE;
        }
        stream->nodes   = antlr3VectorNew(hint);
        stream->p               = -1;

        // Install the navigation nodes
        //
        antlr3SetCTAPI(&(stream->UP));
        antlr3SetCTAPI(&(stream->DOWN));
        antlr3SetCTAPI(&(stream->EOF_NODE));
        antlr3SetCTAPI(&(stream->INVALID_NODE));

        token                                           = antlr3CommonTokenNew(ANTLR3_TOKEN_UP);
        token->strFactory                       = strFactory;
        token->textState                        = ANTLR3_TEXT_CHARP;
        token->tokText.chars            = (pANTLR3_UCHAR)"UP";
        stream->UP.token                        = token;

        token                                           = antlr3CommonTokenNew(ANTLR3_TOKEN_DOWN);
        token->strFactory                       = strFactory;
        token->textState                        = ANTLR3_TEXT_CHARP;
        token->tokText.chars            = (pANTLR3_UCHAR)"DOWN";
        stream->DOWN.token                      = token;

        token                                           = antlr3CommonTokenNew(ANTLR3_TOKEN_EOF);
        token->strFactory                       = strFactory;
        token->textState                        = ANTLR3_TEXT_CHARP;
        token->tokText.chars            = (pANTLR3_UCHAR)"EOF";
        stream->EOF_NODE.token          = token;

        token                                           = antlr3CommonTokenNew(ANTLR3_TOKEN_INVALID);
        token->strFactory                       = strFactory;
        token->textState                        = ANTLR3_TEXT_CHARP;
        token->tokText.chars            = (pANTLR3_UCHAR)"INVALID";
        stream->INVALID_NODE.token      = token;


        return  stream;
}