Bug fix: closing the file
[codimension.git] / thirdparty / libantlr3c-3.2 / src / antlr3basetreeadaptor.c
blob150f135fe1a44fc8e2982c01d57cdfb4fb0b4438
1 /** \file
2 * Contains the base functions that all tree adaptors start with.
3 * this implementation can then be overridden by any higher implementation.
4 *
5 */
7 // [The "BSD licence"]
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
16 // are met:
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 #include <antlr3basetreeadaptor.h>
38 #ifdef ANTLR3_WINDOWS
39 #pragma warning( disable : 4100 )
40 #endif
42 /* Interface functions
44 static pANTLR3_BASE_TREE nilNode (pANTLR3_BASE_TREE_ADAPTOR adaptor);
45 static pANTLR3_BASE_TREE dbgNil (pANTLR3_BASE_TREE_ADAPTOR adaptor);
46 static pANTLR3_BASE_TREE dupTree (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t);
47 static pANTLR3_BASE_TREE dbgDupTree (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t);
48 static pANTLR3_BASE_TREE dupTreeTT (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t, pANTLR3_BASE_TREE parent);
49 static void addChild (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t, pANTLR3_BASE_TREE child);
50 static void dbgAddChild (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t, pANTLR3_BASE_TREE child);
51 static pANTLR3_BASE_TREE becomeRoot (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE newRoot, pANTLR3_BASE_TREE oldRoot);
52 static pANTLR3_BASE_TREE dbgBecomeRoot (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE newRoot, pANTLR3_BASE_TREE oldRoot);
53 static pANTLR3_BASE_TREE rulePostProcessing (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE root);
54 static void addChildToken (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t, pANTLR3_COMMON_TOKEN child);
55 static void dbgAddChildToken (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t, pANTLR3_COMMON_TOKEN child);
56 static pANTLR3_BASE_TREE becomeRootToken (pANTLR3_BASE_TREE_ADAPTOR adaptor, void * newRoot, pANTLR3_BASE_TREE oldRoot);
57 static pANTLR3_BASE_TREE dbgBecomeRootToken (pANTLR3_BASE_TREE_ADAPTOR adaptor, void * newRoot, pANTLR3_BASE_TREE oldRoot);
58 static pANTLR3_BASE_TREE createTypeToken (pANTLR3_BASE_TREE_ADAPTOR adaptor, ANTLR3_UINT32 tokenType, pANTLR3_COMMON_TOKEN fromToken);
59 static pANTLR3_BASE_TREE dbgCreateTypeToken (pANTLR3_BASE_TREE_ADAPTOR adaptor, ANTLR3_UINT32 tokenType, pANTLR3_COMMON_TOKEN fromToken);
60 static pANTLR3_BASE_TREE createTypeTokenText (pANTLR3_BASE_TREE_ADAPTOR adaptor, ANTLR3_UINT32 tokenType, pANTLR3_COMMON_TOKEN fromToken, pANTLR3_UINT8 text);
61 static pANTLR3_BASE_TREE dbgCreateTypeTokenText (pANTLR3_BASE_TREE_ADAPTOR adaptor, ANTLR3_UINT32 tokenType, pANTLR3_COMMON_TOKEN fromToken, pANTLR3_UINT8 text);
62 static pANTLR3_BASE_TREE createTypeText (pANTLR3_BASE_TREE_ADAPTOR adaptor, ANTLR3_UINT32 tokenType, pANTLR3_UINT8 text);
63 static pANTLR3_BASE_TREE dbgCreateTypeText (pANTLR3_BASE_TREE_ADAPTOR adaptor, ANTLR3_UINT32 tokenType, pANTLR3_UINT8 text);
64 static ANTLR3_UINT32 getType (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t);
65 static void setType (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t, ANTLR3_UINT32 type);
66 static pANTLR3_STRING getText (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t);
67 static void setText (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_STRING t);
68 static void setText8 (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_UINT8 t);
69 static pANTLR3_BASE_TREE getChild (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t, ANTLR3_UINT32 i);
70 static ANTLR3_UINT32 getChildCount (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t);
71 static ANTLR3_UINT32 getUniqueID (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t);
72 static ANTLR3_BOOLEAN isNilNode (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t);
73 static pANTLR3_STRING makeDot (pANTLR3_BASE_TREE_ADAPTOR adaptor, void * theTree);
75 /** Given a pointer to a base tree adaptor structure (which is usually embedded in the
76 * super class the implements the tree adaptor used in the parse), initialize its
77 * function pointers and so on.
79 ANTLR3_API void
80 antlr3BaseTreeAdaptorInit(pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_DEBUG_EVENT_LISTENER debugger)
82 // Initialize the interface
84 if (debugger == NULL)
86 adaptor->nilNode = (void * (*)(pANTLR3_BASE_TREE_ADAPTOR))
87 nilNode;
88 adaptor->addChild = (void (*)(pANTLR3_BASE_TREE_ADAPTOR, void *, void *))
89 addChild;
90 adaptor->becomeRoot = (void * (*)(pANTLR3_BASE_TREE_ADAPTOR, void *, void *))
91 becomeRoot;
92 adaptor->addChildToken = (void (*)(pANTLR3_BASE_TREE_ADAPTOR, void *, pANTLR3_COMMON_TOKEN))
93 addChildToken;
94 adaptor->becomeRootToken = (void * (*)(pANTLR3_BASE_TREE_ADAPTOR, void *, void *))
95 becomeRootToken;
96 adaptor->createTypeToken = (void * (*)(pANTLR3_BASE_TREE_ADAPTOR, ANTLR3_UINT32, pANTLR3_COMMON_TOKEN))
97 createTypeToken;
98 adaptor->createTypeTokenText = (void * (*)(pANTLR3_BASE_TREE_ADAPTOR, ANTLR3_UINT32, pANTLR3_COMMON_TOKEN, pANTLR3_UINT8))
99 createTypeTokenText;
100 adaptor->createTypeText = (void * (*)(pANTLR3_BASE_TREE_ADAPTOR, ANTLR3_UINT32, pANTLR3_UINT8))
101 createTypeText;
102 adaptor->dupTree = (void * (*)(pANTLR3_BASE_TREE_ADAPTOR, void *))
103 dupTree;
105 else
107 adaptor->nilNode = (void * (*)(pANTLR3_BASE_TREE_ADAPTOR))
108 dbgNil;
109 adaptor->addChild = (void (*)(pANTLR3_BASE_TREE_ADAPTOR, void *, void *))
110 dbgAddChild;
111 adaptor->becomeRoot = (void * (*)(pANTLR3_BASE_TREE_ADAPTOR, void *, void *))
112 dbgBecomeRoot;
113 adaptor->addChildToken = (void (*)(pANTLR3_BASE_TREE_ADAPTOR, void *, pANTLR3_COMMON_TOKEN))
114 dbgAddChildToken;
115 adaptor->becomeRootToken = (void * (*)(pANTLR3_BASE_TREE_ADAPTOR, void *, void *))
116 dbgBecomeRootToken;
117 adaptor->createTypeToken = (void * (*)(pANTLR3_BASE_TREE_ADAPTOR, ANTLR3_UINT32, pANTLR3_COMMON_TOKEN))
118 dbgCreateTypeToken;
119 adaptor->createTypeTokenText = (void * (*)(pANTLR3_BASE_TREE_ADAPTOR, ANTLR3_UINT32, pANTLR3_COMMON_TOKEN, pANTLR3_UINT8))
120 dbgCreateTypeTokenText;
121 adaptor->createTypeText = (void * (*)(pANTLR3_BASE_TREE_ADAPTOR, ANTLR3_UINT32, pANTLR3_UINT8))
122 dbgCreateTypeText;
123 adaptor->dupTree = (void * (*)(pANTLR3_BASE_TREE_ADAPTOR, void *))
124 dbgDupTree;
125 debugger->adaptor = adaptor;
128 adaptor->dupTreeTT = (void * (*)(pANTLR3_BASE_TREE_ADAPTOR, void *, void *))
129 dupTreeTT;
130 adaptor->rulePostProcessing = (void * (*)(pANTLR3_BASE_TREE_ADAPTOR, void *))
131 rulePostProcessing;
132 adaptor->getType = (ANTLR3_UINT32 (*)(pANTLR3_BASE_TREE_ADAPTOR, void *))
133 getType;
134 adaptor->setType = (void (*)(pANTLR3_BASE_TREE_ADAPTOR, void *, ANTLR3_UINT32))
135 setType;
136 adaptor->getText = (pANTLR3_STRING (*) (pANTLR3_BASE_TREE_ADAPTOR, void *))
137 getText;
138 adaptor->setText8 = (void (*)(pANTLR3_BASE_TREE_ADAPTOR, pANTLR3_UINT8))
139 setText8;
140 adaptor->setText = (void (*)(pANTLR3_BASE_TREE_ADAPTOR, pANTLR3_STRING))
141 setText;
142 adaptor->getChild = (void * (*)(pANTLR3_BASE_TREE_ADAPTOR, void *, ANTLR3_UINT32))
143 getChild;
144 adaptor->getChildCount = (ANTLR3_UINT32 (*)(pANTLR3_BASE_TREE_ADAPTOR, void *))
145 getChildCount;
146 adaptor->getUniqueID = (ANTLR3_UINT32 (*)(pANTLR3_BASE_TREE_ADAPTOR, void *))
147 getUniqueID;
148 adaptor->isNilNode = (ANTLR3_BOOLEAN (*)(pANTLR3_BASE_TREE_ADAPTOR, void *))
149 isNilNode;
151 adaptor->makeDot = (pANTLR3_STRING (*)(pANTLR3_BASE_TREE_ADAPTOR, void *))
152 makeDot;
154 /* Remaining functions filled in by the caller.
156 return;
159 static void
160 defineDotNodes(pANTLR3_BASE_TREE_ADAPTOR adaptor, void * t, pANTLR3_STRING dotSpec )
162 // How many nodes are we talking about?
164 int nCount;
165 int i;
166 pANTLR3_BASE_TREE child;
167 char buff[64];
168 pANTLR3_STRING text;
169 int j;
175 // Count the nodes
177 nCount = adaptor->getChildCount(adaptor, t);
179 if (nCount == 0)
181 // This will already have been included as a child of another node
182 // so there is nothing to add.
184 return;
187 // For each child of the current tree, define a node using the
188 // memory address of the node to name it
190 for (i = 0; i<nCount; i++)
193 // Pick up a pointer for the child
195 child = adaptor->getChild(adaptor, t, i);
197 // Name the node
199 sprintf(buff, "\tn%p[label=\"", child);
200 dotSpec->append8(dotSpec, buff);
201 text = adaptor->getText(adaptor, child);
202 for (j = 0; j < (ANTLR3_INT32)(text->len); j++)
204 switch(text->charAt(text, j))
206 case '"':
208 dotSpec->append8(dotSpec, "\\\"");
209 break;
211 case '\n':
213 dotSpec->append8(dotSpec, "\\n");
214 break;
216 case '\r':
218 dotSpec->append8(dotSpec, "\\r");
219 break;
221 default:
223 dotSpec->addc(dotSpec, text->charAt(text, j));
224 break;
227 dotSpec->append8(dotSpec, "\"]\n");
229 // And now define the children of this child (if any)
231 defineDotNodes(adaptor, child, dotSpec);
234 // Done
236 return;
239 static void
240 defineDotEdges(pANTLR3_BASE_TREE_ADAPTOR adaptor, void * t, pANTLR3_STRING dotSpec)
242 // How many nodes are we talking about?
244 int nCount;
245 int i;
247 if (t == NULL)
249 // No tree, so do nothing
251 return;
254 // Count the nodes
256 nCount = adaptor->getChildCount(adaptor, t);
258 if (nCount == 0)
260 // This will already have been included as a child of another node
261 // so there is nothing to add.
263 return;
266 // For each child, define an edge from this parent, then process
267 // and children of this child in the same way
269 for (i=0; i<nCount; i++)
271 pANTLR3_BASE_TREE child;
272 char buff[128];
273 pANTLR3_STRING text;
274 int j;
276 // Next child
278 child = adaptor->getChild(adaptor, t, i);
280 // Create the edge relation
282 sprintf(buff, "\t\tn%p -> n%p\t\t// ", t, child);
284 dotSpec->append8(dotSpec, buff);
286 // Document the relationship
288 text = adaptor->getText(adaptor, t);
289 for (j = 0; j < (ANTLR3_INT32)(text->len); j++)
291 switch(text->charAt(text, j))
293 case '"':
295 dotSpec->append8(dotSpec, "\\\"");
296 break;
298 case '\n':
300 dotSpec->append8(dotSpec, "\\n");
301 break;
303 case '\r':
305 dotSpec->append8(dotSpec, "\\r");
306 break;
308 default:
310 dotSpec->addc(dotSpec, text->charAt(text, j));
311 break;
315 dotSpec->append8(dotSpec, " -> ");
317 text = adaptor->getText(adaptor, child);
318 for (j = 0; j < (ANTLR3_INT32)(text->len); j++)
320 switch(text->charAt(text, j))
322 case '"':
324 dotSpec->append8(dotSpec, "\\\"");
325 break;
327 case '\n':
329 dotSpec->append8(dotSpec, "\\n");
330 break;
332 case '\r':
334 dotSpec->append8(dotSpec, "\\r");
335 break;
337 default:
339 dotSpec->addc(dotSpec, text->charAt(text, j));
340 break;
343 dotSpec->append8(dotSpec, "\n");
346 // Define edges for this child
348 defineDotEdges(adaptor, child, dotSpec);
351 // Done
353 return;
356 /// Produce a DOT specification for graphviz
358 static pANTLR3_STRING
359 makeDot (pANTLR3_BASE_TREE_ADAPTOR adaptor, void * theTree)
361 // The string we are building up
363 pANTLR3_STRING dotSpec;
364 char buff[64];
365 pANTLR3_STRING text;
366 int j;
368 dotSpec = adaptor->strFactory->newStr8
371 adaptor->strFactory,
373 // Default look and feel
375 (pANTLR3_UINT8)
376 "digraph {\n\n"
377 "\tordering=out;\n"
378 "\tranksep=.4;\n"
379 "\tbgcolor=\"lightgrey\"; node [shape=box, fixedsize=false, fontsize=12, fontname=\"Helvetica-bold\", fontcolor=\"blue\"\n"
380 "\twidth=.25, height=.25, color=\"black\", fillcolor=\"white\", style=\"filled, solid, bold\"];\n\n"
381 "\tedge [arrowsize=.5, color=\"black\", style=\"bold\"]\n\n"
384 if (theTree == NULL)
386 // No tree, so create a blank spec
388 dotSpec->append8(dotSpec, "n0[label=\"EMPTY TREE\"]\n");
389 return dotSpec;
392 sprintf(buff, "\tn%p[label=\"", theTree);
393 dotSpec->append8(dotSpec, buff);
394 text = adaptor->getText(adaptor, theTree);
395 for (j = 0; j < (ANTLR3_INT32)(text->len); j++)
397 switch(text->charAt(text, j))
399 case '"':
401 dotSpec->append8(dotSpec, "\\\"");
402 break;
404 case '\n':
406 dotSpec->append8(dotSpec, "\\n");
407 break;
409 case '\r':
411 dotSpec->append8(dotSpec, "\\r");
412 break;
414 default:
416 dotSpec->addc(dotSpec, text->charAt(text, j));
417 break;
420 dotSpec->append8(dotSpec, "\"]\n");
422 // First produce the node defintions
424 defineDotNodes(adaptor, theTree, dotSpec);
425 dotSpec->append8(dotSpec, "\n");
426 defineDotEdges(adaptor, theTree, dotSpec);
428 // Terminate the spec
430 dotSpec->append8(dotSpec, "\n}");
432 // Result
434 return dotSpec;
438 /** Create and return a nil tree node (no token payload)
440 static pANTLR3_BASE_TREE
441 nilNode (pANTLR3_BASE_TREE_ADAPTOR adaptor)
443 return adaptor->create(adaptor, NULL);
446 static pANTLR3_BASE_TREE
447 dbgNil (pANTLR3_BASE_TREE_ADAPTOR adaptor)
449 pANTLR3_BASE_TREE t;
451 t = adaptor->create (adaptor, NULL);
452 adaptor->debugger->createNode (adaptor->debugger, t);
454 return t;
457 /** Return a duplicate of the entire tree (implementation provided by the
458 * BASE_TREE interface.)
460 static pANTLR3_BASE_TREE
461 dupTree (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t)
463 return adaptor->dupTreeTT(adaptor, t, NULL);
466 pANTLR3_BASE_TREE
467 dupTreeTT (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t, pANTLR3_BASE_TREE parent)
469 pANTLR3_BASE_TREE newTree;
470 pANTLR3_BASE_TREE child;
471 pANTLR3_BASE_TREE newSubTree;
472 ANTLR3_UINT32 n;
473 ANTLR3_UINT32 i;
475 if (t == NULL)
477 return NULL;
479 newTree = t->dupNode(t);
481 // Ensure new subtree root has parent/child index set
483 adaptor->setChildIndex (adaptor, newTree, t->getChildIndex(t));
484 adaptor->setParent (adaptor, newTree, parent);
485 n = adaptor->getChildCount (adaptor, t);
487 for (i=0; i < n; i++)
489 child = adaptor->getChild (adaptor, t, i);
490 newSubTree = adaptor->dupTreeTT (adaptor, child, t);
491 adaptor->addChild (adaptor, newTree, newSubTree);
493 return newTree;
496 /// Sends the required debugging events for duplicating a tree
497 /// to the debugger.
499 static void
500 simulateTreeConstruction(pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE tree)
502 ANTLR3_UINT32 n;
503 ANTLR3_UINT32 i;
504 pANTLR3_BASE_TREE child;
506 // Send the create node event
508 adaptor->debugger->createNode(adaptor->debugger, tree);
510 n = adaptor->getChildCount(adaptor, tree);
511 for (i = 0; i < n; i++)
513 child = adaptor->getChild(adaptor, tree, i);
514 simulateTreeConstruction(adaptor, child);
515 adaptor->debugger->addChild(adaptor->debugger, tree, child);
519 pANTLR3_BASE_TREE
520 dbgDupTree (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE tree)
522 pANTLR3_BASE_TREE t;
524 // Call the normal dup tree mechanism first
526 t = adaptor->dupTreeTT(adaptor, tree, NULL);
528 // In order to tell the debugger what we have just done, we now
529 // simulate the tree building mechanism. THis will fire
530 // lots of debugging events to the client and look like we
531 // duped the tree..
533 simulateTreeConstruction(adaptor, t);
535 return t;
538 /** Add a child to the tree t. If child is a flat tree (a list), make all
539 * in list children of t. Warning: if t has no children, but child does
540 * and child isNilNode then it is ok to move children to t via
541 * t.children = child.children; i.e., without copying the array. This
542 * is for construction and I'm not sure it's completely general for
543 * a tree's addChild method to work this way. Make sure you differentiate
544 * between your tree's addChild and this parser tree construction addChild
545 * if it's not ok to move children to t with a simple assignment.
547 static void
548 addChild (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t, pANTLR3_BASE_TREE child)
550 if (t != NULL && child != NULL)
552 t->addChild(t, child);
555 static void
556 dbgAddChild (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t, pANTLR3_BASE_TREE child)
558 if (t != NULL && child != NULL)
560 t->addChild(t, child);
561 adaptor->debugger->addChild(adaptor->debugger, t, child);
564 /** Use the adaptor implementation to add a child node with the supplied token
566 static void
567 addChildToken (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t, pANTLR3_COMMON_TOKEN child)
569 if (t != NULL && child != NULL)
571 adaptor->addChild(adaptor, t, adaptor->create(adaptor, child));
574 static void
575 dbgAddChildToken (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t, pANTLR3_COMMON_TOKEN child)
577 pANTLR3_BASE_TREE tc;
579 if (t != NULL && child != NULL)
581 tc = adaptor->create(adaptor, child);
582 adaptor->addChild(adaptor, t, tc);
583 adaptor->debugger->addChild(adaptor->debugger, t, tc);
587 /** If oldRoot is a nil root, just copy or move the children to newRoot.
588 * If not a nil root, make oldRoot a child of newRoot.
590 * \code
591 * old=^(nil a b c), new=r yields ^(r a b c)
592 * old=^(a b c), new=r yields ^(r ^(a b c))
593 * \endcode
595 * If newRoot is a nil-rooted single child tree, use the single
596 * child as the new root node.
598 * \code
599 * old=^(nil a b c), new=^(nil r) yields ^(r a b c)
600 * old=^(a b c), new=^(nil r) yields ^(r ^(a b c))
601 * \endcode
603 * If oldRoot was null, it's ok, just return newRoot (even if isNilNode).
605 * \code
606 * old=null, new=r yields r
607 * old=null, new=^(nil r) yields ^(nil r)
608 * \endcode
610 * Return newRoot. Throw an exception if newRoot is not a
611 * simple node or nil root with a single child node--it must be a root
612 * node. If newRoot is <code>^(nil x)</endcode> return x as newRoot.
614 * Be advised that it's ok for newRoot to point at oldRoot's
615 * children; i.e., you don't have to copy the list. We are
616 * constructing these nodes so we should have this control for
617 * efficiency.
619 static pANTLR3_BASE_TREE
620 becomeRoot (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE newRootTree, pANTLR3_BASE_TREE oldRootTree)
622 pANTLR3_BASE_TREE saveRoot;
624 /* Protect against tree rewrites if we are in some sort of error
625 * state, but have tried to recover. In C we can end up with a null pointer
626 * for a tree that was not produced.
628 if (newRootTree == NULL)
630 return oldRootTree;
633 /* root is just the new tree as is if there is no
634 * current root tree.
636 if (oldRootTree == NULL)
638 return newRootTree;
641 /* Produce ^(nil real-node)
643 if (newRootTree->isNilNode(newRootTree))
645 if (getBaseTreeChildCount(newRootTree) > 1)
647 /* TODO: Handle tree exceptions
649 ANTLR3_FPRINTF(stderr, "More than one node as root! TODO: Create tree exception handling\n");
650 return newRootTree;
653 /* The new root is the first child, keep track of the original newRoot
654 * because if it was a Nil Node, then we can reuse it now.
656 saveRoot = newRootTree;
657 newRootTree = getBaseTreeChild(newRootTree, 0);
659 // Reclaim the old nilNode()
661 saveRoot->reuse(saveRoot);
664 /* Add old root into new root. addChild takes care of the case where oldRoot
665 * is a flat list (nill rooted tree). All children of oldroot are added to
666 * new root.
668 newRootTree->addChild(newRootTree, oldRootTree);
670 // If the oldroot tree was a nil node, then we know at this point
671 // it has become orphaned by the rewrite logic, so we tell it to do
672 // whatever it needs to do to be reused.
674 if (oldRootTree->isNilNode(oldRootTree))
676 // We have taken an old Root Tree and appended all its children to the new
677 // root. In addition though it was a nil node, which means the generated code
678 // will not reuse it again, so we will reclaim it here. First we want to zero out
679 // any pointers it was carrying around. We are just the baseTree handler so we
680 // don't know necessarilly know how to do this for the real node, we just ask the tree itself
681 // to do it.
683 oldRootTree->reuse(oldRootTree);
685 /* Always returns new root structure
687 return newRootTree;
690 static pANTLR3_BASE_TREE
691 dbgBecomeRoot (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE newRootTree, pANTLR3_BASE_TREE oldRootTree)
693 pANTLR3_BASE_TREE t;
695 t = becomeRoot(adaptor, newRootTree, oldRootTree);
697 adaptor->debugger->becomeRoot(adaptor->debugger, newRootTree, oldRootTree);
699 return t;
701 /** Transform ^(nil x) to x
703 static pANTLR3_BASE_TREE
704 rulePostProcessing (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE root)
706 pANTLR3_BASE_TREE saveRoot;
708 // Keep track of the root we are given. If it is a nilNode, then we
709 // can reuse it rather than orphaning it!
711 saveRoot = root;
713 if (root != NULL && root->isNilNode(root))
715 if (getBaseTreeChildCount(root) == 0)
717 root = NULL;
719 else if (getBaseTreeChildCount(root) == 1)
721 root = getBaseTreeChild(root, 0);
722 root->setParent(root, NULL);
723 root->setChildIndex(root, -1);
725 // The root we were given was a nil node, wiht one child, which means it has
726 // been abandoned and would be lost in the node factory. However
727 // nodes can be flagged as resuable to prevent this terrible waste
729 saveRoot->reuse(saveRoot);
733 return root;
736 /** Use the adaptor interface to set a new tree node with the supplied token
737 * to the root of the tree.
739 static pANTLR3_BASE_TREE
740 becomeRootToken (pANTLR3_BASE_TREE_ADAPTOR adaptor, void * newRoot, pANTLR3_BASE_TREE oldRoot)
742 return adaptor->becomeRoot(adaptor, adaptor->create(adaptor, newRoot), oldRoot);
744 static pANTLR3_BASE_TREE
745 dbgBecomeRootToken (pANTLR3_BASE_TREE_ADAPTOR adaptor, void * newRoot, pANTLR3_BASE_TREE oldRoot)
747 pANTLR3_BASE_TREE t;
749 t = adaptor->becomeRoot(adaptor, adaptor->create(adaptor, newRoot), oldRoot);
751 adaptor->debugger->becomeRoot(adaptor->debugger,t, oldRoot);
753 return t;
756 /** Use the super class supplied create() method to create a new node
757 * from the supplied token.
759 static pANTLR3_BASE_TREE
760 createTypeToken (pANTLR3_BASE_TREE_ADAPTOR adaptor, ANTLR3_UINT32 tokenType, pANTLR3_COMMON_TOKEN fromToken)
762 /* Create the new token
764 fromToken = adaptor->createTokenFromToken(adaptor, fromToken);
766 /* Set the type of the new token to that supplied
768 fromToken->type = tokenType;
770 /* Return a new node based upon this token
772 return adaptor->create(adaptor, fromToken);
774 static pANTLR3_BASE_TREE
775 dbgCreateTypeToken (pANTLR3_BASE_TREE_ADAPTOR adaptor, ANTLR3_UINT32 tokenType, pANTLR3_COMMON_TOKEN fromToken)
777 pANTLR3_BASE_TREE t;
779 t = createTypeToken(adaptor, tokenType, fromToken);
781 adaptor->debugger->createNode(adaptor->debugger, t);
783 return t;
786 static pANTLR3_BASE_TREE
787 createTypeTokenText (pANTLR3_BASE_TREE_ADAPTOR adaptor, ANTLR3_UINT32 tokenType, pANTLR3_COMMON_TOKEN fromToken, pANTLR3_UINT8 text)
789 /* Create the new token
791 fromToken = adaptor->createTokenFromToken(adaptor, fromToken);
793 /* Set the type of the new token to that supplied
795 fromToken->type = tokenType;
797 /* Set the text of the token accordingly
799 fromToken->setText8(fromToken, text);
801 /* Return a new node based upon this token
803 return adaptor->create(adaptor, fromToken);
805 static pANTLR3_BASE_TREE
806 dbgCreateTypeTokenText (pANTLR3_BASE_TREE_ADAPTOR adaptor, ANTLR3_UINT32 tokenType, pANTLR3_COMMON_TOKEN fromToken, pANTLR3_UINT8 text)
808 pANTLR3_BASE_TREE t;
810 t = createTypeTokenText(adaptor, tokenType, fromToken, text);
812 adaptor->debugger->createNode(adaptor->debugger, t);
814 return t;
817 static pANTLR3_BASE_TREE
818 createTypeText (pANTLR3_BASE_TREE_ADAPTOR adaptor, ANTLR3_UINT32 tokenType, pANTLR3_UINT8 text)
820 pANTLR3_COMMON_TOKEN fromToken;
822 /* Create the new token
824 fromToken = adaptor->createToken(adaptor, tokenType, text);
826 /* Return a new node based upon this token
828 return adaptor->create(adaptor, fromToken);
830 static pANTLR3_BASE_TREE
831 dbgCreateTypeText (pANTLR3_BASE_TREE_ADAPTOR adaptor, ANTLR3_UINT32 tokenType, pANTLR3_UINT8 text)
833 pANTLR3_BASE_TREE t;
835 t = createTypeText(adaptor, tokenType, text);
837 adaptor->debugger->createNode(adaptor->debugger, t);
839 return t;
842 /** Dummy implementation - will be supplied by super class
844 static ANTLR3_UINT32
845 getType (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t)
847 return 0;
850 /** Dummy implementation - will be supplied by super class
852 static void
853 setType (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t, ANTLR3_UINT32 type)
855 ANTLR3_FPRINTF(stderr, "Internal error - implementor of superclass containing ANTLR3_TREE_ADAPTOR did not implement setType()\n");
858 /** Dummy implementation - will be supplied by super class
860 static pANTLR3_STRING
861 getText (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t)
863 ANTLR3_FPRINTF(stderr, "Internal error - implementor of superclass containing ANTLR3_TREE_ADAPTOR did not implement getText()\n");
864 return NULL;
867 /** Dummy implementation - will be supplied by super class
869 static void
870 setText (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_STRING t)
872 ANTLR3_FPRINTF(stderr, "Internal error - implementor of superclass containing ANTLR3_TREE_ADAPTOR did not implement setText()\n");
874 /** Dummy implementation - will be supplied by super class
876 static void
877 setText8 (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_UINT8 t)
879 ANTLR3_FPRINTF(stderr, "Internal error - implementor of superclass containing ANTLR3_TREE_ADAPTOR did not implement setText()\n");
882 static pANTLR3_BASE_TREE
883 getChild (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE tree, ANTLR3_UINT32 i)
885 ANTLR3_FPRINTF(stderr, "Internal error - implementor of superclass containing ANTLR3_TREE_ADAPTOR did not implement getChild()\n");
886 return NULL;
889 static ANTLR3_UINT32
890 getChildCount (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE tree)
892 ANTLR3_FPRINTF(stderr, "Internal error - implementor of superclass containing ANTLR3_TREE_ADAPTOR did not implement getChildCount()\n");
893 return 0;
896 /** Returns a uniqueID for the node. Because this is the C implementation
897 * we can just use its address suitably converted/cast to an integer.
899 static ANTLR3_UINT32
900 getUniqueID (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE node)
902 return ANTLR3_UINT32_CAST(node);
905 static ANTLR3_BOOLEAN
906 isNilNode (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t)
908 return t->isNilNode(t);