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 #if DOTNET1
00037 namespace Antlr.Runtime.Tree
00038 {
00039 using System;
00040 using IList = System.Collections.IList;
00041 using ArrayList = System.Collections.ArrayList;
00042 using IToken = Antlr.Runtime.IToken;
00043 using CommonToken = Antlr.Runtime.CommonToken;
00044
00059 public abstract class RewriteRuleElementStream
00060 {
00065 protected int cursor = 0;
00066
00070 protected object singleElement;
00071
00075 protected IList elements;
00076
00086 protected bool dirty = false;
00087
00093 protected string elementDescription;
00094 protected ITreeAdaptor adaptor;
00095
00096 public RewriteRuleElementStream(ITreeAdaptor adaptor, string elementDescription)
00097 {
00098 this.elementDescription = elementDescription;
00099 this.adaptor = adaptor;
00100 }
00101
00105 public RewriteRuleElementStream(ITreeAdaptor adaptor, string elementDescription, object oneElement)
00106 : this(adaptor, elementDescription)
00107 {
00108 Add(oneElement);
00109 }
00110
00114 public RewriteRuleElementStream(ITreeAdaptor adaptor, string elementDescription, IList elements)
00115 : this(adaptor, elementDescription)
00116 {
00117 this.singleElement = null;
00118 this.elements = elements;
00119 }
00120
00121 public void Add(object el)
00122 {
00123 if ( el == null )
00124 {
00125 return;
00126 }
00127 if ( elements != null )
00128 {
00129 elements.Add(el);
00130 return;
00131 }
00132 if ( singleElement == null )
00133 {
00134 singleElement = el;
00135 return;
00136 }
00137
00138 elements = new ArrayList(5);
00139 elements.Add(singleElement);
00140 singleElement = null;
00141 elements.Add(el);
00142 }
00143
00152 public virtual void Reset()
00153 {
00154 cursor = 0;
00155 dirty = true;
00156 }
00157
00158 public bool HasNext()
00159 {
00160 return ( ((singleElement != null) && (cursor < 1)) ||
00161 ((elements != null) && (cursor < elements.Count)) );
00162 }
00163
00174 public virtual object NextTree()
00175 {
00176 int size = Size();
00177 if ( dirty || ((cursor >= size) && (size == 1)) )
00178 {
00179
00180 return Dup(_Next());
00181 }
00182
00183 return _Next();
00184 }
00185
00195 protected object _Next()
00196 {
00197 int size = Size();
00198 if ( size == 0 )
00199 {
00200 throw new RewriteEmptyStreamException(elementDescription);
00201 }
00202 if ( cursor >= size )
00203 {
00204 if ( size == 1 )
00205 {
00206 return ToTree(singleElement);
00207 }
00208
00209 throw new RewriteCardinalityException(elementDescription);
00210 }
00211
00212 if ( singleElement != null )
00213 {
00214 cursor++;
00215 return ToTree(singleElement);
00216 }
00217
00218 return ToTree(elements[cursor++]);
00219 }
00220
00227 protected abstract object Dup(object el);
00228
00233 protected virtual object ToTree(object el)
00234 {
00235 return el;
00236 }
00237
00238 #warning Size() should be converted into a property named Count.
00239 public int Size()
00240 {
00241 if (singleElement != null) {
00242 return 1;
00243 }
00244 if (elements != null) {
00245 return elements.Count;
00246 }
00247 return 0;
00248 }
00249
00250 public string Description
00251 {
00252 get { return elementDescription; }
00253 }
00254 }
00255 }
00256 #elif DOTNET2
00257 namespace Antlr.Runtime.Tree {
00258 using System;
00259 using System.Collections.Generic;
00260 using IToken = Antlr.Runtime.IToken;
00261 using CommonToken = Antlr.Runtime.CommonToken;
00262
00277 public abstract class RewriteRuleElementStream<T> {
00282 protected int cursor = 0;
00283
00287 protected T singleElement;
00288
00292 protected IList<T> elements;
00293
00303 protected bool dirty = false;
00304
00310 protected string elementDescription;
00311 protected ITreeAdaptor adaptor;
00312
00313 public RewriteRuleElementStream(ITreeAdaptor adaptor, string elementDescription) {
00314 this.elementDescription = elementDescription;
00315 this.adaptor = adaptor;
00316 }
00317
00321 public RewriteRuleElementStream(
00322 ITreeAdaptor adaptor,
00323 string elementDescription,
00324 T oneElement
00325 )
00326 : this(adaptor, elementDescription) {
00327 Add(oneElement);
00328 }
00329
00333 public RewriteRuleElementStream(
00334 ITreeAdaptor adaptor,
00335 string elementDescription,
00336 IList<T> elements
00337 )
00338 : this(adaptor, elementDescription) {
00339 this.singleElement = default(T);
00340 this.elements = elements;
00341 }
00342
00346 [Obsolete("This constructor is for internal use only and might be phased out soon. Use instead the one with IList<T>.")]
00347 public RewriteRuleElementStream(
00348 ITreeAdaptor adaptor,
00349 string elementDescription,
00350 System.Collections.IList elements
00351 )
00352 : this(adaptor, elementDescription) {
00353 this.singleElement = default(T);
00354 this.elements = new List<T>();
00355 if (elements != null) {
00356 foreach (T t in elements) {
00357 this.elements.Add(t);
00358 }
00359 }
00360 }
00361
00362 public void Add(T el) {
00363 if (el == null) {
00364 return;
00365 }
00366 if (elements != null) {
00367 elements.Add(el);
00368 return;
00369 }
00370 if (singleElement == null) {
00371 singleElement = el;
00372 return;
00373 }
00374
00375 elements = new List<T>(5);
00376 elements.Add(singleElement);
00377 singleElement = default(T);
00378 elements.Add(el);
00379 }
00380
00389 public virtual void Reset() {
00390 cursor = 0;
00391 dirty = true;
00392 }
00393
00394 public bool HasNext() {
00395 return (((singleElement != null) && (cursor < 1)) ||
00396 ((elements != null) && (cursor < elements.Count)));
00397 }
00398
00402 public virtual object NextTree() {
00403 return _Next();
00404 }
00405
00415 protected object _Next() {
00416 int size = Size();
00417 if (size == 0) {
00418 throw new RewriteEmptyStreamException(elementDescription);
00419 }
00420 if (cursor >= size) {
00421 if (size == 1) {
00422 return ToTree(singleElement);
00423 }
00424
00425 throw new RewriteCardinalityException(elementDescription);
00426 }
00427
00428 if (singleElement != null) {
00429 cursor++;
00430 return ToTree(singleElement);
00431 }
00432
00433 return ToTree(elements[cursor++]);
00434 }
00435
00440 protected virtual object ToTree(T el) {
00441 return el;
00442 }
00443
00444 #warning Size() should be converted into a property named Count.
00445 public int Size() {
00446 if (singleElement != null) {
00447 return 1;
00448 }
00449 if (elements != null) {
00450 return elements.Count;
00451 }
00452 return 0;
00453 }
00454
00455 public string Description {
00456 get { return elementDescription; }
00457 }
00458 }
00459 }
00460 #endif