Debian Codegraph @

rhino/src/org/mozilla/javascript/ast/SwitchCase.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 * Switch-case AST node type. The switch case is always part of a
16 * switch statement.
17 * Node type is {@link Token#CASE}.<p>
18 *
19 * <pre><i>CaseBlock</i> :
20 * { [CaseClauses] }
21 * { [CaseClauses] DefaultClause [CaseClauses] }
22 * <i>CaseClauses</i> :
23 * CaseClause
24 * CaseClauses CaseClause
25 * <i>CaseClause</i> :
26 * <b>case</b> Expression : [StatementList]
27 * <i>DefaultClause</i> :
28 * <b>default</b> : [StatementList]</pre>
29 */
30 public class SwitchCase extends AstNode {
31
32 private AstNode expression;
33 private List<AstNode> statements;
34
35 {
36 type = Token.CASE;
37 }
38
39 public SwitchCase() {
40 }
41
42 public SwitchCase(int pos) {
43 super(pos);
44 }
45
46 public SwitchCase(int pos, int len) {
47 super(pos, len);
48 }
49
50 /**
51 * Returns the case expression, {@code null} for default case
52 */
53 public AstNode getExpression() {
54 return expression;
55 }
56
57 /**
58 * Sets the case expression, {@code null} for default case.
59 * Note that for empty fall-through cases, they still have
60 * a case expression. In {@code case 0: case 1: break;} the
61 * first case has an {@code expression} that is a
62 * {@link NumberLiteral} with value {@code 0}.
63 */
64 public void setExpression(AstNode expression) {
65 this.expression = expression;
66 if (expression != null)
67 expression.setParent(this);
68 }
69
70 /**
71 * Return true if this is a default case.
72 * @return true if {@link #getExpression} would return {@code null}
73 */
74 public boolean isDefault() {
75 return expression == null;
76 }
77
78 /**
79 * Returns statement list, which may be {@code null}.
80 */
81 public List<AstNode> getStatements() {
82 return statements;
83 }
84
85 /**
86 * Sets statement list. May be {@code null}. Replaces any existing
87 * statements. Each element in the list has its parent set to this node.
88 */
89 public void setStatements(List<AstNode> statements) {
90 if (this.statements != null) {
91 this.statements.clear();
92 }
93 for (AstNode s : statements) {
94 addStatement(s);
95 }
96 }
97
98 /**
99 * Adds a statement to the end of the statement list.
100 * Sets the parent of the new statement to this node, updates
101 * its start offset to be relative to this node, and sets the
102 * length of this node to include the new child.
103 *
104 * @param statement a child statement
105 * @throws IllegalArgumentException} if statement is {@code null}
106 */
107 public void addStatement(AstNode statement) {
108 assertNotNull(statement);
109 if (statements == null) {
110 statements = new ArrayList<AstNode>();
111 }
112 int end = statement.getPosition() + statement.getLength();
113 this.setLength(end - this.getPosition());
114 statements.add(statement);
115 statement.setParent(this);
116 }
117
118 @Override
119 public String toSource(int depth) {
120 StringBuilder sb = new StringBuilder();
121 sb.append(makeIndent(depth));
122 if (expression == null) {
123 sb.append("default:\n");
124 } else {
125 sb.append("case ");
126 sb.append(expression.toSource(0));
127 sb.append(":\n");
128 }
129 if (statements != null) {
130 for (AstNode s : statements) {
131 sb.append(s.toSource(depth+1));
132 }
133 }
134 return sb.toString();
135 }
136
137 /**
138 * Visits this node, then the case expression if present, then
139 * each statement (if any are specified).
140 */
141 @Override
142 public void visit(NodeVisitor v) {
143 if (v.visit(this)) {
144 if (expression != null) {
145 expression.visit(v);
146 }
147 if (statements != null) {
148 for (AstNode s : statements) {
149 s.visit(v);
150 }
151 }
152 }
153 }
154 }