duke@1: /*
duke@1: * Copyright 2002-2006 Sun Microsystems, Inc. All Rights Reserved.
duke@1: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1: *
duke@1: * This code is free software; you can redistribute it and/or modify it
duke@1: * under the terms of the GNU General Public License version 2 only, as
duke@1: * published by the Free Software Foundation. Sun designates this
duke@1: * particular file as subject to the "Classpath" exception as provided
duke@1: * by Sun in the LICENSE file that accompanied this code.
duke@1: *
duke@1: * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1: * version 2 for more details (a copy is included in the LICENSE file that
duke@1: * accompanied this code).
duke@1: *
duke@1: * You should have received a copy of the GNU General Public License version
duke@1: * 2 along with this work; if not, write to the Free Software Foundation,
duke@1: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1: *
duke@1: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@1: * CA 95054 USA or visit www.sun.com if you need additional information or
duke@1: * have any questions.
duke@1: */
duke@1:
duke@1: package com.sun.tools.javac.parser;
duke@1:
duke@1: import com.sun.tools.javac.util.Context;
duke@1: import com.sun.tools.javac.util.Log;
duke@1: import com.sun.tools.javac.util.Name;
duke@1:
duke@1: import static com.sun.tools.javac.parser.Token.*;
duke@1:
duke@1: /**
duke@1: * Map from Name to Token and Token to String.
duke@1: *
duke@1: *
This is NOT part of any API supported by Sun Microsystems.
duke@1: * If you write code that depends on this, you do so at your own risk.
duke@1: * This code and its internal interfaces are subject to change or
duke@1: * deletion without notice.
duke@1: */
duke@1: public class Keywords {
duke@1: public static final Context.Key keywordsKey =
duke@1: new Context.Key();
duke@1:
duke@1: public static Keywords instance(Context context) {
duke@1: Keywords instance = context.get(keywordsKey);
duke@1: if (instance == null)
duke@1: instance = new Keywords(context);
duke@1: return instance;
duke@1: }
duke@1:
duke@1: private final Log log;
duke@1: private final Name.Table names;
duke@1:
duke@1: protected Keywords(Context context) {
duke@1: context.put(keywordsKey, this);
duke@1: log = Log.instance(context);
duke@1: names = Name.Table.instance(context);
duke@1:
duke@1: for (Token t : Token.values()) {
duke@1: if (t.name != null)
duke@1: enterKeyword(t.name, t);
duke@1: else
duke@1: tokenName[t.ordinal()] = null;
duke@1: }
duke@1:
duke@1: key = new Token[maxKey+1];
duke@1: for (int i = 0; i <= maxKey; i++) key[i] = IDENTIFIER;
duke@1: for (Token t : Token.values()) {
duke@1: if (t.name != null)
duke@1: key[tokenName[t.ordinal()].index] = t;
duke@1: }
duke@1: }
duke@1:
duke@1:
duke@1: public Token key(Name name) {
duke@1: return (name.index > maxKey) ? IDENTIFIER : key[name.index];
duke@1: }
duke@1:
duke@1: /**
duke@1: * Keyword array. Maps name indices to Token.
duke@1: */
duke@1: private final Token[] key;
duke@1:
duke@1: /** The number of the last entered keyword.
duke@1: */
duke@1: private int maxKey = 0;
duke@1:
duke@1: /** The names of all tokens.
duke@1: */
duke@1: private Name[] tokenName = new Name[Token.values().length];
duke@1:
duke@1: public String token2string(Token token) {
duke@1: switch (token) {
duke@1: case IDENTIFIER:
duke@1: return log.getLocalizedString("token.identifier");
duke@1: case CHARLITERAL:
duke@1: return log.getLocalizedString("token.character");
duke@1: case STRINGLITERAL:
duke@1: return log.getLocalizedString("token.string");
duke@1: case INTLITERAL:
duke@1: return log.getLocalizedString("token.integer");
duke@1: case LONGLITERAL:
duke@1: return log.getLocalizedString("token.long-integer");
duke@1: case FLOATLITERAL:
duke@1: return log.getLocalizedString("token.float");
duke@1: case DOUBLELITERAL:
duke@1: return log.getLocalizedString("token.double");
duke@1: case ERROR:
duke@1: return log.getLocalizedString("token.bad-symbol");
duke@1: case EOF:
duke@1: return log.getLocalizedString("token.end-of-input");
duke@1: case DOT: case COMMA: case SEMI: case LPAREN: case RPAREN:
duke@1: case LBRACKET: case RBRACKET: case LBRACE: case RBRACE:
duke@1: return "'" + token.name + "'";
duke@1: default:
duke@1: return token.name;
duke@1: }
duke@1: }
duke@1:
duke@1: private void enterKeyword(String s, Token token) {
duke@1: Name n = names.fromString(s);
duke@1: tokenName[token.ordinal()] = n;
duke@1: if (n.index > maxKey) maxKey = n.index;
duke@1: }
duke@1: }