--- /dev/null
+/** \file\r
+ * Contains the base functions that all tree adaptors start with.\r
+ * this implementation can then be overridden by any higher implementation.\r
+ * \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 <antlr3basetreeadaptor.h>\r
+\r
+#ifdef ANTLR3_WINDOWS\r
+#pragma warning( disable : 4100 )\r
+#endif\r
+\r
+/* Interface functions\r
+ */\r
+static pANTLR3_BASE_TREE nilNode (pANTLR3_BASE_TREE_ADAPTOR adaptor);\r
+static pANTLR3_BASE_TREE dbgNil (pANTLR3_BASE_TREE_ADAPTOR adaptor);\r
+static pANTLR3_BASE_TREE dupTree (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t);\r
+static pANTLR3_BASE_TREE dbgDupTree (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t);\r
+static pANTLR3_BASE_TREE dupTreeTT (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t, pANTLR3_BASE_TREE parent);\r
+static void addChild (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t, pANTLR3_BASE_TREE child);\r
+static void dbgAddChild (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t, pANTLR3_BASE_TREE child);\r
+static pANTLR3_BASE_TREE becomeRoot (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE newRoot, pANTLR3_BASE_TREE oldRoot);\r
+static pANTLR3_BASE_TREE dbgBecomeRoot (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE newRoot, pANTLR3_BASE_TREE oldRoot);\r
+static pANTLR3_BASE_TREE rulePostProcessing (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE root);\r
+static void addChildToken (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t, pANTLR3_COMMON_TOKEN child);\r
+static void dbgAddChildToken (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t, pANTLR3_COMMON_TOKEN child);\r
+static pANTLR3_BASE_TREE becomeRootToken (pANTLR3_BASE_TREE_ADAPTOR adaptor, void * newRoot, pANTLR3_BASE_TREE oldRoot);\r
+static pANTLR3_BASE_TREE dbgBecomeRootToken (pANTLR3_BASE_TREE_ADAPTOR adaptor, void * newRoot, pANTLR3_BASE_TREE oldRoot);\r
+static pANTLR3_BASE_TREE createTypeToken (pANTLR3_BASE_TREE_ADAPTOR adaptor, ANTLR3_UINT32 tokenType, pANTLR3_COMMON_TOKEN fromToken);\r
+static pANTLR3_BASE_TREE dbgCreateTypeToken (pANTLR3_BASE_TREE_ADAPTOR adaptor, ANTLR3_UINT32 tokenType, pANTLR3_COMMON_TOKEN fromToken);\r
+static pANTLR3_BASE_TREE createTypeTokenText (pANTLR3_BASE_TREE_ADAPTOR adaptor, ANTLR3_UINT32 tokenType, pANTLR3_COMMON_TOKEN fromToken, pANTLR3_UINT8 text);\r
+static pANTLR3_BASE_TREE dbgCreateTypeTokenText (pANTLR3_BASE_TREE_ADAPTOR adaptor, ANTLR3_UINT32 tokenType, pANTLR3_COMMON_TOKEN fromToken, pANTLR3_UINT8 text);\r
+static pANTLR3_BASE_TREE createTypeText (pANTLR3_BASE_TREE_ADAPTOR adaptor, ANTLR3_UINT32 tokenType, pANTLR3_UINT8 text);\r
+static pANTLR3_BASE_TREE dbgCreateTypeText (pANTLR3_BASE_TREE_ADAPTOR adaptor, ANTLR3_UINT32 tokenType, pANTLR3_UINT8 text);\r
+static ANTLR3_UINT32 getType (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t);\r
+static void setType (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t, ANTLR3_UINT32 type);\r
+static pANTLR3_STRING getText (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t);\r
+static void setText (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_STRING t);\r
+static void setText8 (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_UINT8 t);\r
+static pANTLR3_BASE_TREE getChild (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t, ANTLR3_UINT32 i);\r
+static ANTLR3_UINT32 getChildCount (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t);\r
+static ANTLR3_UINT32 getUniqueID (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t);\r
+static ANTLR3_BOOLEAN isNilNode (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t);\r
+static pANTLR3_STRING makeDot (pANTLR3_BASE_TREE_ADAPTOR adaptor, void * theTree);\r
+\r
+/** Given a pointer to a base tree adaptor structure (which is usually embedded in the\r
+ * super class the implements the tree adaptor used in the parse), initialize its\r
+ * function pointers and so on.\r
+ */\r
+ANTLR3_API void\r
+antlr3BaseTreeAdaptorInit(pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_DEBUG_EVENT_LISTENER debugger)\r
+{\r
+ // Initialize the interface\r
+ //\r
+ if (debugger == NULL)\r
+ {\r
+ adaptor->nilNode = (void * (*)(pANTLR3_BASE_TREE_ADAPTOR)) \r
+ nilNode;\r
+ adaptor->addChild = (void (*)(pANTLR3_BASE_TREE_ADAPTOR, void *, void *)) \r
+ addChild;\r
+ adaptor->becomeRoot = (void * (*)(pANTLR3_BASE_TREE_ADAPTOR, void *, void *)) \r
+ becomeRoot;\r
+ adaptor->addChildToken = (void (*)(pANTLR3_BASE_TREE_ADAPTOR, void *, pANTLR3_COMMON_TOKEN)) \r
+ addChildToken;\r
+ adaptor->becomeRootToken = (void * (*)(pANTLR3_BASE_TREE_ADAPTOR, void *, void *))\r
+ becomeRootToken;\r
+ adaptor->createTypeToken = (void * (*)(pANTLR3_BASE_TREE_ADAPTOR, ANTLR3_UINT32, pANTLR3_COMMON_TOKEN))\r
+ createTypeToken;\r
+ adaptor->createTypeTokenText = (void * (*)(pANTLR3_BASE_TREE_ADAPTOR, ANTLR3_UINT32, pANTLR3_COMMON_TOKEN, pANTLR3_UINT8))\r
+ createTypeTokenText;\r
+ adaptor->createTypeText = (void * (*)(pANTLR3_BASE_TREE_ADAPTOR, ANTLR3_UINT32, pANTLR3_UINT8))\r
+ createTypeText;\r
+ adaptor->dupTree = (void * (*)(pANTLR3_BASE_TREE_ADAPTOR, void *)) \r
+ dupTree;\r
+ }\r
+ else\r
+ {\r
+ adaptor->nilNode = (void * (*)(pANTLR3_BASE_TREE_ADAPTOR))\r
+ dbgNil;\r
+ adaptor->addChild = (void (*)(pANTLR3_BASE_TREE_ADAPTOR, void *, void *))\r
+ dbgAddChild;\r
+ adaptor->becomeRoot = (void * (*)(pANTLR3_BASE_TREE_ADAPTOR, void *, void *))\r
+ dbgBecomeRoot;\r
+ adaptor->addChildToken = (void (*)(pANTLR3_BASE_TREE_ADAPTOR, void *, pANTLR3_COMMON_TOKEN))\r
+ dbgAddChildToken;\r
+ adaptor->becomeRootToken = (void * (*)(pANTLR3_BASE_TREE_ADAPTOR, void *, void *))\r
+ dbgBecomeRootToken;\r
+ adaptor->createTypeToken = (void * (*)(pANTLR3_BASE_TREE_ADAPTOR, ANTLR3_UINT32, pANTLR3_COMMON_TOKEN))\r
+ dbgCreateTypeToken;\r
+ adaptor->createTypeTokenText = (void * (*)(pANTLR3_BASE_TREE_ADAPTOR, ANTLR3_UINT32, pANTLR3_COMMON_TOKEN, pANTLR3_UINT8))\r
+ dbgCreateTypeTokenText;\r
+ adaptor->createTypeText = (void * (*)(pANTLR3_BASE_TREE_ADAPTOR, ANTLR3_UINT32, pANTLR3_UINT8))\r
+ dbgCreateTypeText;\r
+ adaptor->dupTree = (void * (*)(pANTLR3_BASE_TREE_ADAPTOR, void *))\r
+ dbgDupTree;\r
+ debugger->adaptor = adaptor;\r
+ }\r
+\r
+ adaptor->dupTreeTT = (void * (*)(pANTLR3_BASE_TREE_ADAPTOR, void *, void *))\r
+ dupTreeTT;\r
+ adaptor->rulePostProcessing = (void * (*)(pANTLR3_BASE_TREE_ADAPTOR, void *))\r
+ rulePostProcessing;\r
+ adaptor->getType = (ANTLR3_UINT32 (*)(pANTLR3_BASE_TREE_ADAPTOR, void *))\r
+ getType;\r
+ adaptor->setType = (void (*)(pANTLR3_BASE_TREE_ADAPTOR, void *, ANTLR3_UINT32))\r
+ setType;\r
+ adaptor->getText = (pANTLR3_STRING (*) (pANTLR3_BASE_TREE_ADAPTOR, void *))\r
+ getText;\r
+ adaptor->setText8 = (void (*)(pANTLR3_BASE_TREE_ADAPTOR, pANTLR3_UINT8))\r
+ setText8;\r
+ adaptor->setText = (void (*)(pANTLR3_BASE_TREE_ADAPTOR, pANTLR3_STRING))\r
+ setText;\r
+ adaptor->getChild = (void * (*)(pANTLR3_BASE_TREE_ADAPTOR, void *, ANTLR3_UINT32))\r
+ getChild;\r
+ adaptor->getChildCount = (ANTLR3_UINT32 (*)(pANTLR3_BASE_TREE_ADAPTOR, void *))\r
+ getChildCount;\r
+ adaptor->getUniqueID = (ANTLR3_UINT32 (*)(pANTLR3_BASE_TREE_ADAPTOR, void *))\r
+ getUniqueID;\r
+ adaptor->isNilNode = (ANTLR3_BOOLEAN (*)(pANTLR3_BASE_TREE_ADAPTOR, void *))\r
+ isNilNode;\r
+\r
+ adaptor->makeDot = (pANTLR3_STRING (*)(pANTLR3_BASE_TREE_ADAPTOR, void *))\r
+ makeDot;\r
+ \r
+ /* Remaining functions filled in by the caller.\r
+ */\r
+ return;\r
+}\r
+\r
+static void\r
+defineDotNodes(pANTLR3_BASE_TREE_ADAPTOR adaptor, void * t, pANTLR3_STRING dotSpec )\r
+{\r
+ // How many nodes are we talking about?\r
+ //\r
+ int nCount;\r
+ int i;\r
+ pANTLR3_BASE_TREE child;\r
+ char buff[64];\r
+ pANTLR3_STRING text;\r
+ int j;\r
+\r
+\r
+\r
+\r
+\r
+ // Count the nodes\r
+ //\r
+ nCount = adaptor->getChildCount(adaptor, t);\r
+\r
+ if (nCount == 0)\r
+ {\r
+ // This will already have been included as a child of another node\r
+ // so there is nothing to add.\r
+ //\r
+ return;\r
+ }\r
+\r
+ // For each child of the current tree, define a node using the\r
+ // memory address of the node to name it\r
+ //\r
+ for (i = 0; i<nCount; i++)\r
+ {\r
+\r
+ // Pick up a pointer for the child\r
+ //\r
+ child = adaptor->getChild(adaptor, t, i);\r
+\r
+ // Name the node\r
+ //\r
+ sprintf(buff, "\tn%p[label=\"", child);\r
+ dotSpec->append8(dotSpec, buff);\r
+ text = adaptor->getText(adaptor, child);\r
+ for (j = 0; j < (ANTLR3_INT32)(text->len); j++)\r
+ {\r
+ switch(text->charAt(text, j))\r
+ {\r
+ case '"':\r
+\r
+ dotSpec->append8(dotSpec, "\\\"");\r
+ break;\r
+\r
+ case '\n':\r
+\r
+ dotSpec->append8(dotSpec, "\\n");\r
+ break;\r
+\r
+ case '\r':\r
+\r
+ dotSpec->append8(dotSpec, "\\r");\r
+ break;\r
+\r
+ default:\r
+\r
+ dotSpec->addc(dotSpec, text->charAt(text, j));\r
+ break;\r
+ }\r
+ }\r
+ dotSpec->append8(dotSpec, "\"]\n");\r
+\r
+ // And now define the children of this child (if any)\r
+ //\r
+ defineDotNodes(adaptor, child, dotSpec);\r
+ }\r
+ \r
+ // Done\r
+ //\r
+ return;\r
+}\r
+\r
+static void\r
+defineDotEdges(pANTLR3_BASE_TREE_ADAPTOR adaptor, void * t, pANTLR3_STRING dotSpec)\r
+{\r
+ // How many nodes are we talking about?\r
+ //\r
+ int nCount;\r
+ int i;\r
+\r
+ if (t == NULL)\r
+ {\r
+ // No tree, so do nothing\r
+ //\r
+ return;\r
+ }\r
+\r
+ // Count the nodes\r
+ //\r
+ nCount = adaptor->getChildCount(adaptor, t);\r
+\r
+ if (nCount == 0)\r
+ {\r
+ // This will already have been included as a child of another node\r
+ // so there is nothing to add.\r
+ //\r
+ return;\r
+ }\r
+\r
+ // For each child, define an edge from this parent, then process\r
+ // and children of this child in the same way\r
+ //\r
+ for (i=0; i<nCount; i++)\r
+ {\r
+ pANTLR3_BASE_TREE child;\r
+ char buff[128];\r
+ pANTLR3_STRING text;\r
+ int j;\r
+\r
+ // Next child\r
+ //\r
+ child = adaptor->getChild(adaptor, t, i);\r
+\r
+ // Create the edge relation\r
+ //\r
+ sprintf(buff, "\t\tn%p -> n%p\t\t// ", t, child);\r
+ \r
+ dotSpec->append8(dotSpec, buff);\r
+\r
+ // Document the relationship\r
+ //\r
+ text = adaptor->getText(adaptor, t);\r
+ for (j = 0; j < (ANTLR3_INT32)(text->len); j++)\r
+ {\r
+ switch(text->charAt(text, j))\r
+ {\r
+ case '"':\r
+\r
+ dotSpec->append8(dotSpec, "\\\"");\r
+ break;\r
+\r
+ case '\n':\r
+\r
+ dotSpec->append8(dotSpec, "\\n");\r
+ break;\r
+\r
+ case '\r':\r
+\r
+ dotSpec->append8(dotSpec, "\\r");\r
+ break;\r
+\r
+ default:\r
+\r
+ dotSpec->addc(dotSpec, text->charAt(text, j));\r
+ break;\r
+ }\r
+ }\r
+\r
+ dotSpec->append8(dotSpec, " -> ");\r
+\r
+ text = adaptor->getText(adaptor, child);\r
+ for (j = 0; j < (ANTLR3_INT32)(text->len); j++)\r
+ {\r
+ switch(text->charAt(text, j))\r
+ {\r
+ case '"':\r
+\r
+ dotSpec->append8(dotSpec, "\\\"");\r
+ break;\r
+\r
+ case '\n':\r
+\r
+ dotSpec->append8(dotSpec, "\\n");\r
+ break;\r
+\r
+ case '\r':\r
+\r
+ dotSpec->append8(dotSpec, "\\r");\r
+ break;\r
+\r
+ default:\r
+\r
+ dotSpec->addc(dotSpec, text->charAt(text, j));\r
+ break;\r
+ }\r
+ }\r
+ dotSpec->append8(dotSpec, "\n");\r
+\r
+ \r
+ // Define edges for this child\r
+ //\r
+ defineDotEdges(adaptor, child, dotSpec);\r
+ }\r
+\r
+ // Done\r
+ //\r
+ return;\r
+}\r
+\r
+/// Produce a DOT specification for graphviz\r
+//\r
+static pANTLR3_STRING\r
+makeDot (pANTLR3_BASE_TREE_ADAPTOR adaptor, void * theTree)\r
+{\r
+ // The string we are building up\r
+ //\r
+ pANTLR3_STRING dotSpec;\r
+ char buff[64];\r
+ pANTLR3_STRING text;\r
+ int j;\r
+\r
+ dotSpec = adaptor->strFactory->newStr8\r
+ \r
+ (\r
+ adaptor->strFactory,\r
+\r
+ // Default look and feel\r
+ //\r
+ (pANTLR3_UINT8)\r
+ "digraph {\n\n"\r
+ "\tordering=out;\n"\r
+ "\tranksep=.4;\n"\r
+ "\tbgcolor=\"lightgrey\"; node [shape=box, fixedsize=false, fontsize=12, fontname=\"Helvetica-bold\", fontcolor=\"blue\"\n"\r
+ "\twidth=.25, height=.25, color=\"black\", fillcolor=\"white\", style=\"filled, solid, bold\"];\n\n"\r
+ "\tedge [arrowsize=.5, color=\"black\", style=\"bold\"]\n\n"\r
+ );\r
+\r
+ if (theTree == NULL)\r
+ {\r
+ // No tree, so create a blank spec\r
+ //\r
+ dotSpec->append8(dotSpec, "n0[label=\"EMPTY TREE\"]\n");\r
+ return dotSpec;\r
+ }\r
+\r
+ sprintf(buff, "\tn%p[label=\"", theTree);\r
+ dotSpec->append8(dotSpec, buff);\r
+ text = adaptor->getText(adaptor, theTree);\r
+ for (j = 0; j < (ANTLR3_INT32)(text->len); j++)\r
+ {\r
+ switch(text->charAt(text, j))\r
+ {\r
+ case '"':\r
+\r
+ dotSpec->append8(dotSpec, "\\\"");\r
+ break;\r
+\r
+ case '\n':\r
+\r
+ dotSpec->append8(dotSpec, "\\n");\r
+ break;\r
+\r
+ case '\r':\r
+\r
+ dotSpec->append8(dotSpec, "\\r");\r
+ break;\r
+\r
+ default:\r
+\r
+ dotSpec->addc(dotSpec, text->charAt(text, j));\r
+ break;\r
+ }\r
+ }\r
+ dotSpec->append8(dotSpec, "\"]\n");\r
+\r
+ // First produce the node defintions\r
+ //\r
+ defineDotNodes(adaptor, theTree, dotSpec);\r
+ dotSpec->append8(dotSpec, "\n");\r
+ defineDotEdges(adaptor, theTree, dotSpec);\r
+ \r
+ // Terminate the spec\r
+ //\r
+ dotSpec->append8(dotSpec, "\n}");\r
+\r
+ // Result\r
+ //\r
+ return dotSpec;\r
+}\r
+\r
+\r
+/** Create and return a nil tree node (no token payload)\r
+ */\r
+static pANTLR3_BASE_TREE \r
+nilNode (pANTLR3_BASE_TREE_ADAPTOR adaptor)\r
+{\r
+ return adaptor->create(adaptor, NULL);\r
+}\r
+\r
+static pANTLR3_BASE_TREE \r
+dbgNil (pANTLR3_BASE_TREE_ADAPTOR adaptor)\r
+{\r
+ pANTLR3_BASE_TREE t;\r
+\r
+ t = adaptor->create (adaptor, NULL);\r
+ adaptor->debugger->createNode (adaptor->debugger, t);\r
+\r
+ return t;\r
+}\r
+\r
+/** Return a duplicate of the entire tree (implementation provided by the \r
+ * BASE_TREE interface.)\r
+ */\r
+static pANTLR3_BASE_TREE \r
+dupTree (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t)\r
+{\r
+ return adaptor->dupTreeTT(adaptor, t, NULL);\r
+}\r
+\r
+pANTLR3_BASE_TREE\r
+dupTreeTT (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t, pANTLR3_BASE_TREE parent)\r
+{\r
+ pANTLR3_BASE_TREE newTree;\r
+ pANTLR3_BASE_TREE child;\r
+ pANTLR3_BASE_TREE newSubTree;\r
+ ANTLR3_UINT32 n;\r
+ ANTLR3_UINT32 i;\r
+\r
+ if (t == NULL)\r
+ {\r
+ return NULL;\r
+ }\r
+ newTree = t->dupNode(t);\r
+\r
+ // Ensure new subtree root has parent/child index set\r
+ //\r
+ adaptor->setChildIndex (adaptor, newTree, t->getChildIndex(t));\r
+ adaptor->setParent (adaptor, newTree, parent);\r
+ n = adaptor->getChildCount (adaptor, t);\r
+\r
+ for (i=0; i < n; i++)\r
+ {\r
+ child = adaptor->getChild (adaptor, t, i);\r
+ newSubTree = adaptor->dupTreeTT (adaptor, child, t);\r
+ adaptor->addChild (adaptor, newTree, newSubTree);\r
+ }\r
+ return newTree;\r
+}\r
+\r
+/// Sends the required debugging events for duplicating a tree\r
+/// to the debugger.\r
+///\r
+static void\r
+simulateTreeConstruction(pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE tree)\r
+{\r
+ ANTLR3_UINT32 n;\r
+ ANTLR3_UINT32 i;\r
+ pANTLR3_BASE_TREE child;\r
+\r
+ // Send the create node event\r
+ //\r
+ adaptor->debugger->createNode(adaptor->debugger, tree);\r
+\r
+ n = adaptor->getChildCount(adaptor, tree);\r
+ for (i = 0; i < n; i++)\r
+ {\r
+ child = adaptor->getChild(adaptor, tree, i);\r
+ simulateTreeConstruction(adaptor, child);\r
+ adaptor->debugger->addChild(adaptor->debugger, tree, child);\r
+ }\r
+}\r
+\r
+pANTLR3_BASE_TREE\r
+dbgDupTree (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE tree)\r
+{\r
+ pANTLR3_BASE_TREE t;\r
+\r
+ // Call the normal dup tree mechanism first\r
+ //\r
+ t = adaptor->dupTreeTT(adaptor, tree, NULL);\r
+\r
+ // In order to tell the debugger what we have just done, we now\r
+ // simulate the tree building mechanism. THis will fire\r
+ // lots of debugging events to the client and look like we\r
+ // duped the tree..\r
+ //\r
+ simulateTreeConstruction(adaptor, t);\r
+\r
+ return t;\r
+}\r
+\r
+/** Add a child to the tree t. If child is a flat tree (a list), make all\r
+ * in list children of t. Warning: if t has no children, but child does\r
+ * and child isNilNode then it is ok to move children to t via\r
+ * t.children = child.children; i.e., without copying the array. This\r
+ * is for construction and I'm not sure it's completely general for\r
+ * a tree's addChild method to work this way. Make sure you differentiate\r
+ * between your tree's addChild and this parser tree construction addChild\r
+ * if it's not ok to move children to t with a simple assignment.\r
+ */\r
+static void \r
+addChild (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t, pANTLR3_BASE_TREE child)\r
+{\r
+ if (t != NULL && child != NULL)\r
+ {\r
+ t->addChild(t, child);\r
+ }\r
+}\r
+static void \r
+dbgAddChild (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t, pANTLR3_BASE_TREE child)\r
+{\r
+ if (t != NULL && child != NULL)\r
+ {\r
+ t->addChild(t, child);\r
+ adaptor->debugger->addChild(adaptor->debugger, t, child);\r
+ }\r
+}\r
+/** Use the adaptor implementation to add a child node with the supplied token\r
+ */\r
+static void \r
+addChildToken (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t, pANTLR3_COMMON_TOKEN child)\r
+{\r
+ if (t != NULL && child != NULL)\r
+ {\r
+ adaptor->addChild(adaptor, t, adaptor->create(adaptor, child));\r
+ }\r
+}\r
+static void \r
+dbgAddChildToken (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t, pANTLR3_COMMON_TOKEN child)\r
+{\r
+ pANTLR3_BASE_TREE tc;\r
+\r
+ if (t != NULL && child != NULL)\r
+ {\r
+ tc = adaptor->create(adaptor, child);\r
+ adaptor->addChild(adaptor, t, tc);\r
+ adaptor->debugger->addChild(adaptor->debugger, t, tc);\r
+ }\r
+}\r
+\r
+/** If oldRoot is a nil root, just copy or move the children to newRoot.\r
+ * If not a nil root, make oldRoot a child of newRoot.\r
+ *\r
+ * \code\r
+ * old=^(nil a b c), new=r yields ^(r a b c)\r
+ * old=^(a b c), new=r yields ^(r ^(a b c))\r
+ * \endcode\r
+ *\r
+ * If newRoot is a nil-rooted single child tree, use the single\r
+ * child as the new root node.\r
+ *\r
+ * \code\r
+ * old=^(nil a b c), new=^(nil r) yields ^(r a b c)\r
+ * old=^(a b c), new=^(nil r) yields ^(r ^(a b c))\r
+ * \endcode\r
+ *\r
+ * If oldRoot was null, it's ok, just return newRoot (even if isNilNode).\r
+ *\r
+ * \code\r
+ * old=null, new=r yields r\r
+ * old=null, new=^(nil r) yields ^(nil r)\r
+ * \endcode\r
+ *\r
+ * Return newRoot. Throw an exception if newRoot is not a\r
+ * simple node or nil root with a single child node--it must be a root\r
+ * node. If newRoot is <code>^(nil x)</endcode> return x as newRoot.\r
+ *\r
+ * Be advised that it's ok for newRoot to point at oldRoot's\r
+ * children; i.e., you don't have to copy the list. We are\r
+ * constructing these nodes so we should have this control for\r
+ * efficiency.\r
+ */\r
+static pANTLR3_BASE_TREE \r
+becomeRoot (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE newRootTree, pANTLR3_BASE_TREE oldRootTree)\r
+{\r
+ pANTLR3_BASE_TREE saveRoot;\r
+\r
+ /* Protect against tree rewrites if we are in some sort of error\r
+ * state, but have tried to recover. In C we can end up with a null pointer\r
+ * for a tree that was not produced.\r
+ */\r
+ if (newRootTree == NULL)\r
+ {\r
+ return oldRootTree;\r
+ }\r
+\r
+ /* root is just the new tree as is if there is no\r
+ * current root tree.\r
+ */\r
+ if (oldRootTree == NULL)\r
+ {\r
+ return newRootTree;\r
+ }\r
+\r
+ /* Produce ^(nil real-node)\r
+ */\r
+ if (newRootTree->isNilNode(newRootTree))\r
+ {\r
+ if (newRootTree->getChildCount(newRootTree) > 1)\r
+ {\r
+ /* TODO: Handle tree exceptions \r
+ */\r
+ ANTLR3_FPRINTF(stderr, "More than one node as root! TODO: Create tree exception handling\n");\r
+ return newRootTree;\r
+ }\r
+\r
+ /* The new root is the first child, keep track of the original newRoot\r
+ * because if it was a Nil Node, then we can reuse it now.\r
+ */\r
+ saveRoot = newRootTree;\r
+ newRootTree = newRootTree->getChild(newRootTree, 0);\r
+\r
+ // Reclaim the old nilNode()\r
+ //\r
+ saveRoot->reuse(saveRoot);\r
+ }\r
+\r
+ /* Add old root into new root. addChild takes care of the case where oldRoot\r
+ * is a flat list (nill rooted tree). All children of oldroot are added to\r
+ * new root.\r
+ */\r
+ newRootTree->addChild(newRootTree, oldRootTree);\r
+\r
+ // If the oldroot tree was a nil node, then we know at this point\r
+ // it has become orphaned by the rewrite logic, so we tell it to do\r
+ // whatever it needs to do to be reused.\r
+ //\r
+ if (oldRootTree->isNilNode(oldRootTree))\r
+ {\r
+ // We have taken an old Root Tree and appended all its children to the new\r
+ // root. In addition though it was a nil node, which means the generated code\r
+ // will not reuse it again, so we will reclaim it here. First we want to zero out\r
+ // any pointers it was carrying around. We are just the baseTree handler so we\r
+ // don't know necessarilly know how to do this for the real node, we just ask the tree itself\r
+ // to do it.\r
+ //\r
+ oldRootTree->reuse(oldRootTree);\r
+ }\r
+ /* Always returns new root structure\r
+ */\r
+ return newRootTree;\r
+\r
+}\r
+static pANTLR3_BASE_TREE \r
+dbgBecomeRoot (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE newRootTree, pANTLR3_BASE_TREE oldRootTree)\r
+{\r
+ pANTLR3_BASE_TREE t;\r
+ \r
+ t = becomeRoot(adaptor, newRootTree, oldRootTree);\r
+\r
+ adaptor->debugger->becomeRoot(adaptor->debugger, newRootTree, oldRootTree);\r
+\r
+ return t;\r
+}\r
+/** Transform ^(nil x) to x \r
+ */\r
+static pANTLR3_BASE_TREE \r
+ rulePostProcessing (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE root)\r
+{\r
+ pANTLR3_BASE_TREE saveRoot;\r
+\r
+ // Keep track of the root we are given. If it is a nilNode, then we\r
+ // can reuse it rather than orphaning it!\r
+ //\r
+ saveRoot = root;\r
+\r
+ if (root != NULL && root->isNilNode(root))\r
+ {\r
+ if (root->getChildCount(root) == 0)\r
+ {\r
+ root = NULL;\r
+ }\r
+ else if (root->getChildCount(root) == 1)\r
+ {\r
+ root = root->getChild(root, 0);\r
+ root->setParent(root, NULL);\r
+ root->setChildIndex(root, -1);\r
+\r
+ // The root we were given was a nil node, wiht one child, which means it has\r
+ // been abandoned and would be lost in the node factory. However\r
+ // nodes can be flagged as resuable to prevent this terrible waste\r
+ //\r
+ saveRoot->reuse(saveRoot);\r
+ }\r
+ }\r
+\r
+ return root;\r
+}\r
+ \r
+/** Use the adaptor interface to set a new tree node with the supplied token\r
+ * to the root of the tree.\r
+ */\r
+static pANTLR3_BASE_TREE \r
+ becomeRootToken (pANTLR3_BASE_TREE_ADAPTOR adaptor, void * newRoot, pANTLR3_BASE_TREE oldRoot)\r
+{\r
+ return adaptor->becomeRoot(adaptor, adaptor->create(adaptor, newRoot), oldRoot);\r
+}\r
+static pANTLR3_BASE_TREE \r
+dbgBecomeRootToken (pANTLR3_BASE_TREE_ADAPTOR adaptor, void * newRoot, pANTLR3_BASE_TREE oldRoot)\r
+{\r
+ pANTLR3_BASE_TREE t;\r
+\r
+ t = adaptor->becomeRoot(adaptor, adaptor->create(adaptor, newRoot), oldRoot);\r
+\r
+ adaptor->debugger->becomeRoot(adaptor->debugger,t, oldRoot);\r
+\r
+ return t;\r
+}\r
+\r
+/** Use the super class supplied create() method to create a new node\r
+ * from the supplied token.\r
+ */\r
+static pANTLR3_BASE_TREE \r
+createTypeToken (pANTLR3_BASE_TREE_ADAPTOR adaptor, ANTLR3_UINT32 tokenType, pANTLR3_COMMON_TOKEN fromToken)\r
+{\r
+ /* Create the new token\r
+ */\r
+ fromToken = adaptor->createTokenFromToken(adaptor, fromToken);\r
+\r
+ /* Set the type of the new token to that supplied\r
+ */\r
+ fromToken->setType(fromToken, tokenType);\r
+\r
+ /* Return a new node based upon this token\r
+ */\r
+ return adaptor->create(adaptor, fromToken);\r
+}\r
+static pANTLR3_BASE_TREE \r
+dbgCreateTypeToken (pANTLR3_BASE_TREE_ADAPTOR adaptor, ANTLR3_UINT32 tokenType, pANTLR3_COMMON_TOKEN fromToken)\r
+{\r
+ pANTLR3_BASE_TREE t;\r
+\r
+ t = createTypeToken(adaptor, tokenType, fromToken);\r
+\r
+ adaptor->debugger->createNode(adaptor->debugger, t);\r
+\r
+ return t;\r
+}\r
+\r
+static pANTLR3_BASE_TREE \r
+createTypeTokenText (pANTLR3_BASE_TREE_ADAPTOR adaptor, ANTLR3_UINT32 tokenType, pANTLR3_COMMON_TOKEN fromToken, pANTLR3_UINT8 text)\r
+{\r
+ /* Create the new token\r
+ */\r
+ fromToken = adaptor->createTokenFromToken(adaptor, fromToken);\r
+\r
+ /* Set the type of the new token to that supplied\r
+ */\r
+ fromToken->setType(fromToken, tokenType);\r
+\r
+ /* Set the text of the token accordingly\r
+ */\r
+ fromToken->setText8(fromToken, text);\r
+\r
+ /* Return a new node based upon this token\r
+ */\r
+ return adaptor->create(adaptor, fromToken);\r
+}\r
+static pANTLR3_BASE_TREE \r
+dbgCreateTypeTokenText (pANTLR3_BASE_TREE_ADAPTOR adaptor, ANTLR3_UINT32 tokenType, pANTLR3_COMMON_TOKEN fromToken, pANTLR3_UINT8 text)\r
+{\r
+ pANTLR3_BASE_TREE t;\r
+\r
+ t = createTypeTokenText(adaptor, tokenType, fromToken, text);\r
+\r
+ adaptor->debugger->createNode(adaptor->debugger, t);\r
+\r
+ return t;\r
+}\r
+\r
+static pANTLR3_BASE_TREE \r
+ createTypeText (pANTLR3_BASE_TREE_ADAPTOR adaptor, ANTLR3_UINT32 tokenType, pANTLR3_UINT8 text)\r
+{\r
+ pANTLR3_COMMON_TOKEN fromToken;\r
+\r
+ /* Create the new token\r
+ */\r
+ fromToken = adaptor->createToken(adaptor, tokenType, text);\r
+\r
+ /* Return a new node based upon this token\r
+ */\r
+ return adaptor->create(adaptor, fromToken);\r
+}\r
+static pANTLR3_BASE_TREE \r
+ dbgCreateTypeText (pANTLR3_BASE_TREE_ADAPTOR adaptor, ANTLR3_UINT32 tokenType, pANTLR3_UINT8 text)\r
+{\r
+ pANTLR3_BASE_TREE t;\r
+\r
+ t = createTypeText(adaptor, tokenType, text);\r
+\r
+ adaptor->debugger->createNode(adaptor->debugger, t);\r
+\r
+ return t;\r
+\r
+}\r
+/** Dummy implementation - will be supplied by super class\r
+ */\r
+static ANTLR3_UINT32 \r
+ getType (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t)\r
+{\r
+ return 0;\r
+}\r
+\r
+/** Dummy implementation - will be supplied by super class\r
+ */\r
+static void \r
+ setType (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t, ANTLR3_UINT32 type)\r
+{\r
+ ANTLR3_FPRINTF(stderr, "Internal error - implementor of superclass containing ANTLR3_TREE_ADAPTOR did not implement setType()\n");\r
+}\r
+\r
+/** Dummy implementation - will be supplied by super class\r
+ */\r
+static pANTLR3_STRING \r
+ getText (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t)\r
+{\r
+ ANTLR3_FPRINTF(stderr, "Internal error - implementor of superclass containing ANTLR3_TREE_ADAPTOR did not implement getText()\n");\r
+ return NULL;\r
+}\r
+\r
+/** Dummy implementation - will be supplied by super class\r
+ */\r
+static void \r
+ setText (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_STRING t)\r
+{\r
+ ANTLR3_FPRINTF(stderr, "Internal error - implementor of superclass containing ANTLR3_TREE_ADAPTOR did not implement setText()\n");\r
+}\r
+/** Dummy implementation - will be supplied by super class\r
+ */\r
+static void \r
+setText8 (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_UINT8 t)\r
+{\r
+ ANTLR3_FPRINTF(stderr, "Internal error - implementor of superclass containing ANTLR3_TREE_ADAPTOR did not implement setText()\n");\r
+}\r
+\r
+static pANTLR3_BASE_TREE \r
+ getChild (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE tree, ANTLR3_UINT32 i)\r
+{\r
+ ANTLR3_FPRINTF(stderr, "Internal error - implementor of superclass containing ANTLR3_TREE_ADAPTOR did not implement getChild()\n");\r
+ return NULL;\r
+}\r
+\r
+static ANTLR3_UINT32 \r
+ getChildCount (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE tree)\r
+{\r
+ ANTLR3_FPRINTF(stderr, "Internal error - implementor of superclass containing ANTLR3_TREE_ADAPTOR did not implement getChildCount()\n");\r
+ return 0;\r
+}\r
+\r
+/** Returns a uniqueID for the node. Because this is the C implementation\r
+ * we can just use its address suitably converted/cast to an integer.\r
+ */\r
+static ANTLR3_UINT32 \r
+ getUniqueID (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE node)\r
+{\r
+ return ANTLR3_UINT32_CAST(node);\r
+}\r
+\r
+static ANTLR3_BOOLEAN\r
+isNilNode (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t)\r
+{\r
+ return t->isNilNode(t);\r
+}\r