/** \brief Create a new corpconfParser parser and return a context for it.
 *
 * \param[in] instream Pointer to an input stream interface.
 *
 * \return Pointer to new parser context upon success.
 */
ANTLR3_API pcorpconfParser
corpconfParserNewSSD   (pANTLR3_COMMON_TOKEN_STREAM instream, pANTLR3_RECOGNIZER_SHARED_STATE state)
{
    pcorpconfParser ctx;	    /* Context structure we will build and return   */
    
    ctx	= (pcorpconfParser) ANTLR3_CALLOC(1, sizeof(corpconfParser));
    
    if	(ctx == NULL)
    {
		// Failed to allocate memory for parser context
		//
        return  NULL;
    }
    
    /* -------------------------------------------------------------------
     * Memory for basic structure is allocated, now to fill in
     * the base ANTLR3 structures. We initialize the function pointers
     * for the standard ANTLR3 parser function set, but upon return
     * from here, the programmer may set the pointers to provide custom
     * implementations of each function. 
     *
     * We don't use the macros defined in corpconfParser.h here, in order that you can get a sense
     * of what goes where.
     */

    /* Create a base parser/recognizer, using the supplied token stream
     */
    ctx->pParser	    = antlr3ParserNewStream(ANTLR3_SIZE_HINT, instream->tstream, state);
    /* Install the implementation of our corpconfParser interface
     */
    ctx->config	= config;
    ctx->block	= block;
    ctx->line	= line;
    ctx->value	= value;
    ctx->free			= corpconfParserFree;
    ctx->getGrammarFileName	= getGrammarFileName;
    
    /* Install the scope pushing methods.
     */
    ADAPTOR	= ANTLR3_TREE_ADAPTORNew(instream->tstream->tokenSource->strFactory);
    ctx->vectors	= antlr3VectorFactoryNew(0);
    

	
    /* Install the token table
     */
    PSRSTATE->tokenNames   = corpconfParserTokenNames;
    
    
    /* Return the newly built parser to the caller
     */
    return  ctx;
}
/// Debugging version of the tree adaptor (not normally called as generated code
/// calls setDebugEventListener instead which changes a normal token stream to
/// a debugging stream and means that a user's instantiation code does not need
/// to be changed just to debug with AW.
///
ANTLR3_API pANTLR3_BASE_TREE_ADAPTOR
ANTLR3_TREE_ADAPTORDebugNew(pANTLR3_STRING_FACTORY strFactory, pANTLR3_DEBUG_EVENT_LISTENER	debugger)
{
	pANTLR3_BASE_TREE_ADAPTOR	ta;

	// Create a normal one first
	//
	ta	= ANTLR3_TREE_ADAPTORNew(strFactory);
	
	if	(ta != NULL)
	{
		// Reinitialize as a debug version
		//
		antlr3BaseTreeAdaptorInit(ta, debugger);
		ta->create				= (void * (*) (pANTLR3_BASE_TREE_ADAPTOR, pANTLR3_COMMON_TOKEN))
									dbgCreate;
		ta->setTokenBoundaries	= (void   (*) (pANTLR3_BASE_TREE_ADAPTOR, void *, pANTLR3_COMMON_TOKEN, pANTLR3_COMMON_TOKEN))
									dbgSetTokenBoundaries;
	}

	return	ta;
}
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;
}