1 /* 2 [The "BSD license"] 3 Copyright (c) 2005-2009 Terence Parr 4 All rights reserved. 5 6 Redistribution and use in source and binary forms, with or without 7 modification, are permitted provided that the following conditions 8 are met: 9 1. Redistributions of source code must retain the above copyright 10 notice, this list of conditions and the following disclaimer. 11 2. Redistributions in binary form must reproduce the above copyright 12 notice, this list of conditions and the following disclaimer in the 13 documentation and/or other materials provided with the distribution. 14 3. The name of the author may not be used to endorse or promote products 15 derived from this software without specific prior written permission. 16 17 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 package org.antlr.runtime.tree; 29 30 import org.antlr.runtime.Token; 31 import org.antlr.runtime.TokenStream; 32 import org.antlr.runtime.misc.LookaheadStream; 33 import org.antlr.runtime.misc.IntArray; 34 35 import java.util.*; 36 37 public class CommonTreeNodeStream extends LookaheadStream<Object> implements TreeNodeStream { 38 public static final int DEFAULT_INITIAL_BUFFER_SIZE = 100; 39 public static final int INITIAL_CALL_STACK_SIZE = 10; 40 41 /** Pull nodes from which tree? */ 42 protected Object root; 43 44 /** If this tree (root) was created from a token stream, track it. */ 45 protected TokenStream tokens; 46 47 /** What tree adaptor was used to build these trees */ 48 TreeAdaptor adaptor; 49 50 /** The tree iterator we using */ 51 protected TreeIterator it; 52 53 /** Stack of indexes used for push/pop calls */ 54 protected IntArray calls; 55 56 /** Tree (nil A B C) trees like flat A B C streams */ 57 protected boolean hasNilRoot = false; 58 59 /** Tracks tree depth. Level=0 means we're at root node level. */ 60 protected int level = 0; 61 CommonTreeNodeStream(Object tree)62 public CommonTreeNodeStream(Object tree) { 63 this(new CommonTreeAdaptor(), tree); 64 } 65 CommonTreeNodeStream(TreeAdaptor adaptor, Object tree)66 public CommonTreeNodeStream(TreeAdaptor adaptor, Object tree) { 67 this.root = tree; 68 this.adaptor = adaptor; 69 it = new TreeIterator(adaptor,root); 70 } 71 reset()72 public void reset() { 73 super.reset(); 74 it.reset(); 75 hasNilRoot = false; 76 level = 0; 77 if ( calls != null ) calls.clear(); 78 } 79 80 /** Pull elements from tree iterator. Track tree level 0..max_level. 81 * If nil rooted tree, don't give initial nil and DOWN nor final UP. 82 */ nextElement()83 public Object nextElement() { 84 Object t = it.next(); 85 //System.out.println("pulled "+adaptor.getType(t)); 86 if ( t == it.up ) { 87 level--; 88 if ( level==0 && hasNilRoot ) return it.next(); // don't give last UP; get EOF 89 } 90 else if ( t == it.down ) level++; 91 if ( level==0 && adaptor.isNil(t) ) { // if nil root, scarf nil, DOWN 92 hasNilRoot = true; 93 t = it.next(); // t is now DOWN, so get first real node next 94 level++; 95 t = it.next(); 96 } 97 return t; 98 } 99 isEOF(Object o)100 public boolean isEOF(Object o) { return adaptor.getType(o) == Token.EOF; } 101 setUniqueNavigationNodes(boolean uniqueNavigationNodes)102 public void setUniqueNavigationNodes(boolean uniqueNavigationNodes) { } 103 getTreeSource()104 public Object getTreeSource() { return root; } 105 getSourceName()106 public String getSourceName() { return getTokenStream().getSourceName(); } 107 getTokenStream()108 public TokenStream getTokenStream() { return tokens; } 109 setTokenStream(TokenStream tokens)110 public void setTokenStream(TokenStream tokens) { this.tokens = tokens; } 111 getTreeAdaptor()112 public TreeAdaptor getTreeAdaptor() { return adaptor; } 113 setTreeAdaptor(TreeAdaptor adaptor)114 public void setTreeAdaptor(TreeAdaptor adaptor) { this.adaptor = adaptor; } 115 get(int i)116 public Object get(int i) { 117 throw new UnsupportedOperationException("Absolute node indexes are meaningless in an unbuffered stream"); 118 } 119 LA(int i)120 public int LA(int i) { return adaptor.getType(LT(i)); } 121 122 /** Make stream jump to a new location, saving old location. 123 * Switch back with pop(). 124 */ push(int index)125 public void push(int index) { 126 if ( calls==null ) { 127 calls = new IntArray(); 128 } 129 calls.push(p); // save current index 130 seek(index); 131 } 132 133 /** Seek back to previous index saved during last push() call. 134 * Return top of stack (return index). 135 */ pop()136 public int pop() { 137 int ret = calls.pop(); 138 seek(ret); 139 return ret; 140 } 141 142 // TREE REWRITE INTERFACE 143 replaceChildren(Object parent, int startChildIndex, int stopChildIndex, Object t)144 public void replaceChildren(Object parent, int startChildIndex, int stopChildIndex, Object t) { 145 if ( parent!=null ) { 146 adaptor.replaceChildren(parent, startChildIndex, stopChildIndex, t); 147 } 148 } 149 toString(Object start, Object stop)150 public String toString(Object start, Object stop) { 151 // we'll have to walk from start to stop in tree; we're not keeping 152 // a complete node stream buffer 153 return "n/a"; 154 } 155 156 /** For debugging; destructive: moves tree iterator to end. */ toTokenTypeString()157 public String toTokenTypeString() { 158 reset(); 159 StringBuffer buf = new StringBuffer(); 160 Object o = LT(1); 161 int type = adaptor.getType(o); 162 while ( type!=Token.EOF ) { 163 buf.append(" "); 164 buf.append(type); 165 consume(); 166 o = LT(1); 167 type = adaptor.getType(o); 168 } 169 return buf.toString(); 170 } 171 } 172