001: /*
002: * Javassist, a Java-bytecode translator toolkit.
003: * Copyright (C) 1999-2006 Shigeru Chiba. All Rights Reserved.
004: *
005: * The contents of this file are subject to the Mozilla Public License Version
006: * 1.1 (the "License"); you may not use this file except in compliance with
007: * the License. Alternatively, the contents of this file may be used under
008: * the terms of the GNU Lesser General Public License Version 2.1 or later.
009: *
010: * Software distributed under the License is distributed on an "AS IS" basis,
011: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
012: * for the specific language governing rights and limitations under the
013: * License.
014: */
015:
016: package javassist.compiler.ast;
017:
018: import javassist.compiler.CompileError;
019:
020: /**
021: * A linked list.
022: * The right subtree must be an ASTList object or null.
023: */
024: public class ASTList extends ASTree {
025: private ASTree left;
026: private ASTList right;
027:
028: public ASTList(ASTree _head, ASTList _tail) {
029: left = _head;
030: right = _tail;
031: }
032:
033: public ASTList(ASTree _head) {
034: left = _head;
035: right = null;
036: }
037:
038: public static ASTList make(ASTree e1, ASTree e2, ASTree e3) {
039: return new ASTList(e1, new ASTList(e2, new ASTList(e3)));
040: }
041:
042: public ASTree getLeft() {
043: return left;
044: }
045:
046: public ASTree getRight() {
047: return right;
048: }
049:
050: public void setLeft(ASTree _left) {
051: left = _left;
052: }
053:
054: public void setRight(ASTree _right) {
055: right = (ASTList) _right;
056: }
057:
058: /**
059: * Returns the car part of the list.
060: */
061: public ASTree head() {
062: return left;
063: }
064:
065: public void setHead(ASTree _head) {
066: left = _head;
067: }
068:
069: /**
070: * Returns the cdr part of the list.
071: */
072: public ASTList tail() {
073: return right;
074: }
075:
076: public void setTail(ASTList _tail) {
077: right = _tail;
078: }
079:
080: public void accept(Visitor v) throws CompileError {
081: v.atASTList(this );
082: }
083:
084: public String toString() {
085: StringBuffer sbuf = new StringBuffer();
086: sbuf.append("(<");
087: sbuf.append(getTag());
088: sbuf.append('>');
089: ASTList list = this ;
090: while (list != null) {
091: sbuf.append(' ');
092: ASTree a = list.left;
093: sbuf.append(a == null ? "<null>" : a.toString());
094: list = list.right;
095: }
096:
097: sbuf.append(')');
098: return sbuf.toString();
099: }
100:
101: /**
102: * Returns the number of the elements in this list.
103: */
104: public int length() {
105: return length(this );
106: }
107:
108: public static int length(ASTList list) {
109: if (list == null)
110: return 0;
111:
112: int n = 0;
113: while (list != null) {
114: list = list.right;
115: ++n;
116: }
117:
118: return n;
119: }
120:
121: /**
122: * Returns a sub list of the list. The sub list begins with the
123: * n-th element of the list.
124: *
125: * @param nth zero or more than zero.
126: */
127: public ASTList sublist(int nth) {
128: ASTList list = this ;
129: while (nth-- > 0)
130: list = list.right;
131:
132: return list;
133: }
134:
135: /**
136: * Substitutes <code>newObj</code> for <code>oldObj</code> in the
137: * list.
138: */
139: public boolean subst(ASTree newObj, ASTree oldObj) {
140: for (ASTList list = this ; list != null; list = list.right)
141: if (list.left == oldObj) {
142: list.left = newObj;
143: return true;
144: }
145:
146: return false;
147: }
148:
149: /**
150: * Appends an object to a list.
151: */
152: public static ASTList append(ASTList a, ASTree b) {
153: return concat(a, new ASTList(b));
154: }
155:
156: /**
157: * Concatenates two lists.
158: */
159: public static ASTList concat(ASTList a, ASTList b) {
160: if (a == null)
161: return b;
162: else {
163: ASTList list = a;
164: while (list.right != null)
165: list = list.right;
166:
167: list.right = b;
168: return a;
169: }
170: }
171: }
|