Debian Codegraph @

rhino/src/org/mozilla/javascript/ast/IfStatement.java

raw symbols SCIP
1 /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 package org.mozilla.javascript.ast;
8
9 import org.mozilla.javascript.Token;
10
11 /**
12 * If-else statement. Node type is {@link Token#IF}.<p>
13 *
14 * <pre><i>IfStatement</i> :
15 * <b>if</b> ( Expression ) Statement <b>else</b> Statement
16 * <b>if</b> ( Expression ) Statement</pre>
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 * Returns if condition
44 */
45 public AstNode getCondition() {
46 return condition;
47 }
48
49 /**
50 * Sets if condition.
51 * @throws IllegalArgumentException if {@code condition} is {@code null}.
52 */
53 public void setCondition(AstNode condition) {
54 assertNotNull(condition);
55 this.condition = condition;
56 condition.setParent(this);
57 }
58
59 /**
60 * Returns statement to execute if condition is true
61 */
62 public AstNode getThenPart() {
63 return thenPart;
64 }
65
66 /**
67 * Sets statement to execute if condition is true
68 * @throws IllegalArgumentException if thenPart is {@code null}
69 */
70 public void setThenPart(AstNode thenPart) {
71 assertNotNull(thenPart);
72 this.thenPart = thenPart;
73 thenPart.setParent(this);
74 }
75
76 /**
77 * Returns statement to execute if condition is false
78 */
79 public AstNode getElsePart() {
80 return elsePart;
81 }
82
83 /**
84 * Sets statement to execute if condition is false
85 * @param elsePart statement to execute if condition is false.
86 * Can be {@code null}.
87 */
88 public void setElsePart(AstNode elsePart) {
89 this.elsePart = elsePart;
90 if (elsePart != null)
91 elsePart.setParent(this);
92 }
93
94 /**
95 * Returns position of "else" keyword, or -1
96 */
97 public int getElsePosition() {
98 return elsePosition;
99 }
100
101 /**
102 * Sets position of "else" keyword, -1 if not present
103 */
104 public void setElsePosition(int elsePosition) {
105 this.elsePosition = elsePosition;
106 }
107
108 /**
109 * Returns left paren offset
110 */
111 public int getLp() {
112 return lp;
113 }
114
115 /**
116 * Sets left paren offset
117 */
118 public void setLp(int lp) {
119 this.lp = lp;
120 }
121
122 /**
123 * Returns right paren position, -1 if missing
124 */
125 public int getRp() {
126 return rp;
127 }
128
129 /**
130 * Sets right paren position, -1 if missing
131 */
132 public void setRp(int rp) {
133 this.rp = rp;
134 }
135
136 /**
137 * Sets both paren positions
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 * Visits this node, the condition, the then-part, and
174 * if supplied, the else-part.
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 }