pANTLR3_TOKEN_FACTORY
BoundedTokenFactoryNew(pANTLR3_INPUT_STREAM input,ANTLR3_UINT32 size)
{
    pANTLR3_TOKEN_FACTORY   factory;
    pANTLR3_COMMON_TOKEN tok;
    ANTLR3_UINT32 i;

    /* allocate memory
     */
    factory     = (pANTLR3_TOKEN_FACTORY) ANTLR3_MALLOC(sizeof(ANTLR3_TOKEN_FACTORY));

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

    /* Install factory API
     */
    factory->newToken       =  newPoolToken;
    factory->close          =  factoryClose;
    factory->setInputStream = setInputStream;

    /* Allocate the initial pool
     */
    factory->thisPool   = size;
    factory->nextToken  = 0;
    factory->pools      = (pANTLR3_COMMON_TOKEN*) ANTLR3_MALLOC(sizeof(pANTLR3_COMMON_TOKEN));
    factory->pools[0]  =
        (pANTLR3_COMMON_TOKEN)
        ANTLR3_MALLOC((size_t)(sizeof(ANTLR3_COMMON_TOKEN) * size));

    /* Set up the tokens once and for all */
    for( i=0; i < size; i++ ) {
      tok = factory->pools[0] + i;
      antlr3SetTokenAPI(tok);

      /* It is factory made, and we need to copy the string factory pointer
       */
      tok->factoryMade  = ANTLR3_TRUE;
      tok->strFactory   = input == NULL ? NULL : input->strFactory;
      tok->input        = input;
    }

    /* 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
     */
    antlr3SetTokenAPI(&factory->unTruc);

    /* Set some initial variables for future copying
     */
    factory->unTruc.factoryMade = ANTLR3_TRUE;

    // Input stream
    //
    setInputStream(factory, input);

    return  factory;

}
ANTLR3_API pANTLR3_TOKEN_FACTORY
antlr3TokenFactoryNew(pANTLR3_INPUT_STREAM input)
{
    pANTLR3_TOKEN_FACTORY   factory;

    /* allocate memory
     */
    factory     = (pANTLR3_TOKEN_FACTORY) ANTLR3_MALLOC((size_t)sizeof(ANTLR3_TOKEN_FACTORY));

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

    /* Install factory API
     */
    factory->newToken       = newPoolToken;
    factory->close          = factoryClose;
    factory->setInputStream = setInputStream;
    factory->reset          = factoryReset;

    /* Allocate the initial pool
     */
    factory->thisPool   = -1;
    factory->pools      = NULL;
    factory->maxPool    = -1;
    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
     */
    antlr3SetTokenAPI(&factory->unTruc);

    /* Set some initial variables for future copying
     */
    factory->unTruc.factoryMade = ANTLR3_TRUE;

    // Input stream
    //
    setInputStream(factory, input);

    return  factory;

}
static  pANTLR3_COMMON_TOKEN
newToken(void)
{
    pANTLR3_COMMON_TOKEN    token;

    /* Allocate memory for this
     */
    token   = (pANTLR3_COMMON_TOKEN) ANTLR3_CALLOC(1, (size_t)(sizeof(ANTLR3_COMMON_TOKEN)));

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

    // Install the API
    //
    antlr3SetTokenAPI(token);
    token->factoryMade = ANTLR3_FALSE;

    return  token;
}
示例#4
0
static	pANTLR3_COMMON_TOKEN	
newToken(void)
{
    pANTLR3_COMMON_TOKEN    token;

    /* Allocate memory for this
     */
    token   = (pANTLR3_COMMON_TOKEN) ANTLR3_MALLOC((size_t)(sizeof(ANTLR3_COMMON_TOKEN)));

    if	(token == NULL)
    {
	return	(pANTLR3_COMMON_TOKEN)ANTLR3_FUNC_PTR(ANTLR3_ERR_NOMEM);
    }

    /* Install the API
     */
    antlr3SetTokenAPI(token);
    token->factoryMade = ANTLR3_FALSE;

    return  token;
}
static pANTLR3_COMMON_TOKEN
newPoolToken(pANTLR3_TOKEN_FACTORY factory)
{
    pANTLR3_COMMON_TOKEN token;

    /* See if we need a new token pool before allocating a new
     * one
     */
    if (factory->nextToken >= 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 token.
     */
    token = factory->pools[factory->thisPool] + factory->nextToken;
    factory->nextToken++;

    /* We have our token pointer now, so we can initialize it to the predefined model.
     * We only need do this though if the token is not already initialized, we just check
     * an api function pointer for this as they are allocated via calloc.
     */
    if  (token->setStartIndex == NULL)
    {
        antlr3SetTokenAPI(token);

        // It is factory made, and we need to copy the string factory pointer
        //
        token->factoryMade  = ANTLR3_TRUE;
        token->strFactory   = factory->input == NULL ? NULL : factory->input->strFactory;
        token->input        = factory->input;
    }

    /* And we are done
     */
    return token;
}
示例#6
0
ANTLR3_API pANTLR3_LEXER
antlr3LexerNew(ANTLR3_UINT32 sizeHint, pANTLR3_RECOGNIZER_SHARED_STATE state)
{
    pANTLR3_LEXER   lexer;
    pANTLR3_COMMON_TOKEN	specialT;

	/* Allocate memory
	*/
	lexer   = (pANTLR3_LEXER) ANTLR3_MALLOC(sizeof(ANTLR3_LEXER));

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

	/* Now we need to create the base recognizer
	*/
	lexer->rec	    =  antlr3BaseRecognizerNew(ANTLR3_TYPE_LEXER, sizeHint, state);

	if	(lexer->rec == NULL)
	{
		lexer->free(lexer);
		return	NULL;
	}
	lexer->rec->super  =  lexer;

	lexer->rec->displayRecognitionError	    = displayRecognitionError;
	lexer->rec->reportError					= reportError;
	lexer->rec->reset						= reset;
	lexer->rec->getCurrentInputSymbol		= getCurrentInputSymbol;
	lexer->rec->getMissingSymbol			= getMissingSymbol;

	/* Now install the token source interface
	*/
	if	(lexer->rec->state->tokSource == NULL) 
	{
		lexer->rec->state->tokSource	= (pANTLR3_TOKEN_SOURCE)ANTLR3_MALLOC(sizeof(ANTLR3_TOKEN_SOURCE));

		if	(lexer->rec->state->tokSource == NULL) 
		{
			lexer->rec->free(lexer->rec);
			lexer->free(lexer);

			return	NULL;
		}
		lexer->rec->state->tokSource->super    =  lexer;

		/* Install the default nextToken() method, which may be overridden
		 * by generated code, or by anything else in fact.
		 */
		lexer->rec->state->tokSource->nextToken	    =  nextToken;
		lexer->rec->state->tokSource->strFactory    = NULL;

		lexer->rec->state->tokFactory				= NULL;
	}

    /* Install the lexer API
     */
    lexer->setCharStream			=  setCharStream;
    lexer->mTokens					= (void (*)(void *))(mTokens);
    lexer->setCharStream			=  setCharStream;
    lexer->pushCharStream			=  pushCharStream;
    lexer->popCharStream			=  popCharStream;
    lexer->emit						=  emit;
    lexer->emitNew					=  emitNew;
    lexer->matchs					=  matchs;
    lexer->matchc					=  matchc;
    lexer->matchRange				=  matchRange;
    lexer->matchAny					=  matchAny;
    lexer->recover					=  recover;
    lexer->getLine					=  getLine;
    lexer->getCharIndex				=  getCharIndex;
    lexer->getCharPositionInLine    =  getCharPositionInLine;
    lexer->getText					=  getText;
    lexer->free						=  freeLexer;
    
    /* Initialise the eof token
     */
    specialT					= &(lexer->rec->state->tokSource->eofToken);
    antlr3SetTokenAPI	  (specialT);
    specialT->setType	  (specialT, ANTLR3_TOKEN_EOF);
    specialT->factoryMade		= ANTLR3_TRUE;					// Prevent things trying to free() it
    specialT->strFactory        = NULL;
	specialT->textState			= ANTLR3_TEXT_NONE;
	specialT->custom			= NULL;
	specialT->user1				= 0;
	specialT->user2				= 0;
	specialT->user3				= 0;

	// Initialize the skip token.
	//
    specialT					= &(lexer->rec->state->tokSource->skipToken);
    antlr3SetTokenAPI	  (specialT);
    specialT->setType	  (specialT, ANTLR3_TOKEN_INVALID);
    specialT->factoryMade		= ANTLR3_TRUE;					// Prevent things trying to free() it
    specialT->strFactory        = NULL;
	specialT->custom			= NULL;
	specialT->user1				= 0;
	specialT->user2				= 0;
	specialT->user3				= 0;
    return  lexer;
}