2 /// Defines the implementation of the common node stream the default
3 /// tree node stream used by ANTLR.
7 // Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC
8 // http://www.temporal-wave.com
9 // http://www.linkedin.com/in/jimidle
11 // All rights reserved.
13 // Redistribution and use in source and binary forms, with or without
14 // modification, are permitted provided that the following conditions
16 // 1. Redistributions of source code must retain the above copyright
17 // notice, this list of conditions and the following disclaimer.
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 // 3. The name of the author may not be used to endorse or promote products
22 // derived from this software without specific prior written permission.
24 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <antlr3commontreenodestream.h>
38 #pragma warning( disable : 4100 )
41 // COMMON TREE STREAM API
43 static void addNavigationNode (pANTLR3_COMMON_TREE_NODE_STREAM ctns, ANTLR3_UINT32 ttype);
44 static ANTLR3_BOOLEAN hasUniqueNavigationNodes (pANTLR3_COMMON_TREE_NODE_STREAM ctns);
45 static pANTLR3_BASE_TREE newDownNode (pANTLR3_COMMON_TREE_NODE_STREAM ctns);
46 static pANTLR3_BASE_TREE newUpNode (pANTLR3_COMMON_TREE_NODE_STREAM ctns);
47 static void reset (pANTLR3_COMMON_TREE_NODE_STREAM ctns);
48 static void push (pANTLR3_COMMON_TREE_NODE_STREAM ctns, ANTLR3_INT32 index);
49 static ANTLR3_INT32 pop (pANTLR3_COMMON_TREE_NODE_STREAM ctns);
50 //static ANTLR3_INT32 index (pANTLR3_COMMON_TREE_NODE_STREAM ctns);
51 static ANTLR3_UINT32 getLookaheadSize (pANTLR3_COMMON_TREE_NODE_STREAM ctns);
52 // TREE NODE STREAM API
54 static pANTLR3_BASE_TREE_ADAPTOR getTreeAdaptor (pANTLR3_TREE_NODE_STREAM tns);
55 static pANTLR3_BASE_TREE getTreeSource (pANTLR3_TREE_NODE_STREAM tns);
56 static pANTLR3_BASE_TREE _LT (pANTLR3_TREE_NODE_STREAM tns, ANTLR3_INT32 k);
57 static pANTLR3_BASE_TREE get (pANTLR3_TREE_NODE_STREAM tns, ANTLR3_INT32 k);
58 static void setUniqueNavigationNodes (pANTLR3_TREE_NODE_STREAM tns, ANTLR3_BOOLEAN uniqueNavigationNodes);
59 static pANTLR3_STRING toString (pANTLR3_TREE_NODE_STREAM tns);
60 static pANTLR3_STRING toStringSS (pANTLR3_TREE_NODE_STREAM tns, pANTLR3_BASE_TREE start, pANTLR3_BASE_TREE stop);
61 static void toStringWork (pANTLR3_TREE_NODE_STREAM tns, pANTLR3_BASE_TREE start, pANTLR3_BASE_TREE stop, pANTLR3_STRING buf);
62 static void replaceChildren (pANTLR3_TREE_NODE_STREAM tns, pANTLR3_BASE_TREE parent, ANTLR3_INT32 startChildIndex, ANTLR3_INT32 stopChildIndex, pANTLR3_BASE_TREE t);
66 static void consume (pANTLR3_INT_STREAM is);
67 static ANTLR3_MARKER tindex (pANTLR3_INT_STREAM is);
68 static ANTLR3_UINT32 _LA (pANTLR3_INT_STREAM is, ANTLR3_INT32 i);
69 static ANTLR3_MARKER mark (pANTLR3_INT_STREAM is);
70 static void release (pANTLR3_INT_STREAM is, ANTLR3_MARKER marker);
71 static void rewindMark (pANTLR3_INT_STREAM is, ANTLR3_MARKER marker);
72 static void rewindLast (pANTLR3_INT_STREAM is);
73 static void seek (pANTLR3_INT_STREAM is, ANTLR3_MARKER index);
74 static ANTLR3_UINT32 size (pANTLR3_INT_STREAM is);
79 static void fillBuffer (pANTLR3_COMMON_TREE_NODE_STREAM ctns, pANTLR3_BASE_TREE t);
80 static void fillBufferRoot (pANTLR3_COMMON_TREE_NODE_STREAM ctns);
84 static void antlr3TreeNodeStreamFree (pANTLR3_TREE_NODE_STREAM tns);
85 static void antlr3CommonTreeNodeStreamFree (pANTLR3_COMMON_TREE_NODE_STREAM ctns);
87 ANTLR3_API pANTLR3_TREE_NODE_STREAM
88 antlr3TreeNodeStreamNew()
90 pANTLR3_TREE_NODE_STREAM stream;
92 // Memory for the interface structure
94 stream = (pANTLR3_TREE_NODE_STREAM) ANTLR3_CALLOC(1, sizeof(ANTLR3_TREE_NODE_STREAM));
103 stream->replaceChildren = replaceChildren;
104 stream->free = antlr3TreeNodeStreamFree;
110 antlr3TreeNodeStreamFree(pANTLR3_TREE_NODE_STREAM stream)
115 ANTLR3_API pANTLR3_COMMON_TREE_NODE_STREAM
116 antlr3CommonTreeNodeStreamNewTree(pANTLR3_BASE_TREE tree, ANTLR3_UINT32 hint)
118 pANTLR3_COMMON_TREE_NODE_STREAM stream;
120 stream = antlr3CommonTreeNodeStreamNew(tree->strFactory, hint);
131 ANTLR3_API pANTLR3_COMMON_TREE_NODE_STREAM
132 antlr3CommonTreeNodeStreamNewStream(pANTLR3_COMMON_TREE_NODE_STREAM inStream)
134 pANTLR3_COMMON_TREE_NODE_STREAM stream;
136 // Memory for the interface structure
138 stream = (pANTLR3_COMMON_TREE_NODE_STREAM) ANTLR3_CALLOC(1, sizeof(ANTLR3_COMMON_TREE_NODE_STREAM));
145 // Copy in all the reusable parts of the originating stream and create new
146 // pieces where necessary.
149 // String factory for tree walker
151 stream->stringFactory = inStream->stringFactory;
153 // Create an adaptor for the common tree node stream
155 stream->adaptor = inStream->adaptor;
157 // Create space for the tree node stream interface
159 stream->tnstream = antlr3TreeNodeStreamNew();
161 if (stream->tnstream == NULL)
163 stream->free (stream);
168 // Create space for the INT_STREAM interface
170 stream->tnstream->istream = antlr3IntStreamNew();
172 if (stream->tnstream->istream == NULL)
174 stream->tnstream->free (stream->tnstream);
175 stream->free (stream);
180 // Install the common tree node stream API
182 stream->addNavigationNode = addNavigationNode;
183 stream->hasUniqueNavigationNodes = hasUniqueNavigationNodes;
184 stream->newDownNode = newDownNode;
185 stream->newUpNode = newUpNode;
186 stream->reset = reset;
189 stream->getLookaheadSize = getLookaheadSize;
191 stream->free = antlr3CommonTreeNodeStreamFree;
193 // Install the tree node stream API
195 stream->tnstream->getTreeAdaptor = getTreeAdaptor;
196 stream->tnstream->getTreeSource = getTreeSource;
197 stream->tnstream->_LT = _LT;
198 stream->tnstream->setUniqueNavigationNodes = setUniqueNavigationNodes;
199 stream->tnstream->toString = toString;
200 stream->tnstream->toStringSS = toStringSS;
201 stream->tnstream->toStringWork = toStringWork;
202 stream->tnstream->get = get;
204 // Install INT_STREAM interface
206 stream->tnstream->istream->consume = consume;
207 stream->tnstream->istream->index = tindex;
208 stream->tnstream->istream->_LA = _LA;
209 stream->tnstream->istream->mark = mark;
210 stream->tnstream->istream->release = release;
211 stream->tnstream->istream->rewind = rewindMark;
212 stream->tnstream->istream->rewindLast = rewindLast;
213 stream->tnstream->istream->seek = seek;
214 stream->tnstream->istream->size = size;
216 // Initialize data elements of INT stream
218 stream->tnstream->istream->type = ANTLR3_COMMONTREENODE;
219 stream->tnstream->istream->super = (stream->tnstream);
221 // Initialize data elements of TREE stream
223 stream->tnstream->ctns = stream;
225 // Initialize data elements of the COMMON TREE NODE stream
227 stream->super = NULL;
228 stream->uniqueNavigationNodes = ANTLR3_FALSE;
229 stream->markers = NULL;
230 stream->nodeStack = inStream->nodeStack;
232 // Create the node list map
234 stream->nodes = antlr3VectorNew(DEFAULT_INITIAL_BUFFER_SIZE);
237 // Install the navigation nodes
240 // Install the navigation nodes
242 antlr3SetCTAPI(&(stream->UP));
243 antlr3SetCTAPI(&(stream->DOWN));
244 antlr3SetCTAPI(&(stream->EOF_NODE));
245 antlr3SetCTAPI(&(stream->INVALID_NODE));
247 stream->UP.token = inStream->UP.token;
248 inStream->UP.token->strFactory = stream->stringFactory;
249 stream->DOWN.token = inStream->DOWN.token;
250 inStream->DOWN.token->strFactory = stream->stringFactory;
251 stream->EOF_NODE.token = inStream->EOF_NODE.token;
252 inStream->EOF_NODE.token->strFactory = stream->stringFactory;
253 stream->INVALID_NODE.token = inStream->INVALID_NODE.token;
254 inStream->INVALID_NODE.token->strFactory= stream->stringFactory;
256 // Reuse the root tree of the originating stream
258 stream->root = inStream->root;
260 // Signal that this is a rewriting stream so we don't
261 // free the originating tree. Anything that we rewrite or
262 // duplicate here will be done through the adaptor or
263 // the original tree factory.
265 stream->isRewriter = ANTLR3_TRUE;
269 ANTLR3_API pANTLR3_COMMON_TREE_NODE_STREAM
270 antlr3CommonTreeNodeStreamNew(pANTLR3_STRING_FACTORY strFactory, ANTLR3_UINT32 hint)
272 pANTLR3_COMMON_TREE_NODE_STREAM stream;
273 pANTLR3_COMMON_TOKEN token;
275 // Memory for the interface structure
277 stream = (pANTLR3_COMMON_TREE_NODE_STREAM) ANTLR3_CALLOC(1, sizeof(ANTLR3_COMMON_TREE_NODE_STREAM));
284 // String factory for tree walker
286 stream->stringFactory = strFactory;
288 // Create an adaptor for the common tree node stream
290 stream->adaptor = ANTLR3_TREE_ADAPTORNew(strFactory);
292 if (stream->adaptor == NULL)
294 stream->free(stream);
298 // Create space for the tree node stream interface
300 stream->tnstream = antlr3TreeNodeStreamNew();
302 if (stream->tnstream == NULL)
304 stream->adaptor->free (stream->adaptor);
305 stream->free (stream);
310 // Create space for the INT_STREAM interface
312 stream->tnstream->istream = antlr3IntStreamNew();
314 if (stream->tnstream->istream == NULL)
316 stream->adaptor->free (stream->adaptor);
317 stream->tnstream->free (stream->tnstream);
318 stream->free (stream);
323 // Install the common tree node stream API
325 stream->addNavigationNode = addNavigationNode;
326 stream->hasUniqueNavigationNodes = hasUniqueNavigationNodes;
327 stream->newDownNode = newDownNode;
328 stream->newUpNode = newUpNode;
329 stream->reset = reset;
333 stream->free = antlr3CommonTreeNodeStreamFree;
335 // Install the tree node stream API
337 stream->tnstream->getTreeAdaptor = getTreeAdaptor;
338 stream->tnstream->getTreeSource = getTreeSource;
339 stream->tnstream->_LT = _LT;
340 stream->tnstream->setUniqueNavigationNodes = setUniqueNavigationNodes;
341 stream->tnstream->toString = toString;
342 stream->tnstream->toStringSS = toStringSS;
343 stream->tnstream->toStringWork = toStringWork;
344 stream->tnstream->get = get;
346 // Install INT_STREAM interface
348 stream->tnstream->istream->consume = consume;
349 stream->tnstream->istream->index = tindex;
350 stream->tnstream->istream->_LA = _LA;
351 stream->tnstream->istream->mark = mark;
352 stream->tnstream->istream->release = release;
353 stream->tnstream->istream->rewind = rewindMark;
354 stream->tnstream->istream->rewindLast = rewindLast;
355 stream->tnstream->istream->seek = seek;
356 stream->tnstream->istream->size = size;
358 // Initialize data elements of INT stream
360 stream->tnstream->istream->type = ANTLR3_COMMONTREENODE;
361 stream->tnstream->istream->super = (stream->tnstream);
363 // Initialize data elements of TREE stream
365 stream->tnstream->ctns = stream;
367 // Initialize data elements of the COMMON TREE NODE stream
369 stream->super = NULL;
370 stream->uniqueNavigationNodes = ANTLR3_FALSE;
371 stream->markers = NULL;
372 stream->nodeStack = antlr3StackNew(INITIAL_CALL_STACK_SIZE);
374 // Create the node list map
378 hint = DEFAULT_INITIAL_BUFFER_SIZE;
380 stream->nodes = antlr3VectorNew(hint);
383 // Install the navigation nodes
385 antlr3SetCTAPI(&(stream->UP));
386 antlr3SetCTAPI(&(stream->DOWN));
387 antlr3SetCTAPI(&(stream->EOF_NODE));
388 antlr3SetCTAPI(&(stream->INVALID_NODE));
390 token = antlr3CommonTokenNew(ANTLR3_TOKEN_UP);
391 token->strFactory = strFactory;
392 token->textState = ANTLR3_TEXT_CHARP;
393 token->tokText.chars = (pANTLR3_UCHAR)"UP";
394 stream->UP.token = token;
396 token = antlr3CommonTokenNew(ANTLR3_TOKEN_DOWN);
397 token->strFactory = strFactory;
398 token->textState = ANTLR3_TEXT_CHARP;
399 token->tokText.chars = (pANTLR3_UCHAR)"DOWN";
400 stream->DOWN.token = token;
402 token = antlr3CommonTokenNew(ANTLR3_TOKEN_EOF);
403 token->strFactory = strFactory;
404 token->textState = ANTLR3_TEXT_CHARP;
405 token->tokText.chars = (pANTLR3_UCHAR)"EOF";
406 stream->EOF_NODE.token = token;
408 token = antlr3CommonTokenNew(ANTLR3_TOKEN_INVALID);
409 token->strFactory = strFactory;
410 token->textState = ANTLR3_TEXT_CHARP;
411 token->tokText.chars = (pANTLR3_UCHAR)"INVALID";
412 stream->INVALID_NODE.token = token;
418 /// Free up any resources that belong to this common tree node stream.
420 static void antlr3CommonTreeNodeStreamFree (pANTLR3_COMMON_TREE_NODE_STREAM ctns)
423 // If this is a rewrting stream, then certain resources
424 // belong to the originating node stream and we do not
427 if (ctns->isRewriter != ANTLR3_TRUE)
429 ctns->adaptor ->free (ctns->adaptor);
431 if (ctns->nodeStack != NULL)
433 ctns->nodeStack->free(ctns->nodeStack);
436 ANTLR3_FREE(ctns->INVALID_NODE.token);
437 ANTLR3_FREE(ctns->EOF_NODE.token);
438 ANTLR3_FREE(ctns->DOWN.token);
439 ANTLR3_FREE(ctns->UP.token);
442 if (ctns->nodes != NULL)
444 ctns->nodes ->free (ctns->nodes);
446 ctns->tnstream->istream ->free (ctns->tnstream->istream);
447 ctns->tnstream ->free (ctns->tnstream);
453 // ------------------------------------------------------------------------------
457 /// Walk and fill the tree node buffer from the root tree
460 fillBufferRoot(pANTLR3_COMMON_TREE_NODE_STREAM ctns)
462 // Call the generic buffer routine with the root as the
465 fillBuffer(ctns, ctns->root);
466 ctns->p = 0; // Indicate we are at buffer start
469 /// Walk tree with depth-first-search and fill nodes buffer.
470 /// Don't add in DOWN, UP nodes if the supplied tree is a list (t is isNilNode)
471 // such as the root tree is.
474 fillBuffer(pANTLR3_COMMON_TREE_NODE_STREAM ctns, pANTLR3_BASE_TREE t)
476 ANTLR3_BOOLEAN nilNode;
477 ANTLR3_UINT32 nCount;
480 nilNode = ctns->adaptor->isNilNode(ctns->adaptor, t);
482 // If the supplied node is not a nil (list) node then we
483 // add in the node itself to the vector
485 if (nilNode == ANTLR3_FALSE)
487 ctns->nodes->add(ctns->nodes, t, NULL);
490 // Only add a DOWN node if the tree is not a nil tree and
491 // the tree does have children.
493 nCount = t->getChildCount(t);
495 if (nilNode == ANTLR3_FALSE && nCount>0)
497 ctns->addNavigationNode(ctns, ANTLR3_TOKEN_DOWN);
500 // We always add any children the tree contains, which is
501 // a recursive call to this function, which will cause similar
502 // recursion and implement a depth first addition
504 for (c = 0; c < nCount; c++)
506 fillBuffer(ctns, ctns->adaptor->getChild(ctns->adaptor, t, c));
509 // If the tree had children and was not a nil (list) node, then we
510 // we need to add an UP node here to match the DOWN node
512 if (nilNode == ANTLR3_FALSE && nCount > 0)
514 ctns->addNavigationNode(ctns, ANTLR3_TOKEN_UP);
519 // ------------------------------------------------------------------------------
520 // Interface functions
523 /// Reset the input stream to the start of the input nodes.
526 reset (pANTLR3_COMMON_TREE_NODE_STREAM ctns)
532 ctns->tnstream->istream->lastMarker = 0;
535 // Free and reset the node stack only if this is not
536 // a rewriter, which is going to reuse the originating
537 // node streams node stack
539 if (ctns->isRewriter != ANTLR3_TRUE)
541 if (ctns->nodeStack != NULL)
543 ctns->nodeStack->free(ctns->nodeStack);
544 ctns->nodeStack = antlr3StackNew(INITIAL_CALL_STACK_SIZE);
550 static pANTLR3_BASE_TREE
551 LB(pANTLR3_TREE_NODE_STREAM tns, ANTLR3_INT32 k)
555 return &(tns->ctns->INVALID_NODE.baseTree);
558 if ( (tns->ctns->p - k) < 0)
560 return &(tns->ctns->INVALID_NODE.baseTree);
563 return tns->ctns->nodes->get(tns->ctns->nodes, tns->ctns->p - k);
566 /// Get tree node at current input pointer + i ahead where i=1 is next node.
567 /// i<0 indicates nodes in the past. So -1 is previous node and -2 is
568 /// two nodes ago. LT(0) is undefined. For i>=n, return null.
569 /// Return null for LT(0) and any index that results in an absolute address
570 /// that is negative.
572 /// This is analogous to the _LT() method of the TokenStream, but this
573 /// returns a tree node instead of a token. Makes code gen identical
574 /// for both parser and tree grammars. :)
576 static pANTLR3_BASE_TREE
577 _LT (pANTLR3_TREE_NODE_STREAM tns, ANTLR3_INT32 k)
579 if (tns->ctns->p == -1)
581 fillBufferRoot(tns->ctns);
590 return &(tns->ctns->INVALID_NODE.baseTree);
593 // k was a legitimate request,
595 if (( tns->ctns->p + k - 1) >= (ANTLR3_INT32)(tns->ctns->nodes->count))
597 return &(tns->ctns->EOF_NODE.baseTree);
600 return tns->ctns->nodes->get(tns->ctns->nodes, tns->ctns->p + k - 1);
603 /// Where is this stream pulling nodes from? This is not the name, but
604 /// the object that provides node objects.
606 static pANTLR3_BASE_TREE
607 getTreeSource (pANTLR3_TREE_NODE_STREAM tns)
609 return tns->ctns->root;
612 /// Consume the next node from the input stream
615 consume (pANTLR3_INT_STREAM is)
617 pANTLR3_TREE_NODE_STREAM tns;
618 pANTLR3_COMMON_TREE_NODE_STREAM ctns;
620 tns = (pANTLR3_TREE_NODE_STREAM)(is->super);
625 fillBufferRoot(ctns);
631 _LA (pANTLR3_INT_STREAM is, ANTLR3_INT32 i)
633 pANTLR3_TREE_NODE_STREAM tns;
636 tns = (pANTLR3_TREE_NODE_STREAM)(is->super);
638 // Ask LT for the 'token' at that position
640 t = tns->_LT(tns, i);
644 return ANTLR3_TOKEN_INVALID;
647 // Token node was there so return the type of it
649 return t->getType(t);
652 /// Mark the state of the input stream so that we can come back to it
653 /// after a syntactic predicate and so on.
656 mark (pANTLR3_INT_STREAM is)
658 pANTLR3_TREE_NODE_STREAM tns;
659 pANTLR3_COMMON_TREE_NODE_STREAM ctns;
661 tns = (pANTLR3_TREE_NODE_STREAM)(is->super);
664 if (tns->ctns->p == -1)
666 fillBufferRoot(tns->ctns);
669 // Return the current mark point
671 ctns->tnstream->istream->lastMarker = ctns->tnstream->istream->index(ctns->tnstream->istream);
673 return ctns->tnstream->istream->lastMarker;
677 release (pANTLR3_INT_STREAM is, ANTLR3_MARKER marker)
681 /// Rewind the current state of the tree walk to the state it
682 /// was in when mark() was called and it returned marker. Also,
683 /// wipe out the lookahead which will force reloading a few nodes
684 /// but it is better than making a copy of the lookahead buffer
688 rewindMark (pANTLR3_INT_STREAM is, ANTLR3_MARKER marker)
690 is->seek(is, marker);
694 rewindLast (pANTLR3_INT_STREAM is)
696 is->seek(is, is->lastMarker);
699 /// consume() ahead until we hit index. Can't just jump ahead--must
700 /// spit out the navigation nodes.
703 seek (pANTLR3_INT_STREAM is, ANTLR3_MARKER index)
705 pANTLR3_TREE_NODE_STREAM tns;
706 pANTLR3_COMMON_TREE_NODE_STREAM ctns;
708 tns = (pANTLR3_TREE_NODE_STREAM)(is->super);
711 ctns->p = ANTLR3_UINT32_CAST(index);
715 tindex (pANTLR3_INT_STREAM is)
717 pANTLR3_TREE_NODE_STREAM tns;
718 pANTLR3_COMMON_TREE_NODE_STREAM ctns;
720 tns = (pANTLR3_TREE_NODE_STREAM)(is->super);
723 return (ANTLR3_MARKER)(ctns->p);
726 /// Expensive to compute the size of the whole tree while parsing.
727 /// This method only returns how much input has been seen so far. So
728 /// after parsing it returns true size.
731 size (pANTLR3_INT_STREAM is)
733 pANTLR3_TREE_NODE_STREAM tns;
734 pANTLR3_COMMON_TREE_NODE_STREAM ctns;
736 tns = (pANTLR3_TREE_NODE_STREAM)(is->super);
741 fillBufferRoot(ctns);
744 return ctns->nodes->size(ctns->nodes);
747 /// As we flatten the tree, we use UP, DOWN nodes to represent
748 /// the tree structure. When debugging we need unique nodes
749 /// so instantiate new ones when uniqueNavigationNodes is true.
752 addNavigationNode (pANTLR3_COMMON_TREE_NODE_STREAM ctns, ANTLR3_UINT32 ttype)
754 pANTLR3_BASE_TREE node;
758 if (ttype == ANTLR3_TOKEN_DOWN)
760 if (ctns->hasUniqueNavigationNodes(ctns) == ANTLR3_TRUE)
762 node = ctns->newDownNode(ctns);
766 node = &(ctns->DOWN.baseTree);
771 if (ctns->hasUniqueNavigationNodes(ctns) == ANTLR3_TRUE)
773 node = ctns->newUpNode(ctns);
777 node = &(ctns->UP.baseTree);
781 // Now add the node we decided upon.
783 ctns->nodes->add(ctns->nodes, node, NULL);
787 static pANTLR3_BASE_TREE_ADAPTOR
788 getTreeAdaptor (pANTLR3_TREE_NODE_STREAM tns)
790 return tns->ctns->adaptor;
793 static ANTLR3_BOOLEAN
794 hasUniqueNavigationNodes (pANTLR3_COMMON_TREE_NODE_STREAM ctns)
796 return ctns->uniqueNavigationNodes;
800 setUniqueNavigationNodes (pANTLR3_TREE_NODE_STREAM tns, ANTLR3_BOOLEAN uniqueNavigationNodes)
802 tns->ctns->uniqueNavigationNodes = uniqueNavigationNodes;
806 /// Print out the entire tree including DOWN/UP nodes. Uses
807 /// a recursive walk. Mostly useful for testing as it yields
808 /// the token types not text.
810 static pANTLR3_STRING
811 toString (pANTLR3_TREE_NODE_STREAM tns)
814 return tns->toStringSS(tns, tns->ctns->root, NULL);
817 static pANTLR3_STRING
818 toStringSS (pANTLR3_TREE_NODE_STREAM tns, pANTLR3_BASE_TREE start, pANTLR3_BASE_TREE stop)
822 buf = tns->ctns->stringFactory->newRaw(tns->ctns->stringFactory);
824 tns->toStringWork(tns, start, stop, buf);
830 toStringWork (pANTLR3_TREE_NODE_STREAM tns, pANTLR3_BASE_TREE p, pANTLR3_BASE_TREE stop, pANTLR3_STRING buf)
836 if (!p->isNilNode(p) )
840 text = p->toString(p);
844 text = tns->ctns->stringFactory->newRaw(tns->ctns->stringFactory);
846 text->addc (text, ' ');
847 text->addi (text, p->getType(p));
850 buf->appendS(buf, text);
855 return; /* Finished */
858 n = p->getChildCount(p);
860 if (n > 0 && ! p->isNilNode(p) )
862 buf->addc (buf, ' ');
863 buf->addi (buf, ANTLR3_TOKEN_DOWN);
866 for (c = 0; c<n ; c++)
868 pANTLR3_BASE_TREE child;
870 child = p->getChild(p, c);
871 tns->toStringWork(tns, child, stop, buf);
874 if (n > 0 && ! p->isNilNode(p) )
876 buf->addc (buf, ' ');
877 buf->addi (buf, ANTLR3_TOKEN_UP);
882 getLookaheadSize (pANTLR3_COMMON_TREE_NODE_STREAM ctns)
884 return ctns->tail < ctns->head
885 ? (ctns->lookAheadLength - ctns->head + ctns->tail)
886 : (ctns->tail - ctns->head);
889 static pANTLR3_BASE_TREE
890 newDownNode (pANTLR3_COMMON_TREE_NODE_STREAM ctns)
892 pANTLR3_COMMON_TREE dNode;
893 pANTLR3_COMMON_TOKEN token;
895 token = antlr3CommonTokenNew(ANTLR3_TOKEN_DOWN);
896 token->textState = ANTLR3_TEXT_CHARP;
897 token->tokText.chars = (pANTLR3_UCHAR)"DOWN";
898 dNode = antlr3CommonTreeNewFromToken(token);
900 return &(dNode->baseTree);
903 static pANTLR3_BASE_TREE
904 newUpNode (pANTLR3_COMMON_TREE_NODE_STREAM ctns)
906 pANTLR3_COMMON_TREE uNode;
907 pANTLR3_COMMON_TOKEN token;
909 token = antlr3CommonTokenNew(ANTLR3_TOKEN_UP);
910 token->textState = ANTLR3_TEXT_CHARP;
911 token->tokText.chars = (pANTLR3_UCHAR)"UP";
912 uNode = antlr3CommonTreeNewFromToken(token);
914 return &(uNode->baseTree);
917 /// Replace from start to stop child index of parent with t, which might
918 /// be a list. Number of children may be different
919 /// after this call. The stream is notified because it is walking the
920 /// tree and might need to know you are monkey-ing with the underlying
921 /// tree. Also, it might be able to modify the node stream to avoid
922 /// re-streaming for future phases.
924 /// If parent is null, don't do anything; must be at root of overall tree.
925 /// Can't replace whatever points to the parent externally. Do nothing.
928 replaceChildren (pANTLR3_TREE_NODE_STREAM tns, pANTLR3_BASE_TREE parent, ANTLR3_INT32 startChildIndex, ANTLR3_INT32 stopChildIndex, pANTLR3_BASE_TREE t)
932 pANTLR3_BASE_TREE_ADAPTOR adaptor;
933 pANTLR3_COMMON_TREE_ADAPTOR cta;
935 adaptor = tns->getTreeAdaptor(tns);
936 cta = (pANTLR3_COMMON_TREE_ADAPTOR)(adaptor->super);
938 adaptor->replaceChildren(adaptor, parent, startChildIndex, stopChildIndex, t);
942 static pANTLR3_BASE_TREE
943 get (pANTLR3_TREE_NODE_STREAM tns, ANTLR3_INT32 k)
945 if (tns->ctns->p == -1)
947 fillBufferRoot(tns->ctns);
950 return tns->ctns->nodes->get(tns->ctns->nodes, k);
954 push (pANTLR3_COMMON_TREE_NODE_STREAM ctns, ANTLR3_INT32 index)
956 ctns->nodeStack->push(ctns->nodeStack, ANTLR3_FUNC_PTR(ctns->p), NULL); // Save current index
957 ctns->tnstream->istream->seek(ctns->tnstream->istream, index);
961 pop (pANTLR3_COMMON_TREE_NODE_STREAM ctns)
965 retVal = ANTLR3_UINT32_CAST(ctns->nodeStack->pop(ctns->nodeStack));
966 ctns->tnstream->istream->seek(ctns->tnstream->istream, retVal);