Ejemplo n.º 1
0
void
antlr3GenericSetupStream  (pANTLR3_INPUT_STREAM input, ANTLR3_UINT32 type)
{

    /* Install function pointers for an 8 bit ASCII input
     */

    /* Allocate stream interface
     */
    input->istream			=  antlr3IntStreamNew();
    input->istream->type    = ANTLR3_CHARSTREAM;
    input->istream->super   =  input;

    input->istream->type	= type;

    /* Intstream API
     */
    input->istream->consume			=  antlr3AsciiConsume;			/* Consume the next 8 bit character in the buffer			*/
    input->istream->_LA				=  antlr3AsciiLA;				/* Return the UTF32 character at offset n (1 based)			*/
    input->istream->index			=  antlr3AsciiIndex;			/* Current index (offset from first character			    */
    input->istream->mark			=  antlr3AsciiMark;				/* Record the current lex state for later restore			*/
    input->istream->rewind			=  antlr3AsciiRewind;			/* How to rewind the input									*/
    input->istream->rewindLast		=  antlr3AsciiRewindLast;		/* How to rewind the input									*/
    input->istream->seek			=  antlr3AsciiSeek;				/* How to seek to a specific point in the stream		    */
    input->istream->release			=  antlr3AsciiRelease;			/* Reset marks after mark n									*/
	input->istream->getSourceName	=  antlr3AsciiGetSourceName;	// Return a string that names the input source

	/* Charstream API
     */
    input->close					=  antlr3InputClose;			/* Close down the stream completely										*/
    input->free						=  antlr3InputClose;			/* Synonym for free														*/
    input->reset					=  antlr3InputReset;			/* Reset input to start													*/
    input->_LT						=  antlr3AsciiLT;				/* Same as _LA for 8 bit Ascii file										*/
    input->size						=  antlr3AsciiSize;				/* Return the size of the input buffer									*/
    input->substr					=  antlr3AsciiSubstr;			/* Return a string from the input stream								*/
    input->getLine					=  antlr3AsciiGetLine;			/* Return the current line number in the input stream					*/
    input->getLineBuf				=  antlr3AsciiGetLineBuf;	    /* Return a pointer to the start of the current line being consumed	    */
    input->getCharPositionInLine    =  antlr3AsciiGetCharPosition;  /* Return the offset into the current line of input						*/
    input->setLine					=  antlr3AsciiSetLine;			/* Set the input stream line number (does not set buffer pointers)	    */
    input->setCharPositionInLine    =  antlr3AsciiSetCharPosition;  /* Set the offset in to the current line (does not set any pointers	)   */
    input->SetNewLineChar			=  antlr3AsciiSetNewLineChar;   /* Set the value of the newline trigger character						*/
	input->setUcaseLA				= antlr3AsciiSetUcaseLA;

	input->charByteSize				= 1;		// Size in bytes of characters in this stream.

    /* Initialize entries for tables etc
     */
    input->markers  = NULL;

    /* Set up the input stream brand new
     */
    input->reset(input);
    
    /* Install default line separator character (it can be replaced
     * by the grammar programmer later)
     */
    input->SetNewLineChar(input, (ANTLR3_UCHAR)'\n');
}
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;
}
Ejemplo n.º 4
0
ANTLR3_API pANTLR3_COMMON_TOKEN_STREAM
antlr3CommonTokenStreamNew(ANTLR3_UINT32 hint)
{
    pANTLR3_COMMON_TOKEN_STREAM stream;

    /* Memory for the interface structure
     */
    stream  = (pANTLR3_COMMON_TOKEN_STREAM) ANTLR3_MALLOC(sizeof(ANTLR3_COMMON_TOKEN_STREAM));

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

    /* Create space for the token stream interface
     */
    stream->tstream	    = antlr3TokenStreamNew();
    stream->tstream->super  =  stream;

    /* Create space for the INT_STREAM interfacce
     */
    stream->tstream->istream		    =  antlr3IntStreamNew();
    stream->tstream->istream->super	    =  (stream->tstream);
    stream->tstream->istream->type	    = ANTLR3_TOKENSTREAM;

    /* Install the token tracking tables
     */
    stream->tokens  = antlr3VectorNew(0);

    /* Defaults
     */
    stream->p	    = -1;

    /* Install the common token stream API
     */
    stream->setTokenTypeChannel	    = setTokenTypeChannel;
    stream->discardTokenType	    = discardTokenType;
    stream->discardOffChannelToks   = discardOffChannel;
    stream->getTokens		    = getTokens;
    stream->getTokenRange	    = getTokenRange;
    stream->getTokensSet	    = getTokensSet;
    stream->getTokensList	    = getTokensList;
    stream->getTokensType	    = getTokensType;
    stream->reset                   = reset;

    /* Install the token stream API
     */
    stream->tstream->_LT			=  tokLT;
    stream->tstream->get			=  get;
    stream->tstream->getTokenSource	        =  getTokenSource;
    stream->tstream->setTokenSource	        =  setTokenSource;
    stream->tstream->toString		        =  toString;
    stream->tstream->toStringSS		        =  toStringSS;
    stream->tstream->toStringTT		        =  toStringTT;
    stream->tstream->setDebugListener           =  setDebugListener;

    /* Install INT_STREAM interface
     */
    stream->tstream->istream->_LA	=  _LA;
    stream->tstream->istream->mark	=  mark;
    stream->tstream->istream->release	=  release;
    stream->tstream->istream->size	=  size;
    stream->tstream->istream->index	=  tindex;
    stream->tstream->istream->rewind	=  rewindStream;
    stream->tstream->istream->rewindLast=  rewindLast;
    stream->tstream->istream->seek	=  seek;
    stream->tstream->istream->consume	=  consume;
    stream->tstream->istream->getSourceName = getSourceName;

    return  stream;
}