| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
package org.mozilla.javascript.ast; |
| 8 |
|
| 9 |
import org.mozilla.javascript.Token; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
public class ForLoop extends Loop { |
| 19 |
|
| 20 |
private AstNode initializer; |
| 21 |
private AstNode condition; |
| 22 |
private AstNode increment; |
| 23 |
|
| 24 |
{ |
| 25 |
type = Token.FOR; |
| 26 |
} |
| 27 |
|
| 28 |
public ForLoop() { |
| 29 |
} |
| 30 |
|
| 31 |
public ForLoop(int pos) { |
| 32 |
super(pos); |
| 33 |
} |
| 34 |
|
| 35 |
public ForLoop(int pos, int len) { |
| 36 |
super(pos, len); |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public AstNode getInitializer() { |
| 46 |
return initializer; |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public void setInitializer(AstNode initializer) { |
| 58 |
assertNotNull(initializer); |
| 59 |
this.initializer = initializer; |
| 60 |
initializer.setParent(this); |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
public AstNode getCondition() { |
| 67 |
return condition; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
public void setCondition(AstNode condition) { |
| 77 |
assertNotNull(condition); |
| 78 |
this.condition = condition; |
| 79 |
condition.setParent(this); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
public AstNode getIncrement() { |
| 86 |
return increment; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
public void setIncrement(AstNode increment) { |
| 97 |
assertNotNull(increment); |
| 98 |
this.increment = increment; |
| 99 |
increment.setParent(this); |
| 100 |
} |
| 101 |
|
| 102 |
@Override |
| 103 |
public String toSource(int depth) { |
| 104 |
StringBuilder sb = new StringBuilder(); |
| 105 |
sb.append(makeIndent(depth)); |
| 106 |
sb.append("for ("); |
| 107 |
sb.append(initializer.toSource(0)); |
| 108 |
sb.append("; "); |
| 109 |
sb.append(condition.toSource(0)); |
| 110 |
sb.append("; "); |
| 111 |
sb.append(increment.toSource(0)); |
| 112 |
sb.append(") "); |
| 113 |
if (body.getType() == Token.BLOCK) { |
| 114 |
sb.append(body.toSource(depth).trim()).append("\n"); |
| 115 |
} else { |
| 116 |
sb.append("\n").append(body.toSource(depth+1)); |
| 117 |
} |
| 118 |
return sb.toString(); |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
@Override |
| 126 |
public void visit(NodeVisitor v) { |
| 127 |
if (v.visit(this)) { |
| 128 |
initializer.visit(v); |
| 129 |
condition.visit(v); |
| 130 |
increment.visit(v); |
| 131 |
body.visit(v); |
| 132 |
} |
| 133 |
} |
| 134 |
} |