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.javascript.editing.lexer;
042:
043: import java.util.Collection;
044: import java.util.EnumSet;
045: import java.util.HashMap;
046: import java.util.Map;
047:
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.Token;
052: import org.netbeans.api.lexer.TokenId;
053: import org.netbeans.modules.javascript.editing.JsMimeResolver;
054: import org.netbeans.modules.javascript.editing.lexer.JsCommentTokenId;
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: * @todo Update for JavaScript
062: * @todo I should handle embeddings of =begin/=end token pairs such that they
063: * get comment/rdoc highlighting!
064: *
065: * @author Tor Norbye
066: */
067: public enum JsTokenId implements TokenId {
068: ERROR(null, "error"), NEW("new", "keyword"),
069:
070: IDENTIFIER(null, "identifier"), GLOBAL_VAR(null, "static"), CONSTANT(
071: null, "constant"), INT_LITERAL(null, "number"), REGEXP_LITERAL(
072: null, "regexp"), FLOAT_LITERAL(null, "number"),
073: // CHAR_LITERAL(null, "character"),
074: STRING_LITERAL(null, "string"), WHITESPACE(null, "whitespace"), EOL(
075: null, "whitespace"), LINE_COMMENT(null, "comment"), BLOCK_COMMENT(
076: null, "comment"), LPAREN("(", "separator"), RPAREN(")",
077: "separator"), LBRACE("{", "separator"), RBRACE("}",
078: "separator"), LBRACKET("[", "separator"), RBRACKET("]",
079: "separator"), STRING_BEGIN(null, "string"), STRING_END(
080: null, "string"), REGEXP_BEGIN(null, "regexp"), // or separator,
081: REGEXP_END(null, "regexp"),
082: // Cheating: out of laziness just map all keywords returning from JRuby
083: // into a single KEYWORD token; eventually I will have separate tokens
084: // for each here such that the various helper methods for formatting,
085: // smart indent, brace matching etc. can refer to specific keywords
086: ANY_KEYWORD(null, "keyword"), ANY_OPERATOR(null, "operator"), DOT(
087: null, "operator"), THIS("this", "keyword"), FOR("for",
088: "keyword"), IF("if", "keyword"), ELSE("else", "keyword"), WHILE(
089: "while", "keyword"),
090:
091: SEMI(";", "operator"), FUNCTION("function", "keyword"),
092:
093: // Non-unary operators which indicate a line continuation if used at the end of a line
094: NONUNARY_OP(null, "operator");
095:
096: private final String fixedText;
097: private final String primaryCategory;
098:
099: JsTokenId(String fixedText, String primaryCategory) {
100: this .fixedText = fixedText;
101: this .primaryCategory = primaryCategory;
102: }
103:
104: public String fixedText() {
105: return fixedText;
106: }
107:
108: public String primaryCategory() {
109: return primaryCategory;
110: }
111:
112: private static final Language<JsTokenId> language = new LanguageHierarchy<JsTokenId>() {
113: protected String mimeType() {
114: return JsMimeResolver.JAVASCRIPT_MIME_TYPE;
115: }
116:
117: protected Collection<JsTokenId> createTokenIds() {
118: return EnumSet.allOf(JsTokenId.class);
119: }
120:
121: @Override
122: protected Map<String, Collection<JsTokenId>> createTokenCategories() {
123: Map<String, Collection<JsTokenId>> cats = new HashMap<String, Collection<JsTokenId>>();
124: return cats;
125: }
126:
127: protected Lexer<JsTokenId> createLexer(
128: LexerRestartInfo<JsTokenId> info) {
129: return JsLexer.create(info);
130: }
131:
132: @Override
133: protected LanguageEmbedding<?> embedding(
134: Token<JsTokenId> token, LanguagePath languagePath,
135: InputAttributes inputAttributes) {
136: JsTokenId id = token.id();
137:
138: if (id == STRING_LITERAL) {
139: return LanguageEmbedding.create(JsStringTokenId
140: .language(), 0, 0);
141: } else if (id == BLOCK_COMMENT || id == LINE_COMMENT) {
142: return LanguageEmbedding.create(JsCommentTokenId
143: .language(), 0, 0);
144: }
145:
146: return null; // No embedding
147: }
148: }.language();
149:
150: public static Language<JsTokenId> language() {
151: return language;
152: }
153: }
|