1 19 20 package org.netbeans.lib.lexer.test.simple; 21 22 import java.util.Collection ; 23 import java.util.EnumSet ; 24 import java.util.HashMap ; 25 import java.util.Map ; 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 41 public enum SimpleTokenId implements TokenId { 42 43 IDENTIFIER(null, null), 44 WHITESPACE(null, null), 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 fixedText; 65 66 private final String primaryCategory; 67 68 SimpleTokenId(String fixedText, String primaryCategory) { 69 this.fixedText = fixedText; 70 this.primaryCategory = primaryCategory; 71 } 72 73 public String fixedText() { 74 return fixedText; 75 } 76 77 public String primaryCategory() { 78 return primaryCategory; 79 } 80 81 private static final Language<SimpleTokenId> language 82 = new LanguageHierarchy<SimpleTokenId>() { 83 84 @Override 85 protected String mimeType() { 86 return "text/x-simple"; 87 } 88 89 @Override 90 protected Collection <SimpleTokenId> createTokenIds() { 91 return EnumSet.allOf(SimpleTokenId.class); 92 } 93 94 @Override 95 protected Map <String ,Collection <SimpleTokenId>> createTokenCategories() { 96 Map <String ,Collection <SimpleTokenId>> cats = new HashMap <String ,Collection <SimpleTokenId>>(); 97 cats.put("operator", EnumSet.of(SimpleTokenId.PLUS_MINUS_PLUS)); 98 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 119 protected Lexer<SimpleTokenId> createLexer(LexerRestartInfo<SimpleTokenId> info) { 120 return new SimpleLexer(info); 121 } 122 123 @Override 124 public LanguageEmbedding<? extends TokenId> embedding( 125 Token<SimpleTokenId> token, LanguagePath languagePath, InputAttributes inputAttributes) { 126 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; } 140 141 145 }.language(); 146 147 public static Language<SimpleTokenId> language() { 148 return language; 149 } 150 151 } 152 | Popular Tags |