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-2006 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: package org.netbeans.modules.el.lexer.api;
042:
043: import java.util.Collection;
044: import java.util.EnumSet;
045: import java.util.HashMap;
046: import java.util.Map;
047: import org.netbeans.api.lexer.InputAttributes;
048: import org.netbeans.api.lexer.Language;
049: import org.netbeans.api.lexer.LanguagePath;
050: import org.netbeans.api.lexer.Token;
051: import org.netbeans.api.lexer.TokenId;
052: import org.netbeans.modules.el.lexer.ELLexer;
053: import org.netbeans.spi.lexer.LanguageEmbedding;
054: import org.netbeans.spi.lexer.LanguageHierarchy;
055: import org.netbeans.spi.lexer.Lexer;
056: import org.netbeans.spi.lexer.LexerRestartInfo;
057:
058: /**
059: * Token IDs of Expression Language
060: *
061: * @author Petr Pisl
062: * @author Marek.Fukala@Sun.COM
063: */
064: public enum ELTokenId implements TokenId {
065:
066: //operators
067: LT("<", ELTokenCategories.OPERATORS.categoryName), GT(">",
068: ELTokenCategories.OPERATORS.categoryName), DOT(".",
069: ELTokenCategories.OPERATORS.categoryName), COMMA(",",
070: ELTokenCategories.OPERATORS.categoryName), QUESTION("?",
071: ELTokenCategories.OPERATORS.categoryName), PLUS("+",
072: ELTokenCategories.OPERATORS.categoryName), MINUS("-",
073: ELTokenCategories.OPERATORS.categoryName), MUL("*",
074: ELTokenCategories.OPERATORS.categoryName), DIV("/",
075: ELTokenCategories.OPERATORS.categoryName), MOD("%",
076: ELTokenCategories.OPERATORS.categoryName), EQ_EQ("==",
077: ELTokenCategories.OPERATORS.categoryName), LT_EQ("<=",
078: ELTokenCategories.OPERATORS.categoryName), GT_EQ(">=",
079: ELTokenCategories.OPERATORS.categoryName), NOT_EQ("!=",
080: ELTokenCategories.OPERATORS.categoryName), AND_AND("&&",
081: ELTokenCategories.OPERATORS.categoryName), OR_OR("||",
082: ELTokenCategories.OPERATORS.categoryName), COLON(":",
083: ELTokenCategories.OPERATORS.categoryName), NOT("!",
084: ELTokenCategories.OPERATORS.categoryName), LPAREN("(",
085: ELTokenCategories.OPERATORS.categoryName), RPAREN(")",
086: ELTokenCategories.OPERATORS.categoryName), LBRACKET("[",
087: ELTokenCategories.OPERATORS.categoryName), RBRACKET("]",
088: ELTokenCategories.OPERATORS.categoryName),
089:
090: //keywords
091: AND_KEYWORD("and", ELTokenCategories.KEYWORDS.categoryName), DIV_KEYWORD(
092: "div", ELTokenCategories.KEYWORDS.categoryName), EMPTY_KEYWORD(
093: "empty", ELTokenCategories.KEYWORDS.categoryName), EQ_KEYWORD(
094: "eq", ELTokenCategories.KEYWORDS.categoryName), FALSE_KEYWORD(
095: "false", ELTokenCategories.KEYWORDS.categoryName), GE_KEYWORD(
096: "ge", ELTokenCategories.KEYWORDS.categoryName), GT_KEYWORD(
097: "gt", ELTokenCategories.KEYWORDS.categoryName), INSTANCEOF_KEYWORD(
098: "instanceof", ELTokenCategories.KEYWORDS.categoryName), LE_KEYWORD(
099: "le", ELTokenCategories.KEYWORDS.categoryName), LT_KEYWORD(
100: "lt", ELTokenCategories.KEYWORDS.categoryName), MOD_KEYWORD(
101: "mod", ELTokenCategories.KEYWORDS.categoryName), NE_KEYWORD(
102: "ne", ELTokenCategories.KEYWORDS.categoryName), NOT_KEYWORD(
103: "not", ELTokenCategories.KEYWORDS.categoryName), NULL_KEYWORD(
104: "null", ELTokenCategories.KEYWORDS.categoryName), OR_KEYWORD(
105: "or", ELTokenCategories.KEYWORDS.categoryName), TRUE_KEYWORD(
106: "true", ELTokenCategories.KEYWORDS.categoryName),
107:
108: //literals
109: WHITESPACE(null, "whitespace"), EOL("\n", "eol"), STRING_LITERAL(
110: null, "string"), TAG_LIB_PREFIX(null, "tag-lib-prefix"), IDENTIFIER(
111: null, "identifier"), CHAR_LITERAL(null, "char-literal"),
112:
113: //numeric literals
114: /** Java integer literal e.g. 1234 */
115: INT_LITERAL(null, "int-literal"),
116: /** Java long literal e.g. 12L */
117: LONG_LITERAL(null, "long-literal"),
118: /** Java hexadecimal literal e.g. 0x5a */
119: HEX_LITERAL(null, "hex-literal"),
120: /** Java octal literal e.g. 0123 */
121: OCTAL_LITERAL(null, "octal-literal"),
122: /** Java float literal e.g. 1.5e+20f */
123: FLOAT_LITERAL(null, "float-literal"),
124: /** Java double literal e.g. 1.5e+20 */
125: DOUBLE_LITERAL(null, "double-literal"),
126: // Incomplete and error token-ids
127: INVALID_OCTAL_LITERAL(null, "invalid-octal-literal"), INVALID_CHAR(
128: null, "invalid-char");
129:
130: /** EL token categories enum.*/
131: public static enum ELTokenCategories {
132:
133: /** Token category for EL keywords like and, false etc. */
134: KEYWORDS("keyword"),
135: /** Token category for EL operators like ==, => etc. */
136: OPERATORS("operators"),
137: /** Token category for EL numeric literals. */
138: NUMERIC_LITERALS("numeric-literals"),
139: /** Token category for EL errors. */
140: ERRORS("error");
141:
142: private final String categoryName;
143:
144: ELTokenCategories(String categoryName) {
145: this .categoryName = categoryName;
146: }
147:
148: }
149:
150: private final String fixedText; // Used by lexer for production of flyweight tokens
151:
152: private final String primaryCategory;
153:
154: ELTokenId(String fixedText, String primaryCategory) {
155: this .fixedText = fixedText;
156: this .primaryCategory = primaryCategory;
157: }
158:
159: /** Get fixed text of the token. */
160: public String fixedText() {
161: return fixedText;
162: }
163:
164: /**
165: * Get name of primary token category into which this token belongs.
166: * <br/>
167: * Other token categories for this id can be defined in the language hierarchy.
168: *
169: * @return name of the primary token category into which this token belongs
170: * or null if there is no primary category for this token.
171: */
172:
173: public String primaryCategory() {
174: return primaryCategory;
175: }
176:
177: private static final Language<ELTokenId> language = new LanguageHierarchy<ELTokenId>() {
178: @Override
179: protected Collection<ELTokenId> createTokenIds() {
180: return EnumSet.allOf(ELTokenId.class);
181: }
182:
183: @Override
184: protected Map<String, Collection<ELTokenId>> createTokenCategories() {
185: Map<String, Collection<ELTokenId>> cats = new HashMap<String, Collection<ELTokenId>>();
186:
187: cats.put(ELTokenCategories.NUMERIC_LITERALS.categoryName,
188: EnumSet.of(ELTokenId.INT_LITERAL,
189: ELTokenId.LONG_LITERAL,
190: ELTokenId.HEX_LITERAL,
191: ELTokenId.OCTAL_LITERAL,
192: ELTokenId.FLOAT_LITERAL,
193: ELTokenId.DOUBLE_LITERAL));
194:
195: cats.put(ELTokenCategories.ERRORS.categoryName, EnumSet.of(
196: ELTokenId.INVALID_OCTAL_LITERAL,
197: ELTokenId.INVALID_CHAR));
198:
199: return cats;
200: }
201:
202: @Override
203: protected Lexer<ELTokenId> createLexer(
204: LexerRestartInfo<ELTokenId> info) {
205: return new ELLexer(info);
206: }
207:
208: @Override
209: public LanguageEmbedding<?> embedding(Token<ELTokenId> token,
210: LanguagePath languagePath,
211: InputAttributes inputAttributes) {
212: return null; // No embedding
213: }
214:
215: @Override
216: protected String mimeType() {
217: return "text/x-el"; //???
218: }
219: }.language();
220:
221: /** Gets a LanguageDescription describing a set of token ids
222: * that comprise the given language.
223: *
224: * @return non-null LanguageDescription
225: */
226: public static Language<ELTokenId> language() {
227: return language;
228: }
229:
230: }
|