PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/RewriteRuleElementStream.cs

https://bitbucket.org/jwalton/antlr3
C# | 253 lines | 128 code | 18 blank | 107 comment | 25 complexity | 1f22bc333c3fe093b6c30ba7343108a8 MD5 | raw file
  1. /*
  2. * [The "BSD licence"]
  3. * Copyright (c) 2005-2008 Terence Parr
  4. * All rights reserved.
  5. *
  6. * Conversion to C#:
  7. * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc.
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. * 3. The name of the author may not be used to endorse or promote products
  19. * derived from this software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  22. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  23. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  24. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  26. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  30. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. namespace Antlr.Runtime.Tree
  33. {
  34. using System.Collections.Generic;
  35. using IList = System.Collections.IList;
  36. /** <summary>
  37. * A generic list of elements tracked in an alternative to be used in
  38. * a -> rewrite rule. We need to subclass to fill in the next() method,
  39. * which returns either an AST node wrapped around a token payload or
  40. * an existing subtree.
  41. * </summary>
  42. *
  43. * <remarks>
  44. * Once you start next()ing, do not try to add more elements. It will
  45. * break the cursor tracking I believe.
  46. *
  47. * TODO: add mechanism to detect/puke on modification after reading from stream
  48. * </remarks>
  49. *
  50. * <see cref="RewriteRuleSubtreeStream"/>
  51. * <see cref="RewriteRuleTokenStream"/>
  52. */
  53. [System.Serializable]
  54. public abstract class RewriteRuleElementStream
  55. {
  56. /** <summary>
  57. * Cursor 0..n-1. If singleElement!=null, cursor is 0 until you next(),
  58. * which bumps it to 1 meaning no more elements.
  59. * </summary>
  60. */
  61. protected int cursor = 0;
  62. /** <summary>Track single elements w/o creating a list. Upon 2nd add, alloc list */
  63. protected object singleElement;
  64. /** <summary>The list of tokens or subtrees we are tracking */
  65. protected IList elements;
  66. /** <summary>Once a node / subtree has been used in a stream, it must be dup'd
  67. * from then on. Streams are reset after subrules so that the streams
  68. * can be reused in future subrules. So, reset must set a dirty bit.
  69. * If dirty, then next() always returns a dup.</summary>
  70. */
  71. protected bool dirty = false;
  72. /** <summary>The element or stream description; usually has name of the token or
  73. * rule reference that this list tracks. Can include rulename too, but
  74. * the exception would track that info.
  75. */
  76. protected string elementDescription;
  77. protected ITreeAdaptor adaptor;
  78. public RewriteRuleElementStream( ITreeAdaptor adaptor, string elementDescription )
  79. {
  80. this.elementDescription = elementDescription;
  81. this.adaptor = adaptor;
  82. }
  83. /** <summary>Create a stream with one element</summary> */
  84. public RewriteRuleElementStream( ITreeAdaptor adaptor, string elementDescription, object oneElement )
  85. : this( adaptor, elementDescription )
  86. {
  87. Add( oneElement );
  88. }
  89. /** <summary>Create a stream, but feed off an existing list</summary> */
  90. public RewriteRuleElementStream( ITreeAdaptor adaptor, string elementDescription, IList elements )
  91. : this( adaptor, elementDescription )
  92. {
  93. this.singleElement = null;
  94. this.elements = elements;
  95. }
  96. /** <summary>
  97. * Reset the condition of this stream so that it appears we have
  98. * not consumed any of its elements. Elements themselves are untouched.
  99. * Once we reset the stream, any future use will need duplicates. Set
  100. * the dirty bit.
  101. * </summary>
  102. */
  103. public virtual void Reset()
  104. {
  105. cursor = 0;
  106. dirty = true;
  107. }
  108. public virtual void Add( object el )
  109. {
  110. //System.out.println("add '"+elementDescription+"' is "+el);
  111. if ( el == null )
  112. {
  113. return;
  114. }
  115. if ( elements != null )
  116. { // if in list, just add
  117. elements.Add( el );
  118. return;
  119. }
  120. if ( singleElement == null )
  121. { // no elements yet, track w/o list
  122. singleElement = el;
  123. return;
  124. }
  125. // adding 2nd element, move to list
  126. elements = new List<object>( 5 );
  127. elements.Add( singleElement );
  128. singleElement = null;
  129. elements.Add( el );
  130. }
  131. /** <summary>
  132. * Return the next element in the stream. If out of elements, throw
  133. * an exception unless size()==1. If size is 1, then return elements[0].
  134. * Return a duplicate node/subtree if stream is out of elements and
  135. * size==1. If we've already used the element, dup (dirty bit set).
  136. * </summary>
  137. */
  138. public virtual object NextTree()
  139. {
  140. int n = Count;
  141. if ( dirty || ( cursor >= n && n == 1 ) )
  142. {
  143. // if out of elements and size is 1, dup
  144. object el = NextCore();
  145. return Dup( el );
  146. }
  147. // test size above then fetch
  148. object el2 = NextCore();
  149. return el2;
  150. }
  151. /** <summary>
  152. * Do the work of getting the next element, making sure that it's
  153. * a tree node or subtree. Deal with the optimization of single-
  154. * element list versus list of size > 1. Throw an exception
  155. * if the stream is empty or we're out of elements and size>1.
  156. * protected so you can override in a subclass if necessary.
  157. * </summary>
  158. */
  159. protected virtual object NextCore()
  160. {
  161. int n = Count;
  162. if ( n == 0 )
  163. {
  164. throw new RewriteEmptyStreamException( elementDescription );
  165. }
  166. if ( cursor >= n )
  167. { // out of elements?
  168. if ( n == 1 )
  169. { // if size is 1, it's ok; return and we'll dup
  170. return ToTree( singleElement );
  171. }
  172. // out of elements and size was not 1, so we can't dup
  173. throw new RewriteCardinalityException( elementDescription );
  174. }
  175. // we have elements
  176. if ( singleElement != null )
  177. {
  178. cursor++; // move cursor even for single element list
  179. return ToTree( singleElement );
  180. }
  181. // must have more than one in list, pull from elements
  182. object o = ToTree( elements[cursor] );
  183. cursor++;
  184. return o;
  185. }
  186. /** <summary>
  187. * When constructing trees, sometimes we need to dup a token or AST
  188. * subtree. Dup'ing a token means just creating another AST node
  189. * around it. For trees, you must call the adaptor.dupTree() unless
  190. * the element is for a tree root; then it must be a node dup.
  191. * </summary>
  192. */
  193. protected abstract object Dup( object el );
  194. /** <summary>
  195. * Ensure stream emits trees; tokens must be converted to AST nodes.
  196. * AST nodes can be passed through unmolested.
  197. * </summary>
  198. */
  199. protected virtual object ToTree( object el )
  200. {
  201. return el;
  202. }
  203. public virtual bool HasNext
  204. {
  205. get
  206. {
  207. return ( singleElement != null && cursor < 1 ) ||
  208. ( elements != null && cursor < elements.Count );
  209. }
  210. }
  211. public virtual int Count
  212. {
  213. get
  214. {
  215. int n = 0;
  216. if ( singleElement != null )
  217. {
  218. n = 1;
  219. }
  220. if ( elements != null )
  221. {
  222. return elements.Count;
  223. }
  224. return n;
  225. }
  226. }
  227. public virtual string Description
  228. {
  229. get
  230. {
  231. return elementDescription;
  232. }
  233. }
  234. }
  235. }