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
00029
00030
00031
00032
00033
00034
00035
00036 namespace Antlr.Runtime.Tree
00037 {
00038 using System;
00039 using IEnumerable = System.Collections.IEnumerable;
00040 using IEnumerator = System.Collections.IEnumerator;
00041 using IDictionary = System.Collections.IDictionary;
00042 using IList = System.Collections.IList;
00043 using ArrayList = System.Collections.ArrayList;
00044 using Hashtable = System.Collections.Hashtable;
00045 using StringBuilder = System.Text.StringBuilder;
00046 using StackList = Antlr.Runtime.Collections.StackList;
00047 using Token = Antlr.Runtime.Token;
00048 using ITokenStream = Antlr.Runtime.ITokenStream;
00049
00069 public class CommonTreeNodeStream : ITreeNodeStream, IEnumerable
00070 {
00071 public const int DEFAULT_INITIAL_BUFFER_SIZE = 100;
00072 public const int INITIAL_CALL_STACK_SIZE = 10;
00073
00074 #region Helper classes
00075 protected sealed class CommonTreeNodeStreamEnumerator : IEnumerator
00076 {
00077 private CommonTreeNodeStream _nodeStream;
00078 private int _index;
00079 private object _currentItem;
00080
00081 #region Constructors
00082
00083 internal CommonTreeNodeStreamEnumerator()
00084 {
00085 }
00086
00087 internal CommonTreeNodeStreamEnumerator(CommonTreeNodeStream nodeStream)
00088 {
00089 _nodeStream = nodeStream;
00090 Reset();
00091 }
00092
00093 #endregion
00094
00095 #region IEnumerator Members
00096
00097 public void Reset()
00098 {
00099
00100
00101
00102
00103 _index = 0;
00104 _currentItem = null;
00105 }
00106
00107 public object Current
00108 {
00109 get
00110 {
00111 if (_currentItem == null)
00112 {
00113 throw new InvalidOperationException("Enumeration has either not started or has already finished.");
00114 }
00115 return _currentItem;
00116 }
00117 }
00118
00119 public bool MoveNext()
00120 {
00121
00122
00123
00124
00125
00126 if ( _index >= _nodeStream.nodes.Count )
00127 {
00128 int current = _index;
00129 _index++;
00130 if ( current < _nodeStream.nodes.Count )
00131 {
00132 _currentItem = _nodeStream.nodes[current];
00133 }
00134 _currentItem = _nodeStream.eof;
00135 return true;
00136 }
00137 _currentItem = null;
00138 return false;
00139 }
00140
00141 #endregion
00142 }
00143
00144 #endregion
00145
00146 #region IEnumerable Members
00147
00148 public IEnumerator GetEnumerator()
00149 {
00150 if ( p == -1 )
00151 {
00152 FillBuffer();
00153 }
00154 return new CommonTreeNodeStreamEnumerator(this);
00155 }
00156
00157 #endregion
00158
00159 #region Data Members
00160
00161
00162
00163 protected object down;
00164 protected object up;
00165 protected object eof;
00166
00176 protected IList nodes;
00177
00179 protected internal object root;
00180
00182 protected ITokenStream tokens;
00183
00185 ITreeAdaptor adaptor;
00186
00190 protected bool uniqueNavigationNodes = false;
00191
00196 protected int p = -1;
00197
00201 protected int lastMarker;
00202
00206 protected StackList calls;
00207
00208 #endregion
00209
00210 #region Constructors
00211
00212 public CommonTreeNodeStream(object tree)
00213 : this(new CommonTreeAdaptor(), tree)
00214 {
00215 }
00216
00217 public CommonTreeNodeStream(ITreeAdaptor adaptor, object tree)
00218 : this(adaptor, tree, DEFAULT_INITIAL_BUFFER_SIZE)
00219 {
00220 }
00221
00222 public CommonTreeNodeStream(ITreeAdaptor adaptor, object tree, int initialBufferSize)
00223 {
00224 this.root = tree;
00225 this.adaptor = adaptor;
00226 nodes = new ArrayList(initialBufferSize);
00227 down = adaptor.Create(Token.DOWN, "DOWN");
00228 up = adaptor.Create(Token.UP, "UP");
00229 eof = adaptor.Create(Token.EOF, "EOF");
00230 }
00231
00232 #endregion
00233
00234 #region Public API
00235
00240 protected void FillBuffer()
00241 {
00242 FillBuffer(root);
00243 p = 0;
00244 }
00245
00246 public void FillBuffer(object t)
00247 {
00248 bool nil = adaptor.IsNil(t);
00249 if ( !nil )
00250 {
00251 nodes.Add(t);
00252 }
00253
00254 int n = adaptor.GetChildCount(t);
00255 if ( !nil && (n > 0) )
00256 {
00257 AddNavigationNode(Token.DOWN);
00258 }
00259
00260 for (int c = 0; c < n; c++)
00261 {
00262 object child = adaptor.GetChild(t, c);
00263 FillBuffer(child);
00264 }
00265
00266 if ( !nil && (n > 0) )
00267 {
00268 AddNavigationNode(Token.UP);
00269 }
00270 }
00271
00276 protected int GetNodeIndex(object node)
00277 {
00278 if ( p == -1 )
00279 {
00280 FillBuffer();
00281 }
00282 for (int i = 0; i < nodes.Count; i++)
00283 {
00284 object t = (object) nodes[i];
00285 if ( t == node )
00286 {
00287 return i;
00288 }
00289 }
00290 return -1;
00291 }
00292
00298 protected void AddNavigationNode(int ttype)
00299 {
00300 object navNode = null;
00301 if ( ttype == Token.DOWN )
00302 {
00303 if ( HasUniqueNavigationNodes )
00304 {
00305 navNode = adaptor.Create(Token.DOWN, "DOWN");
00306 }
00307 else
00308 {
00309 navNode = down;
00310 }
00311 }
00312 else
00313 {
00314 if ( HasUniqueNavigationNodes )
00315 {
00316 navNode = adaptor.Create(Token.UP, "UP");
00317 }
00318 else
00319 {
00320 navNode = up;
00321 }
00322 }
00323 nodes.Add(navNode);
00324 }
00325
00326 public object Get(int i)
00327 {
00328 if ( p == -1 )
00329 {
00330 FillBuffer();
00331 }
00332 return nodes[i];
00333 }
00334
00335 public object LT(int k)
00336 {
00337 if ( p == -1 )
00338 {
00339 FillBuffer();
00340 }
00341 if ( k == 0 )
00342 {
00343 return null;
00344 }
00345 if ( k < 0 )
00346 {
00347 return LB(-k);
00348 }
00349 if ( (p+k-1) >= nodes.Count )
00350 {
00351 return eof;
00352 }
00353 return nodes[p+k-1];
00354 }
00355
00356 public virtual object CurrentSymbol {
00357 get { return LT(1); }
00358 }
00359
00363 protected object LB(int k)
00364 {
00365 if ( k == 0 )
00366 {
00367 return null;
00368 }
00369 if ( (p-k) < 0 )
00370 {
00371 return null;
00372 }
00373 return nodes[p-k];
00374 }
00375
00380 virtual public object TreeSource
00381 {
00382 get { return root; }
00383 }
00384
00385 virtual public string SourceName {
00386 get { return TokenStream.SourceName; }
00387 }
00388
00389 virtual public ITokenStream TokenStream
00390 {
00391 get { return tokens; }
00392 set { this.tokens = value; }
00393 }
00394
00395 public ITreeAdaptor TreeAdaptor
00396 {
00397 get { return adaptor; }
00398 set { adaptor = value; }
00399 }
00400
00401 public bool HasUniqueNavigationNodes
00402 {
00403 get { return uniqueNavigationNodes; }
00404 set { uniqueNavigationNodes = value; }
00405 }
00406
00411 public void Push(int index)
00412 {
00413 if ( calls == null )
00414 {
00415 calls = new StackList();
00416 }
00417 calls.Push(p);
00418 Seek(index);
00419 }
00420
00425 public int Pop()
00426 {
00427 int ret = (int)calls.Pop();
00428 Seek(ret);
00429 return ret;
00430 }
00431
00432 public void Reset()
00433 {
00434 p = -1;
00435 lastMarker = 0;
00436 if (calls != null)
00437 {
00438 calls.Clear();
00439 }
00440 }
00441
00442 #endregion
00443
00444 #region Tree Rewrite Interface
00445
00446 public void ReplaceChildren(object parent, int startChildIndex, int stopChildIndex, object t)
00447 {
00448 if (parent != null)
00449 {
00450 adaptor.ReplaceChildren(parent, startChildIndex, stopChildIndex, t);
00451 }
00452 }
00453
00454 #endregion
00455
00456 #region Satisfy IntStream interface
00457
00458 public virtual void Consume()
00459 {
00460 if (p == -1)
00461 {
00462 FillBuffer();
00463 }
00464 p++;
00465 }
00466
00467 public virtual int LA(int i)
00468 {
00469 return adaptor.GetNodeType( LT(i) );
00470 }
00471
00476 public virtual int Mark()
00477 {
00478 if ( p == -1 )
00479 {
00480 FillBuffer();
00481 }
00482 lastMarker = Index();
00483 return lastMarker;
00484 }
00485
00486 public virtual void Release(int marker)
00487 {
00488
00489 }
00490
00498 public virtual void Rewind(int marker)
00499 {
00500 Seek(marker);
00501 }
00502
00503 public void Rewind()
00504 {
00505 Seek(lastMarker);
00506 }
00507
00512 public virtual void Seek(int index)
00513 {
00514 if ( p == -1 )
00515 {
00516 FillBuffer();
00517 }
00518 p = index;
00519 }
00520
00521 public virtual int Index()
00522 {
00523 return p;
00524 }
00525
00531 public virtual int Size()
00532 {
00533 if ( p == -1 )
00534 {
00535 FillBuffer();
00536 }
00537 return nodes.Count;
00538 }
00539
00540 #endregion
00541
00545 public override string ToString()
00546 {
00547 if ( p == -1 )
00548 {
00549 FillBuffer();
00550 }
00551 StringBuilder buf = new StringBuilder();
00552 for (int i = 0; i < nodes.Count; i++)
00553 {
00554 object t = (object) nodes[i];
00555 buf.Append(" ");
00556 buf.Append(adaptor.GetNodeType(t));
00557 }
00558 return buf.ToString();
00559 }
00560
00562 public String ToTokenString(int start, int stop) {
00563 if ( p==-1 ) {
00564 FillBuffer();
00565 }
00566 StringBuilder buf = new StringBuilder();
00567 for (int i = start; i < nodes.Count && i <= stop; i++) {
00568 Object t = (Object) nodes[i];
00569 buf.Append(" ");
00570 buf.Append(adaptor.GetToken(t));
00571 }
00572 return buf.ToString();
00573 }
00574
00575 public virtual string ToString(object start, object stop)
00576 {
00577 Console.Out.WriteLine("ToString");
00578 if ( (start == null) || (stop == null) )
00579 {
00580 return null;
00581 }
00582 if ( p == -1 )
00583 {
00584 FillBuffer();
00585 }
00586
00587 if ( start is CommonTree )
00588 Console.Out.Write("ToString: " + ((CommonTree)start).Token + ", ");
00589 else
00590 Console.Out.WriteLine(start);
00591 if ( stop is CommonTree )
00592 Console.Out.WriteLine(((CommonTree)stop).Token);
00593 else
00594 Console.Out.WriteLine(stop);
00595
00596 if ( tokens != null )
00597 {
00598 int beginTokenIndex = adaptor.GetTokenStartIndex(start);
00599 int endTokenIndex = adaptor.GetTokenStopIndex(stop);
00600
00601
00602 if ( adaptor.GetNodeType(stop) == Token.UP )
00603 {
00604 endTokenIndex = adaptor.GetTokenStopIndex(start);
00605 }
00606 else if ( adaptor.GetNodeType(stop) == Token.EOF )
00607 {
00608 endTokenIndex = Size()-2;
00609 }
00610 return tokens.ToString(beginTokenIndex, endTokenIndex);
00611 }
00612
00613 object t = null;
00614 int i = 0;
00615 for (; i < nodes.Count; i++)
00616 {
00617 t = nodes[i];
00618 if ( t == start )
00619 {
00620 break;
00621 }
00622 }
00623
00624 StringBuilder buf = new StringBuilder();
00625 t = nodes[i];
00626 string text;
00627 while ( t != stop )
00628 {
00629 text = adaptor.GetNodeText(t);
00630 if ( text == null )
00631 {
00632 text = " " + adaptor.GetNodeType(t);
00633 }
00634 buf.Append(text);
00635 i++;
00636 t = nodes[i];
00637 }
00638
00639 text = adaptor.GetNodeText(stop);
00640 if ( text == null )
00641 {
00642 text = " " + adaptor.GetNodeType(stop);
00643 }
00644 buf.Append(text);
00645 return buf.ToString();
00646 }
00647
00648 }
00649 }