Debian Codegraph @

rhino/src/org/mozilla/javascript/ast/Label.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 * AST node representing a label. It is a distinct node type so it can
13 * record its length and position for code-processing tools.
14 * Node type is {@link Token#LABEL}.<p>
15 */
16 public class Label extends Jump {
17
18 private String name;
19
20 {
21 type = Token.LABEL;
22 }
23
24 public Label() {
25 }
26
27 public Label(int pos) {
28 this(pos, -1);
29 }
30
31 public Label(int pos, int len) {
32 // can't call super (Jump) for historical reasons
33 position = pos;
34 length = len;
35 }
36
37 public Label(int pos, int len, String name) {
38 this(pos, len);
39 setName(name);
40 }
41
42 /**
43 * Returns the label text
44 */
45 public String getName() {
46 return name;
47 }
48
49 /**
50 * Sets the label text
51 * @throws IllegalArgumentException if name is {@code null} or the
52 * empty string.
53 */
54 public void setName(String name) {
55 name = name == null ? null : name.trim();
56 if (name == null || "".equals(name))
57 throw new IllegalArgumentException("invalid label name");
58 this.name = name;
59 }
60
61 @Override
62 public String toSource(int depth) {
63 StringBuilder sb = new StringBuilder();
64 sb.append(makeIndent(depth));
65 sb.append(name);
66 sb.append(":\n");
67 return sb.toString();
68 }
69
70 /**
71 * Visits this label. There are no children to visit.
72 */
73 @Override
74 public void visit(NodeVisitor v) {
75 v.visit(this);
76 }
77 }