1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/classes/com/sun/tools/javac/parser/Keywords.java Sat Dec 01 00:00:00 2007 +0000 1.3 @@ -0,0 +1,127 @@ 1.4 +/* 1.5 + * Copyright 2002-2006 Sun Microsystems, Inc. All Rights Reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Sun designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Sun in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 1.25 + * CA 95054 USA or visit www.sun.com if you need additional information or 1.26 + * have any questions. 1.27 + */ 1.28 + 1.29 +package com.sun.tools.javac.parser; 1.30 + 1.31 +import com.sun.tools.javac.util.Context; 1.32 +import com.sun.tools.javac.util.Log; 1.33 +import com.sun.tools.javac.util.Name; 1.34 + 1.35 +import static com.sun.tools.javac.parser.Token.*; 1.36 + 1.37 +/** 1.38 + * Map from Name to Token and Token to String. 1.39 + * 1.40 + * <p><b>This is NOT part of any API supported by Sun Microsystems. 1.41 + * If you write code that depends on this, you do so at your own risk. 1.42 + * This code and its internal interfaces are subject to change or 1.43 + * deletion without notice.</b> 1.44 + */ 1.45 +public class Keywords { 1.46 + public static final Context.Key<Keywords> keywordsKey = 1.47 + new Context.Key<Keywords>(); 1.48 + 1.49 + public static Keywords instance(Context context) { 1.50 + Keywords instance = context.get(keywordsKey); 1.51 + if (instance == null) 1.52 + instance = new Keywords(context); 1.53 + return instance; 1.54 + } 1.55 + 1.56 + private final Log log; 1.57 + private final Name.Table names; 1.58 + 1.59 + protected Keywords(Context context) { 1.60 + context.put(keywordsKey, this); 1.61 + log = Log.instance(context); 1.62 + names = Name.Table.instance(context); 1.63 + 1.64 + for (Token t : Token.values()) { 1.65 + if (t.name != null) 1.66 + enterKeyword(t.name, t); 1.67 + else 1.68 + tokenName[t.ordinal()] = null; 1.69 + } 1.70 + 1.71 + key = new Token[maxKey+1]; 1.72 + for (int i = 0; i <= maxKey; i++) key[i] = IDENTIFIER; 1.73 + for (Token t : Token.values()) { 1.74 + if (t.name != null) 1.75 + key[tokenName[t.ordinal()].index] = t; 1.76 + } 1.77 + } 1.78 + 1.79 + 1.80 + public Token key(Name name) { 1.81 + return (name.index > maxKey) ? IDENTIFIER : key[name.index]; 1.82 + } 1.83 + 1.84 + /** 1.85 + * Keyword array. Maps name indices to Token. 1.86 + */ 1.87 + private final Token[] key; 1.88 + 1.89 + /** The number of the last entered keyword. 1.90 + */ 1.91 + private int maxKey = 0; 1.92 + 1.93 + /** The names of all tokens. 1.94 + */ 1.95 + private Name[] tokenName = new Name[Token.values().length]; 1.96 + 1.97 + public String token2string(Token token) { 1.98 + switch (token) { 1.99 + case IDENTIFIER: 1.100 + return log.getLocalizedString("token.identifier"); 1.101 + case CHARLITERAL: 1.102 + return log.getLocalizedString("token.character"); 1.103 + case STRINGLITERAL: 1.104 + return log.getLocalizedString("token.string"); 1.105 + case INTLITERAL: 1.106 + return log.getLocalizedString("token.integer"); 1.107 + case LONGLITERAL: 1.108 + return log.getLocalizedString("token.long-integer"); 1.109 + case FLOATLITERAL: 1.110 + return log.getLocalizedString("token.float"); 1.111 + case DOUBLELITERAL: 1.112 + return log.getLocalizedString("token.double"); 1.113 + case ERROR: 1.114 + return log.getLocalizedString("token.bad-symbol"); 1.115 + case EOF: 1.116 + return log.getLocalizedString("token.end-of-input"); 1.117 + case DOT: case COMMA: case SEMI: case LPAREN: case RPAREN: 1.118 + case LBRACKET: case RBRACKET: case LBRACE: case RBRACE: 1.119 + return "'" + token.name + "'"; 1.120 + default: 1.121 + return token.name; 1.122 + } 1.123 + } 1.124 + 1.125 + private void enterKeyword(String s, Token token) { 1.126 + Name n = names.fromString(s); 1.127 + tokenName[token.ordinal()] = n; 1.128 + if (n.index > maxKey) maxKey = n.index; 1.129 + } 1.130 +}