-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToken.java
More file actions
65 lines (48 loc) · 1.36 KB
/
Token.java
File metadata and controls
65 lines (48 loc) · 1.36 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import java.util.Optional;
public class Token {
public enum TokenType implements Symbol {
PLUS, MINUS, TIMES, DIVIDE, MOD, ASSIGN, EQUAL, NEQUAL, LT, LE, GT, GE, LPAREN, RPAREN, LBRACE, RBRACE, AND, OR,
SEMICOLON, PUBLIC, CLASS, STATIC, VOID, MAIN, STRINGARR, ARGS, TYPE, PRINT, WHILE, FOR, IF, ELSE, DQUOTE,
SQUOTE, ID, NUM, CHARLIT, TRUE, FALSE, STRINGLIT, EPSILON, DOLLAR;
@Override
public boolean isVariable() {
return false;
}
};
private TokenType type;
private Optional<String> value;
public Token(TokenType type) {
this.type = type;
this.value = Optional.empty();
}
public Token(TokenType type, String value) {
this.type = type;
this.value = Optional.of(value);
}
public Optional<String> getValue() {
return this.value;
}
public TokenType getType() {
return this.type;
}
@Override
public String toString() {
switch (type) {
case ID :
case NUM :
case CHARLIT :
case TYPE :
case STRINGLIT : return "[" + type + ": " + value + "]";
default : return "[" + type + "]";
}
}
@Override
public boolean equals(Object other) {
if (other == null) return false;
if (!Token.class.isAssignableFrom(other.getClass())) return false;
Token t = (Token) other;
if (t.type != this.type) return false;
if (t.value == null || this.value == null) return t.value == this.value;
return t.value.equals(this.value);
}
}