You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ASTList.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999-2003 Shigeru Chiba. All Rights Reserved.
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. Alternatively, the contents of this file may be used under
  8. * the terms of the GNU Lesser General Public License Version 2.1 or later.
  9. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. */
  15. package javassist.compiler.ast;
  16. import javassist.compiler.CompileError;
  17. /**
  18. * A linked list.
  19. * The right subtree must be an ASTList object or null.
  20. */
  21. public class ASTList extends ASTree {
  22. private ASTree left;
  23. private ASTList right;
  24. public ASTList(ASTree _head, ASTList _tail) {
  25. left = _head;
  26. right = _tail;
  27. }
  28. public ASTList(ASTree _head) {
  29. left = _head;
  30. right = null;
  31. }
  32. public static ASTList make(ASTree e1, ASTree e2, ASTree e3) {
  33. return new ASTList(e1, new ASTList(e2, new ASTList(e3)));
  34. }
  35. public ASTree getLeft() { return left; }
  36. public ASTree getRight() { return right; }
  37. public void setLeft(ASTree _left) { left = _left; }
  38. public void setRight(ASTree _right) {
  39. right = (ASTList)_right;
  40. }
  41. /**
  42. * Returns the car part of the list.
  43. */
  44. public ASTree head() { return left; }
  45. public void setHead(ASTree _head) {
  46. left = _head;
  47. }
  48. /**
  49. * Returns the cdr part of the list.
  50. */
  51. public ASTList tail() { return right; }
  52. public void setTail(ASTList _tail) {
  53. right = _tail;
  54. }
  55. public void accept(Visitor v) throws CompileError { v.atASTList(this); }
  56. public String toString() {
  57. StringBuffer sbuf = new StringBuffer();
  58. sbuf.append("(<");
  59. sbuf.append(getTag());
  60. sbuf.append('>');
  61. ASTList list = this;
  62. while (list != null) {
  63. sbuf.append(' ');
  64. ASTree a = list.left;
  65. sbuf.append(a == null ? "<null>" : a.toString());
  66. list = list.right;
  67. }
  68. sbuf.append(')');
  69. return sbuf.toString();
  70. }
  71. /**
  72. * Returns the number of the elements in this list.
  73. */
  74. public int length() {
  75. return length(this);
  76. }
  77. public static int length(ASTList list) {
  78. if (list == null)
  79. return 0;
  80. int n = 0;
  81. while (list != null) {
  82. list = list.right;
  83. ++n;
  84. }
  85. return n;
  86. }
  87. /**
  88. * Returns a sub list of the list. The sub list begins with the
  89. * n-th element of the list.
  90. *
  91. * @param nth zero or more than zero.
  92. */
  93. public ASTList sublist(int nth) {
  94. ASTList list = this;
  95. while (nth-- > 0)
  96. list = list.right;
  97. return list;
  98. }
  99. /**
  100. * Substitutes <code>newObj</code> for <code>oldObj</code> in the
  101. * list.
  102. */
  103. public boolean subst(ASTree newObj, ASTree oldObj) {
  104. for (ASTList list = this; list != null; list = list.right)
  105. if (list.left == oldObj) {
  106. list.left = newObj;
  107. return true;
  108. }
  109. return false;
  110. }
  111. /**
  112. * Appends an object to a list.
  113. */
  114. public static ASTList append(ASTList a, ASTree b) {
  115. return concat(a, new ASTList(b));
  116. }
  117. /**
  118. * Concatenates two lists.
  119. */
  120. public static ASTList concat(ASTList a, ASTList b) {
  121. if (a == null)
  122. return b;
  123. else {
  124. ASTList list = a;
  125. while (list.right != null)
  126. list = list.right;
  127. list.right = b;
  128. return a;
  129. }
  130. }
  131. }