10815
|
1 |
/*
|
|
2 |
* Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package com.sun.tools.javac.parser;
|
|
27 |
|
|
28 |
import java.util.Locale;
|
|
29 |
|
|
30 |
import com.sun.tools.javac.api.Formattable;
|
|
31 |
import com.sun.tools.javac.api.Messages;
|
|
32 |
import com.sun.tools.javac.parser.Tokens.Token.Tag;
|
|
33 |
import com.sun.tools.javac.util.Name;
|
|
34 |
import com.sun.tools.javac.util.Context;
|
|
35 |
import com.sun.tools.javac.util.Names;
|
|
36 |
|
|
37 |
/** A class that defines codes/utilities for Java source tokens
|
|
38 |
* returned from lexical analysis.
|
|
39 |
*
|
|
40 |
* <p><b>This is NOT part of any supported API.
|
|
41 |
* If you write code that depends on this, you do so at your own risk.
|
|
42 |
* This code and its internal interfaces are subject to change or
|
|
43 |
* deletion without notice.</b>
|
|
44 |
*/
|
|
45 |
public class Tokens {
|
|
46 |
|
|
47 |
private final Names names;
|
|
48 |
|
|
49 |
/**
|
|
50 |
* Keyword array. Maps name indices to Token.
|
|
51 |
*/
|
|
52 |
private final TokenKind[] key;
|
|
53 |
|
|
54 |
/** The number of the last entered keyword.
|
|
55 |
*/
|
|
56 |
private int maxKey = 0;
|
|
57 |
|
|
58 |
/** The names of all tokens.
|
|
59 |
*/
|
|
60 |
private Name[] tokenName = new Name[TokenKind.values().length];
|
|
61 |
|
|
62 |
public static final Context.Key<Tokens> tokensKey =
|
|
63 |
new Context.Key<Tokens>();
|
|
64 |
|
|
65 |
public static Tokens instance(Context context) {
|
|
66 |
Tokens instance = context.get(tokensKey);
|
|
67 |
if (instance == null)
|
|
68 |
instance = new Tokens(context);
|
|
69 |
return instance;
|
|
70 |
}
|
|
71 |
|
|
72 |
protected Tokens(Context context) {
|
|
73 |
context.put(tokensKey, this);
|
|
74 |
names = Names.instance(context);
|
|
75 |
|
|
76 |
for (TokenKind t : TokenKind.values()) {
|
|
77 |
if (t.name != null)
|
|
78 |
enterKeyword(t.name, t);
|
|
79 |
else
|
|
80 |
tokenName[t.ordinal()] = null;
|
|
81 |
}
|
|
82 |
|
|
83 |
key = new TokenKind[maxKey+1];
|
|
84 |
for (int i = 0; i <= maxKey; i++) key[i] = TokenKind.IDENTIFIER;
|
|
85 |
for (TokenKind t : TokenKind.values()) {
|
|
86 |
if (t.name != null)
|
|
87 |
key[tokenName[t.ordinal()].getIndex()] = t;
|
|
88 |
}
|
|
89 |
}
|
|
90 |
|
|
91 |
private void enterKeyword(String s, TokenKind token) {
|
|
92 |
Name n = names.fromString(s);
|
|
93 |
tokenName[token.ordinal()] = n;
|
|
94 |
if (n.getIndex() > maxKey) maxKey = n.getIndex();
|
|
95 |
}
|
|
96 |
|
|
97 |
/**
|
|
98 |
* Create a new token given a name; if the name corresponds to a token name,
|
|
99 |
* a new token of the corresponding kind is returned; otherwise, an
|
|
100 |
* identifier token is returned.
|
|
101 |
*/
|
|
102 |
TokenKind lookupKind(Name name) {
|
|
103 |
return (name.getIndex() > maxKey) ? TokenKind.IDENTIFIER : key[name.getIndex()];
|
|
104 |
}
|
|
105 |
|
|
106 |
TokenKind lookupKind(String name) {
|
|
107 |
return lookupKind(names.fromString(name));
|
|
108 |
}
|
|
109 |
|
|
110 |
/**
|
|
111 |
* This enum defines all tokens used by the javac scanner. A token is
|
|
112 |
* optionally associated with a name.
|
|
113 |
*/
|
|
114 |
public enum TokenKind implements Formattable {
|
|
115 |
EOF(),
|
|
116 |
ERROR(),
|
|
117 |
IDENTIFIER(Tag.NAMED),
|
|
118 |
ABSTRACT("abstract"),
|
|
119 |
ASSERT("assert", Tag.NAMED),
|
|
120 |
BOOLEAN("boolean", Tag.NAMED),
|
|
121 |
BREAK("break"),
|
|
122 |
BYTE("byte", Tag.NAMED),
|
|
123 |
CASE("case"),
|
|
124 |
CATCH("catch"),
|
|
125 |
CHAR("char", Tag.NAMED),
|
|
126 |
CLASS("class"),
|
|
127 |
CONST("const"),
|
|
128 |
CONTINUE("continue"),
|
|
129 |
DEFAULT("default"),
|
|
130 |
DO("do"),
|
|
131 |
DOUBLE("double", Tag.NAMED),
|
|
132 |
ELSE("else"),
|
|
133 |
ENUM("enum", Tag.NAMED),
|
|
134 |
EXTENDS("extends"),
|
|
135 |
FINAL("final"),
|
|
136 |
FINALLY("finally"),
|
|
137 |
FLOAT("float", Tag.NAMED),
|
|
138 |
FOR("for"),
|
|
139 |
GOTO("goto"),
|
|
140 |
IF("if"),
|
|
141 |
IMPLEMENTS("implements"),
|
|
142 |
IMPORT("import"),
|
|
143 |
INSTANCEOF("instanceof"),
|
|
144 |
INT("int", Tag.NAMED),
|
|
145 |
INTERFACE("interface"),
|
|
146 |
LONG("long", Tag.NAMED),
|
|
147 |
NATIVE("native"),
|
|
148 |
NEW("new"),
|
|
149 |
PACKAGE("package"),
|
|
150 |
PRIVATE("private"),
|
|
151 |
PROTECTED("protected"),
|
|
152 |
PUBLIC("public"),
|
|
153 |
RETURN("return"),
|
|
154 |
SHORT("short", Tag.NAMED),
|
|
155 |
STATIC("static"),
|
|
156 |
STRICTFP("strictfp"),
|
|
157 |
SUPER("super", Tag.NAMED),
|
|
158 |
SWITCH("switch"),
|
|
159 |
SYNCHRONIZED("synchronized"),
|
|
160 |
THIS("this", Tag.NAMED),
|
|
161 |
THROW("throw"),
|
|
162 |
THROWS("throws"),
|
|
163 |
TRANSIENT("transient"),
|
|
164 |
TRY("try"),
|
|
165 |
VOID("void", Tag.NAMED),
|
|
166 |
VOLATILE("volatile"),
|
|
167 |
WHILE("while"),
|
|
168 |
INTLITERAL(Tag.NUMERIC),
|
|
169 |
LONGLITERAL(Tag.NUMERIC),
|
|
170 |
FLOATLITERAL(Tag.NUMERIC),
|
|
171 |
DOUBLELITERAL(Tag.NUMERIC),
|
|
172 |
CHARLITERAL(Tag.NUMERIC),
|
|
173 |
STRINGLITERAL(Tag.STRING),
|
|
174 |
TRUE("true", Tag.NAMED),
|
|
175 |
FALSE("false", Tag.NAMED),
|
|
176 |
NULL("null", Tag.NAMED),
|
|
177 |
LPAREN("("),
|
|
178 |
RPAREN(")"),
|
|
179 |
LBRACE("{"),
|
|
180 |
RBRACE("}"),
|
|
181 |
LBRACKET("["),
|
|
182 |
RBRACKET("]"),
|
|
183 |
SEMI(";"),
|
|
184 |
COMMA(","),
|
|
185 |
DOT("."),
|
|
186 |
ELLIPSIS("..."),
|
|
187 |
EQ("="),
|
|
188 |
GT(">"),
|
|
189 |
LT("<"),
|
|
190 |
BANG("!"),
|
|
191 |
TILDE("~"),
|
|
192 |
QUES("?"),
|
|
193 |
COLON(":"),
|
|
194 |
EQEQ("=="),
|
|
195 |
LTEQ("<="),
|
|
196 |
GTEQ(">="),
|
|
197 |
BANGEQ("!="),
|
|
198 |
AMPAMP("&&"),
|
|
199 |
BARBAR("||"),
|
|
200 |
PLUSPLUS("++"),
|
|
201 |
SUBSUB("--"),
|
|
202 |
PLUS("+"),
|
|
203 |
SUB("-"),
|
|
204 |
STAR("*"),
|
|
205 |
SLASH("/"),
|
|
206 |
AMP("&"),
|
|
207 |
BAR("|"),
|
|
208 |
CARET("^"),
|
|
209 |
PERCENT("%"),
|
|
210 |
LTLT("<<"),
|
|
211 |
GTGT(">>"),
|
|
212 |
GTGTGT(">>>"),
|
|
213 |
PLUSEQ("+="),
|
|
214 |
SUBEQ("-="),
|
|
215 |
STAREQ("*="),
|
|
216 |
SLASHEQ("/="),
|
|
217 |
AMPEQ("&="),
|
|
218 |
BAREQ("|="),
|
|
219 |
CARETEQ("^="),
|
|
220 |
PERCENTEQ("%="),
|
|
221 |
LTLTEQ("<<="),
|
|
222 |
GTGTEQ(">>="),
|
|
223 |
GTGTGTEQ(">>>="),
|
|
224 |
MONKEYS_AT("@"),
|
|
225 |
CUSTOM;
|
|
226 |
|
|
227 |
public final String name;
|
|
228 |
public final Tag tag;
|
|
229 |
|
|
230 |
TokenKind() {
|
|
231 |
this(null, Tag.DEFAULT);
|
|
232 |
}
|
|
233 |
|
|
234 |
TokenKind(String name) {
|
|
235 |
this(name, Tag.DEFAULT);
|
|
236 |
}
|
|
237 |
|
|
238 |
TokenKind(Tag tag) {
|
|
239 |
this(null, tag);
|
|
240 |
}
|
|
241 |
|
|
242 |
TokenKind(String name, Tag tag) {
|
|
243 |
this.name = name;
|
|
244 |
this.tag = tag;
|
|
245 |
}
|
|
246 |
|
|
247 |
public String toString() {
|
|
248 |
switch (this) {
|
|
249 |
case IDENTIFIER:
|
|
250 |
return "token.identifier";
|
|
251 |
case CHARLITERAL:
|
|
252 |
return "token.character";
|
|
253 |
case STRINGLITERAL:
|
|
254 |
return "token.string";
|
|
255 |
case INTLITERAL:
|
|
256 |
return "token.integer";
|
|
257 |
case LONGLITERAL:
|
|
258 |
return "token.long-integer";
|
|
259 |
case FLOATLITERAL:
|
|
260 |
return "token.float";
|
|
261 |
case DOUBLELITERAL:
|
|
262 |
return "token.double";
|
|
263 |
case ERROR:
|
|
264 |
return "token.bad-symbol";
|
|
265 |
case EOF:
|
|
266 |
return "token.end-of-input";
|
|
267 |
case DOT: case COMMA: case SEMI: case LPAREN: case RPAREN:
|
|
268 |
case LBRACKET: case RBRACKET: case LBRACE: case RBRACE:
|
|
269 |
return "'" + name + "'";
|
|
270 |
default:
|
|
271 |
return name;
|
|
272 |
}
|
|
273 |
}
|
|
274 |
|
|
275 |
public String getKind() {
|
|
276 |
return "Token";
|
|
277 |
}
|
|
278 |
|
|
279 |
public String toString(Locale locale, Messages messages) {
|
|
280 |
return name != null ? toString() : messages.getLocalizedString(locale, "compiler.misc." + toString());
|
|
281 |
}
|
|
282 |
}
|
|
283 |
|
|
284 |
/**
|
|
285 |
* This is the class representing a javac token. Each token has several fields
|
|
286 |
* that are set by the javac lexer (i.e. start/end position, string value, etc).
|
|
287 |
*/
|
|
288 |
public static class Token {
|
|
289 |
|
|
290 |
/** tags constants **/
|
|
291 |
enum Tag {
|
|
292 |
DEFAULT,
|
|
293 |
NAMED,
|
|
294 |
STRING,
|
|
295 |
NUMERIC;
|
|
296 |
}
|
|
297 |
|
|
298 |
/** The token kind */
|
|
299 |
public final TokenKind kind;
|
|
300 |
|
|
301 |
/** The start position of this token */
|
|
302 |
public final int pos;
|
|
303 |
|
|
304 |
/** The end position of this token */
|
|
305 |
public final int endPos;
|
|
306 |
|
|
307 |
/** Is this token preceeded by a deprecated comment? */
|
|
308 |
public final boolean deprecatedFlag;
|
|
309 |
|
|
310 |
/** Is this token preceeded by a deprecated comment? */
|
|
311 |
public String docComment;
|
|
312 |
|
|
313 |
Token(TokenKind kind, int pos, int endPos,
|
|
314 |
boolean deprecatedFlag) {
|
|
315 |
this.kind = kind;
|
|
316 |
this.pos = pos;
|
|
317 |
this.endPos = endPos;
|
|
318 |
this.deprecatedFlag = deprecatedFlag;
|
|
319 |
checkKind();
|
|
320 |
}
|
|
321 |
|
|
322 |
Token[] split(Tokens tokens) {
|
|
323 |
if (kind.name.length() < 2 || kind.tag != Tag.DEFAULT) {
|
|
324 |
throw new AssertionError("Cant split" + kind);
|
|
325 |
}
|
|
326 |
|
|
327 |
TokenKind t1 = tokens.lookupKind(kind.name.substring(0, 1));
|
|
328 |
TokenKind t2 = tokens.lookupKind(kind.name.substring(1));
|
|
329 |
|
|
330 |
if (t1 == null || t2 == null) {
|
|
331 |
throw new AssertionError("Cant split - bad subtokens");
|
|
332 |
}
|
|
333 |
return new Token[] {
|
|
334 |
new Token(t1, pos, pos + t1.name.length(), deprecatedFlag),
|
|
335 |
new Token(t2, pos + t1.name.length(), endPos, false)
|
|
336 |
};
|
|
337 |
}
|
|
338 |
|
|
339 |
protected void checkKind() {
|
|
340 |
if (kind.tag != Tag.DEFAULT) {
|
|
341 |
throw new AssertionError("Bad token kind - expected " + Tag.STRING);
|
|
342 |
}
|
|
343 |
}
|
|
344 |
|
|
345 |
public Name name() {
|
|
346 |
throw new UnsupportedOperationException();
|
|
347 |
}
|
|
348 |
|
|
349 |
public String stringVal() {
|
|
350 |
throw new UnsupportedOperationException();
|
|
351 |
}
|
|
352 |
|
|
353 |
public int radix() {
|
|
354 |
throw new UnsupportedOperationException();
|
|
355 |
}
|
|
356 |
}
|
|
357 |
|
|
358 |
final static class NamedToken extends Token {
|
|
359 |
/** The name of this token */
|
|
360 |
public final Name name;
|
|
361 |
|
|
362 |
public NamedToken(TokenKind kind, int pos, int endPos, Name name, boolean deprecatedFlag) {
|
|
363 |
super(kind, pos, endPos, deprecatedFlag);
|
|
364 |
this.name = name;
|
|
365 |
}
|
|
366 |
|
|
367 |
protected void checkKind() {
|
|
368 |
if (kind.tag != Tag.NAMED) {
|
|
369 |
throw new AssertionError("Bad token kind - expected " + Tag.NAMED);
|
|
370 |
}
|
|
371 |
}
|
|
372 |
|
|
373 |
@Override
|
|
374 |
public Name name() {
|
|
375 |
return name;
|
|
376 |
}
|
|
377 |
}
|
|
378 |
|
|
379 |
static class StringToken extends Token {
|
|
380 |
/** The string value of this token */
|
|
381 |
public final String stringVal;
|
|
382 |
|
|
383 |
public StringToken(TokenKind kind, int pos, int endPos, String stringVal, boolean deprecatedFlag) {
|
|
384 |
super(kind, pos, endPos, deprecatedFlag);
|
|
385 |
this.stringVal = stringVal;
|
|
386 |
}
|
|
387 |
|
|
388 |
protected void checkKind() {
|
|
389 |
if (kind.tag != Tag.STRING) {
|
|
390 |
throw new AssertionError("Bad token kind - expected " + Tag.STRING);
|
|
391 |
}
|
|
392 |
}
|
|
393 |
|
|
394 |
@Override
|
|
395 |
public String stringVal() {
|
|
396 |
return stringVal;
|
|
397 |
}
|
|
398 |
}
|
|
399 |
|
|
400 |
final static class NumericToken extends StringToken {
|
|
401 |
/** The 'radix' value of this token */
|
|
402 |
public final int radix;
|
|
403 |
|
|
404 |
public NumericToken(TokenKind kind, int pos, int endPos, String stringVal, int radix, boolean deprecatedFlag) {
|
|
405 |
super(kind, pos, endPos, stringVal, deprecatedFlag);
|
|
406 |
this.radix = radix;
|
|
407 |
}
|
|
408 |
|
|
409 |
protected void checkKind() {
|
|
410 |
if (kind.tag != Tag.NUMERIC) {
|
|
411 |
throw new AssertionError("Bad token kind - expected " + Tag.NUMERIC);
|
|
412 |
}
|
|
413 |
}
|
|
414 |
|
|
415 |
@Override
|
|
416 |
public int radix() {
|
|
417 |
return radix;
|
|
418 |
}
|
|
419 |
}
|
|
420 |
|
|
421 |
public static final Token DUMMY =
|
|
422 |
new Token(TokenKind.ERROR, 0, 0, false);
|
|
423 |
}
|