2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
42
package org.netbeans.modules.lexer.gen;
44
import java.lang.reflect.Field;
45
import java.lang.SecurityException;
46
import java.util.ArrayList;
47
import java.util.Arrays;
48
import java.util.Collections;
50
import java.util.HashMap;
51
import java.util.Iterator;
53
import java.util.List;
54
import org.netbeans.modules.lexer.gen.util.LexerGenUtilities;
57
* The lexer generators often generate a class or interface
58
* that contains integer fields of token types
59
* named e.g. xxxConstants or xxxTokenTypes etc.
60
* <BR>The <CODE>TokenConstants</CODE> class encapsulates the information
61
* contained in such token types class.
62
* <P>The reflection is used to collect
63
* the "public static final int" fields in the token types class.
64
* All these fields are collected but subclasses
65
* may wish to hide some of the fields (e.g. some fields
66
* may be related to states of an automaton instead of token types
69
* @author Miloslav Metelka
73
public class TokenTypes {
75
private final Class tokenTypesClass;
77
private boolean inspected;
79
/** Map of [tokenTypeName, tokenTypeValue] */
80
protected final Map name2value = new HashMap();
82
/** Map of [tokenTypeValue, tokenTypeName] */
83
protected final Map value2name = new HashMap();
85
public TokenTypes(Class tokenTypesClass) {
86
this.tokenTypesClass = tokenTypesClass;
90
* Called by <CODE>LanguageData.registerTokenTypes()</CODE>
91
* to update the language data into which it's being registered.
92
* By default it adds mutable token ids that correspond
93
* to the constants discovered in token types.
94
* Can be overriden by subclasses to provide some more functionality.
96
protected void updateData(LanguageData languageData) {
99
for (Iterator it = tokenTypeNamesIterator(); it.hasNext();) {
100
String tokenTypeName = (String)it.next();
101
MutableTokenId id = languageData.findIdByTokenTypeName(tokenTypeName);
104
String idName = LexerGenUtilities.idToLowerCase(tokenTypeName);
105
id = languageData.newId(idName);
106
id.updateByTokenType(tokenTypeName); // updateId() called automatically
115
* Update a newly created or an existing token-id by the information
116
* contained in this token-types.
117
* The passed token-id already has tokenTypeName
120
protected void updateId(MutableTokenId id) {
121
String tokenTypeName = id.getTokenTypeName();
122
if (tokenTypeName != null) { // no associated tokenTypeName
123
Integer value = getTokenTypeValue(tokenTypeName);
125
throw new IllegalArgumentException("tokenTypeName=" + tokenTypeName
126
+ " is not declared in " + getTokenTypesClass().getName());
130
id.setIntId(value.intValue());
134
public Class getTokenTypesClass() {
135
return tokenTypesClass;
139
* @return Integer value of the static field with the given name
140
* or null if the field does not exist.
142
public Integer getTokenTypeValue(String tokenTypeName) {
145
return (Integer)name2value.get(tokenTypeName);
148
public String getTokenTypeName(int tokenTypeValue) {
151
return (String)value2name.get(new Integer(tokenTypeValue));
155
* @return all the field names
157
public Iterator tokenTypeNamesIterator() {
160
return name2value.keySet().iterator();
164
public int findMaxTokenTypeValue() {
168
for (Iterator it = value2name.keySet().iterator(); it.hasNext();) {
169
Integer i = (Integer)it.next();
170
maxValue = Math.max(maxValue, i.intValue());
175
/** Inspect the token types class.
176
* This method can be overriden by children if necessary.
177
* The method goes through the class
178
* and puts the [field-name, integer-constant-value]
179
* for all the static fields into the info map.
180
* The <CODE>null</CODE> key is mapped to maximum constant value
181
* found in the token types class.
182
* The <CODE>List.class</CODE> key is mapped to the list
183
* of all the field names in the order in which they
184
* were found in the token types class.
185
* @return true if the inspection was really done
186
* or false if the inspection was already done previously.
188
protected boolean inspect() {
195
Field[] fields = getTokenTypesClass().getDeclaredFields();
196
for (int i = 0; i < fields.length; i++) {
198
if (f.getType() == int.class) {
199
int value = f.getInt(null);
200
String fieldName = f.getName();
201
if (isAccepted(fieldName, value)) {
202
Integer valueInteger = new Integer(value);
203
name2value.put(fieldName, valueInteger);
204
value2name.put(valueInteger, fieldName);
209
} catch (SecurityException e) {
211
throw new IllegalStateException(e.toString());
213
} catch (IllegalAccessException e) {
215
throw new IllegalStateException(e.toString());
218
return true; // inspection really done
222
* Whether it's ok to add the given field name to the list
223
* of the [tokenTypeName, Integer] pairs.
224
* <BR>Subclasses can exclude some field(s) if necessary.
226
protected boolean isAccepted(String tokenTypeName, int tokenTypeValue) {