KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > el > lexer > api > ELTokenId


1 /*
2  * Sun Public License Notice
3  *
4  * The contents of this file are subject to the Sun Public License
5  * Version 1.0 (the "License"). You may not use this file except in
6  * compliance with the License. A copy of the License is available at
7  * http://www.sun.com/
8  *
9  * The Original Code is NetBeans. The Initial Developer of the Original
10  * Code is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
11  * Microsystems, Inc. All Rights Reserved.
12  */

13 package org.netbeans.modules.el.lexer.api;
14
15 import java.util.Collection JavaDoc;
16 import java.util.EnumSet JavaDoc;
17 import java.util.HashMap JavaDoc;
18 import java.util.Map JavaDoc;
19 import org.netbeans.api.lexer.InputAttributes;
20 import org.netbeans.api.lexer.Language;
21 import org.netbeans.api.lexer.LanguagePath;
22 import org.netbeans.api.lexer.Token;
23 import org.netbeans.api.lexer.TokenId;
24 import org.netbeans.modules.el.lexer.ELLexer;
25 import org.netbeans.spi.lexer.LanguageEmbedding;
26 import org.netbeans.spi.lexer.LanguageHierarchy;
27 import org.netbeans.spi.lexer.Lexer;
28 import org.netbeans.spi.lexer.LexerRestartInfo;
29
30 /**
31  * Token IDs of Expression Language
32  *
33  * @author Petr Pisl
34  * @author Marek.Fukala@Sun.COM
35  */

36 public enum ELTokenId implements TokenId {
37     
38     //operators
39
LT("<", ELTokenCategories.OPERATORS.categoryName),
40     GT(">", ELTokenCategories.OPERATORS.categoryName),
41     DOT(".", ELTokenCategories.OPERATORS.categoryName),
42     COMMA(",", ELTokenCategories.OPERATORS.categoryName),
43     QUESTION("?", ELTokenCategories.OPERATORS.categoryName),
44     PLUS("+", ELTokenCategories.OPERATORS.categoryName),
45     MINUS("-", ELTokenCategories.OPERATORS.categoryName),
46     MUL("*", ELTokenCategories.OPERATORS.categoryName),
47     DIV("/", ELTokenCategories.OPERATORS.categoryName),
48     MOD("%", ELTokenCategories.OPERATORS.categoryName),
49     EQ_EQ("==", ELTokenCategories.OPERATORS.categoryName),
50     LT_EQ("<=", ELTokenCategories.OPERATORS.categoryName),
51     GT_EQ(">=", ELTokenCategories.OPERATORS.categoryName),
52     NOT_EQ("!=", ELTokenCategories.OPERATORS.categoryName),
53     AND_AND("&&", ELTokenCategories.OPERATORS.categoryName),
54     OR_OR("||", ELTokenCategories.OPERATORS.categoryName),
55     COLON(":", ELTokenCategories.OPERATORS.categoryName),
56     NOT("!", ELTokenCategories.OPERATORS.categoryName),
57     LPAREN("(", ELTokenCategories.OPERATORS.categoryName),
58     RPAREN(")", ELTokenCategories.OPERATORS.categoryName),
59     LBRACKET("[", ELTokenCategories.OPERATORS.categoryName),
60     RBRACKET("]", ELTokenCategories.OPERATORS.categoryName),
61     
62     //keywords
63
AND_KEYWORD("and", ELTokenCategories.KEYWORDS.categoryName),
64     DIV_KEYWORD("div", ELTokenCategories.KEYWORDS.categoryName),
65     EMPTY_KEYWORD("empty", ELTokenCategories.KEYWORDS.categoryName),
66     EQ_KEYWORD("eq", ELTokenCategories.KEYWORDS.categoryName),
67     FALSE_KEYWORD("false", ELTokenCategories.KEYWORDS.categoryName),
68     GE_KEYWORD("ge", ELTokenCategories.KEYWORDS.categoryName),
69     GT_KEYWORD("gt", ELTokenCategories.KEYWORDS.categoryName),
70     INSTANCEOF_KEYWORD("instanceof", ELTokenCategories.KEYWORDS.categoryName),
71     LE_KEYWORD("le", ELTokenCategories.KEYWORDS.categoryName),
72     LT_KEYWORD("lt", ELTokenCategories.KEYWORDS.categoryName),
73     MOD_KEYWORD("mod", ELTokenCategories.KEYWORDS.categoryName),
74     NE_KEYWORD("ne", ELTokenCategories.KEYWORDS.categoryName),
75     NOT_KEYWORD("not", ELTokenCategories.KEYWORDS.categoryName),
76     NULL_KEYWORD("null", ELTokenCategories.KEYWORDS.categoryName),
77     OR_KEYWORD("or", ELTokenCategories.KEYWORDS.categoryName),
78     TRUE_KEYWORD("true", ELTokenCategories.KEYWORDS.categoryName),
79     
80     //literals
81
WHITESPACE(null, "whitespace"),
82     EOL("\n", "eol"),
83     STRING_LITERAL(null, "string"),
84     TAG_LIB_PREFIX(null, "tag-lib-prefix"),
85     IDENTIFIER(null, "identifier"),
86     CHAR_LITERAL(null, "char-literal"),
87     
88     //numeric literals
89
/** Java integer literal e.g. 1234 */
90     INT_LITERAL(null, "int-literal"),
91     /** Java long literal e.g. 12L */
92     LONG_LITERAL(null, "long-literal"),
93     /** Java hexadecimal literal e.g. 0x5a */
94     HEX_LITERAL(null, "hex-literal"),
95     /** Java octal literal e.g. 0123 */
96     OCTAL_LITERAL(null, "octal-literal"),
97     /** Java float literal e.g. 1.5e+20f */
98     FLOAT_LITERAL(null, "float-literal"),
99     /** Java double literal e.g. 1.5e+20 */
100     DOUBLE_LITERAL(null, "double-literal"),
101     // Incomplete and error token-ids
102
INVALID_OCTAL_LITERAL(null, "invalid-octal-literal"),
103     INVALID_CHAR(null, "invalid-char");
104     
105     
106     /** EL token categories enum.*/
107     public static enum ELTokenCategories {
108         
109         /** Token category for EL keywords like and, false etc. */
110         KEYWORDS("keyword"),
111         /** Token category for EL operators like ==, => etc. */
112         OPERATORS("operators"),
113         /** Token category for EL numeric literals. */
114         NUMERIC_LITERALS("numeric-literals"),
115         /** Token category for EL errors. */
116         ERRORS("error");
117         
118         private final String JavaDoc categoryName;
119         
120         ELTokenCategories(String JavaDoc categoryName) {
121             this.categoryName = categoryName;
122         }
123         
124     }
125     
126     private final String JavaDoc fixedText; // Used by lexer for production of flyweight tokens
127

128     private final String JavaDoc primaryCategory;
129     
130     ELTokenId(String JavaDoc fixedText, String JavaDoc primaryCategory) {
131         this.fixedText = fixedText;
132         this.primaryCategory = primaryCategory;
133     }
134     
135     /** Get fixed text of the token. */
136     public String JavaDoc fixedText() {
137         return fixedText;
138     }
139     
140     /**
141      * Get name of primary token category into which this token belongs.
142      * <br/>
143      * Other token categories for this id can be defined in the language hierarchy.
144      *
145      * @return name of the primary token category into which this token belongs
146      * or null if there is no primary category for this token.
147      */

148     
149     public String JavaDoc primaryCategory() {
150         return primaryCategory;
151     }
152     
153     private static final Language<ELTokenId> language = new LanguageHierarchy<ELTokenId>() {
154         @Override JavaDoc
155         protected Collection JavaDoc<ELTokenId> createTokenIds() {
156             return EnumSet.allOf(ELTokenId.class);
157         }
158         
159         @Override JavaDoc
160         protected Map JavaDoc<String JavaDoc,Collection JavaDoc<ELTokenId>> createTokenCategories() {
161             Map JavaDoc<String JavaDoc,Collection JavaDoc<ELTokenId>> cats = new HashMap JavaDoc<String JavaDoc,Collection JavaDoc<ELTokenId>>();
162             
163             cats.put(ELTokenCategories.NUMERIC_LITERALS.categoryName, EnumSet.of(
164                     ELTokenId.INT_LITERAL,
165                     ELTokenId.LONG_LITERAL,
166                     ELTokenId.HEX_LITERAL,
167                     ELTokenId.OCTAL_LITERAL,
168                     ELTokenId.FLOAT_LITERAL,
169                     ELTokenId.DOUBLE_LITERAL));
170             
171             cats.put(ELTokenCategories.ERRORS.categoryName, EnumSet.of(
172                     ELTokenId.INVALID_OCTAL_LITERAL,
173                     ELTokenId.INVALID_CHAR));
174             
175             return cats;
176         }
177         
178         @Override JavaDoc
179         protected Lexer<ELTokenId> createLexer(LexerRestartInfo<ELTokenId> info) {
180             return new ELLexer(info);
181         }
182         
183         @Override JavaDoc
184         public LanguageEmbedding<? extends TokenId> embedding(
185                 Token<ELTokenId> token, LanguagePath languagePath, InputAttributes inputAttributes) {
186             return null; // No embedding
187
}
188         
189         @Override JavaDoc
190         protected String JavaDoc mimeType() {
191             return "text/x-el"; //???
192
}
193     }.language();
194     
195     /** Gets a LanguageDescription describing a set of token ids
196      * that comprise the given language.
197      *
198      * @return non-null LanguageDescription
199      */

200     public static Language<ELTokenId> language() {
201         return language;
202     }
203     
204     
205 }
206
Popular Tags