KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > lexer > editorbridge > calc > lang > CalcTokenId


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.modules.lexer.editorbridge.calc.lang;
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.modules.lexer.editorbridge.calc.CalcDataLoader;
32 import org.netbeans.spi.lexer.LanguageEmbedding;
33 import org.netbeans.spi.lexer.LanguageHierarchy;
34 import org.netbeans.spi.lexer.Lexer;
35 import org.netbeans.spi.lexer.LexerRestartInfo;
36
37 /**
38  * Calc token id definition.
39  *
40  * @author Miloslav Metelka
41  * @version 1.00
42  */

43
44 public enum CalcTokenId implements TokenId {
45
46     WHITESPACE(null, "whitespace"),
47     LINE_COMMENT(null, "comment"),
48     BLOCK_COMMENT(null, "comment"),
49     E("e", "keyword"),
50     PI("pi", "keyword"),
51     IDENTIFIER(null, null),
52     INT_LITERAL(null, "number"),
53     FLOAT_LITERAL(null, "number"),
54     PLUS("+", "operator"),
55     MINUS("-", "operator"),
56     STAR("*", "operator"),
57     SLASH("/", "operator"),
58     LPAREN("(", "separator"),
59     RPAREN(")", "separator"),
60     ERROR(null, "error"),
61     BLOCK_COMMENT_INCOMPLETE(null, "comment");
62
63
64     private final String JavaDoc fixedText;
65
66     private final String JavaDoc primaryCategory;
67
68     private CalcTokenId(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<CalcTokenId> language = new LanguageHierarchy<CalcTokenId>() {
82         @Override JavaDoc
83         protected Collection JavaDoc<CalcTokenId> createTokenIds() {
84             return EnumSet.allOf(CalcTokenId.class);
85         }
86         
87         @Override JavaDoc
88         protected Map JavaDoc<String JavaDoc,Collection JavaDoc<CalcTokenId>> createTokenCategories() {
89             Map JavaDoc<String JavaDoc,Collection JavaDoc<CalcTokenId>> cats = new HashMap JavaDoc<String JavaDoc,Collection JavaDoc<CalcTokenId>>();
90
91             // Incomplete literals
92
cats.put("incomplete", EnumSet.of(CalcTokenId.BLOCK_COMMENT_INCOMPLETE));
93             // Additional literals being a lexical error
94
cats.put("error", EnumSet.of(CalcTokenId.BLOCK_COMMENT_INCOMPLETE));
95             
96             return cats;
97         }
98
99         @Override JavaDoc
100         protected Lexer<CalcTokenId> createLexer(LexerRestartInfo<CalcTokenId> info) {
101             return new CalcLexer(info);
102         }
103
104         @Override JavaDoc
105         protected LanguageEmbedding embedding(
106         Token<CalcTokenId> token, LanguagePath languagePath, InputAttributes inputAttributes) {
107             return null; // No embedding
108
}
109
110         @Override JavaDoc
111         protected String JavaDoc mimeType() {
112             return CalcDataLoader.CALC_MIME_TYPE;
113         }
114         
115     }.language();
116
117     public static final Language<CalcTokenId> language() {
118         return language;
119     }
120
121 }
122
Popular Tags