Debian Codegraph @

rhino/src/org/mozilla/javascript/ast/ArrayLiteral.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.Collections;
13 import java.util.List;
14
15 /**
16 * AST node for an Array literal. The elements list will always be
17 * non-{@code null}, although the list will have no elements if the Array literal
18 * is empty.<p>
19 *
20 * Node type is {@link Token#ARRAYLIT}.<p>
21 *
22 * <pre><i>ArrayLiteral</i> :
23 * <b>[</b> Elisionopt <b>]</b>
24 * <b>[</b> ElementList <b>]</b>
25 * <b>[</b> ElementList , Elisionopt <b>]</b>
26 * <i>ElementList</i> :
27 * Elisionopt AssignmentExpression
28 * ElementList , Elisionopt AssignmentExpression
29 * <i>Elision</i> :
30 * <b>,</b>
31 * Elision <b>,</b></pre>
32 */
33 public class ArrayLiteral extends AstNode implements DestructuringForm {
34
35 private static final List<AstNode> NO_ELEMS =
36 Collections.unmodifiableList(new ArrayList<AstNode>());
37
38 private List<AstNode> elements;
39 private int destructuringLength;
40 private int skipCount;
41 private boolean isDestructuring;
42
43 {
44 type = Token.ARRAYLIT;
45 }
46
47 public ArrayLiteral() {
48 }
49
50 public ArrayLiteral(int pos) {
51 super(pos);
52 }
53
54 public ArrayLiteral(int pos, int len) {
55 super(pos, len);
56 }
57
58 /**
59 * Returns the element list
60 * @return the element list. If there are no elements, returns an immutable
61 * empty list. Elisions are represented as {@link EmptyExpression}
62 * nodes.
63 */
64 public List<AstNode> getElements() {
65 return elements != null ? elements : NO_ELEMS;
66 }
67
68 /**
69 * Sets the element list, and sets each element's parent to this node.
70 * @param elements the element list. Can be {@code null}.
71 */
72 public void setElements(List<AstNode> elements) {
73 if (elements == null) {
74 this.elements = null;
75 } else {
76 if (this.elements != null)
77 this.elements.clear();
78 for (AstNode e : elements)
79 addElement(e);
80 }
81 }
82
83 /**
84 * Adds an element to the list, and sets its parent to this node.
85 * @param element the element to add
86 * @throws IllegalArgumentException if element is {@code null}. To indicate
87 * an empty element, use an {@link EmptyExpression} node.
88 */
89 public void addElement(AstNode element) {
90 assertNotNull(element);
91 if (elements == null)
92 elements = new ArrayList<AstNode>();
93 elements.add(element);
94 element.setParent(this);
95 }
96
97 /**
98 * Returns the number of elements in this {@code Array} literal,
99 * including empty elements.
100 */
101 public int getSize() {
102 return elements == null ? 0 : elements.size();
103 }
104
105 /**
106 * Returns element at specified index.
107 * @param index the index of the element to retrieve
108 * @return the element
109 * @throws IndexOutOfBoundsException if the index is invalid
110 */
111 public AstNode getElement(int index) {
112 if (elements == null)
113 throw new IndexOutOfBoundsException("no elements");
114 return elements.get(index);
115 }
116
117 /**
118 * Returns destructuring length
119 */
120 public int getDestructuringLength() {
121 return destructuringLength;
122 }
123
124 /**
125 * Sets destructuring length. This is set by the parser and used
126 * by the code generator. {@code for ([a,] in obj)} is legal,
127 * but {@code for ([a] in obj)} is not since we have both key and
128 * value supplied. The difference is only meaningful in array literals
129 * used in destructuring-assignment contexts.
130 */
131 public void setDestructuringLength(int destructuringLength) {
132 this.destructuringLength = destructuringLength;
133 }
134
135 /**
136 * Used by code generator.
137 * @return the number of empty elements
138 */
139 public int getSkipCount() {
140 return skipCount;
141 }
142
143 /**
144 * Used by code generator.
145 * @param count the count of empty elements
146 */
147 public void setSkipCount(int count) {
148 skipCount = count;
149 }
150
151 /**
152 * Marks this node as being a destructuring form - that is, appearing
153 * in a context such as {@code for ([a, b] in ...)} where it's the
154 * target of a destructuring assignment.
155 */
156 public void setIsDestructuring(boolean destructuring) {
157 isDestructuring = destructuring;
158 }
159
160 /**
161 * Returns true if this node is in a destructuring position:
162 * a function parameter, the target of a variable initializer, the
163 * iterator of a for..in loop, etc.
164 */
165 public boolean isDestructuring() {
166 return isDestructuring;
167 }
168
169 @Override
170 public String toSource(int depth) {
171 StringBuilder sb = new StringBuilder();
172 sb.append(makeIndent(depth));
173 sb.append("[");
174 if (elements != null) {
175 printList(elements, sb);
176 }
177 sb.append("]");
178 return sb.toString();
179 }
180
181 /**
182 * Visits this node, then visits its element expressions in order.
183 * Any empty elements are represented by {@link EmptyExpression}
184 * objects, so the callback will never be passed {@code null}.
185 */
186 @Override
187 public void visit(NodeVisitor v) {
188 if (v.visit(this)) {
189 for (AstNode e : getElements()) {
190 e.visit(v);
191 }
192 }
193 }
194 }