~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to lexer/gen/src/org/netbeans/modules/lexer/gen/TokenTypes.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
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]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
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.
 
29
 *
 
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.
 
40
 */
 
41
 
 
42
package org.netbeans.modules.lexer.gen;
 
43
 
 
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;
 
49
import java.util.Map;
 
50
import java.util.HashMap;
 
51
import java.util.Iterator;
 
52
import java.util.Set;
 
53
import java.util.List;
 
54
import org.netbeans.modules.lexer.gen.util.LexerGenUtilities;
 
55
 
 
56
/**
 
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
 
67
 * identification).
 
68
 * 
 
69
 * @author Miloslav Metelka
 
70
 * @version 1.00
 
71
 */
 
72
 
 
73
public class TokenTypes {
 
74
    
 
75
    private final Class tokenTypesClass;
 
76
    
 
77
    private boolean inspected;
 
78
 
 
79
    /** Map of [tokenTypeName, tokenTypeValue] */
 
80
    protected final Map name2value = new HashMap();
 
81
    
 
82
    /** Map of [tokenTypeValue, tokenTypeName] */
 
83
    protected final Map value2name = new HashMap();
 
84
    
 
85
    public TokenTypes(Class tokenTypesClass) {
 
86
        this.tokenTypesClass = tokenTypesClass;
 
87
    }
 
88
    
 
89
    /**
 
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.
 
95
     */
 
96
    protected void updateData(LanguageData languageData) {
 
97
        inspect();
 
98
 
 
99
        for (Iterator it = tokenTypeNamesIterator(); it.hasNext();) {
 
100
            String tokenTypeName = (String)it.next();
 
101
            MutableTokenId id = languageData.findIdByTokenTypeName(tokenTypeName);
 
102
            
 
103
            if (id == null) {
 
104
                String idName = LexerGenUtilities.idToLowerCase(tokenTypeName);
 
105
                id = languageData.newId(idName);
 
106
                id.updateByTokenType(tokenTypeName); // updateId() called automatically
 
107
 
 
108
            } else {
 
109
                updateId(id);
 
110
            }
 
111
        }
 
112
    }
 
113
    
 
114
    /**
 
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
 
118
     * filled in.
 
119
     */
 
120
    protected void updateId(MutableTokenId id) {
 
121
        String tokenTypeName = id.getTokenTypeName();
 
122
        if (tokenTypeName != null) { // no associated tokenTypeName
 
123
            Integer value = getTokenTypeValue(tokenTypeName);
 
124
            if (value == null) {
 
125
                throw new IllegalArgumentException("tokenTypeName=" + tokenTypeName
 
126
                    + " is not declared in " + getTokenTypesClass().getName());
 
127
            }
 
128
 
 
129
            // assign intId
 
130
            id.setIntId(value.intValue());
 
131
        }
 
132
    }
 
133
    
 
134
    public Class getTokenTypesClass() {
 
135
        return tokenTypesClass;
 
136
    }
 
137
    
 
138
    /**
 
139
     * @return Integer value of the static field with the given name
 
140
     *  or null if the field does not exist.
 
141
     */
 
142
    public Integer getTokenTypeValue(String tokenTypeName) {
 
143
        inspect();
 
144
 
 
145
        return (Integer)name2value.get(tokenTypeName);
 
146
    }
 
147
 
 
148
    public String getTokenTypeName(int tokenTypeValue) {
 
149
        inspect();
 
150
 
 
151
        return (String)value2name.get(new Integer(tokenTypeValue));
 
152
    }
 
153
 
 
154
    /**
 
155
     * @return all the field names 
 
156
     */
 
157
    public Iterator tokenTypeNamesIterator() {
 
158
        inspect();
 
159
 
 
160
        return name2value.keySet().iterator();
 
161
    }
 
162
    
 
163
    
 
164
    public int findMaxTokenTypeValue() {
 
165
        inspect();
 
166
 
 
167
        int maxValue = 0;
 
168
        for (Iterator it = value2name.keySet().iterator(); it.hasNext();) {
 
169
            Integer i = (Integer)it.next();
 
170
            maxValue = Math.max(maxValue, i.intValue());
 
171
        }
 
172
        return maxValue;
 
173
    }
 
174
            
 
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.
 
187
     */
 
188
    protected boolean inspect() {
 
189
        if (inspected) {
 
190
            return false;
 
191
        }
 
192
        inspected = true;
 
193
 
 
194
        try {
 
195
            Field[] fields = getTokenTypesClass().getDeclaredFields();
 
196
            for (int i = 0; i < fields.length; i++) {
 
197
                Field f = fields[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);
 
205
                    }
 
206
                }
 
207
            }
 
208
            
 
209
        } catch (SecurityException e) {
 
210
            e.printStackTrace();
 
211
            throw new IllegalStateException(e.toString());
 
212
 
 
213
        } catch (IllegalAccessException e) {
 
214
            e.printStackTrace();
 
215
            throw new IllegalStateException(e.toString());
 
216
        }
 
217
        
 
218
        return true; // inspection really done
 
219
    }
 
220
 
 
221
    /**
 
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.
 
225
     */
 
226
    protected boolean isAccepted(String tokenTypeName, int tokenTypeValue) {
 
227
        return true;
 
228
    }
 
229
 
 
230
}
 
231