Debian Codegraph @

rhino/src/org/mozilla/javascript/ast/LabeledStatement.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 * A labeled statement. A statement can have more than one label. In
16 * this AST representation, all labels for a statement are collapsed into
17 * the "labels" list of a single {@link LabeledStatement} node. <p>
18 *
19 * Node type is {@link Token#EXPR_VOID}. <p>
20 */
21 public class LabeledStatement extends AstNode {
22
23 private List<Label> labels = new ArrayList<Label>(); // always at least 1
24 private AstNode statement;
25
26 {
27 type = Token.EXPR_VOID;
28 }
29
30 public LabeledStatement() {
31 }
32
33 public LabeledStatement(int pos) {
34 super(pos);
35 }
36
37 public LabeledStatement(int pos, int len) {
38 super(pos, len);
39 }
40
41 /**
42 * Returns label list
43 */
44 public List<Label> getLabels() {
45 return labels;
46 }
47
48 /**
49 * Sets label list, setting the parent of each label
50 * in the list. Replaces any existing labels.
51 * @throws IllegalArgumentException} if labels is {@code null}
52 */
53 public void setLabels(List<Label> labels) {
54 assertNotNull(labels);
55 if (this.labels != null)
56 this.labels.clear();
57 for (Label l : labels) {
58 addLabel(l);
59 }
60 }
61
62 /**
63 * Adds a label and sets its parent to this node.
64 * @throws IllegalArgumentException} if label is {@code null}
65 */
66 public void addLabel(Label label) {
67 assertNotNull(label);
68 labels.add(label);
69 label.setParent(this);
70 }
71
72 /**
73 * Returns the labeled statement
74 */
75 public AstNode getStatement() {
76 return statement;
77 }
78
79 /**
80 * Returns label with specified name from the label list for
81 * this labeled statement. Returns {@code null} if there is no
82 * label with that name in the list.
83 */
84 public Label getLabelByName(String name) {
85 for (Label label : labels) {
86 if (name.equals(label.getName())) {
87 return label;
88 }
89 }
90 return null;
91 }
92
93 /**
94 * Sets the labeled statement, and sets its parent to this node.
95 * @throws IllegalArgumentException if {@code statement} is {@code null}
96 */
97 public void setStatement(AstNode statement) {
98 assertNotNull(statement);
99 this.statement = statement;
100 statement.setParent(this);
101 }
102
103 public Label getFirstLabel() {
104 return labels.get(0);
105 }
106
107 @Override
108 public boolean hasSideEffects() {
109 // just to avoid the default case for EXPR_VOID in AstNode
110 return true;
111 }
112
113 @Override
114 public String toSource(int depth) {
115 StringBuilder sb = new StringBuilder();
116 for (Label label : labels) {
117 sb.append(label.toSource(depth)); // prints newline
118 }
119 sb.append(statement.toSource(depth + 1));
120 return sb.toString();
121 }
122
123 /**
124 * Visits this node, then each label in the label-list, and finally the
125 * statement.
126 */
127 @Override
128 public void visit(NodeVisitor v) {
129 if (v.visit(this)) {
130 for (AstNode label : labels) {
131 label.visit(v);
132 }
133 statement.visit(v);
134 }
135 }
136 }