PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/runtime/Java/src/main/java/org/antlr/runtime/tree/RewriteRuleSubtreeStream.java

https://bitbucket.org/jwalton/antlr3
Java | 89 lines | 35 code | 7 blank | 47 comment | 6 complexity | 8f486c6ab3a617c61d3eaad3f55cba98 MD5 | raw file
  1. /*
  2. [The "BSD license"]
  3. Copyright (c) 2005-2009 Terence Parr
  4. All rights reserved.
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions
  7. are met:
  8. 1. Redistributions of source code must retain the above copyright
  9. notice, this list of conditions and the following disclaimer.
  10. 2. Redistributions in binary form must reproduce the above copyright
  11. notice, this list of conditions and the following disclaimer in the
  12. documentation and/or other materials provided with the distribution.
  13. 3. The name of the author may not be used to endorse or promote products
  14. derived from this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  16. IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  17. OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  18. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  19. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  24. THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. package org.antlr.runtime.tree;
  27. import java.util.List;
  28. public class RewriteRuleSubtreeStream extends RewriteRuleElementStream {
  29. public RewriteRuleSubtreeStream(TreeAdaptor adaptor, String elementDescription) {
  30. super(adaptor, elementDescription);
  31. }
  32. /** Create a stream with one element */
  33. public RewriteRuleSubtreeStream(TreeAdaptor adaptor,
  34. String elementDescription,
  35. Object oneElement)
  36. {
  37. super(adaptor, elementDescription, oneElement);
  38. }
  39. /** Create a stream, but feed off an existing list */
  40. public RewriteRuleSubtreeStream(TreeAdaptor adaptor,
  41. String elementDescription,
  42. List<Object> elements)
  43. {
  44. super(adaptor, elementDescription, elements);
  45. }
  46. /** Treat next element as a single node even if it's a subtree.
  47. * This is used instead of next() when the result has to be a
  48. * tree root node. Also prevents us from duplicating recently-added
  49. * children; e.g., ^(type ID)+ adds ID to type and then 2nd iteration
  50. * must dup the type node, but ID has been added.
  51. *
  52. * Referencing a rule result twice is ok; dup entire tree as
  53. * we can't be adding trees as root; e.g., expr expr.
  54. *
  55. * Hideous code duplication here with super.next(). Can't think of
  56. * a proper way to refactor. This needs to always call dup node
  57. * and super.next() doesn't know which to call: dup node or dup tree.
  58. */
  59. public Object nextNode() {
  60. //System.out.println("nextNode: elements="+elements+", singleElement="+((Tree)singleElement).toStringTree());
  61. int n = size();
  62. if ( dirty || (cursor>=n && n==1) ) {
  63. // if out of elements and size is 1, dup (at most a single node
  64. // since this is for making root nodes).
  65. Object el = _next();
  66. return adaptor.dupNode(el);
  67. }
  68. // test size above then fetch
  69. Object tree = _next();
  70. while (adaptor.isNil(tree) && adaptor.getChildCount(tree) == 1)
  71. tree = adaptor.getChild(tree, 0);
  72. //System.out.println("_next="+((Tree)tree).toStringTree());
  73. Object el = adaptor.dupNode(tree); // dup just the root (want node here)
  74. return el;
  75. }
  76. @Override
  77. protected Object dup(Object el) {
  78. return adaptor.dupTree(el);
  79. }
  80. }