Debian Codegraph @

rhino/src/org/mozilla/javascript/ast/ForLoop.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 * C-style for-loop statement.
13 * Node type is {@link Token#FOR}.<p>
14 *
15 * <pre><b>for</b> ( ExpressionNoInopt; Expressionopt ; Expressionopt ) Statement</pre>
16 * <pre><b>for</b> ( <b>var</b> VariableDeclarationListNoIn; Expressionopt ; Expressionopt ) Statement</pre>
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 * Returns loop initializer variable declaration list.
41 * This is either a {@link VariableDeclaration}, an
42 * {@link Assignment}, or an {@link InfixExpression} of
43 * type COMMA that chains multiple variable assignments.
44 */
45 public AstNode getInitializer() {
46 return initializer;
47 }
48
49 /**
50 * Sets loop initializer expression, and sets its parent
51 * to this node. Virtually any expression can be in the initializer,
52 * so no error-checking is done other than a {@code null}-check.
53 * @param initializer loop initializer. Pass an
54 * {@link EmptyExpression} if the initializer is not specified.
55 * @throws IllegalArgumentException if condition is {@code null}
56 */
57 public void setInitializer(AstNode initializer) {
58 assertNotNull(initializer);
59 this.initializer = initializer;
60 initializer.setParent(this);
61 }
62
63 /**
64 * Returns loop condition
65 */
66 public AstNode getCondition() {
67 return condition;
68 }
69
70 /**
71 * Sets loop condition, and sets its parent to this node.
72 * @param condition loop condition. Pass an {@link EmptyExpression}
73 * if the condition is missing.
74 * @throws IllegalArgumentException} if condition is {@code null}
75 */
76 public void setCondition(AstNode condition) {
77 assertNotNull(condition);
78 this.condition = condition;
79 condition.setParent(this);
80 }
81
82 /**
83 * Returns loop increment expression
84 */
85 public AstNode getIncrement() {
86 return increment;
87 }
88
89 /**
90 * Sets loop increment expression, and sets its parent to
91 * this node.
92 * @param increment loop increment expression. Pass an
93 * {@link EmptyExpression} if increment is {@code null}.
94 * @throws IllegalArgumentException} if increment is {@code null}
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 * Visits this node, the initializer expression, the loop condition
123 * expression, the increment expression, and then the loop body.
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 }