--- /dev/null
+// \file\r
+//\r
+// Implementation of ANTLR3 CommonTree, which you can use as a\r
+// starting point for your own tree. Though it is often easier just to tag things on\r
+// to the user pointer in the tree unless you are building a different type\r
+// of structure.\r
+//\r
+\r
+// [The "BSD licence"]\r
+// Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC\r
+// http://www.temporal-wave.com\r
+// http://www.linkedin.com/in/jimidle\r
+//\r
+// All rights reserved.\r
+//\r
+// Redistribution and use in source and binary forms, with or without\r
+// modification, are permitted provided that the following conditions\r
+// are met:\r
+// 1. Redistributions of source code must retain the above copyright\r
+// notice, this list of conditions and the following disclaimer.\r
+// 2. Redistributions in binary form must reproduce the above copyright\r
+// notice, this list of conditions and the following disclaimer in the\r
+// documentation and/or other materials provided with the distribution.\r
+// 3. The name of the author may not be used to endorse or promote products\r
+// derived from this software without specific prior written permission.\r
+//\r
+// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR\r
+// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES\r
+// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.\r
+// IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,\r
+// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\r
+// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\r
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\r
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF\r
+// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
+\r
+#include <antlr3commontree.h>\r
+\r
+\r
+static pANTLR3_COMMON_TOKEN getToken (pANTLR3_BASE_TREE tree);\r
+static pANTLR3_BASE_TREE dupNode (pANTLR3_BASE_TREE tree);\r
+static ANTLR3_BOOLEAN isNilNode (pANTLR3_BASE_TREE tree);\r
+static ANTLR3_UINT32 getType (pANTLR3_BASE_TREE tree);\r
+static pANTLR3_STRING getText (pANTLR3_BASE_TREE tree);\r
+static ANTLR3_UINT32 getLine (pANTLR3_BASE_TREE tree);\r
+static ANTLR3_UINT32 getCharPositionInLine (pANTLR3_BASE_TREE tree);\r
+static pANTLR3_STRING toString (pANTLR3_BASE_TREE tree);\r
+static pANTLR3_BASE_TREE getParent (pANTLR3_BASE_TREE tree);\r
+static void setParent (pANTLR3_BASE_TREE tree, pANTLR3_BASE_TREE parent);\r
+static void setChildIndex (pANTLR3_BASE_TREE tree, ANTLR3_INT32 i);\r
+static ANTLR3_INT32 getChildIndex (pANTLR3_BASE_TREE tree);\r
+static void createChildrenList (pANTLR3_BASE_TREE tree);\r
+static void reuse (pANTLR3_BASE_TREE tree);\r
+\r
+// Factory functions for the Arboretum\r
+//\r
+static void newPool (pANTLR3_ARBORETUM factory);\r
+static pANTLR3_BASE_TREE newPoolTree (pANTLR3_ARBORETUM factory);\r
+static pANTLR3_BASE_TREE newFromTree (pANTLR3_ARBORETUM factory, pANTLR3_COMMON_TREE tree);\r
+static pANTLR3_BASE_TREE newFromToken (pANTLR3_ARBORETUM factory, pANTLR3_COMMON_TOKEN token);\r
+static void factoryClose (pANTLR3_ARBORETUM factory);\r
+\r
+ANTLR3_API pANTLR3_ARBORETUM\r
+antlr3ArboretumNew(pANTLR3_STRING_FACTORY strFactory)\r
+{\r
+ pANTLR3_ARBORETUM factory;\r
+\r
+ // Allocate memory\r
+ //\r
+ factory = (pANTLR3_ARBORETUM) ANTLR3_MALLOC((size_t)sizeof(ANTLR3_ARBORETUM));\r
+ if (factory == NULL)\r
+ {\r
+ return NULL;\r
+ }\r
+\r
+ // Install a vector factory to create, track and free() any child\r
+ // node lists.\r
+ //\r
+ factory->vFactory = antlr3VectorFactoryNew(0);\r
+ if (factory->vFactory == NULL)\r
+ {\r
+ free(factory);\r
+ return NULL;\r
+ }\r
+\r
+ // We also keep a reclaim stack, so that any Nil nodes that are\r
+ // orphaned are not just left in the pool but are reused, other wise\r
+ // we create 6 times as many nilNodes as ordinary nodes and use loads of\r
+ // memory. Perhaps at some point, the analysis phase will generate better\r
+ // code and we won't need to do this here.\r
+ //\r
+ factory->nilStack = antlr3StackNew(0);\r
+\r
+ // Install factory API\r
+ //\r
+ factory->newTree = newPoolTree;\r
+ factory->newFromTree = newFromTree;\r
+ factory->newFromToken = newFromToken;\r
+ factory->close = factoryClose;\r
+\r
+ // Allocate the initial pool\r
+ //\r
+ factory->thisPool = -1;\r
+ factory->pools = NULL;\r
+ newPool(factory);\r
+\r
+ // Factory space is good, we now want to initialize our cheating token\r
+ // which one it is initialized is the model for all tokens we manufacture\r
+ //\r
+ antlr3SetCTAPI(&factory->unTruc);\r
+\r
+ // Set some initial variables for future copying, including a string factory\r
+ // that we can use later for converting trees to strings.\r
+ //\r
+ factory->unTruc.factory = factory;\r
+ factory->unTruc.baseTree.strFactory = strFactory;\r
+\r
+ return factory;\r
+\r
+}\r
+\r
+static void\r
+newPool(pANTLR3_ARBORETUM factory)\r
+{\r
+ // Increment factory count\r
+ //\r
+ factory->thisPool++;\r
+\r
+ // Ensure we have enough pointers allocated\r
+ //\r
+ factory->pools = (pANTLR3_COMMON_TREE *)\r
+ ANTLR3_REALLOC( (void *)factory->pools, // Current pools pointer (starts at NULL)\r
+ (ANTLR3_UINT32)((factory->thisPool + 1) * sizeof(pANTLR3_COMMON_TREE *)) // Memory for new pool pointers\r
+ );\r
+\r
+ // Allocate a new pool for the factory\r
+ //\r
+ factory->pools[factory->thisPool] =\r
+ (pANTLR3_COMMON_TREE) \r
+ ANTLR3_MALLOC((size_t)(sizeof(ANTLR3_COMMON_TREE) * ANTLR3_FACTORY_POOL_SIZE));\r
+\r
+\r
+ // Reset the counters\r
+ //\r
+ factory->nextTree = 0;\r
+ \r
+ // Done\r
+ //\r
+ return;\r
+}\r
+\r
+static pANTLR3_BASE_TREE \r
+newPoolTree (pANTLR3_ARBORETUM factory)\r
+{\r
+ pANTLR3_COMMON_TREE tree;\r
+\r
+ // If we have anything on the re claim stack, reuse that sucker first\r
+ //\r
+ tree = factory->nilStack->peek(factory->nilStack);\r
+\r
+ if (tree != NULL)\r
+ {\r
+ // Cool we got something we could reuse, it will have been cleaned up by\r
+ // whatever put it back on the stack (for instance if it had a child vector,\r
+ // that will have been cleared to hold zero entries and that vector will get reused too.\r
+ // It is the basetree pointer that is placed on the stack of course\r
+ //\r
+ factory->nilStack->pop(factory->nilStack);\r
+ return (pANTLR3_BASE_TREE)tree;\r
+\r
+ }\r
+ // See if we need a new tree pool before allocating a new tree\r
+ //\r
+ if (factory->nextTree >= ANTLR3_FACTORY_POOL_SIZE)\r
+ {\r
+ // We ran out of tokens in the current pool, so we need a new pool\r
+ //\r
+ newPool(factory);\r
+ }\r
+\r
+ // Assuming everything went well - we are trying for performance here so doing minimal\r
+ // error checking - then we can work out what the pointer is to the next commontree.\r
+ //\r
+ tree = factory->pools[factory->thisPool] + factory->nextTree;\r
+ factory->nextTree++;\r
+\r
+ // We have our token pointer now, so we can initialize it to the predefined model.\r
+ //\r
+ antlr3SetCTAPI(tree);\r
+\r
+ // Set some initial variables for future copying, including a string factory\r
+ // that we can use later for converting trees to strings.\r
+ //\r
+ tree->factory = factory;\r
+ tree->baseTree.strFactory = factory->unTruc.baseTree.strFactory;\r
+\r
+ // The super points to the common tree so we must override the one used by\r
+ // by the pre-built tree as otherwise we will always poitn to the same initial\r
+ // common tree and we might spend 3 hours trying to debug why - this would never\r
+ // happen to me of course! :-(\r
+ //\r
+ tree->baseTree.super = tree;\r
+\r
+ // And we are done\r
+ //\r
+ return &(tree->baseTree);\r
+}\r
+\r
+\r
+static pANTLR3_BASE_TREE \r
+newFromTree(pANTLR3_ARBORETUM factory, pANTLR3_COMMON_TREE tree)\r
+{\r
+ pANTLR3_BASE_TREE newTree;\r
+\r
+ newTree = factory->newTree(factory);\r
+\r
+ if (newTree == NULL)\r
+ {\r
+ return NULL;\r
+ }\r
+\r
+ // Pick up the payload we had in the supplied tree\r
+ //\r
+ ((pANTLR3_COMMON_TREE)(newTree->super))->token = tree->token;\r
+ newTree->u = tree->baseTree.u; // Copy any user pointer\r
+\r
+ return newTree;\r
+}\r
+\r
+static pANTLR3_BASE_TREE \r
+newFromToken(pANTLR3_ARBORETUM factory, pANTLR3_COMMON_TOKEN token)\r
+{\r
+ pANTLR3_BASE_TREE newTree;\r
+\r
+ newTree = factory->newTree(factory);\r
+\r
+ if (newTree == NULL)\r
+ {\r
+ return NULL;\r
+ }\r
+\r
+ // Pick up the payload we had in the supplied tree\r
+ //\r
+ ((pANTLR3_COMMON_TREE)(newTree->super))->token = token;\r
+\r
+ return newTree;\r
+}\r
+\r
+static void\r
+factoryClose (pANTLR3_ARBORETUM factory)\r
+{\r
+ ANTLR3_INT32 poolCount;\r
+\r
+ // First close the vector factory that supplied all the child pointer\r
+ // vectors.\r
+ //\r
+ factory->vFactory->close(factory->vFactory);\r
+\r
+ if (factory->nilStack != NULL)\r
+ {\r
+ factory->nilStack->free(factory->nilStack);\r
+ }\r
+\r
+ // We now JUST free the pools because the C runtime CommonToken based tree\r
+ // cannot contain anything that was not made by this factory.\r
+ //\r
+ for (poolCount = 0; poolCount <= factory->thisPool; poolCount++)\r
+ {\r
+ // We can now free this pool allocation\r
+ //\r
+ ANTLR3_FREE(factory->pools[poolCount]);\r
+ factory->pools[poolCount] = NULL;\r
+ }\r
+\r
+ // All the pools are deallocated we can free the pointers to the pools\r
+ // now.\r
+ //\r
+ ANTLR3_FREE(factory->pools);\r
+\r
+ // Finally, we can free the space for the factory itself\r
+ //\r
+ ANTLR3_FREE(factory);\r
+}\r
+\r
+\r
+ANTLR3_API void \r
+antlr3SetCTAPI(pANTLR3_COMMON_TREE tree)\r
+{\r
+ // Init base tree\r
+ //\r
+ antlr3BaseTreeNew(&(tree->baseTree));\r
+\r
+ // We need a pointer to ourselves for \r
+ // the payload and few functions that we\r
+ // provide.\r
+ //\r
+ tree->baseTree.super = tree;\r
+\r
+ // Common tree overrides\r
+\r
+ tree->baseTree.isNilNode = isNilNode;\r
+ tree->baseTree.toString = toString;\r
+ tree->baseTree.dupNode = (void *(*)(pANTLR3_BASE_TREE))(dupNode);\r
+ tree->baseTree.getLine = getLine;\r
+ tree->baseTree.getCharPositionInLine = getCharPositionInLine;\r
+ tree->baseTree.toString = toString;\r
+ tree->baseTree.getType = getType;\r
+ tree->baseTree.getText = getText;\r
+ tree->baseTree.getToken = getToken;\r
+ tree->baseTree.getParent = getParent;\r
+ tree->baseTree.setParent = setParent;\r
+ tree->baseTree.setChildIndex = setChildIndex;\r
+ tree->baseTree.getChildIndex = getChildIndex;\r
+ tree->baseTree.createChildrenList = createChildrenList;\r
+ tree->baseTree.reuse = reuse;\r
+ tree->baseTree.free = NULL; // Factory trees have no free function\r
+ \r
+ tree->baseTree.children = NULL;\r
+\r
+ tree->token = NULL; // No token as yet\r
+ tree->startIndex = 0;\r
+ tree->stopIndex = 0;\r
+ tree->parent = NULL; // No parent yet\r
+ tree->childIndex = -1;\r
+\r
+ return;\r
+}\r
+\r
+// --------------------------------------\r
+// Non factory node constructors.\r
+//\r
+\r
+ANTLR3_API pANTLR3_COMMON_TREE\r
+antlr3CommonTreeNew()\r
+{\r
+ pANTLR3_COMMON_TREE tree;\r
+ tree = ANTLR3_MALLOC(sizeof(ANTLR3_COMMON_TREE));\r
+\r
+ if (tree == NULL)\r
+ {\r
+ return NULL;\r
+ }\r
+\r
+ antlr3SetCTAPI(tree);\r
+\r
+ return tree;\r
+}\r
+\r
+ANTLR3_API pANTLR3_COMMON_TREE \r
+antlr3CommonTreeNewFromToken(pANTLR3_COMMON_TOKEN token)\r
+{\r
+ pANTLR3_COMMON_TREE newTree;\r
+\r
+ newTree = antlr3CommonTreeNew();\r
+\r
+ if (newTree == NULL)\r
+ {\r
+ return NULL;\r
+ }\r
+\r
+ //Pick up the payload we had in the supplied tree\r
+ //\r
+ newTree->token = token;\r
+ return newTree;\r
+}\r
+\r
+/// Create a new vector for holding child nodes using the inbuilt\r
+/// vector factory.\r
+///\r
+static void\r
+createChildrenList (pANTLR3_BASE_TREE tree)\r
+{\r
+ tree->children = ((pANTLR3_COMMON_TREE)(tree->super))->factory->vFactory->newVector(((pANTLR3_COMMON_TREE)(tree->super))->factory->vFactory);\r
+}\r
+\r
+\r
+static pANTLR3_COMMON_TOKEN \r
+getToken (pANTLR3_BASE_TREE tree)\r
+{\r
+ // The token is the payload of the common tree or other implementor\r
+ // so it is stored within ourselves, which is the super pointer.Note \r
+ // that whatever the actual token is, it is passed around by its pointer\r
+ // to the common token implementation, which it may of course surround\r
+ // with its own super structure.\r
+ //\r
+ return ((pANTLR3_COMMON_TREE)(tree->super))->token;\r
+}\r
+\r
+static pANTLR3_BASE_TREE \r
+dupNode (pANTLR3_BASE_TREE tree)\r
+{\r
+ // The node we are duplicating is in fact the common tree (that's why we are here)\r
+ // so we use the super pointer to duplicate.\r
+ //\r
+ pANTLR3_COMMON_TREE theOld;\r
+ \r
+ theOld = (pANTLR3_COMMON_TREE)(tree->super);\r
+\r
+ // The pointer we return is the base implementation of course\r
+ //\r
+ return theOld->factory->newFromTree(theOld->factory, theOld);\r
+}\r
+\r
+static ANTLR3_BOOLEAN \r
+isNilNode (pANTLR3_BASE_TREE tree)\r
+{\r
+ // This is a Nil tree if it has no payload (Token in our case)\r
+ //\r
+ if (((pANTLR3_COMMON_TREE)(tree->super))->token == NULL)\r
+ {\r
+ return ANTLR3_TRUE;\r
+ }\r
+ else\r
+ {\r
+ return ANTLR3_FALSE;\r
+ }\r
+}\r
+\r
+static ANTLR3_UINT32 \r
+getType (pANTLR3_BASE_TREE tree)\r
+{\r
+ pANTLR3_COMMON_TREE theTree;\r
+\r
+ theTree = (pANTLR3_COMMON_TREE)(tree->super);\r
+\r
+ if (theTree->token == NULL)\r
+ {\r
+ return 0;\r
+ }\r
+ else\r
+ {\r
+ return theTree->token->getType(theTree->token);\r
+ }\r
+}\r
+\r
+static pANTLR3_STRING \r
+getText (pANTLR3_BASE_TREE tree)\r
+{\r
+ return tree->toString(tree);\r
+}\r
+\r
+static ANTLR3_UINT32 getLine (pANTLR3_BASE_TREE tree)\r
+{\r
+ pANTLR3_COMMON_TREE cTree;\r
+ pANTLR3_COMMON_TOKEN token;\r
+\r
+ cTree = (pANTLR3_COMMON_TREE)(tree->super);\r
+\r
+ token = cTree->token;\r
+\r
+ if (token == NULL || token->getLine(token) == 0)\r
+ {\r
+ if (tree->getChildCount(tree) > 0)\r
+ {\r
+ pANTLR3_BASE_TREE child;\r
+\r
+ child = (pANTLR3_BASE_TREE)tree->getChild(tree, 0);\r
+ return child->getLine(child);\r
+ }\r
+ return 0;\r
+ }\r
+ return token->getLine(token);\r
+}\r
+\r
+static ANTLR3_UINT32 getCharPositionInLine (pANTLR3_BASE_TREE tree)\r
+{\r
+ pANTLR3_COMMON_TOKEN token;\r
+\r
+ token = ((pANTLR3_COMMON_TREE)(tree->super))->token;\r
+\r
+ if (token == NULL || token->getCharPositionInLine(token) == -1)\r
+ {\r
+ if (tree->getChildCount(tree) > 0)\r
+ {\r
+ pANTLR3_BASE_TREE child;\r
+\r
+ child = (pANTLR3_BASE_TREE)tree->getChild(tree, 0);\r
+\r
+ return child->getCharPositionInLine(child);\r
+ }\r
+ return 0;\r
+ }\r
+ return token->getCharPositionInLine(token);\r
+}\r
+\r
+static pANTLR3_STRING toString (pANTLR3_BASE_TREE tree)\r
+{\r
+ if (tree->isNilNode(tree) == ANTLR3_TRUE)\r
+ {\r
+ pANTLR3_STRING nilNode;\r
+\r
+ nilNode = tree->strFactory->newPtr(tree->strFactory, (pANTLR3_UINT8)"nil", 3);\r
+\r
+ return nilNode;\r
+ }\r
+\r
+ return ((pANTLR3_COMMON_TREE)(tree->super))->token->getText(((pANTLR3_COMMON_TREE)(tree->super))->token);\r
+}\r
+\r
+static pANTLR3_BASE_TREE \r
+getParent (pANTLR3_BASE_TREE tree)\r
+{\r
+ return & (((pANTLR3_COMMON_TREE)(tree->super))->parent->baseTree);\r
+}\r
+\r
+static void \r
+setParent (pANTLR3_BASE_TREE tree, pANTLR3_BASE_TREE parent)\r
+{\r
+ ((pANTLR3_COMMON_TREE)(tree->super))->parent = parent == NULL ? NULL : ((pANTLR3_COMMON_TREE)(parent->super))->parent;\r
+}\r
+\r
+static void \r
+setChildIndex (pANTLR3_BASE_TREE tree, ANTLR3_INT32 i)\r
+{\r
+ ((pANTLR3_COMMON_TREE)(tree->super))->childIndex = i;\r
+}\r
+static ANTLR3_INT32 \r
+getChildIndex (pANTLR3_BASE_TREE tree )\r
+{\r
+ return ((pANTLR3_COMMON_TREE)(tree->super))->childIndex;\r
+}\r
+\r
+/** Clean up any child vector that the tree might have, so it can be reused,\r
+ * then add it into the reuse stack.\r
+ */\r
+static void\r
+reuse (pANTLR3_BASE_TREE tree)\r
+{\r
+ pANTLR3_COMMON_TREE cTree;\r
+\r
+ cTree = (pANTLR3_COMMON_TREE)(tree->super);\r
+\r
+ if (cTree->factory != NULL)\r
+ {\r
+\r
+ if (cTree->baseTree.children != NULL)\r
+ {\r
+ \r
+ cTree->baseTree.children->clear(cTree->baseTree.children);\r
+ }\r
+ cTree->factory->nilStack->push(cTree->factory->nilStack, tree, NULL);\r
+ \r
+ }\r
+}\r