PageRenderTime 38ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/cyanogenmod/android_external_antlr
C# | 230 lines | 102 code | 18 blank | 110 comment | 25 complexity | c53e0cda086755c8090c911f4f170fed 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. using System.Collections.Generic;
  34. using IList = System.Collections.IList;
  35. /** <summary>
  36. * A generic list of elements tracked in an alternative to be used in
  37. * a -> rewrite rule. We need to subclass to fill in the next() method,
  38. * which returns either an AST node wrapped around a token payload or
  39. * an existing subtree.
  40. * </summary>
  41. *
  42. * <remarks>
  43. * Once you start next()ing, do not try to add more elements. It will
  44. * break the cursor tracking I believe.
  45. *
  46. * TODO: add mechanism to detect/puke on modification after reading from stream
  47. * </remarks>
  48. *
  49. * <see cref="RewriteRuleSubtreeStream"/>
  50. * <see cref="RewriteRuleTokenStream"/>
  51. */
  52. [System.Serializable]
  53. public abstract class RewriteRuleElementStream {
  54. /** <summary>
  55. * Cursor 0..n-1. If singleElement!=null, cursor is 0 until you next(),
  56. * which bumps it to 1 meaning no more elements.
  57. * </summary>
  58. */
  59. protected int cursor = 0;
  60. /** <summary>Track single elements w/o creating a list. Upon 2nd add, alloc list */
  61. protected object singleElement;
  62. /** <summary>The list of tokens or subtrees we are tracking */
  63. protected IList elements;
  64. /** <summary>Once a node / subtree has been used in a stream, it must be dup'd
  65. * from then on. Streams are reset after subrules so that the streams
  66. * can be reused in future subrules. So, reset must set a dirty bit.
  67. * If dirty, then next() always returns a dup.
  68. *
  69. * I wanted to use "naughty bit" here, but couldn't think of a way
  70. * to use "naughty".
  71. */
  72. protected bool dirty = false;
  73. /** <summary>The element or stream description; usually has name of the token or
  74. * rule reference that this list tracks. Can include rulename too, but
  75. * the exception would track that info.
  76. */
  77. protected string elementDescription;
  78. protected ITreeAdaptor adaptor;
  79. public RewriteRuleElementStream(ITreeAdaptor adaptor, string elementDescription) {
  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. Add(oneElement);
  87. }
  88. /** <summary>Create a stream, but feed off an existing list</summary> */
  89. public RewriteRuleElementStream(ITreeAdaptor adaptor, string elementDescription, IList elements)
  90. : this(adaptor, elementDescription) {
  91. this.singleElement = null;
  92. this.elements = elements;
  93. }
  94. /** <summary>
  95. * Reset the condition of this stream so that it appears we have
  96. * not consumed any of its elements. Elements themselves are untouched.
  97. * Once we reset the stream, any future use will need duplicates. Set
  98. * the dirty bit.
  99. * </summary>
  100. */
  101. public virtual void Reset() {
  102. cursor = 0;
  103. dirty = true;
  104. }
  105. public virtual void Add(object el) {
  106. //System.out.println("add '"+elementDescription+"' is "+el);
  107. if (el == null) {
  108. return;
  109. }
  110. if (elements != null) { // if in list, just add
  111. elements.Add(el);
  112. return;
  113. }
  114. if (singleElement == null) { // no elements yet, track w/o list
  115. singleElement = el;
  116. return;
  117. }
  118. // adding 2nd element, move to list
  119. elements = new List<object>(5);
  120. elements.Add(singleElement);
  121. singleElement = null;
  122. elements.Add(el);
  123. }
  124. /** <summary>
  125. * Return the next element in the stream. If out of elements, throw
  126. * an exception unless size()==1. If size is 1, then return elements[0].
  127. * Return a duplicate node/subtree if stream is out of elements and
  128. * size==1. If we've already used the element, dup (dirty bit set).
  129. * </summary>
  130. */
  131. public virtual object NextTree() {
  132. int n = Count;
  133. if (dirty || (cursor >= n && n == 1)) {
  134. // if out of elements and size is 1, dup
  135. object el = NextCore();
  136. return Dup(el);
  137. }
  138. // test size above then fetch
  139. object el2 = NextCore();
  140. return el2;
  141. }
  142. /** <summary>
  143. * Do the work of getting the next element, making sure that it's
  144. * a tree node or subtree. Deal with the optimization of single-
  145. * element list versus list of size > 1. Throw an exception
  146. * if the stream is empty or we're out of elements and size>1.
  147. * protected so you can override in a subclass if necessary.
  148. * </summary>
  149. */
  150. protected virtual object NextCore() {
  151. int n = Count;
  152. if (n == 0) {
  153. throw new RewriteEmptyStreamException(elementDescription);
  154. }
  155. if (cursor >= n) { // out of elements?
  156. if (n == 1) { // if size is 1, it's ok; return and we'll dup
  157. return ToTree(singleElement);
  158. }
  159. // out of elements and size was not 1, so we can't dup
  160. throw new RewriteCardinalityException(elementDescription);
  161. }
  162. // we have elements
  163. if (singleElement != null) {
  164. cursor++; // move cursor even for single element list
  165. return ToTree(singleElement);
  166. }
  167. // must have more than one in list, pull from elements
  168. object o = ToTree(elements[cursor]);
  169. cursor++;
  170. return o;
  171. }
  172. /** <summary>
  173. * When constructing trees, sometimes we need to dup a token or AST
  174. * subtree. Dup'ing a token means just creating another AST node
  175. * around it. For trees, you must call the adaptor.dupTree() unless
  176. * the element is for a tree root; then it must be a node dup.
  177. * </summary>
  178. */
  179. protected abstract object Dup(object el);
  180. /** <summary>
  181. * Ensure stream emits trees; tokens must be converted to AST nodes.
  182. * AST nodes can be passed through unmolested.
  183. * </summary>
  184. */
  185. protected virtual object ToTree(object el) {
  186. return el;
  187. }
  188. public virtual bool HasNext {
  189. get {
  190. return (singleElement != null && cursor < 1) ||
  191. (elements != null && cursor < elements.Count);
  192. }
  193. }
  194. public virtual int Count {
  195. get {
  196. int n = 0;
  197. if (singleElement != null) {
  198. n = 1;
  199. }
  200. if (elements != null) {
  201. return elements.Count;
  202. }
  203. return n;
  204. }
  205. }
  206. public virtual string Description {
  207. get {
  208. return elementDescription;
  209. }
  210. }
  211. }
  212. }