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