Debian Codegraph @

rhino/src/org/mozilla/javascript/ast/BreakStatement.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 /**
12 * A break statement. Node type is {@link Token#BREAK}.<p>
13 *
14 * <pre><i>BreakStatement</i> :
15 * <b>break</b> [<i>no LineTerminator here</i>] [Identifier] ;</pre>
16 */
17 public class BreakStatement extends Jump {
18
19 private Name breakLabel;
20 private AstNode target;
21
22 {
23 type = Token.BREAK;
24 }
25
26 public BreakStatement() {
27 }
28
29 public BreakStatement(int pos) {
30 // can't call super (Jump) for historical reasons
31 position = pos;
32 }
33
34 public BreakStatement(int pos, int len) {
35 position = pos;
36 length = len;
37 }
38
39 /**
40 * Returns the intended label of this break statement
41 * @return the break label. {@code null} if the source code did
42 * not specify a specific break label via "break &lt;target&gt;".
43 */
44 public Name getBreakLabel() {
45 return breakLabel;
46 }
47
48 /**
49 * Sets the intended label of this break statement, e.g. 'foo'
50 * in "break foo". Also sets the parent of the label to this node.
51 * @param label the break label, or {@code null} if the statement is
52 * just the "break" keyword by itself.
53 */
54 public void setBreakLabel(Name label) {
55 breakLabel = label;
56 if (label != null)
57 label.setParent(this);
58 }
59
60 /**
61 * Returns the statement to break to
62 * @return the break target. Only {@code null} if the source
63 * code has an error in it.
64 */
65 public AstNode getBreakTarget() {
66 return target;
67 }
68
69 /**
70 * Sets the statement to break to.
71 * @param target the statement to break to
72 * @throws IllegalArgumentException if target is {@code null}
73 */
74 public void setBreakTarget(Jump target) {
75 assertNotNull(target);
76 this.target = target;
77 setJumpStatement(target);
78 }
79
80 @Override
81 public String toSource(int depth) {
82 StringBuilder sb = new StringBuilder();
83 sb.append(makeIndent(depth));
84 sb.append("break");
85 if (breakLabel != null) {
86 sb.append(" ");
87 sb.append(breakLabel.toSource(0));
88 }
89 sb.append(";\n");
90 return sb.toString();
91 }
92
93 /**
94 * Visits this node, then visits the break label if non-{@code null}.
95 */
96 @Override
97 public void visit(NodeVisitor v) {
98 if (v.visit(this) && breakLabel != null) {
99 breakLabel.visit(v);
100 }
101 }
102 }