-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
Token.java
122 lines (101 loc) · 3.12 KB
/
Token.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package net.aufdemrand.denizen.utilities.javaluator;
/** A token.
* <br>When evaluating an expression, it is first split into tokens.
* These tokens can be operators, constants, etc ...
* @author Jean-Marc Astesana
* @see <a href="../../../license.html">License information</a>
*/
public class Token {
private enum Kind {
OPEN_BRACKET,
CLOSE_BRACKET,
FUNCTION_SEPARATOR,
FUNCTION,
OPERATOR,
LITERAL
}
static final Token FUNCTION_ARG_SEPARATOR = new Token(Kind.FUNCTION_SEPARATOR, null);
private Kind kind;
private Object content;
static Token buildLiteral(String literal) {
return new Token(Kind.LITERAL, literal);
}
static Token buildOperator(Operator ope) {
return new Token(Kind.OPERATOR, ope);
}
static Token buildFunction(Function function) {
return new Token(Kind.FUNCTION, function);
}
static Token buildOpenToken(BracketPair pair) {
return new Token(Kind.OPEN_BRACKET, pair);
}
static Token buildCloseToken(BracketPair pair) {
return new Token(Kind.CLOSE_BRACKET, pair);
}
private Token(Kind kind, Object content) {
super();
if ((kind.equals(Kind.OPERATOR) && !(content instanceof Operator)) ||
(kind.equals(Kind.FUNCTION) && !(content instanceof Function)) ||
(kind.equals(Kind.LITERAL) && !(content instanceof String))) throw new IllegalArgumentException();
this.kind = kind;
this.content = content;
}
BracketPair getBrackets() {
return (BracketPair) this.content;
}
Operator getOperator() {
return (Operator) this.content;
}
Function getFunction() {
return (Function) this.content;
}
Kind getKind() {
return kind;
}
/** Tests whether the token is an operator.
* @return true if the token is an operator
*/
public boolean isOperator() {
return kind.equals(Kind.OPERATOR);
}
/** Tests whether the token is a function.
* @return true if the token is a function
*/
public boolean isFunction() {
return kind.equals(Kind.FUNCTION);
}
/** Tests whether the token is an open bracket.
* @return true if the token is an open bracket
*/
public boolean isOpenBracket() {
return kind.equals(Kind.OPEN_BRACKET);
}
/** Tests whether the token is a close bracket.
* @return true if the token is a close bracket
*/
public boolean isCloseBracket() {
return kind.equals(Kind.CLOSE_BRACKET);
}
/** Tests whether the token is a function argument separator.
* @return true if the token is a function argument separator
*/
public boolean isFunctionArgumentSeparator() {
return kind.equals(Kind.FUNCTION_SEPARATOR);
}
/** Tests whether the token is a literal or a constant or a variable name.
* @return true if the token is a literal, a constant or a variable name
*/
public boolean isLiteral() {
return kind.equals(Kind.LITERAL);
}
Operator.Associativity getAssociativity() {
return getOperator().getAssociativity();
}
int getPrecedence() {
return getOperator().getPrecedence();
}
String getLiteral() {
if (!this.kind.equals(Kind.LITERAL)) throw new IllegalArgumentException();
return (String)this.content;
}
}