Repository navigation
Expand file tree
/
Copy pathToken.java
More file actions
215 lines (176 loc) · 4.28 KB
/
Copy pathToken.java
File metadata and controls
215 lines (176 loc) · 4.28 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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package loop;
import java.util.HashMap;
import java.util.Map;
/**
* @author Dhanji R. Prasanna
*/
public class Token {
public final String value;
public final Kind kind;
public final int line;
public final int column;
public Token(String value, Kind kind, int line, int column) {
this.value = kind == Kind.IDENT ? value.trim() : value;
this.kind = kind;
this.line = line;
this.column = column;
}
public static enum Kind {
PRIVATE_FIELD,
ANONYMOUS_TOKEN,
IDENT,
TYPE_IDENT,
BIG_INTEGER,
INTEGER,
FLOAT,
LONG,
STRING,
JAVA_LITERAL,
REGEX,
DOT,
PLUS,
MINUS,
DIVIDE,
STAR,
MODULUS,
COMMA,
PIPE,
ASSIGN,
ARROW,
UNARROW,
HASHROCKET,
// Comparison operators
EQUALS,
LESSER,
GREATER,
LEQ,
GEQ,
LPAREN,
RPAREN,
LBRACE,
RBRACE,
LBRACKET,
RBRACKET,
// keywords
REQUIRE,
MODULE,
IMMUTABLE,
CLASS,
NEW,
OR,
AND,
NOT,
FOR,
IN,
IF,
UNLESS,
THEN,
ELSE,
WHERE,
TRUE,
FALSE,
WHEN,
// specials
EOL,
INDENT;
private static final Map<String, Kind> TOKEN_MAP = new HashMap<String, Kind>();
static {
// can we optimize with chars?
TOKEN_MAP.put("=", ASSIGN);
TOKEN_MAP.put(".", DOT);
TOKEN_MAP.put("+", PLUS);
TOKEN_MAP.put("-", MINUS);
TOKEN_MAP.put("/", DIVIDE);
TOKEN_MAP.put("*", STAR);
TOKEN_MAP.put("%", MODULUS);
TOKEN_MAP.put(":", ASSIGN);
TOKEN_MAP.put("|", PIPE);
TOKEN_MAP.put(",", COMMA);
TOKEN_MAP.put("->", ARROW);
TOKEN_MAP.put("<-", UNARROW);
TOKEN_MAP.put("==", EQUALS);
TOKEN_MAP.put("<=", LEQ);
TOKEN_MAP.put(">=", GEQ);
TOKEN_MAP.put("<", LESSER);
TOKEN_MAP.put(">", GREATER);
TOKEN_MAP.put("=>", HASHROCKET);
TOKEN_MAP.put("(", LPAREN);
TOKEN_MAP.put(")", RPAREN);
TOKEN_MAP.put("{", LBRACE);
TOKEN_MAP.put("}", RBRACE);
TOKEN_MAP.put("[", LBRACKET);
TOKEN_MAP.put("]", RBRACKET);
TOKEN_MAP.put("\n", EOL);
TOKEN_MAP.put("if", IF);
TOKEN_MAP.put("unless", UNLESS);
TOKEN_MAP.put("then", THEN);
TOKEN_MAP.put("else", ELSE);
TOKEN_MAP.put("when", WHEN);
TOKEN_MAP.put("where", WHERE);
TOKEN_MAP.put("for", FOR);
TOKEN_MAP.put("in", IN);
TOKEN_MAP.put("new", NEW);
TOKEN_MAP.put("class", CLASS);
TOKEN_MAP.put("immutable", IMMUTABLE);
TOKEN_MAP.put("require", REQUIRE);
TOKEN_MAP.put("module", MODULE);
TOKEN_MAP.put("or", OR);
TOKEN_MAP.put("and", AND);
TOKEN_MAP.put("not", NOT);
TOKEN_MAP.put("true", TRUE);
TOKEN_MAP.put("false", FALSE);
}
/**
* from token text, determines kind.
*/
public static Kind determine(String value) {
char first = value.charAt(0);
if (value.matches("@[0-9]+"))
return BIG_INTEGER;
else if (first == '@') {
return value.length() > 1 ? PRIVATE_FIELD : ANONYMOUS_TOKEN;
}
if (first == '"' || first == '\'')
return STRING;
else if (first == '`' && value.endsWith("`"))
return JAVA_LITERAL;
Kind knownKind = TOKEN_MAP.get(value);
if (null != knownKind)
return knownKind;
// Integers (can this be more efficient?)
if (value.matches("[0-9]+"))
return INTEGER;
else if (value.matches("[0-9]+F"))
return FLOAT;
else if (value.matches("[0-9]+L"))
return LONG;
if (Character.isUpperCase(first)) {
return TYPE_IDENT;
}
if (value.endsWith("/"))
return REGEX;
return IDENT;
}
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Token token = (Token) o;
return (kind == token.kind)
&& !(value != null ? !value.equals(token.value) : token.value != null);
}
@Override
public int hashCode() {
int result = value != null ? value.hashCode() : 0;
result = 31 * result + (kind != null ? kind.hashCode() : 0);
return result;
}
@Override
public String toString() {
return "Token{" +
"value='" + value + '\'' +
", kind=" + kind +
'}';
}
}