Debian Codegraph @

rhino/src/org/mozilla/javascript/ast/KeywordLiteral.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 for keyword literals: currently, {@code this},
13 * {@code null}, {@code true}, {@code false}, and {@code debugger}.
14 * Node type is one of
15 * {@link Token#THIS},
16 * {@link Token#NULL},
17 * {@link Token#TRUE},
18 * {@link Token#FALSE}, or
19 * {@link Token#DEBUGGER}.
20 */
21 public class KeywordLiteral extends AstNode {
22
23 public KeywordLiteral() {
24 }
25
26 public KeywordLiteral(int pos) {
27 super(pos);
28 }
29
30 public KeywordLiteral(int pos, int len) {
31 super(pos, len);
32 }
33
34 /**
35 * Constructs a new KeywordLiteral
36 * @param nodeType the token type
37 */
38 public KeywordLiteral(int pos, int len, int nodeType) {
39 super(pos, len);
40 setType(nodeType);
41 }
42
43 /**
44 * Sets node token type
45 * @throws IllegalArgumentException if {@code nodeType} is unsupported
46 */
47 @Override
48 public KeywordLiteral setType(int nodeType) {
49 if (!(nodeType == Token.THIS
50 || nodeType == Token.NULL
51 || nodeType == Token.TRUE
52 || nodeType == Token.FALSE
53 || nodeType == Token.DEBUGGER))
54 throw new IllegalArgumentException("Invalid node type: "
55 + nodeType);
56 type = nodeType;
57 return this;
58 }
59
60 /**
61 * Returns true if the token type is {@link Token#TRUE} or
62 * {@link Token#FALSE}.
63 */
64 public boolean isBooleanLiteral() {
65 return type == Token.TRUE || type == Token.FALSE;
66 }
67
68 @Override
69 public String toSource(int depth) {
70 StringBuilder sb = new StringBuilder();
71 sb.append(makeIndent(depth));
72 switch (getType()) {
73 case Token.THIS:
74 sb.append("this");
75 break;
76 case Token.NULL:
77 sb.append("null");
78 break;
79 case Token.TRUE:
80 sb.append("true");
81 break;
82 case Token.FALSE:
83 sb.append("false");
84 break;
85 case Token.DEBUGGER:
86 sb.append("debugger;\n");
87 break;
88 default:
89 throw new IllegalStateException("Invalid keyword literal type: "
90 + getType());
91 }
92 return sb.toString();
93 }
94
95 /**
96 * Visits this node. There are no children to visit.
97 */
98 @Override
99 public void visit(NodeVisitor v) {
100 v.visit(this);
101 }
102 }