-
Notifications
You must be signed in to change notification settings - Fork 0
/
Symbol.java
45 lines (38 loc) · 1.16 KB
/
Symbol.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package lexer;
/**
* The Symbol class is used to store all user strings along with
* an indication of the kind of strings they are; e.g. the id "abc" will
* store the "abc" in name and Sym.Tokens.Identifier in kind
*/
public class Symbol {
private String name;
private Tokens kind; // token kind of symbol
private Symbol(String n, Tokens kind) {
name=n;
this.kind = kind;
}
// symbols contains all strings in the source program
private static java.util.HashMap<String,Symbol> symbols = new java.util.HashMap<String,Symbol>();
public String toString() {
return name;
}
public Tokens getKind() {
return kind;
}
/**
* Return the unique symbol associated with a string.
* Repeated calls to <tt>symbol("abc")</tt> will return the same Symbol.
*/
public static Symbol symbol(String newTokenString, Tokens kind) {
Symbol s = symbols.get(newTokenString);
if (s == null) {
if (kind == Tokens.BogusToken) { // bogus string so don't enter into symbols
return null;
}
//System.out.println("new symbol: "+u+" Kind: "+kind);
s = new Symbol(newTokenString,kind);
symbols.put(newTokenString,s);
}
return s;
}
}