| 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 IfStatement extends AstNode { |
| 19 |
|
| 20 |
private AstNode condition; |
| 21 |
private AstNode thenPart; |
| 22 |
private int elsePosition = -1; |
| 23 |
private AstNode elsePart; |
| 24 |
private int lp = -1; |
| 25 |
private int rp = -1; |
| 26 |
|
| 27 |
{ |
| 28 |
type = Token.IF; |
| 29 |
} |
| 30 |
|
| 31 |
public IfStatement() { |
| 32 |
} |
| 33 |
|
| 34 |
public IfStatement(int pos) { |
| 35 |
super(pos); |
| 36 |
} |
| 37 |
|
| 38 |
public IfStatement(int pos, int len) { |
| 39 |
super(pos, len); |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public AstNode getCondition() { |
| 46 |
return condition; |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public void setCondition(AstNode condition) { |
| 54 |
assertNotNull(condition); |
| 55 |
this.condition = condition; |
| 56 |
condition.setParent(this); |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
public AstNode getThenPart() { |
| 63 |
return thenPart; |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
public void setThenPart(AstNode thenPart) { |
| 71 |
assertNotNull(thenPart); |
| 72 |
this.thenPart = thenPart; |
| 73 |
thenPart.setParent(this); |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
public AstNode getElsePart() { |
| 80 |
return elsePart; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
public void setElsePart(AstNode elsePart) { |
| 89 |
this.elsePart = elsePart; |
| 90 |
if (elsePart != null) |
| 91 |
elsePart.setParent(this); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
public int getElsePosition() { |
| 98 |
return elsePosition; |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
public void setElsePosition(int elsePosition) { |
| 105 |
this.elsePosition = elsePosition; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
public int getLp() { |
| 112 |
return lp; |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
public void setLp(int lp) { |
| 119 |
this.lp = lp; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
public int getRp() { |
| 126 |
return rp; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
public void setRp(int rp) { |
| 133 |
this.rp = rp; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
public void setParens(int lp, int rp) { |
| 140 |
this.lp = lp; |
| 141 |
this.rp = rp; |
| 142 |
} |
| 143 |
|
| 144 |
@Override |
| 145 |
public String toSource(int depth) { |
| 146 |
String pad = makeIndent(depth); |
| 147 |
StringBuilder sb = new StringBuilder(32); |
| 148 |
sb.append(pad); |
| 149 |
sb.append("if ("); |
| 150 |
sb.append(condition.toSource(0)); |
| 151 |
sb.append(") "); |
| 152 |
if (thenPart.getType() != Token.BLOCK) { |
| 153 |
sb.append("\n").append(makeIndent(depth + 1)); |
| 154 |
} |
| 155 |
sb.append(thenPart.toSource(depth).trim()); |
| 156 |
if (elsePart != null) { |
| 157 |
if (thenPart.getType() != Token.BLOCK) { |
| 158 |
sb.append("\n").append(pad).append("else "); |
| 159 |
} else { |
| 160 |
sb.append(" else "); |
| 161 |
} |
| 162 |
if (elsePart.getType() != Token.BLOCK |
| 163 |
&& elsePart.getType() != Token.IF) { |
| 164 |
sb.append("\n").append(makeIndent(depth + 1)); |
| 165 |
} |
| 166 |
sb.append(elsePart.toSource(depth).trim()); |
| 167 |
} |
| 168 |
sb.append("\n"); |
| 169 |
return sb.toString(); |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
@Override |
| 177 |
public void visit(NodeVisitor v) { |
| 178 |
if (v.visit(this)) { |
| 179 |
condition.visit(v); |
| 180 |
thenPart.visit(v); |
| 181 |
if (elsePart != null) { |
| 182 |
elsePart.visit(v); |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
} |