2 * Contains the base functions that all tree adaptors start with.
3 * this implementation can then be overridden by any higher implementation.
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 #include <antlr3basetreeadaptor.h>
39 #pragma warning( disable : 4100 )
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.
80 antlr3BaseTreeAdaptorInit(pANTLR3_BASE_TREE_ADAPTOR adaptor
, pANTLR3_DEBUG_EVENT_LISTENER debugger
)
82 // Initialize the interface
86 adaptor
->nilNode
= (void * (*)(pANTLR3_BASE_TREE_ADAPTOR
))
88 adaptor
->addChild
= (void (*)(pANTLR3_BASE_TREE_ADAPTOR
, void *, void *))
90 adaptor
->becomeRoot
= (void * (*)(pANTLR3_BASE_TREE_ADAPTOR
, void *, void *))
92 adaptor
->addChildToken
= (void (*)(pANTLR3_BASE_TREE_ADAPTOR
, void *, pANTLR3_COMMON_TOKEN
))
94 adaptor
->becomeRootToken
= (void * (*)(pANTLR3_BASE_TREE_ADAPTOR
, void *, void *))
96 adaptor
->createTypeToken
= (void * (*)(pANTLR3_BASE_TREE_ADAPTOR
, ANTLR3_UINT32
, pANTLR3_COMMON_TOKEN
))
98 adaptor
->createTypeTokenText
= (void * (*)(pANTLR3_BASE_TREE_ADAPTOR
, ANTLR3_UINT32
, pANTLR3_COMMON_TOKEN
, pANTLR3_UINT8
))
100 adaptor
->createTypeText
= (void * (*)(pANTLR3_BASE_TREE_ADAPTOR
, ANTLR3_UINT32
, pANTLR3_UINT8
))
102 adaptor
->dupTree
= (void * (*)(pANTLR3_BASE_TREE_ADAPTOR
, void *))
107 adaptor
->nilNode
= (void * (*)(pANTLR3_BASE_TREE_ADAPTOR
))
109 adaptor
->addChild
= (void (*)(pANTLR3_BASE_TREE_ADAPTOR
, void *, void *))
111 adaptor
->becomeRoot
= (void * (*)(pANTLR3_BASE_TREE_ADAPTOR
, void *, void *))
113 adaptor
->addChildToken
= (void (*)(pANTLR3_BASE_TREE_ADAPTOR
, void *, pANTLR3_COMMON_TOKEN
))
115 adaptor
->becomeRootToken
= (void * (*)(pANTLR3_BASE_TREE_ADAPTOR
, void *, void *))
117 adaptor
->createTypeToken
= (void * (*)(pANTLR3_BASE_TREE_ADAPTOR
, ANTLR3_UINT32
, pANTLR3_COMMON_TOKEN
))
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
))
123 adaptor
->dupTree
= (void * (*)(pANTLR3_BASE_TREE_ADAPTOR
, void *))
125 debugger
->adaptor
= adaptor
;
128 adaptor
->dupTreeTT
= (void * (*)(pANTLR3_BASE_TREE_ADAPTOR
, void *, void *))
130 adaptor
->rulePostProcessing
= (void * (*)(pANTLR3_BASE_TREE_ADAPTOR
, void *))
132 adaptor
->getType
= (ANTLR3_UINT32 (*)(pANTLR3_BASE_TREE_ADAPTOR
, void *))
134 adaptor
->setType
= (void (*)(pANTLR3_BASE_TREE_ADAPTOR
, void *, ANTLR3_UINT32
))
136 adaptor
->getText
= (pANTLR3_STRING (*) (pANTLR3_BASE_TREE_ADAPTOR
, void *))
138 adaptor
->setText8
= (void (*)(pANTLR3_BASE_TREE_ADAPTOR
, pANTLR3_UINT8
))
140 adaptor
->setText
= (void (*)(pANTLR3_BASE_TREE_ADAPTOR
, pANTLR3_STRING
))
142 adaptor
->getChild
= (void * (*)(pANTLR3_BASE_TREE_ADAPTOR
, void *, ANTLR3_UINT32
))
144 adaptor
->getChildCount
= (ANTLR3_UINT32 (*)(pANTLR3_BASE_TREE_ADAPTOR
, void *))
146 adaptor
->getUniqueID
= (ANTLR3_UINT32 (*)(pANTLR3_BASE_TREE_ADAPTOR
, void *))
148 adaptor
->isNilNode
= (ANTLR3_BOOLEAN (*)(pANTLR3_BASE_TREE_ADAPTOR
, void *))
151 adaptor
->makeDot
= (pANTLR3_STRING (*)(pANTLR3_BASE_TREE_ADAPTOR
, void *))
154 /* Remaining functions filled in by the caller.
160 defineDotNodes(pANTLR3_BASE_TREE_ADAPTOR adaptor
, void * t
, pANTLR3_STRING dotSpec
)
162 // How many nodes are we talking about?
166 pANTLR3_BASE_TREE child
;
177 nCount
= adaptor
->getChildCount(adaptor
, t
);
181 // This will already have been included as a child of another node
182 // so there is nothing to add.
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
);
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
))
208 dotSpec
->append8(dotSpec
, "\\\"");
213 dotSpec
->append8(dotSpec
, "\\n");
218 dotSpec
->append8(dotSpec
, "\\r");
223 dotSpec
->addc(dotSpec
, text
->charAt(text
, j
));
227 dotSpec
->append8(dotSpec
, "\"]\n");
229 // And now define the children of this child (if any)
231 defineDotNodes(adaptor
, child
, dotSpec
);
240 defineDotEdges(pANTLR3_BASE_TREE_ADAPTOR adaptor
, void * t
, pANTLR3_STRING dotSpec
)
242 // How many nodes are we talking about?
249 // No tree, so do nothing
256 nCount
= adaptor
->getChildCount(adaptor
, t
);
260 // This will already have been included as a child of another node
261 // so there is nothing to add.
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
;
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
))
295 dotSpec
->append8(dotSpec
, "\\\"");
300 dotSpec
->append8(dotSpec
, "\\n");
305 dotSpec
->append8(dotSpec
, "\\r");
310 dotSpec
->addc(dotSpec
, text
->charAt(text
, j
));
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
))
324 dotSpec
->append8(dotSpec
, "\\\"");
329 dotSpec
->append8(dotSpec
, "\\n");
334 dotSpec
->append8(dotSpec
, "\\r");
339 dotSpec
->addc(dotSpec
, text
->charAt(text
, j
));
343 dotSpec
->append8(dotSpec
, "\n");
346 // Define edges for this child
348 defineDotEdges(adaptor
, child
, dotSpec
);
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
;
368 dotSpec
= adaptor
->strFactory
->newStr8
373 // Default look and feel
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"
386 // No tree, so create a blank spec
388 dotSpec
->append8(dotSpec
, "n0[label=\"EMPTY TREE\"]\n");
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
))
401 dotSpec
->append8(dotSpec
, "\\\"");
406 dotSpec
->append8(dotSpec
, "\\n");
411 dotSpec
->append8(dotSpec
, "\\r");
416 dotSpec
->addc(dotSpec
, text
->charAt(text
, j
));
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}");
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
)
451 t
= adaptor
->create (adaptor
, NULL
);
452 adaptor
->debugger
->createNode (adaptor
->debugger
, 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
);
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
;
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
);
496 /// Sends the required debugging events for duplicating a tree
500 simulateTreeConstruction(pANTLR3_BASE_TREE_ADAPTOR adaptor
, pANTLR3_BASE_TREE tree
)
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
);
520 dbgDupTree (pANTLR3_BASE_TREE_ADAPTOR adaptor
, pANTLR3_BASE_TREE tree
)
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
533 simulateTreeConstruction(adaptor
, 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.
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
);
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
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
));
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.
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))
595 * If newRoot is a nil-rooted single child tree, use the single
596 * child as the new root node.
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))
603 * If oldRoot was null, it's ok, just return newRoot (even if isNilNode).
606 * old=null, new=r yields r
607 * old=null, new=^(nil r) yields ^(nil r)
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
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
)
633 /* root is just the new tree as is if there is no
636 if (oldRootTree
== NULL
)
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");
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
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
683 oldRootTree
->reuse(oldRootTree
);
685 /* Always returns new root structure
690 static pANTLR3_BASE_TREE
691 dbgBecomeRoot (pANTLR3_BASE_TREE_ADAPTOR adaptor
, pANTLR3_BASE_TREE newRootTree
, pANTLR3_BASE_TREE oldRootTree
)
695 t
= becomeRoot(adaptor
, newRootTree
, oldRootTree
);
697 adaptor
->debugger
->becomeRoot(adaptor
->debugger
, newRootTree
, oldRootTree
);
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!
713 if (root
!= NULL
&& root
->isNilNode(root
))
715 if (getBaseTreeChildCount(root
) == 0)
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
);
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
)
749 t
= adaptor
->becomeRoot(adaptor
, adaptor
->create(adaptor
, newRoot
), oldRoot
);
751 adaptor
->debugger
->becomeRoot(adaptor
->debugger
,t
, oldRoot
);
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
)
779 t
= createTypeToken(adaptor
, tokenType
, fromToken
);
781 adaptor
->debugger
->createNode(adaptor
->debugger
, 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
)
810 t
= createTypeTokenText(adaptor
, tokenType
, fromToken
, text
);
812 adaptor
->debugger
->createNode(adaptor
->debugger
, 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
)
835 t
= createTypeText(adaptor
, tokenType
, text
);
837 adaptor
->debugger
->createNode(adaptor
->debugger
, t
);
842 /** Dummy implementation - will be supplied by super class
845 getType (pANTLR3_BASE_TREE_ADAPTOR adaptor
, pANTLR3_BASE_TREE t
)
850 /** Dummy implementation - will be supplied by super class
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");
867 /** Dummy implementation - will be supplied by super class
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
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");
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");
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.
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
);