KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > java > lexer > JavaTokenId


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.api.java.lexer;
21
22 import java.util.Collection JavaDoc;
23 import java.util.EnumSet JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Map JavaDoc;
26 import org.netbeans.api.lexer.InputAttributes;
27 import org.netbeans.api.lexer.Language;
28 import org.netbeans.api.lexer.LanguagePath;
29 import org.netbeans.api.lexer.Token;
30 import org.netbeans.api.lexer.TokenId;
31 import org.netbeans.lib.java.lexer.JavaLexer;
32 import org.netbeans.spi.lexer.LanguageEmbedding;
33 import org.netbeans.spi.lexer.LanguageHierarchy;
34 import org.netbeans.spi.lexer.Lexer;
35 import org.netbeans.spi.lexer.LexerRestartInfo;
36
37 /**
38  * Token ids of java language defined as enum.
39  *
40  * @author Miloslav Metelka
41  */

42 public enum JavaTokenId implements TokenId {
43
44     ERROR(null, "error"),
45     IDENTIFIER(null, "identifier"),
46
47     ABSTRACT("abstract", "keyword"),
48     ASSERT("assert", "keyword"),
49     BOOLEAN("boolean", "keyword"),
50     BREAK("break", "keyword"),
51     BYTE("byte", "keyword"),
52     CASE("case", "keyword"),
53     CATCH("catch", "keyword"),
54     CHAR("char", "keyword"),
55     CLASS("class", "keyword"),
56     CONST("const", "keyword"),
57     CONTINUE("continue", "keyword"),
58     DEFAULT("default", "keyword"),
59     DO("do", "keyword"),
60     DOUBLE("double", "keyword"),
61     ELSE("else", "keyword"),
62     ENUM("enum", "keyword"),
63     EXTENDS("extends", "keyword"),
64     FINAL("final", "keyword"),
65     FINALLY("finally", "keyword"),
66     FLOAT("float", "keyword"),
67     FOR("for", "keyword"),
68     GOTO("goto", "keyword"),
69     IF("if", "keyword"),
70     IMPLEMENTS("implements", "keyword"),
71     IMPORT("import", "keyword"),
72     INSTANCEOF("instanceof", "keyword"),
73     INT("int", "keyword"),
74     INTERFACE("interface", "keyword"),
75     LONG("long", "keyword"),
76     NATIVE("native", "keyword"),
77     NEW("new", "keyword"),
78     PACKAGE("package", "keyword"),
79     PRIVATE("private", "keyword"),
80     PROTECTED("protected", "keyword"),
81     PUBLIC("public", "keyword"),
82     RETURN("return", "keyword"),
83     SHORT("short", "keyword"),
84     STATIC("static", "keyword"),
85     STRICTFP("strictfp", "keyword"),
86     SUPER("super", "keyword"),
87     SWITCH("switch", "keyword"),
88     SYNCHRONIZED("synchronized", "keyword"),
89     THIS("this", "keyword"),
90     THROW("throw", "keyword"),
91     THROWS("throws", "keyword"),
92     TRANSIENT("transient", "keyword"),
93     TRY("try", "keyword"),
94     VOID("void", "keyword"),
95     VOLATILE("volatile", "keyword"),
96     WHILE("while", "keyword"),
97
98     INT_LITERAL(null, "number"),
99     LONG_LITERAL(null, "number"),
100     FLOAT_LITERAL(null, "number"),
101     DOUBLE_LITERAL(null, "number"),
102     CHAR_LITERAL(null, "character"),
103     STRING_LITERAL(null, "string"),
104     
105     TRUE("true", "literal"),
106     FALSE("false", "literal"),
107     NULL("null", "literal"),
108     
109     LPAREN("(", "separator"),
110     RPAREN(")", "separator"),
111     LBRACE("{", "separator"),
112     RBRACE("}", "separator"),
113     LBRACKET("[", "separator"),
114     RBRACKET("]", "separator"),
115     SEMICOLON(";", "separator"),
116     COMMA(",", "separator"),
117     DOT(".", "separator"),
118
119     EQ("=", "operator"),
120     GT(">", "operator"),
121     LT("<", "operator"),
122     BANG("!", "operator"),
123     TILDE("~", "operator"),
124     QUESTION("?", "operator"),
125     COLON(":", "operator"),
126     EQEQ("==", "operator"),
127     LTEQ("<=", "operator"),
128     GTEQ(">=", "operator"),
129     BANGEQ("!=","operator"),
130     AMPAMP("&&", "operator"),
131     BARBAR("||", "operator"),
132     PLUSPLUS("++", "operator"),
133     MINUSMINUS("--","operator"),
134     PLUS("+", "operator"),
135     MINUS("-", "operator"),
136     STAR("*", "operator"),
137     SLASH("/", "operator"),
138     AMP("&", "operator"),
139     BAR("|", "operator"),
140     CARET("^", "operator"),
141     PERCENT("%", "operator"),
142     LTLT("<<", "operator"),
143     GTGT(">>", "operator"),
144     GTGTGT(">>>", "operator"),
145     PLUSEQ("+=", "operator"),
146     MINUSEQ("-=", "operator"),
147     STAREQ("*=", "operator"),
148     SLASHEQ("/=", "operator"),
149     AMPEQ("&=", "operator"),
150     BAREQ("|=", "operator"),
151     CARETEQ("^=", "operator"),
152     PERCENTEQ("%=", "operator"),
153     LTLTEQ("<<=", "operator"),
154     GTGTEQ(">>=", "operator"),
155     GTGTGTEQ(">>>=", "operator"),
156     
157     ELLIPSIS("...", null),
158     AT("@", null),
159     
160     WHITESPACE(null, "whitespace"),
161     LINE_COMMENT(null, "comment"),
162     BLOCK_COMMENT(null, "comment"),
163     JAVADOC_COMMENT(null, "comment"),
164     
165     // Errors
166
INVALID_COMMENT_END("*/", "error"),
167     FLOAT_LITERAL_INVALID(null, "number");
168
169
170     private final String JavaDoc fixedText;
171
172     private final String JavaDoc primaryCategory;
173
174     JavaTokenId(String JavaDoc fixedText, String JavaDoc primaryCategory) {
175         this.fixedText = fixedText;
176         this.primaryCategory = primaryCategory;
177     }
178
179     public String JavaDoc fixedText() {
180         return fixedText;
181     }
182
183     public String JavaDoc primaryCategory() {
184         return primaryCategory;
185     }
186
187     private static final Language<JavaTokenId> language = new LanguageHierarchy<JavaTokenId>() {
188
189         @Override JavaDoc
190         protected String JavaDoc mimeType() {
191             return "text/x-java";
192         }
193
194         @Override JavaDoc
195         protected Collection JavaDoc<JavaTokenId> createTokenIds() {
196             return EnumSet.allOf(JavaTokenId.class);
197         }
198         
199         @Override JavaDoc
200         protected Map JavaDoc<String JavaDoc,Collection JavaDoc<JavaTokenId>> createTokenCategories() {
201             Map JavaDoc<String JavaDoc,Collection JavaDoc<JavaTokenId>> cats = new HashMap JavaDoc<String JavaDoc,Collection JavaDoc<JavaTokenId>>();
202             // Additional literals being a lexical error
203
cats.put("error", EnumSet.of(
204                 JavaTokenId.FLOAT_LITERAL_INVALID
205             ));
206             // Literals category
207
EnumSet JavaDoc<JavaTokenId> l = EnumSet.of(
208                 JavaTokenId.INT_LITERAL,
209                 JavaTokenId.LONG_LITERAL,
210                 JavaTokenId.FLOAT_LITERAL,
211                 JavaTokenId.DOUBLE_LITERAL,
212                 JavaTokenId.CHAR_LITERAL
213             );
214             l.add(JavaTokenId.STRING_LITERAL);
215             cats.put("literal", l);
216
217             return cats;
218         }
219
220         @Override JavaDoc
221         protected Lexer<JavaTokenId> createLexer(LexerRestartInfo<JavaTokenId> info) {
222             return new JavaLexer(info);
223         }
224
225         @Override JavaDoc
226         protected LanguageEmbedding<? extends TokenId> embedding(
227         Token<JavaTokenId> token, LanguagePath languagePath, InputAttributes inputAttributes) {
228             // Test language embedding in the block comment
229
switch (token.id()) {
230                 case JAVADOC_COMMENT:
231                     return LanguageEmbedding.create(JavadocTokenId.language(), 3, 2);
232                 case STRING_LITERAL:
233                     return LanguageEmbedding.create(JavaStringTokenId.language(), 1, 1);
234             }
235             return null; // No embedding
236
}
237
238 // protected CharPreprocessor createCharPreprocessor() {
239
// return CharPreprocessor.createUnicodeEscapesPreprocessor();
240
// }
241

242     }.language();
243
244     public static Language<JavaTokenId> language() {
245         return language;
246     }
247
248 }
249
Popular Tags