10
|
1 |
/*
|
|
2 |
* Copyright 2002-2006 Sun Microsystems, Inc. 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. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package com.sun.tools.javac.parser;
|
|
27 |
|
|
28 |
import com.sun.tools.javac.util.Context;
|
|
29 |
import com.sun.tools.javac.util.Log;
|
|
30 |
import com.sun.tools.javac.util.Name;
|
|
31 |
|
|
32 |
import static com.sun.tools.javac.parser.Token.*;
|
|
33 |
|
|
34 |
/**
|
|
35 |
* Map from Name to Token and Token to String.
|
|
36 |
*
|
|
37 |
* <p><b>This is NOT part of any API supported by Sun Microsystems.
|
|
38 |
* If you write code that depends on this, you do so at your own risk.
|
|
39 |
* This code and its internal interfaces are subject to change or
|
|
40 |
* deletion without notice.</b>
|
|
41 |
*/
|
|
42 |
public class Keywords {
|
|
43 |
public static final Context.Key<Keywords> keywordsKey =
|
|
44 |
new Context.Key<Keywords>();
|
|
45 |
|
|
46 |
public static Keywords instance(Context context) {
|
|
47 |
Keywords instance = context.get(keywordsKey);
|
|
48 |
if (instance == null)
|
|
49 |
instance = new Keywords(context);
|
|
50 |
return instance;
|
|
51 |
}
|
|
52 |
|
|
53 |
private final Log log;
|
|
54 |
private final Name.Table names;
|
|
55 |
|
|
56 |
protected Keywords(Context context) {
|
|
57 |
context.put(keywordsKey, this);
|
|
58 |
log = Log.instance(context);
|
|
59 |
names = Name.Table.instance(context);
|
|
60 |
|
|
61 |
for (Token t : Token.values()) {
|
|
62 |
if (t.name != null)
|
|
63 |
enterKeyword(t.name, t);
|
|
64 |
else
|
|
65 |
tokenName[t.ordinal()] = null;
|
|
66 |
}
|
|
67 |
|
|
68 |
key = new Token[maxKey+1];
|
|
69 |
for (int i = 0; i <= maxKey; i++) key[i] = IDENTIFIER;
|
|
70 |
for (Token t : Token.values()) {
|
|
71 |
if (t.name != null)
|
|
72 |
key[tokenName[t.ordinal()].index] = t;
|
|
73 |
}
|
|
74 |
}
|
|
75 |
|
|
76 |
|
|
77 |
public Token key(Name name) {
|
|
78 |
return (name.index > maxKey) ? IDENTIFIER : key[name.index];
|
|
79 |
}
|
|
80 |
|
|
81 |
/**
|
|
82 |
* Keyword array. Maps name indices to Token.
|
|
83 |
*/
|
|
84 |
private final Token[] key;
|
|
85 |
|
|
86 |
/** The number of the last entered keyword.
|
|
87 |
*/
|
|
88 |
private int maxKey = 0;
|
|
89 |
|
|
90 |
/** The names of all tokens.
|
|
91 |
*/
|
|
92 |
private Name[] tokenName = new Name[Token.values().length];
|
|
93 |
|
|
94 |
private void enterKeyword(String s, Token token) {
|
|
95 |
Name n = names.fromString(s);
|
|
96 |
tokenName[token.ordinal()] = n;
|
|
97 |
if (n.index > maxKey) maxKey = n.index;
|
|
98 |
}
|
|
99 |
}
|