1 #include <antlr3basetree.h>
4 #pragma warning( disable : 4100 )
8 // Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC
9 // http://www.temporal-wave.com
10 // http://www.linkedin.com/in/jimidle
12 // All rights reserved.
14 // Redistribution and use in source and binary forms, with or without
15 // modification, are permitted provided that the following conditions
17 // 1. Redistributions of source code must retain the above copyright
18 // notice, this list of conditions and the following disclaimer.
19 // 2. Redistributions in binary form must reproduce the above copyright
20 // notice, this list of conditions and the following disclaimer in the
21 // documentation and/or other materials provided with the distribution.
22 // 3. The name of the author may not be used to endorse or promote products
23 // derived from this software without specific prior written permission.
25 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 static void * getChild (pANTLR3_BASE_TREE tree, ANTLR3_UINT32 i);
37 static ANTLR3_UINT32 getChildCount (pANTLR3_BASE_TREE tree);
38 static ANTLR3_UINT32 getCharPositionInLine
39 (pANTLR3_BASE_TREE tree);
40 static ANTLR3_UINT32 getLine (pANTLR3_BASE_TREE tree);
41 static pANTLR3_BASE_TREE
43 (pANTLR3_BASE_TREE tree, ANTLR3_UINT32 type);
44 static void addChild (pANTLR3_BASE_TREE tree, pANTLR3_BASE_TREE child);
45 static void addChildren (pANTLR3_BASE_TREE tree, pANTLR3_LIST kids);
46 static void replaceChildren (pANTLR3_BASE_TREE parent, ANTLR3_INT32 startChildIndex, ANTLR3_INT32 stopChildIndex, pANTLR3_BASE_TREE t);
48 static void freshenPACIndexesAll(pANTLR3_BASE_TREE tree);
49 static void freshenPACIndexes (pANTLR3_BASE_TREE tree, ANTLR3_UINT32 offset);
51 static void setChild (pANTLR3_BASE_TREE tree, ANTLR3_UINT32 i, void * child);
52 static void * deleteChild (pANTLR3_BASE_TREE tree, ANTLR3_UINT32 i);
53 static void * dupTree (pANTLR3_BASE_TREE tree);
54 static pANTLR3_STRING toStringTree (pANTLR3_BASE_TREE tree);
57 ANTLR3_API pANTLR3_BASE_TREE
58 antlr3BaseTreeNew(pANTLR3_BASE_TREE tree)
61 tree->getChild = getChild;
62 tree->getChildCount = getChildCount;
63 tree->addChild = (void (*)(pANTLR3_BASE_TREE, void *))(addChild);
64 tree->addChildren = addChildren;
65 tree->setChild = setChild;
66 tree->deleteChild = deleteChild;
67 tree->dupTree = dupTree;
68 tree->toStringTree = toStringTree;
69 tree->getCharPositionInLine = getCharPositionInLine;
70 tree->getLine = getLine;
71 tree->replaceChildren = replaceChildren;
72 tree->freshenPACIndexesAll = freshenPACIndexesAll;
73 tree->freshenPACIndexes = freshenPACIndexes;
74 tree->getFirstChildWithType = (void *(*)(pANTLR3_BASE_TREE, ANTLR3_UINT32))(getFirstChildWithType);
75 tree->children = NULL;
76 tree->strFactory = NULL;
78 /* Rest must be filled in by caller.
84 getCharPositionInLine (pANTLR3_BASE_TREE tree)
90 getLine (pANTLR3_BASE_TREE tree)
94 static pANTLR3_BASE_TREE
95 getFirstChildWithType (pANTLR3_BASE_TREE tree, ANTLR3_UINT32 type)
101 if (tree->children != NULL)
103 cs = tree->children->size(tree->children);
104 for (i = 0; i < cs; i++)
106 t = (pANTLR3_BASE_TREE) (tree->children->get(tree->children, i));
107 if (tree->getType(t) == type)
109 return (pANTLR3_BASE_TREE)t;
119 getChild (pANTLR3_BASE_TREE tree, ANTLR3_UINT32 i)
121 if ( tree->children == NULL
122 || i >= tree->children->size(tree->children))
126 return tree->children->get(tree->children, i);
131 getChildCount (pANTLR3_BASE_TREE tree)
133 if (tree->children == NULL)
139 return tree->children->size(tree->children);
144 addChild (pANTLR3_BASE_TREE tree, pANTLR3_BASE_TREE child)
154 if (child->isNilNode(child) == ANTLR3_TRUE)
156 if (child->children != NULL && child->children == tree->children)
158 // TODO: Change to exception rather than ANTLR3_FPRINTF?
160 ANTLR3_FPRINTF(stderr, "ANTLR3: An attempt was made to add a child list to itself!\n");
164 // Add all of the children's children to this list
166 if (child->children != NULL)
168 if (tree->children == NULL)
170 // We are build ing the tree structure here, so we need not
171 // worry about duplication of pointers as the tree node
172 // factory will only clean up each node once. So we just
173 // copy in the child's children pointer as the child is
174 // a nil node (has not root itself).
176 tree->children = child->children;
177 child->children = NULL;
178 freshenPACIndexesAll(tree);
183 // Need to copy the children
185 n = child->children->size(child->children);
187 for (i = 0; i < n; i++)
189 pANTLR3_BASE_TREE entry;
190 entry = child->children->get(child->children, i);
192 // ANTLR3 lists can be sparse, unlike Array Lists
196 tree->children->add(tree->children, entry, (void (ANTLR3_CDECL *) (void *))child->free);
204 // Tree we are adding is not a Nil and might have children to copy
206 if (tree->children == NULL)
208 // No children in the tree we are adding to, so create a new list on
209 // the fly to hold them.
211 tree->createChildrenList(tree);
214 tree->children->add(tree->children, child, (void (ANTLR3_CDECL *)(void *))child->free);
219 /// Add all elements of the supplied list as children of this node
222 addChildren (pANTLR3_BASE_TREE tree, pANTLR3_LIST kids)
227 s = kids->size(kids);
228 for (i = 0; i<s; i++)
230 tree->addChild(tree, (pANTLR3_BASE_TREE)(kids->get(kids, i+1)));
236 setChild (pANTLR3_BASE_TREE tree, ANTLR3_UINT32 i, void * child)
238 if (tree->children == NULL)
240 tree->createChildrenList(tree);
242 tree->children->set(tree->children, i, child, NULL, ANTLR3_FALSE);
246 deleteChild (pANTLR3_BASE_TREE tree, ANTLR3_UINT32 i)
248 if ( tree->children == NULL)
253 return tree->children->remove(tree->children, i);
257 dupTree (pANTLR3_BASE_TREE tree)
259 pANTLR3_BASE_TREE newTree;
263 newTree = tree->dupNode (tree);
265 if (tree->children != NULL)
267 s = tree->children->size (tree->children);
269 for (i = 0; i < s; i++)
272 pANTLR3_BASE_TREE newNode;
274 t = (pANTLR3_BASE_TREE) tree->children->get(tree->children, i);
278 newNode = t->dupTree(t);
279 newTree->addChild(newTree, newNode);
287 static pANTLR3_STRING
288 toStringTree (pANTLR3_BASE_TREE tree)
290 pANTLR3_STRING string;
295 if (tree->children == NULL || tree->children->size(tree->children) == 0)
297 return tree->toString(tree);
300 /* Need a new string with nothing at all in it.
302 string = tree->strFactory->newRaw(tree->strFactory);
304 if (tree->isNilNode(tree) == ANTLR3_FALSE)
306 string->append8 (string, "(");
307 string->appendS (string, tree->toString(tree));
308 string->append8 (string, " ");
310 if (tree->children != NULL)
312 n = tree->children->size(tree->children);
314 for (i = 0; i < n; i++)
316 t = (pANTLR3_BASE_TREE) tree->children->get(tree->children, i);
320 string->append8(string, " ");
322 string->appendS(string, t->toStringTree(t));
325 if (tree->isNilNode(tree) == ANTLR3_FALSE)
327 string->append8(string,")");
333 /// Delete children from start to stop and replace with t even if t is
334 /// a list (nil-root tree). Num of children can increase or decrease.
335 /// For huge child lists, inserting children can force walking rest of
336 /// children to set their child index; could be slow.
339 replaceChildren (pANTLR3_BASE_TREE parent, ANTLR3_INT32 startChildIndex, ANTLR3_INT32 stopChildIndex, pANTLR3_BASE_TREE newTree)
341 ANTLR3_INT32 replacingHowMany; // How many nodes will go away
342 ANTLR3_INT32 replacingWithHowMany; // How many nodes will replace them
343 ANTLR3_INT32 numNewChildren; // Tracking variable
344 ANTLR3_INT32 delta; // Difference in new vs existing count
349 pANTLR3_VECTOR newChildren; // Iterator for whatever we are going to add in
350 ANTLR3_BOOLEAN freeNewChildren; // Whether we created the iterator locally or reused it
352 if (parent->children == NULL)
354 ANTLR3_FPRINTF(stderr, "replaceChildren call: Indexes are invalid; no children in list for %s", parent->getText(parent)->chars);
358 // Either use the existing list of children in the supplied nil node, or build a vector of the
359 // tree we were given if it is not a nil node, then we treat both situations exactly the same
361 if (newTree->isNilNode(newTree))
363 newChildren = newTree->children;
364 freeNewChildren = ANTLR3_FALSE; // We must NO free this memory
368 newChildren = antlr3VectorNew(1);
369 if (newChildren == NULL)
371 ANTLR3_FPRINTF(stderr, "replaceChildren: out of memory!!");
374 newChildren->add(newChildren, (void *)newTree, NULL);
376 freeNewChildren = ANTLR3_TRUE; // We must free this memory
381 replacingHowMany = stopChildIndex - startChildIndex + 1;
382 replacingWithHowMany = newChildren->size(newChildren);
383 delta = replacingHowMany - replacingWithHowMany;
384 numNewChildren = newChildren->size(newChildren);
386 // If it is the same number of nodes, then do a direct replacement
390 pANTLR3_BASE_TREE child;
392 // Same number of nodes
395 for (i = startChildIndex; i <= stopChildIndex; i++)
397 child = (pANTLR3_BASE_TREE) newChildren->get(newChildren, j);
398 parent->children->set(parent->children, i, child, NULL, ANTLR3_FALSE);
399 child->setParent(child, parent);
400 child->setChildIndex(child, i);
405 ANTLR3_UINT32 indexToDelete;
407 // Less nodes than there were before
408 // reuse what we have then delete the rest
410 for (j = 0; j < numNewChildren; j++)
412 parent->children->set(parent->children, startChildIndex + j, newChildren->get(newChildren, j), NULL, ANTLR3_FALSE);
415 // We just delete the same index position until done
417 indexToDelete = startChildIndex + numNewChildren;
419 for (j = indexToDelete; j <= (ANTLR3_INT32)stopChildIndex; j++)
421 parent->children->remove(parent->children, indexToDelete);
424 parent->freshenPACIndexes(parent, startChildIndex);
428 ANTLR3_UINT32 numToInsert;
430 // More nodes than there were before
431 // Use what we can, then start adding
433 for (j = 0; j < replacingHowMany; j++)
435 parent->children->set(parent->children, startChildIndex + j, newChildren->get(newChildren, j), NULL, ANTLR3_FALSE);
438 numToInsert = replacingWithHowMany - replacingHowMany;
440 for (j = replacingHowMany; j < replacingWithHowMany; j++)
442 parent->children->add(parent->children, newChildren->get(newChildren, j), NULL);
445 parent->freshenPACIndexes(parent, startChildIndex);
448 if (freeNewChildren == ANTLR3_TRUE)
450 ANTLR3_FREE(newChildren->elements);
451 newChildren->elements = NULL;
452 newChildren->size = 0;
453 ANTLR3_FREE(newChildren); // Will not free the nodes
457 /// Set the parent and child indexes for all children of the
461 freshenPACIndexesAll(pANTLR3_BASE_TREE tree)
463 tree->freshenPACIndexes(tree, 0);
466 /// Set the parent and child indexes for some of the children of the
467 /// supplied tree, starting with the child at the supplied index.
470 freshenPACIndexes (pANTLR3_BASE_TREE tree, ANTLR3_UINT32 offset)
475 count = tree->getChildCount(tree); // How many children do we have
477 // Loop from the supplied index and set the indexes and parent
479 for (c = offset; c < count; c++)
481 pANTLR3_BASE_TREE child;
483 child = tree->getChild(tree, c);
485 child->setChildIndex(child, c);
486 child->setParent(child, tree);