Debian Codegraph @

rhino/src/org/mozilla/javascript/ast/SwitchStatement.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 * Switch statement AST node type.
17 * Node type is {@link Token#SWITCH}.<p>
18 *
19 * <pre><i>SwitchStatement</i> :
20 * <b>switch</b> ( Expression ) CaseBlock
21 * <i>CaseBlock</i> :
22 * { [CaseClauses] }
23 * { [CaseClauses] DefaultClause [CaseClauses] }
24 * <i>CaseClauses</i> :
25 * CaseClause
26 * CaseClauses CaseClause
27 * <i>CaseClause</i> :
28 * <b>case</b> Expression : [StatementList]
29 * <i>DefaultClause</i> :
30 * <b>default</b> : [StatementList]</pre>
31 */
32 public class SwitchStatement extends Jump {
33
34 private static final List<SwitchCase> NO_CASES =
35 Collections.unmodifiableList(new ArrayList<SwitchCase>());
36
37 private AstNode expression;
38 private List<SwitchCase> cases;
39 private int lp = -1;
40 private int rp = -1;
41
42 {
43 type = Token.SWITCH;
44 }
45
46 public SwitchStatement() {
47 }
48
49 public SwitchStatement(int pos) {
50 // can't call super (Jump) for historical reasons
51 position = pos;
52 }
53
54 public SwitchStatement(int pos, int len) {
55 position = pos;
56 length = len;
57 }
58
59 /**
60 * Returns the switch discriminant expression
61 */
62 public AstNode getExpression() {
63 return expression;
64 }
65
66 /**
67 * Sets the switch discriminant expression, and sets its parent
68 * to this node.
69 * @throws IllegalArgumentException} if expression is {@code null}
70 */
71 public void setExpression(AstNode expression) {
72 assertNotNull(expression);
73 this.expression = expression;
74 expression.setParent(this);
75 }
76
77 /**
78 * Returns case statement list. If there are no cases,
79 * returns an immutable empty list.
80 */
81 public List<SwitchCase> getCases() {
82 return cases != null ? cases : NO_CASES;
83 }
84
85 /**
86 * Sets case statement list, and sets the parent of each child
87 * case to this node.
88 * @param cases list, which may be {@code null} to remove all the cases
89 */
90 public void setCases(List<SwitchCase> cases) {
91 if (cases == null) {
92 this.cases = null;
93 } else {
94 if (this.cases != null)
95 this.cases.clear();
96 for (SwitchCase sc : cases)
97 addCase(sc);
98 }
99 }
100
101 /**
102 * Adds a switch case statement to the end of the list.
103 * @throws IllegalArgumentException} if switchCase is {@code null}
104 */
105 public void addCase(SwitchCase switchCase) {
106 assertNotNull(switchCase);
107 if (cases == null) {
108 cases = new ArrayList<SwitchCase>();
109 }
110 cases.add(switchCase);
111 switchCase.setParent(this);
112 }
113
114 /**
115 * Returns left paren position, -1 if missing
116 */
117 public int getLp() {
118 return lp;
119 }
120
121 /**
122 * Sets left paren position
123 */
124 public void setLp(int lp) {
125 this.lp = lp;
126 }
127
128 /**
129 * Returns right paren position, -1 if missing
130 */
131 public int getRp() {
132 return rp;
133 }
134
135 /**
136 * Sets right paren position
137 */
138 public void setRp(int rp) {
139 this.rp = rp;
140 }
141
142 /**
143 * Sets both paren positions
144 */
145 public void setParens(int lp, int rp) {
146 this.lp = lp;
147 this.rp = rp;
148 }
149
150 @Override
151 public String toSource(int depth) {
152 String pad = makeIndent(depth);
153 StringBuilder sb = new StringBuilder();
154 sb.append(pad);
155 sb.append("switch (");
156 sb.append(expression.toSource(0));
157 sb.append(") {\n");
158 if (cases != null) {
159 for (SwitchCase sc : cases) {
160 sb.append(sc.toSource(depth + 1));
161 }
162 }
163 sb.append(pad);
164 sb.append("}\n");
165 return sb.toString();
166 }
167
168 /**
169 * Visits this node, then the switch-expression, then the cases
170 * in lexical order.
171 */
172 @Override
173 public void visit(NodeVisitor v) {
174 if (v.visit(this)) {
175 expression.visit(v);
176 for (SwitchCase sc: getCases()) {
177 sc.visit(v);
178 }
179 }
180 }
181 }