Debian Codegraph @

rhino/src/org/mozilla/javascript/ast/Symbol.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.Node;
10 import org.mozilla.javascript.Token;
11
12 /**
13 * Represents a symbol-table entry.
14 */
15 public class Symbol {
16
17 // One of Token.FUNCTION, Token.LP (for parameters), Token.VAR,
18 // Token.LET, or Token.CONST
19 private int declType;
20 private int index = -1;
21 private String name;
22 private Node node;
23 private Scope containingTable;
24
25 public Symbol() {
26 }
27
28 /**
29 * Constructs a new Symbol with a specific name and declaration type
30 * @param declType {@link Token#FUNCTION}, {@link Token#LP}
31 * (for params), {@link Token#VAR}, {@link Token#LET} or {@link Token#CONST}
32 */
33 public Symbol(int declType, String name) {
34 setName(name);
35 setDeclType(declType);
36 }
37
38 /**
39 * Returns symbol declaration type
40 */
41 public int getDeclType() {
42 return declType;
43 }
44
45 /**
46 * Sets symbol declaration type
47 */
48 public void setDeclType(int declType) {
49 if (!(declType == Token.FUNCTION
50 || declType == Token.LP
51 || declType == Token.VAR
52 || declType == Token.LET
53 || declType == Token.CONST))
54 throw new IllegalArgumentException("Invalid declType: " + declType);
55 this.declType = declType;
56 }
57
58 /**
59 * Returns symbol name
60 */
61 public String getName() {
62 return name;
63 }
64
65 /**
66 * Sets symbol name
67 */
68 public void setName(String name) {
69 this.name = name;
70 }
71
72 /**
73 * Returns the node associated with this identifier
74 */
75 public Node getNode() {
76 return node;
77 }
78
79 /**
80 * Returns symbol's index in its scope
81 */
82 public int getIndex() {
83 return index;
84 }
85
86 /**
87 * Sets symbol's index in its scope
88 */
89 public void setIndex(int index) {
90 this.index = index;
91 }
92
93 /**
94 * Sets the node associated with this identifier
95 */
96 public void setNode(Node node) {
97 this.node = node;
98 }
99
100 /**
101 * Returns the Scope in which this symbol is entered
102 */
103 public Scope getContainingTable() {
104 return containingTable;
105 }
106
107 /**
108 * Sets this symbol's Scope
109 */
110 public void setContainingTable(Scope containingTable) {
111 this.containingTable = containingTable;
112 }
113
114 public String getDeclTypeName() {
115 return Token.typeToName(declType);
116 }
117
118 @Override
119 public String toString() {
120 StringBuilder result = new StringBuilder();
121 result.append("Symbol (");
122 result.append(getDeclTypeName());
123 result.append(") name=");
124 result.append(name);
125 if (node != null) {
126 result.append(" line=");
127 result.append(node.getLineno());
128 }
129 return result.toString();
130 }
131 }