Beispiel #1
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;

}
Beispiel #2
0
static	pANTLR3_BASE_TREE    
newPoolTree	    (pANTLR3_ARBORETUM factory)
{
	pANTLR3_COMMON_TREE    tree;

    // If we have anything on the re claim stack, reuse that sucker first
    //
    tree = (pANTLR3_COMMON_TREE) factory->nilStack->peek(factory->nilStack);

    if  (tree != NULL)
    {
        // Cool we got something we could reuse, it will have been cleaned up by
        // whatever put it back on the stack (for instance if it had a child vector,
        // that will have been cleared to hold zero entries and that vector will get reused too.
        // It is the basetree pointer that is placed on the stack of course
        //
        factory->nilStack->pop(factory->nilStack);
        return (pANTLR3_BASE_TREE)tree;

    }
	// See if we need a new tree pool before allocating a new tree
	//
	if	(factory->nextTree >= ANTLR3_FACTORY_POOL_SIZE)
	{
		// We ran out of tokens in the current pool, so we need a new pool
		//
		newPool(factory);
	}

	// Assuming everything went well - we are trying for performance here so doing minimal
	// error checking - then we can work out what the pointer is to the next commontree.
	//
	tree   = factory->pools[factory->thisPool] + factory->nextTree;
	factory->nextTree++;

	// We have our token pointer now, so we can initialize it to the predefined model.
	//
    antlr3SetCTAPI(tree);

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

	// The super points to the common tree so we must override the one used by
	// by the pre-built tree as otherwise we will always poitn to the same initial
	// common tree and we might spend 3 hours trying to debug why - this would never
	// happen to me of course! :-(
	//
	tree->baseTree.super	= tree;

	// And we are done
	//
	return  &(tree->baseTree);
}
Beispiel #3
0
ANTLR3_API pANTLR3_COMMON_TREE
antlr3CommonTreeNew()
{
	pANTLR3_COMMON_TREE	tree;
	tree = (pANTLR3_COMMON_TREE)ANTLR3_CALLOC(1, sizeof(ANTLR3_COMMON_TREE));

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

	antlr3SetCTAPI(tree);

	return tree;
}
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;
}
ANTLR3_API pANTLR3_COMMON_TREE_NODE_STREAM
antlr3CommonTreeNodeStreamNewStream(pANTLR3_COMMON_TREE_NODE_STREAM inStream)
{
        pANTLR3_COMMON_TREE_NODE_STREAM stream;

        // 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;
        }

        // Copy in all the reusable parts of the originating stream and create new
        // pieces where necessary.
        //

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

        // Create an adaptor for the common tree node stream
        //
        stream->adaptor                         = inStream->adaptor;

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

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

                return  NULL;
        }

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

        if      (stream->tnstream->istream == NULL)
        {
                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->getLookaheadSize                        =  getLookaheadSize;

        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                               = inStream->nodeStack;

        // Create the node list map
        //
        stream->nodes   = antlr3VectorNew(DEFAULT_INITIAL_BUFFER_SIZE);
        stream->p               = -1;

        // Install the navigation nodes
        //

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

        stream->UP.token                                                = inStream->UP.token;
        inStream->UP.token->strFactory                  = stream->stringFactory;
        stream->DOWN.token                                              = inStream->DOWN.token;
        inStream->DOWN.token->strFactory                = stream->stringFactory;
        stream->EOF_NODE.token                                  = inStream->EOF_NODE.token;
        inStream->EOF_NODE.token->strFactory    = stream->stringFactory;
        stream->INVALID_NODE.token                              = inStream->INVALID_NODE.token;
        inStream->INVALID_NODE.token->strFactory= stream->stringFactory;

        // Reuse the root tree of the originating stream
        //
        stream->root            = inStream->root;

        // Signal that this is a rewriting stream so we don't
        // free the originating tree. Anything that we rewrite or
        // duplicate here will be done through the adaptor or
        // the original tree factory.
        //
        stream->isRewriter      = ANTLR3_TRUE;
        return stream;
}