KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > lexer > test > simple > SimpleTokenId


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.lib.lexer.test.simple;
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.spi.lexer.LanguageEmbedding;
32 import org.netbeans.spi.lexer.LanguageHierarchy;
33 import org.netbeans.spi.lexer.Lexer;
34 import org.netbeans.spi.lexer.LexerRestartInfo;
35
36 /**
37  * Simple implementation of enumerated token id.
38  *
39  * @author mmetelka
40  */

41 public enum SimpleTokenId implements TokenId {
42     
43     IDENTIFIER(null, null),
44     WHITESPACE(null, null), // normally would be "whitespace" category here but testing to do it in language hierarchy
45
BLOCK_COMMENT(null, "comment"),
46     LINE_COMMENT(null, "comment"),
47     JAVADOC_COMMENT(null, "comment"),
48     PLUS("+", "operator"),
49     MINUS("-", "operator"),
50     PLUS_MINUS_PLUS(null, null),
51     DIV("/", "operator"),
52     STAR("*", "operator"),
53     ERROR(null, "error"),
54     PUBLIC("public", "keyword"),
55     PRIVATE("private", "keyword"),
56     STATIC("static", "keyword"),
57     STRING_LITERAL(null, "string"),
58
59     BLOCK_COMMENT_INCOMPLETE(null, "comment"),
60     JAVADOC_COMMENT_INCOMPLETE(null, "comment"),
61     STRING_LITERAL_INCOMPLETE(null, "string"),
62     ;
63
64     private final String JavaDoc fixedText;
65
66     private final String JavaDoc primaryCategory;
67
68     SimpleTokenId(String JavaDoc fixedText, String JavaDoc primaryCategory) {
69         this.fixedText = fixedText;
70         this.primaryCategory = primaryCategory;
71     }
72     
73     public String JavaDoc fixedText() {
74         return fixedText;
75     }
76
77     public String JavaDoc primaryCategory() {
78         return primaryCategory;
79     }
80
81     private static final Language<SimpleTokenId> language
82     = new LanguageHierarchy<SimpleTokenId>() {
83
84         @Override JavaDoc
85         protected String JavaDoc mimeType() {
86             return "text/x-simple";
87         }
88
89         @Override JavaDoc
90         protected Collection JavaDoc<SimpleTokenId> createTokenIds() {
91             return EnumSet.allOf(SimpleTokenId.class);
92         }
93
94         @Override JavaDoc
95         protected Map JavaDoc<String JavaDoc,Collection JavaDoc<SimpleTokenId>> createTokenCategories() {
96             Map JavaDoc<String JavaDoc,Collection JavaDoc<SimpleTokenId>> cats = new HashMap JavaDoc<String JavaDoc,Collection JavaDoc<SimpleTokenId>>();
97             cats.put("operator", EnumSet.of(SimpleTokenId.PLUS_MINUS_PLUS));
98             // Normally whitespace category would be a primary category in token id's declaration
99
cats.put("whitespace", EnumSet.of(SimpleTokenId.WHITESPACE));
100             cats.put("incomplete", EnumSet.of(
101                     SimpleTokenId.BLOCK_COMMENT_INCOMPLETE,
102                     SimpleTokenId.JAVADOC_COMMENT_INCOMPLETE,
103                     SimpleTokenId.STRING_LITERAL_INCOMPLETE
104             ));
105             cats.put("error", EnumSet.of(
106                     SimpleTokenId.BLOCK_COMMENT_INCOMPLETE,
107                     SimpleTokenId.JAVADOC_COMMENT_INCOMPLETE,
108                     SimpleTokenId.STRING_LITERAL_INCOMPLETE
109             ));
110             cats.put("test-category", EnumSet.of(
111                     SimpleTokenId.IDENTIFIER,
112                     SimpleTokenId.PLUS,
113                     SimpleTokenId.MINUS
114             ));
115             return cats;
116         }
117
118         @Override JavaDoc
119         protected Lexer<SimpleTokenId> createLexer(LexerRestartInfo<SimpleTokenId> info) {
120             return new SimpleLexer(info);
121         }
122         
123         @Override JavaDoc
124         public LanguageEmbedding<? extends TokenId> embedding(
125         Token<SimpleTokenId> token, LanguagePath languagePath, InputAttributes inputAttributes) {
126             // Test language embedding in the block comment
127
switch (token.id()) {
128                 case BLOCK_COMMENT:
129                     return LanguageEmbedding.create(SimplePlainTokenId.language(), 2, 2);
130
131                 case JAVADOC_COMMENT:
132                     return LanguageEmbedding.create(SimpleJavadocTokenId.language(), 3, 2);
133
134                 case STRING_LITERAL:
135                 case STRING_LITERAL_INCOMPLETE:
136                     return LanguageEmbedding.create(SimpleStringTokenId.language(), 1, 1);
137             }
138             return null; // No embedding
139
}
140
141 // protected CharPreprocessor createCharPreprocessor() {
142
// return CharPreprocessor.createUnicodeEscapesPreprocessor();
143
// }
144

145     }.language();
146
147     public static Language<SimpleTokenId> language() {
148         return language;
149     }
150
151 }
152
Popular Tags