PageRenderTime 310ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/antlr-3.4/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/RewriteRuleElementStream.cs

https://bitbucket.org/cyanogenmod/android_external_antlr
C# | 256 lines | 128 code | 18 blank | 110 comment | 25 complexity | eec61b4082e36c1210be477b9e6ba2c3 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.
  70. *
  71. * I wanted to use "naughty bit" here, but couldn't think of a way
  72. * to use "naughty".
  73. */
  74. protected bool dirty = false;
  75. /** <summary>The element or stream description; usually has name of the token or
  76. * rule reference that this list tracks. Can include rulename too, but
  77. * the exception would track that info.
  78. */
  79. protected string elementDescription;
  80. protected ITreeAdaptor adaptor;
  81. public RewriteRuleElementStream( ITreeAdaptor adaptor, string elementDescription )
  82. {
  83. this.elementDescription = elementDescription;
  84. this.adaptor = adaptor;
  85. }
  86. /** <summary>Create a stream with one element</summary> */
  87. public RewriteRuleElementStream( ITreeAdaptor adaptor, string elementDescription, object oneElement )
  88. : this( adaptor, elementDescription )
  89. {
  90. Add( oneElement );
  91. }
  92. /** <summary>Create a stream, but feed off an existing list</summary> */
  93. public RewriteRuleElementStream( ITreeAdaptor adaptor, string elementDescription, IList elements )
  94. : this( adaptor, elementDescription )
  95. {
  96. this.singleElement = null;
  97. this.elements = elements;
  98. }
  99. /** <summary>
  100. * Reset the condition of this stream so that it appears we have
  101. * not consumed any of its elements. Elements themselves are untouched.
  102. * Once we reset the stream, any future use will need duplicates. Set
  103. * the dirty bit.
  104. * </summary>
  105. */
  106. public virtual void Reset()
  107. {
  108. cursor = 0;
  109. dirty = true;
  110. }
  111. public virtual void Add( object el )
  112. {
  113. //System.out.println("add '"+elementDescription+"' is "+el);
  114. if ( el == null )
  115. {
  116. return;
  117. }
  118. if ( elements != null )
  119. { // if in list, just add
  120. elements.Add( el );
  121. return;
  122. }
  123. if ( singleElement == null )
  124. { // no elements yet, track w/o list
  125. singleElement = el;
  126. return;
  127. }
  128. // adding 2nd element, move to list
  129. elements = new List<object>( 5 );
  130. elements.Add( singleElement );
  131. singleElement = null;
  132. elements.Add( el );
  133. }
  134. /** <summary>
  135. * Return the next element in the stream. If out of elements, throw
  136. * an exception unless size()==1. If size is 1, then return elements[0].
  137. * Return a duplicate node/subtree if stream is out of elements and
  138. * size==1. If we've already used the element, dup (dirty bit set).
  139. * </summary>
  140. */
  141. public virtual object NextTree()
  142. {
  143. int n = Count;
  144. if ( dirty || ( cursor >= n && n == 1 ) )
  145. {
  146. // if out of elements and size is 1, dup
  147. object el = NextCore();
  148. return Dup( el );
  149. }
  150. // test size above then fetch
  151. object el2 = NextCore();
  152. return el2;
  153. }
  154. /** <summary>
  155. * Do the work of getting the next element, making sure that it's
  156. * a tree node or subtree. Deal with the optimization of single-
  157. * element list versus list of size > 1. Throw an exception
  158. * if the stream is empty or we're out of elements and size>1.
  159. * protected so you can override in a subclass if necessary.
  160. * </summary>
  161. */
  162. protected virtual object NextCore()
  163. {
  164. int n = Count;
  165. if ( n == 0 )
  166. {
  167. throw new RewriteEmptyStreamException( elementDescription );
  168. }
  169. if ( cursor >= n )
  170. { // out of elements?
  171. if ( n == 1 )
  172. { // if size is 1, it's ok; return and we'll dup
  173. return ToTree( singleElement );
  174. }
  175. // out of elements and size was not 1, so we can't dup
  176. throw new RewriteCardinalityException( elementDescription );
  177. }
  178. // we have elements
  179. if ( singleElement != null )
  180. {
  181. cursor++; // move cursor even for single element list
  182. return ToTree( singleElement );
  183. }
  184. // must have more than one in list, pull from elements
  185. object o = ToTree( elements[cursor] );
  186. cursor++;
  187. return o;
  188. }
  189. /** <summary>
  190. * When constructing trees, sometimes we need to dup a token or AST
  191. * subtree. Dup'ing a token means just creating another AST node
  192. * around it. For trees, you must call the adaptor.dupTree() unless
  193. * the element is for a tree root; then it must be a node dup.
  194. * </summary>
  195. */
  196. protected abstract object Dup( object el );
  197. /** <summary>
  198. * Ensure stream emits trees; tokens must be converted to AST nodes.
  199. * AST nodes can be passed through unmolested.
  200. * </summary>
  201. */
  202. protected virtual object ToTree( object el )
  203. {
  204. return el;
  205. }
  206. public virtual bool HasNext
  207. {
  208. get
  209. {
  210. return ( singleElement != null && cursor < 1 ) ||
  211. ( elements != null && cursor < elements.Count );
  212. }
  213. }
  214. public virtual int Count
  215. {
  216. get
  217. {
  218. int n = 0;
  219. if ( singleElement != null )
  220. {
  221. n = 1;
  222. }
  223. if ( elements != null )
  224. {
  225. return elements.Count;
  226. }
  227. return n;
  228. }
  229. }
  230. public virtual string Description
  231. {
  232. get
  233. {
  234. return elementDescription;
  235. }
  236. }
  237. }
  238. }