| 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 |
|
| 19 |
public class ForInLoop extends Loop { |
| 20 |
|
| 21 |
protected AstNode iterator; |
| 22 |
protected AstNode iteratedObject; |
| 23 |
protected int inPosition = -1; |
| 24 |
protected int eachPosition = -1; |
| 25 |
protected boolean isForEach; |
| 26 |
protected boolean isForOf; |
| 27 |
|
| 28 |
{ |
| 29 |
type = Token.FOR; |
| 30 |
} |
| 31 |
|
| 32 |
public ForInLoop() { |
| 33 |
} |
| 34 |
|
| 35 |
public ForInLoop(int pos) { |
| 36 |
super(pos); |
| 37 |
} |
| 38 |
|
| 39 |
public ForInLoop(int pos, int len) { |
| 40 |
super(pos, len); |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public AstNode getIterator() { |
| 47 |
return iterator; |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public void setIterator(AstNode iterator) { |
| 56 |
assertNotNull(iterator); |
| 57 |
this.iterator = iterator; |
| 58 |
iterator.setParent(this); |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public AstNode getIteratedObject() { |
| 65 |
return iteratedObject; |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
public void setIteratedObject(AstNode object) { |
| 73 |
assertNotNull(object); |
| 74 |
this.iteratedObject = object; |
| 75 |
object.setParent(this); |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
public boolean isForEach() { |
| 82 |
return isForEach; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
public void setIsForEach(boolean isForEach) { |
| 89 |
this.isForEach = isForEach; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
public boolean isForOf() { |
| 96 |
return isForOf; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
public void setIsForOf(boolean isForOf) { |
| 103 |
this.isForOf = isForOf; |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
public int getInPosition() { |
| 110 |
return inPosition; |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
public void setInPosition(int inPosition) { |
| 119 |
this.inPosition = inPosition; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
public int getEachPosition() { |
| 126 |
return eachPosition; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
public void setEachPosition(int eachPosition) { |
| 135 |
this.eachPosition = eachPosition; |
| 136 |
} |
| 137 |
|
| 138 |
@Override |
| 139 |
public String toSource(int depth) { |
| 140 |
StringBuilder sb = new StringBuilder(); |
| 141 |
sb.append(makeIndent(depth)); |
| 142 |
sb.append("for "); |
| 143 |
if (isForEach()) { |
| 144 |
sb.append("each "); |
| 145 |
} |
| 146 |
sb.append("("); |
| 147 |
sb.append(iterator.toSource(0)); |
| 148 |
if (isForOf) { |
| 149 |
sb.append(" of "); |
| 150 |
} else { |
| 151 |
sb.append(" in "); |
| 152 |
} |
| 153 |
sb.append(iteratedObject.toSource(0)); |
| 154 |
sb.append(") "); |
| 155 |
if (body.getType() == Token.BLOCK) { |
| 156 |
sb.append(body.toSource(depth).trim()).append("\n"); |
| 157 |
} else { |
| 158 |
sb.append("\n").append(body.toSource(depth+1)); |
| 159 |
} |
| 160 |
return sb.toString(); |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
@Override |
| 167 |
public void visit(NodeVisitor v) { |
| 168 |
if (v.visit(this)) { |
| 169 |
iterator.visit(v); |
| 170 |
iteratedObject.visit(v); |
| 171 |
body.visit(v); |
| 172 |
} |
| 173 |
} |
| 174 |
} |