duke@1: /* xdono@117: * Copyright 2002-2008 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; jjg@113: import com.sun.tools.javac.util.Names; 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: jjg@113: private final Names names; duke@1: duke@1: protected Keywords(Context context) { duke@1: context.put(keywordsKey, this); jjg@113: names = Names.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) jjg@113: key[tokenName[t.ordinal()].getIndex()] = t; duke@1: } duke@1: } duke@1: duke@1: duke@1: public Token key(Name name) { jjg@113: return (name.getIndex() > maxKey) ? IDENTIFIER : key[name.getIndex()]; 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: private void enterKeyword(String s, Token token) { duke@1: Name n = names.fromString(s); duke@1: tokenName[token.ordinal()] = n; jjg@113: if (n.getIndex() > maxKey) maxKey = n.getIndex(); duke@1: } duke@1: }