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.ruby.lexer;
042:
043: import java.util.ArrayList;
044: import java.util.Collection;
045: import java.util.EnumSet;
046: import java.util.HashMap;
047: import java.util.List;
048: import java.util.Map;
049:
050: import org.netbeans.api.lexer.InputAttributes;
051: import org.netbeans.api.lexer.Language;
052: import org.netbeans.api.lexer.LanguagePath;
053: import org.netbeans.api.lexer.Token;
054: import org.netbeans.api.lexer.TokenId;
055: import org.netbeans.api.lexer.TokenId;
056: import org.netbeans.modules.ruby.RubyMimeResolver;
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: * @todo I should handle embeddings of =begin/=end token pairs such that they
064: * get comment/rdoc highlighting!
065: *
066: * @author Tor Norbye
067: */
068: public enum RubyTokenId implements TokenId {
069: ERROR(null, "error"), IDENTIFIER(null, "identifier"), CLASS_VAR(
070: null, "staticfield"), INSTANCE_VAR(null, "field"), GLOBAL_VAR(
071: null, "static"), CONSTANT(null, "constant"), DOCUMENTATION(
072: null, "comment"), ABSTRACT("abstract", "keyword"), INT_LITERAL(
073: null, "number"), REGEXP_LITERAL(null, "regexp"), FLOAT_LITERAL(
074: null, "number"), STRING_LITERAL(null, "string"), WHITESPACE(
075: null, "whitespace"), LINE_COMMENT(null, "comment"), BLOCK_COMMENT(
076: null, "comment"), TYPE_SYMBOL(null, "typesymbol"), LPAREN(
077: "(", "separator"), RPAREN(")", "separator"), LBRACE("{",
078: "separator"), RBRACE("}", "separator"), LBRACKET("[",
079: "separator"), RBRACKET("]", "separator"), STRING_BEGIN(
080: null, "string"), STRING_END(null, "string"), REGEXP_BEGIN(
081: null, "regexp"), // or separator,
082: REGEXP_END(null, "regexp"),
083: // Cheating: out of laziness just map all keywords returning from JRuby
084: // into a single KEYWORD token; eventually I will have separate tokens
085: // for each here such that the various helper methods for formatting,
086: // smart indent, brace matching etc. can refer to specific keywords
087: ANY_KEYWORD(null, "keyword"), ANY_OPERATOR(null, "operator"),
088: // FixedText on these tokens is risky since the JRuby parser in some cases
089: // seems to use these tokens for multiple source words; when I try opening
090: // for example imap.rb Bad Stuff happens. For now compute token text on the fly.
091: DOT(null, "operator"), RANGE(null, "operator"), COLON3(null,
092: "operator"), SUPER(null, "keyword"), SELF(null, "keyword"), QUOTED_STRING_LITERAL(
093: null, "string"), QUOTED_STRING_BEGIN(null, "string"), QUOTED_STRING_END(
094: null, "string"), EMBEDDED_RUBY(null, "default"), MODULE(
095: null, "keyword"), CLASS(null, "keyword"), DEF(null,
096: "keyword"), END(null, "keyword"), BEGIN(null, "keyword"), FOR(
097: null, "keyword"), CASE(null, "keyword"), LOOP(null,
098: "keyword"),
099: /** The keyword, when used as a block (NOT as a statement modifier or in a block/closure) */
100: DO(null, "keyword"),
101: /** The keyword, when used as a block (NOT as a statement modifier) */
102: IF(null, "keyword"),
103: /** The keyword, when used as a block (NOT as a statement modifier) */
104: WHILE(null, "keyword"),
105: /** The keyword, when used as a block (NOT as a statement modifier) */
106: UNTIL(null, "keyword"),
107: /** The keyword, when used as a block (NOT as a statement modifier) */
108: UNLESS(null, "keyword"),
109:
110: // Indent words
111: ELSE(null, "keyword"), ELSIF(null, "keyword"), ENSURE(null,
112: "keyword"), WHEN(null, "keyword"), RESCUE(null, "keyword"),
113:
114: // Non-unary operators which indicate a line continuation if used at the end of a line
115: NONUNARY_OP(null, "operator");
116:
117: private final String fixedText;
118: private final String primaryCategory;
119:
120: RubyTokenId(String fixedText, String primaryCategory) {
121: this .fixedText = fixedText;
122: this .primaryCategory = primaryCategory;
123: }
124:
125: public String fixedText() {
126: return fixedText;
127: }
128:
129: public String primaryCategory() {
130: return primaryCategory;
131: }
132:
133: private static final Language<RubyTokenId> language = new LanguageHierarchy<RubyTokenId>() {
134: protected String mimeType() {
135: return RubyMimeResolver.RUBY_MIME_TYPE;
136: }
137:
138: protected Collection<RubyTokenId> createTokenIds() {
139: return EnumSet.allOf(RubyTokenId.class);
140: }
141:
142: @Override
143: protected Map<String, Collection<RubyTokenId>> createTokenCategories() {
144: Map<String, Collection<RubyTokenId>> cats = new HashMap<String, Collection<RubyTokenId>>();
145: return cats;
146: }
147:
148: protected Lexer<RubyTokenId> createLexer(
149: LexerRestartInfo<RubyTokenId> info) {
150: return RubyLexer.create(info);
151: }
152:
153: @Override
154: protected LanguageEmbedding<?> embedding(
155: Token<RubyTokenId> token, LanguagePath languagePath,
156: InputAttributes inputAttributes) {
157: RubyTokenId id = token.id();
158:
159: if (id == QUOTED_STRING_LITERAL) {
160: return LanguageEmbedding.create(RubyStringTokenId
161: .languageDouble(), 0, 0);
162: } else if (id == STRING_LITERAL) {
163: return LanguageEmbedding.create(RubyStringTokenId
164: .languageSingle(), 0, 0);
165: } else if ((id == LINE_COMMENT) || (id == BLOCK_COMMENT)) {
166: return LanguageEmbedding.create(RubyCommentTokenId
167: .language(), 1, 0);
168:
169: //} else if (id == REGEXP_LITERAL) {
170: // return LanguageEmbedding.create(RubyRegexpTokenId.language(), 0, 0);
171: } else if (id == DOCUMENTATION) {
172: return LanguageEmbedding.create(RubyCommentTokenId
173: .language(), 0, 0);
174: } else if (id == EMBEDDED_RUBY) {
175: return LanguageEmbedding.create(RubyTokenId.language(),
176: 0, 0);
177: }
178:
179: return null; // No embedding
180: }
181: }.language();
182:
183: public static Language<RubyTokenId> language() {
184: return language;
185: }
186: }
|