001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.lexer.gen;
043:
044: import java.lang.reflect.Field;
045: import java.lang.SecurityException;
046: import java.util.ArrayList;
047: import java.util.Arrays;
048: import java.util.Collections;
049: import java.util.Map;
050: import java.util.HashMap;
051: import java.util.Iterator;
052: import java.util.Set;
053: import java.util.List;
054: import org.netbeans.modules.lexer.gen.util.LexerGenUtilities;
055:
056: /**
057: * The lexer generators often generate a class or interface
058: * that contains integer fields of token types
059: * named e.g. xxxConstants or xxxTokenTypes etc.
060: * <BR>The <CODE>TokenConstants</CODE> class encapsulates the information
061: * contained in such token types class.
062: * <P>The reflection is used to collect
063: * the "public static final int" fields in the token types class.
064: * All these fields are collected but subclasses
065: * may wish to hide some of the fields (e.g. some fields
066: * may be related to states of an automaton instead of token types
067: * identification).
068: *
069: * @author Miloslav Metelka
070: * @version 1.00
071: */
072:
073: public class TokenTypes {
074:
075: private final Class tokenTypesClass;
076:
077: private boolean inspected;
078:
079: /** Map of [tokenTypeName, tokenTypeValue] */
080: protected final Map name2value = new HashMap();
081:
082: /** Map of [tokenTypeValue, tokenTypeName] */
083: protected final Map value2name = new HashMap();
084:
085: public TokenTypes(Class tokenTypesClass) {
086: this .tokenTypesClass = tokenTypesClass;
087: }
088:
089: /**
090: * Called by <CODE>LanguageData.registerTokenTypes()</CODE>
091: * to update the language data into which it's being registered.
092: * By default it adds mutable token ids that correspond
093: * to the constants discovered in token types.
094: * Can be overriden by subclasses to provide some more functionality.
095: */
096: protected void updateData(LanguageData languageData) {
097: inspect();
098:
099: for (Iterator it = tokenTypeNamesIterator(); it.hasNext();) {
100: String tokenTypeName = (String) it.next();
101: MutableTokenId id = languageData
102: .findIdByTokenTypeName(tokenTypeName);
103:
104: if (id == null) {
105: String idName = LexerGenUtilities
106: .idToLowerCase(tokenTypeName);
107: id = languageData.newId(idName);
108: id.updateByTokenType(tokenTypeName); // updateId() called automatically
109:
110: } else {
111: updateId(id);
112: }
113: }
114: }
115:
116: /**
117: * Update a newly created or an existing token-id by the information
118: * contained in this token-types.
119: * The passed token-id already has tokenTypeName
120: * filled in.
121: */
122: protected void updateId(MutableTokenId id) {
123: String tokenTypeName = id.getTokenTypeName();
124: if (tokenTypeName != null) { // no associated tokenTypeName
125: Integer value = getTokenTypeValue(tokenTypeName);
126: if (value == null) {
127: throw new IllegalArgumentException("tokenTypeName="
128: + tokenTypeName + " is not declared in "
129: + getTokenTypesClass().getName());
130: }
131:
132: // assign intId
133: id.setIntId(value.intValue());
134: }
135: }
136:
137: public Class getTokenTypesClass() {
138: return tokenTypesClass;
139: }
140:
141: /**
142: * @return Integer value of the static field with the given name
143: * or null if the field does not exist.
144: */
145: public Integer getTokenTypeValue(String tokenTypeName) {
146: inspect();
147:
148: return (Integer) name2value.get(tokenTypeName);
149: }
150:
151: public String getTokenTypeName(int tokenTypeValue) {
152: inspect();
153:
154: return (String) value2name.get(new Integer(tokenTypeValue));
155: }
156:
157: /**
158: * @return all the field names
159: */
160: public Iterator tokenTypeNamesIterator() {
161: inspect();
162:
163: return name2value.keySet().iterator();
164: }
165:
166: public int findMaxTokenTypeValue() {
167: inspect();
168:
169: int maxValue = 0;
170: for (Iterator it = value2name.keySet().iterator(); it.hasNext();) {
171: Integer i = (Integer) it.next();
172: maxValue = Math.max(maxValue, i.intValue());
173: }
174: return maxValue;
175: }
176:
177: /** Inspect the token types class.
178: * This method can be overriden by children if necessary.
179: * The method goes through the class
180: * and puts the [field-name, integer-constant-value]
181: * for all the static fields into the info map.
182: * The <CODE>null</CODE> key is mapped to maximum constant value
183: * found in the token types class.
184: * The <CODE>List.class</CODE> key is mapped to the list
185: * of all the field names in the order in which they
186: * were found in the token types class.
187: * @return true if the inspection was really done
188: * or false if the inspection was already done previously.
189: */
190: protected boolean inspect() {
191: if (inspected) {
192: return false;
193: }
194: inspected = true;
195:
196: try {
197: Field[] fields = getTokenTypesClass().getDeclaredFields();
198: for (int i = 0; i < fields.length; i++) {
199: Field f = fields[i];
200: if (f.getType() == int.class) {
201: int value = f.getInt(null);
202: String fieldName = f.getName();
203: if (isAccepted(fieldName, value)) {
204: Integer valueInteger = new Integer(value);
205: name2value.put(fieldName, valueInteger);
206: value2name.put(valueInteger, fieldName);
207: }
208: }
209: }
210:
211: } catch (SecurityException e) {
212: e.printStackTrace();
213: throw new IllegalStateException(e.toString());
214:
215: } catch (IllegalAccessException e) {
216: e.printStackTrace();
217: throw new IllegalStateException(e.toString());
218: }
219:
220: return true; // inspection really done
221: }
222:
223: /**
224: * Whether it's ok to add the given field name to the list
225: * of the [tokenTypeName, Integer] pairs.
226: * <BR>Subclasses can exclude some field(s) if necessary.
227: */
228: protected boolean isAccepted(String tokenTypeName,
229: int tokenTypeValue) {
230: return true;
231: }
232:
233: }
|