Debian Codegraph @

rhino/src/org/mozilla/javascript/ast/WhileLoop.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 * While statement. Node type is {@link Token#WHILE}.<p>
13 *
14 * <pre><i>WhileStatement</i>:
15 * <b>while</b> <b>(</b> Expression <b>)</b> Statement</pre>
16 */
17 public class WhileLoop extends Loop {
18
19 private AstNode condition;
20
21 {
22 type = Token.WHILE;
23 }
24
25 public WhileLoop() {
26 }
27
28 public WhileLoop(int pos) {
29 super(pos);
30 }
31
32 public WhileLoop(int pos, int len) {
33 super(pos, len);
34 }
35
36 /**
37 * Returns loop condition
38 */
39 public AstNode getCondition() {
40 return condition;
41 }
42
43 /**
44 * Sets loop condition
45 * @throws IllegalArgumentException} if condition is {@code null}
46 */
47 public void setCondition(AstNode condition) {
48 assertNotNull(condition);
49 this.condition = condition;
50 condition.setParent(this);
51 }
52
53 @Override
54 public String toSource(int depth) {
55 StringBuilder sb = new StringBuilder();
56 sb.append(makeIndent(depth));
57 sb.append("while (");
58 sb.append(condition.toSource(0));
59 sb.append(") ");
60 if (body.getType() == Token.BLOCK) {
61 sb.append(body.toSource(depth).trim());
62 sb.append("\n");
63 } else {
64 sb.append("\n").append(body.toSource(depth+1));
65 }
66 return sb.toString();
67 }
68
69 /**
70 * Visits this node, the condition, then the body.
71 */
72 @Override
73 public void visit(NodeVisitor v) {
74 if (v.visit(this)) {
75 condition.visit(v);
76 body.visit(v);
77 }
78 }
79 }