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:
042: package org.netbeans.api.java.lexer;
043:
044: import java.util.Collection;
045: import java.util.EnumSet;
046: import java.util.HashMap;
047: import java.util.Map;
048: import org.netbeans.api.lexer.InputAttributes;
049: import org.netbeans.api.lexer.Language;
050: import org.netbeans.api.lexer.LanguagePath;
051: import org.netbeans.api.lexer.PartType;
052: import org.netbeans.api.lexer.Token;
053: import org.netbeans.api.lexer.TokenId;
054: import org.netbeans.lib.java.lexer.JavaLexer;
055: import org.netbeans.spi.lexer.LanguageEmbedding;
056: import org.netbeans.spi.lexer.LanguageHierarchy;
057: import org.netbeans.spi.lexer.Lexer;
058: import org.netbeans.spi.lexer.LexerRestartInfo;
059:
060: /**
061: * Token ids of java language defined as enum.
062: *
063: * @author Miloslav Metelka
064: */
065: public enum JavaTokenId implements TokenId {
066:
067: ERROR(null, "error"), IDENTIFIER(null, "identifier"),
068:
069: ABSTRACT("abstract", "keyword"), ASSERT("assert",
070: "keyword-directive"), BOOLEAN("boolean", "keyword"), BREAK(
071: "break", "keyword-directive"), BYTE("byte", "keyword"), CASE(
072: "case", "keyword-directive"), CATCH("catch",
073: "keyword-directive"), CHAR("char", "keyword"), CLASS(
074: "class", "keyword"), CONST("const", "keyword"), CONTINUE(
075: "continue", "keyword-directive"), DEFAULT("default",
076: "keyword-directive"), DO("do", "keyword-directive"), DOUBLE(
077: "double", "keyword"), ELSE("else", "keyword-directive"), ENUM(
078: "enum", "keyword"), EXTENDS("extends", "keyword"), FINAL(
079: "final", "keyword"), FINALLY("finally", "keyword-directive"), FLOAT(
080: "float", "keyword"), FOR("for", "keyword-directive"), GOTO(
081: "goto", "keyword-directive"), IF("if", "keyword-directive"), IMPLEMENTS(
082: "implements", "keyword"), IMPORT("import", "keyword"), INSTANCEOF(
083: "instanceof", "keyword"), INT("int", "keyword"), INTERFACE(
084: "interface", "keyword"), LONG("long", "keyword"), NATIVE(
085: "native", "keyword"), NEW("new", "keyword"), PACKAGE(
086: "package", "keyword"), PRIVATE("private", "keyword"), PROTECTED(
087: "protected", "keyword"), PUBLIC("public", "keyword"), RETURN(
088: "return", "keyword-directive"), SHORT("short", "keyword"), STATIC(
089: "static", "keyword"), STRICTFP("strictfp", "keyword"), SUPER(
090: "super", "keyword"), SWITCH("switch", "keyword-directive"), SYNCHRONIZED(
091: "synchronized", "keyword"), THIS("this", "keyword"), THROW(
092: "throw", "keyword-directive"), THROWS("throws", "keyword"), TRANSIENT(
093: "transient", "keyword"), TRY("try", "keyword-directive"), VOID(
094: "void", "keyword"), VOLATILE("volatile", "keyword"), WHILE(
095: "while", "keyword-directive"),
096:
097: INT_LITERAL(null, "number"), LONG_LITERAL(null, "number"), FLOAT_LITERAL(
098: null, "number"), DOUBLE_LITERAL(null, "number"), CHAR_LITERAL(
099: null, "character"), STRING_LITERAL(null, "string"),
100:
101: TRUE("true", "literal"), FALSE("false", "literal"), NULL("null",
102: "literal"),
103:
104: LPAREN("(", "separator"), RPAREN(")", "separator"), LBRACE("{",
105: "separator"), RBRACE("}", "separator"), LBRACKET("[",
106: "separator"), RBRACKET("]", "separator"), SEMICOLON(";",
107: "separator"), COMMA(",", "separator"), DOT(".", "separator"),
108:
109: EQ("=", "operator"), GT(">", "operator"), LT("<", "operator"), BANG(
110: "!", "operator"), TILDE("~", "operator"), QUESTION("?",
111: "operator"), COLON(":", "operator"), EQEQ("==", "operator"), LTEQ(
112: "<=", "operator"), GTEQ(">=", "operator"), BANGEQ("!=",
113: "operator"), AMPAMP("&&", "operator"), BARBAR("||",
114: "operator"), PLUSPLUS("++", "operator"), MINUSMINUS("--",
115: "operator"), PLUS("+", "operator"), MINUS("-", "operator"), STAR(
116: "*", "operator"), SLASH("/", "operator"), AMP("&",
117: "operator"), BAR("|", "operator"), CARET("^", "operator"), PERCENT(
118: "%", "operator"), LTLT("<<", "operator"), GTGT(">>",
119: "operator"), GTGTGT(">>>", "operator"), PLUSEQ("+=",
120: "operator"), MINUSEQ("-=", "operator"), STAREQ("*=",
121: "operator"), SLASHEQ("/=", "operator"), AMPEQ("&=",
122: "operator"), BAREQ("|=", "operator"), CARETEQ("^=",
123: "operator"), PERCENTEQ("%=", "operator"), LTLTEQ("<<=",
124: "operator"), GTGTEQ(">>=", "operator"), GTGTGTEQ(">>>=",
125: "operator"),
126:
127: ELLIPSIS("...", "special"), AT("@", "special"),
128:
129: WHITESPACE(null, "whitespace"), LINE_COMMENT(null, "comment"), BLOCK_COMMENT(
130: null, "comment"), JAVADOC_COMMENT(null, "comment"),
131:
132: // Errors
133: INVALID_COMMENT_END("*/", "error"), FLOAT_LITERAL_INVALID(null,
134: "number");
135:
136: private final String fixedText;
137:
138: private final String primaryCategory;
139:
140: JavaTokenId(String fixedText, String primaryCategory) {
141: this .fixedText = fixedText;
142: this .primaryCategory = primaryCategory;
143: }
144:
145: public String fixedText() {
146: return fixedText;
147: }
148:
149: public String primaryCategory() {
150: return primaryCategory;
151: }
152:
153: private static final Language<JavaTokenId> language = new LanguageHierarchy<JavaTokenId>() {
154:
155: @Override
156: protected String mimeType() {
157: return "text/x-java";
158: }
159:
160: @Override
161: protected Collection<JavaTokenId> createTokenIds() {
162: return EnumSet.allOf(JavaTokenId.class);
163: }
164:
165: @Override
166: protected Map<String, Collection<JavaTokenId>> createTokenCategories() {
167: Map<String, Collection<JavaTokenId>> cats = new HashMap<String, Collection<JavaTokenId>>();
168: // Additional literals being a lexical error
169: cats.put("error", EnumSet
170: .of(JavaTokenId.FLOAT_LITERAL_INVALID));
171: // Literals category
172: EnumSet<JavaTokenId> l = EnumSet.of(
173: JavaTokenId.INT_LITERAL, JavaTokenId.LONG_LITERAL,
174: JavaTokenId.FLOAT_LITERAL,
175: JavaTokenId.DOUBLE_LITERAL,
176: JavaTokenId.CHAR_LITERAL);
177: l.add(JavaTokenId.STRING_LITERAL);
178: cats.put("literal", l);
179:
180: return cats;
181: }
182:
183: @Override
184: protected Lexer<JavaTokenId> createLexer(
185: LexerRestartInfo<JavaTokenId> info) {
186: return new JavaLexer(info);
187: }
188:
189: @Override
190: protected LanguageEmbedding<?> embedding(
191: Token<JavaTokenId> token, LanguagePath languagePath,
192: InputAttributes inputAttributes) {
193: // Test language embedding in the block comment
194: switch (token.id()) {
195: case JAVADOC_COMMENT:
196: return LanguageEmbedding
197: .create(JavadocTokenId.language(), 3, (token
198: .partType() == PartType.COMPLETE) ? 2
199: : 0);
200: case STRING_LITERAL:
201: return LanguageEmbedding
202: .create(JavaStringTokenId.language(), 1, (token
203: .partType() == PartType.COMPLETE) ? 1
204: : 0);
205: }
206: return null; // No embedding
207: }
208:
209: // protected CharPreprocessor createCharPreprocessor() {
210: // return CharPreprocessor.createUnicodeEscapesPreprocessor();
211: // }
212:
213: }.language();
214:
215: public static Language<JavaTokenId> language() {
216: return language;
217: }
218:
219: }
|