00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 package org.antlr.runtime.tree;
00029
00030 import org.antlr.runtime.Token;
00031 import org.antlr.runtime.TokenStream;
00032 import org.antlr.runtime.misc.IntArray;
00033 import java.util.*;
00034
00051 public class CommonTreeNodeStream implements TreeNodeStream {
00052 public static final int DEFAULT_INITIAL_BUFFER_SIZE = 100;
00053 public static final int INITIAL_CALL_STACK_SIZE = 10;
00054
00055 protected class StreamIterator implements Iterator {
00056 int i = 0;
00057 public boolean hasNext() {
00058 return i<nodes.size();
00059 }
00060
00061 public Object next() {
00062 int current = i;
00063 i++;
00064 if ( current < nodes.size() ) {
00065 return nodes.get(current);
00066 }
00067 return eof;
00068 }
00069
00070 public void remove() {
00071 throw new RuntimeException("cannot remove nodes from stream");
00072 }
00073 }
00074
00075
00076
00077
00078 protected Object down;
00079 protected Object up;
00080 protected Object eof;
00081
00091 protected List nodes;
00092
00094 protected Object root;
00095
00097 protected TokenStream tokens;
00098
00100 TreeAdaptor adaptor;
00101
00103 protected boolean uniqueNavigationNodes = false;
00104
00108 protected int p = -1;
00109
00111 protected int lastMarker;
00112
00114 protected IntArray calls;
00115
00116 public CommonTreeNodeStream(Object tree) {
00117 this(new CommonTreeAdaptor(), tree);
00118 }
00119
00120 public CommonTreeNodeStream(TreeAdaptor adaptor, Object tree) {
00121 this(adaptor, tree, DEFAULT_INITIAL_BUFFER_SIZE);
00122 }
00123
00124 public CommonTreeNodeStream(TreeAdaptor adaptor, Object tree, int initialBufferSize) {
00125 this.root = tree;
00126 this.adaptor = adaptor;
00127 nodes = new ArrayList(initialBufferSize);
00128 down = adaptor.create(Token.DOWN, "DOWN");
00129 up = adaptor.create(Token.UP, "UP");
00130 eof = adaptor.create(Token.EOF, "EOF");
00131 }
00132
00136 protected void fillBuffer() {
00137 fillBuffer(root);
00138
00139 p = 0;
00140 }
00141
00142 public void fillBuffer(Object t) {
00143 boolean nil = adaptor.isNil(t);
00144 if ( !nil ) {
00145 nodes.add(t);
00146 }
00147
00148 int n = adaptor.getChildCount(t);
00149 if ( !nil && n>0 ) {
00150 addNavigationNode(Token.DOWN);
00151 }
00152
00153 for (int c=0; c<n; c++) {
00154 Object child = adaptor.getChild(t,c);
00155 fillBuffer(child);
00156 }
00157
00158 if ( !nil && n>0 ) {
00159 addNavigationNode(Token.UP);
00160 }
00161 }
00162
00166 protected int getNodeIndex(Object node) {
00167 if ( p==-1 ) {
00168 fillBuffer();
00169 }
00170 for (int i = 0; i < nodes.size(); i++) {
00171 Object t = (Object) nodes.get(i);
00172 if ( t==node ) {
00173 return i;
00174 }
00175 }
00176 return -1;
00177 }
00178
00183 protected void addNavigationNode(final int ttype) {
00184 Object navNode = null;
00185 if ( ttype==Token.DOWN ) {
00186 if ( hasUniqueNavigationNodes() ) {
00187 navNode = adaptor.create(Token.DOWN, "DOWN");
00188 }
00189 else {
00190 navNode = down;
00191 }
00192 }
00193 else {
00194 if ( hasUniqueNavigationNodes() ) {
00195 navNode = adaptor.create(Token.UP, "UP");
00196 }
00197 else {
00198 navNode = up;
00199 }
00200 }
00201 nodes.add(navNode);
00202 }
00203
00204 public Object get(int i) {
00205 if ( p==-1 ) {
00206 fillBuffer();
00207 }
00208 return nodes.get(i);
00209 }
00210
00211 public Object LT(int k) {
00212 if ( p==-1 ) {
00213 fillBuffer();
00214 }
00215 if ( k==0 ) {
00216 return null;
00217 }
00218 if ( k<0 ) {
00219 return LB(-k);
00220 }
00221
00222 if ( (p+k-1) >= nodes.size() ) {
00223 return eof;
00224 }
00225 return nodes.get(p+k-1);
00226 }
00227
00228 public Object getCurrentSymbol() { return LT(1); }
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00250 protected Object LB(int k) {
00251 if ( k==0 ) {
00252 return null;
00253 }
00254 if ( (p-k)<0 ) {
00255 return null;
00256 }
00257 return nodes.get(p-k);
00258 }
00259
00260 public Object getTreeSource() {
00261 return root;
00262 }
00263
00264 public String getSourceName() {
00265 return getTokenStream().getSourceName();
00266 }
00267
00268 public TokenStream getTokenStream() {
00269 return tokens;
00270 }
00271
00272 public void setTokenStream(TokenStream tokens) {
00273 this.tokens = tokens;
00274 }
00275
00276 public TreeAdaptor getTreeAdaptor() {
00277 return adaptor;
00278 }
00279
00280 public void setTreeAdaptor(TreeAdaptor adaptor) {
00281 this.adaptor = adaptor;
00282 }
00283
00284 public boolean hasUniqueNavigationNodes() {
00285 return uniqueNavigationNodes;
00286 }
00287
00288 public void setUniqueNavigationNodes(boolean uniqueNavigationNodes) {
00289 this.uniqueNavigationNodes = uniqueNavigationNodes;
00290 }
00291
00292 public void consume() {
00293 if ( p==-1 ) {
00294 fillBuffer();
00295 }
00296 p++;
00297 }
00298
00299 public int LA(int i) {
00300 return adaptor.getType(LT(i));
00301 }
00302
00303 public int mark() {
00304 if ( p==-1 ) {
00305 fillBuffer();
00306 }
00307 lastMarker = index();
00308 return lastMarker;
00309 }
00310
00311 public void release(int marker) {
00312
00313 }
00314
00315 public int index() {
00316 return p;
00317 }
00318
00319 public void rewind(int marker) {
00320 seek(marker);
00321 }
00322
00323 public void rewind() {
00324 seek(lastMarker);
00325 }
00326
00327 public void seek(int index) {
00328 if ( p==-1 ) {
00329 fillBuffer();
00330 }
00331 p = index;
00332 }
00333
00337 public void push(int index) {
00338 if ( calls==null ) {
00339 calls = new IntArray();
00340 }
00341 calls.push(p);
00342 seek(index);
00343 }
00344
00348 public int pop() {
00349 int ret = calls.pop();
00350 seek(ret);
00351 return ret;
00352 }
00353
00354 public void reset() {
00355 p = 0;
00356 lastMarker = 0;
00357 if (calls != null) {
00358 calls.clear();
00359 }
00360 }
00361
00362 public int size() {
00363 if ( p==-1 ) {
00364 fillBuffer();
00365 }
00366 return nodes.size();
00367 }
00368
00369 public Iterator iterator() {
00370 if ( p==-1 ) {
00371 fillBuffer();
00372 }
00373 return new StreamIterator();
00374 }
00375
00376
00377
00378 public void replaceChildren(Object parent, int startChildIndex, int stopChildIndex, Object t) {
00379 if ( parent!=null ) {
00380 adaptor.replaceChildren(parent, startChildIndex, stopChildIndex, t);
00381 }
00382 }
00383
00385 public String toString() {
00386 if ( p==-1 ) {
00387 fillBuffer();
00388 }
00389 StringBuffer buf = new StringBuffer();
00390 for (int i = 0; i < nodes.size(); i++) {
00391 Object t = (Object) nodes.get(i);
00392 buf.append(" ");
00393 buf.append(adaptor.getType(t));
00394 }
00395 return buf.toString();
00396 }
00397
00399 public String toTokenString(int start, int stop) {
00400 if ( p==-1 ) {
00401 fillBuffer();
00402 }
00403 StringBuffer buf = new StringBuffer();
00404 for (int i = start; i < nodes.size() && i <= stop; i++) {
00405 Object t = (Object) nodes.get(i);
00406 buf.append(" ");
00407 buf.append(adaptor.getToken(t));
00408 }
00409 return buf.toString();
00410 }
00411
00412 public String toString(Object start, Object stop) {
00413 System.out.println("toString");
00414 if ( start==null || stop==null ) {
00415 return null;
00416 }
00417 if ( p==-1 ) {
00418 fillBuffer();
00419 }
00420
00421 if ( start instanceof CommonTree )
00422 System.out.print("toString: "+((CommonTree)start).getToken()+", ");
00423 else
00424 System.out.println(start);
00425 if ( stop instanceof CommonTree )
00426 System.out.println(((CommonTree)stop).getToken());
00427 else
00428 System.out.println(stop);
00429
00430 if ( tokens!=null ) {
00431 int beginTokenIndex = adaptor.getTokenStartIndex(start);
00432 int endTokenIndex = adaptor.getTokenStopIndex(stop);
00433
00434
00435 if ( adaptor.getType(stop)==Token.UP ) {
00436 endTokenIndex = adaptor.getTokenStopIndex(start);
00437 }
00438 else if ( adaptor.getType(stop)==Token.EOF ) {
00439 endTokenIndex = size()-2;
00440 }
00441 return tokens.toString(beginTokenIndex, endTokenIndex);
00442 }
00443
00444 Object t = null;
00445 int i = 0;
00446 for (; i < nodes.size(); i++) {
00447 t = nodes.get(i);
00448 if ( t==start ) {
00449 break;
00450 }
00451 }
00452
00453 StringBuffer buf = new StringBuffer();
00454 t = nodes.get(i);
00455 while ( t!=stop ) {
00456 String text = adaptor.getText(t);
00457 if ( text==null ) {
00458 text = " "+String.valueOf(adaptor.getType(t));
00459 }
00460 buf.append(text);
00461 i++;
00462 t = nodes.get(i);
00463 }
00464
00465 String text = adaptor.getText(stop);
00466 if ( text==null ) {
00467 text = " "+String.valueOf(adaptor.getType(stop));
00468 }
00469 buf.append(text);
00470 return buf.toString();
00471 }
00472 }