Debian Codegraph @

rhino/src/org/mozilla/javascript/ast/ForInLoop.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 * For-in or for-each-in or for-of statement. Node type is {@link Token#FOR}.<p>
13 *
14 * <pre><b>for</b> [<b>each</b>] ( LeftHandSideExpression <b>in</b> Expression ) Statement</pre>
15 * <pre><b>for</b> [<b>each</b>] ( <b>var</b> VariableDeclarationNoIn <b>in</b> Expression ) Statement</pre>
16 * <pre><b>for</b> ( LeftHandSideExpression <b>of</b> Expression ) Statement</pre>
17 * <pre><b>for</b> ( ForDeclaration <b>of</b> Expression ) Statement</pre>
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 * Returns loop iterator expression
45 */
46 public AstNode getIterator() {
47 return iterator;
48 }
49
50 /**
51 * Sets loop iterator expression: the part before the "in" or "of" keyword.
52 * Also sets its parent to this node.
53 * @throws IllegalArgumentException if {@code iterator} is {@code null}
54 */
55 public void setIterator(AstNode iterator) {
56 assertNotNull(iterator);
57 this.iterator = iterator;
58 iterator.setParent(this);
59 }
60
61 /**
62 * Returns object being iterated over
63 */
64 public AstNode getIteratedObject() {
65 return iteratedObject;
66 }
67
68 /**
69 * Sets object being iterated over, and sets its parent to this node.
70 * @throws IllegalArgumentException if {@code object} is {@code null}
71 */
72 public void setIteratedObject(AstNode object) {
73 assertNotNull(object);
74 this.iteratedObject = object;
75 object.setParent(this);
76 }
77
78 /**
79 * Returns whether the loop is a for-each loop
80 */
81 public boolean isForEach() {
82 return isForEach;
83 }
84
85 /**
86 * Sets whether the loop is a for-each loop
87 */
88 public void setIsForEach(boolean isForEach) {
89 this.isForEach = isForEach;
90 }
91
92 /**
93 * Returns whether the loop is a for-of loop
94 */
95 public boolean isForOf() {
96 return isForOf;
97 }
98
99 /**
100 * Sets whether the loop is a for-each loop
101 */
102 public void setIsForOf(boolean isForOf) {
103 this.isForOf = isForOf;
104 }
105
106 /**
107 * Returns position of "in" or "of" keyword
108 */
109 public int getInPosition() {
110 return inPosition;
111 }
112
113 /**
114 * Sets position of "in" or "of" keyword
115 * @param inPosition position of "in" or "of" keyword,
116 * or -1 if not present (e.g. in presence of a syntax error)
117 */
118 public void setInPosition(int inPosition) {
119 this.inPosition = inPosition;
120 }
121
122 /**
123 * Returns position of "each" keyword
124 */
125 public int getEachPosition() {
126 return eachPosition;
127 }
128
129 /**
130 * Sets position of "each" keyword
131 * @param eachPosition position of "each" keyword,
132 * or -1 if not present.
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 * Visits this node, the iterator, the iterated object, and the body.
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 }