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-2007 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.lib.lexer.lang;
043:
044: import org.netbeans.lib.lexer.lang.TestStringTokenId;
045: import org.netbeans.lib.lexer.lang.TestPlainTokenId;
046: import org.netbeans.lib.lexer.test.simple.*;
047: import org.netbeans.lib.lexer.lang.TestJavadocTokenId;
048: import java.util.Collection;
049: import java.util.EnumSet;
050: import java.util.HashMap;
051: import java.util.Map;
052: import org.netbeans.api.lexer.InputAttributes;
053: import org.netbeans.api.lexer.Language;
054: import org.netbeans.api.lexer.LanguagePath;
055: import org.netbeans.api.lexer.Token;
056: import org.netbeans.api.lexer.TokenId;
057: import org.netbeans.spi.lexer.LanguageEmbedding;
058: import org.netbeans.spi.lexer.LanguageHierarchy;
059: import org.netbeans.spi.lexer.Lexer;
060: import org.netbeans.spi.lexer.LexerRestartInfo;
061:
062: /**
063: * Simple implementation of enumerated token id.
064: *
065: * @author mmetelka
066: */
067: public enum TestTokenId implements TokenId {
068:
069: IDENTIFIER(null, null), WHITESPACE(null, null), // normally would be "whitespace" category here but testing to do it in language hierarchy
070: BLOCK_COMMENT(null, "comment"), LINE_COMMENT(null, "comment"), JAVADOC_COMMENT(
071: null, "comment"), PLUS("+", "operator"), MINUS("-",
072: "operator"), PLUS_MINUS_PLUS("+-+", null), DIV("/",
073: "operator"), STAR("*", "operator"), ERROR(null, "error"), PUBLIC(
074: "public", "keyword"), PRIVATE("private", "keyword"), STATIC(
075: "static", "keyword"), STRING_LITERAL(null, "string"),
076:
077: BLOCK_COMMENT_INCOMPLETE(null, "comment"), JAVADOC_COMMENT_INCOMPLETE(
078: null, "comment"), STRING_LITERAL_INCOMPLETE(null, "string"), ;
079:
080: private final String fixedText;
081:
082: private final String primaryCategory;
083:
084: TestTokenId(String fixedText, String primaryCategory) {
085: this .fixedText = fixedText;
086: this .primaryCategory = primaryCategory;
087: }
088:
089: public String fixedText() {
090: return fixedText;
091: }
092:
093: public String primaryCategory() {
094: return primaryCategory;
095: }
096:
097: private static final Language<TestTokenId> language = new LanguageHierarchy<TestTokenId>() {
098:
099: @Override
100: protected String mimeType() {
101: return "text/x-simple";
102: }
103:
104: @Override
105: protected Collection<TestTokenId> createTokenIds() {
106: return EnumSet.allOf(TestTokenId.class);
107: }
108:
109: @Override
110: protected Map<String, Collection<TestTokenId>> createTokenCategories() {
111: Map<String, Collection<TestTokenId>> cats = new HashMap<String, Collection<TestTokenId>>();
112: cats.put("operator", EnumSet
113: .of(TestTokenId.PLUS_MINUS_PLUS));
114: // Normally whitespace category would be a primary category in token id's declaration
115: cats.put("whitespace", EnumSet.of(TestTokenId.WHITESPACE));
116: cats.put("incomplete", EnumSet.of(
117: TestTokenId.BLOCK_COMMENT_INCOMPLETE,
118: TestTokenId.JAVADOC_COMMENT_INCOMPLETE,
119: TestTokenId.STRING_LITERAL_INCOMPLETE));
120: cats.put("error", EnumSet.of(
121: TestTokenId.BLOCK_COMMENT_INCOMPLETE,
122: TestTokenId.JAVADOC_COMMENT_INCOMPLETE,
123: TestTokenId.STRING_LITERAL_INCOMPLETE));
124: cats.put("test-category", EnumSet.of(
125: TestTokenId.IDENTIFIER, TestTokenId.PLUS,
126: TestTokenId.MINUS));
127: return cats;
128: }
129:
130: @Override
131: protected Lexer<TestTokenId> createLexer(
132: LexerRestartInfo<TestTokenId> info) {
133: return new TestLexer(info);
134: }
135:
136: @Override
137: public LanguageEmbedding<?> embedding(Token<TestTokenId> token,
138: LanguagePath languagePath,
139: InputAttributes inputAttributes) {
140: // Test language embedding in the block comment
141: switch (token.id()) {
142: case BLOCK_COMMENT:
143: return LanguageEmbedding.create(TestPlainTokenId
144: .language(), 2, 2);
145:
146: case JAVADOC_COMMENT:
147: return LanguageEmbedding.create(TestJavadocTokenId
148: .language(), 3, 2);
149:
150: case STRING_LITERAL:
151: case STRING_LITERAL_INCOMPLETE:
152: return LanguageEmbedding.create(TestStringTokenId
153: .language(), 1, 1);
154: }
155: return null; // No embedding
156: }
157:
158: // protected CharPreprocessor createCharPreprocessor() {
159: // return CharPreprocessor.createUnicodeEscapesPreprocessor();
160: // }
161:
162: }.language();
163:
164: public static Language<TestTokenId> language() {
165: return language;
166: }
167:
168: }
|