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