Debian Codegraph @

rhino/src/org/mozilla/javascript/ast/ArrayComprehension.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 import java.util.ArrayList;
12 import java.util.List;
13
14 /**
15 * AST node for a JavaScript 1.7 Array comprehension.
16 * Node type is {@link Token#ARRAYCOMP}.<p>
17 */
18 public class ArrayComprehension extends Scope {
19
20 private AstNode result;
21 private List<ArrayComprehensionLoop> loops =
22 new ArrayList<ArrayComprehensionLoop>();
23 private AstNode filter;
24 private int ifPosition = -1;
25 private int lp = -1;
26 private int rp = -1;
27
28 {
29 type = Token.ARRAYCOMP;
30 }
31
32 public ArrayComprehension() {
33 }
34
35 public ArrayComprehension(int pos) {
36 super(pos);
37 }
38
39 public ArrayComprehension(int pos, int len) {
40 super(pos, len);
41 }
42
43 /**
44 * Returns result expression node (just after opening bracket)
45 */
46 public AstNode getResult() {
47 return result;
48 }
49
50 /**
51 * Sets result expression, and sets its parent to this node.
52 * @throws IllegalArgumentException if result is {@code null}
53 */
54 public void setResult(AstNode result) {
55 assertNotNull(result);
56 this.result = result;
57 result.setParent(this);
58 }
59
60 /**
61 * Returns loop list
62 */
63 public List<ArrayComprehensionLoop> getLoops() {
64 return loops;
65 }
66
67 /**
68 * Sets loop list
69 * @throws IllegalArgumentException if loops is {@code null}
70 */
71 public void setLoops(List<ArrayComprehensionLoop> loops) {
72 assertNotNull(loops);
73 this.loops.clear();
74 for (ArrayComprehensionLoop acl : loops) {
75 addLoop(acl);
76 }
77 }
78
79 /**
80 * Adds a child loop node, and sets its parent to this node.
81 * @throws IllegalArgumentException if acl is {@code null}
82 */
83 public void addLoop(ArrayComprehensionLoop acl) {
84 assertNotNull(acl);
85 loops.add(acl);
86 acl.setParent(this);
87 }
88
89 /**
90 * Returns filter expression, or {@code null} if not present
91 */
92 public AstNode getFilter() {
93 return filter;
94 }
95
96 /**
97 * Sets filter expression, and sets its parent to this node.
98 * Can be {@code null}.
99 */
100 public void setFilter(AstNode filter) {
101 this.filter = filter;
102 if (filter != null)
103 filter.setParent(this);
104 }
105
106 /**
107 * Returns position of 'if' keyword, -1 if not present
108 */
109 public int getIfPosition() {
110 return ifPosition;
111 }
112
113 /**
114 * Sets position of 'if' keyword
115 */
116 public void setIfPosition(int ifPosition) {
117 this.ifPosition = ifPosition;
118 }
119
120 /**
121 * Returns filter left paren position, or -1 if no filter
122 */
123 public int getFilterLp() {
124 return lp;
125 }
126
127 /**
128 * Sets filter left paren position, or -1 if no filter
129 */
130 public void setFilterLp(int lp) {
131 this.lp = lp;
132 }
133
134 /**
135 * Returns filter right paren position, or -1 if no filter
136 */
137 public int getFilterRp() {
138 return rp;
139 }
140
141 /**
142 * Sets filter right paren position, or -1 if no filter
143 */
144 public void setFilterRp(int rp) {
145 this.rp = rp;
146 }
147
148 @Override
149 public String toSource(int depth) {
150 StringBuilder sb = new StringBuilder(250);
151 sb.append("[");
152 sb.append(result.toSource(0));
153 for (ArrayComprehensionLoop loop : loops) {
154 sb.append(loop.toSource(0));
155 }
156 if (filter != null) {
157 sb.append(" if (");
158 sb.append(filter.toSource(0));
159 sb.append(")");
160 }
161 sb.append("]");
162 return sb.toString();
163 }
164
165 /**
166 * Visits this node, the result expression, the loops, and the optional
167 * filter.
168 */
169 @Override
170 public void visit(NodeVisitor v) {
171 if (!v.visit(this)) {
172 return;
173 }
174 result.visit(v);
175 for (ArrayComprehensionLoop loop : loops) {
176 loop.visit(v);
177 }
178 if (filter != null) {
179 filter.visit(v);
180 }
181 }
182 }