1 19 20 package org.netbeans.lib.lexer.test.dump; 21 22 import org.netbeans.api.lexer.PartType; 23 import org.netbeans.api.lexer.Token; 24 import org.netbeans.spi.lexer.Lexer; 25 import org.netbeans.spi.lexer.LexerInput; 26 import org.netbeans.spi.lexer.LexerRestartInfo; 27 import org.netbeans.spi.lexer.TokenFactory; 28 import org.netbeans.spi.lexer.TokenPropertyProvider; 29 import org.netbeans.spi.lexer.TokenPropertyProvider; 30 31 36 final class TokenDumpLexer implements Lexer<TokenDumpTokenId> { 37 38 private static final int EOF = LexerInput.EOF; 40 41 private LexerInput input; 42 43 private TokenFactory<TokenDumpTokenId> tokenFactory; 44 45 TokenDumpLexer(LexerRestartInfo<TokenDumpTokenId> info) { 46 this.input = info.input(); 47 this.tokenFactory = info.tokenFactory(); 48 } 49 50 public Object state() { 51 return null; 52 } 53 54 public Token<TokenDumpTokenId> nextToken() { 55 int c = input.read(); 56 switch (c) { 57 case '\r': 58 input.consumeNewline(); case '\n': return token(TokenDumpTokenId.NEWLINE); 61 62 case EOF: 63 return null; 64 65 case '.': 66 switch (c = input.read()) { 67 case '\\': if ((c = input.read()) == '.') { switch (c = input.read()) { 70 case 'b': 71 return finishCharLiteralOrText(TokenDumpTokenId.BACKSPACE_CHAR, '\b'); 72 case 'f': 73 return finishCharLiteralOrText(TokenDumpTokenId.FORM_FEED_CHAR, '\f'); 74 case 't': 75 return finishCharLiteralOrText(TokenDumpTokenId.TAB_CHAR, '\t'); 76 case 'r': 77 return finishCharLiteralOrText(TokenDumpTokenId.CR_CHAR, '\r'); 78 case 'n': 79 return finishCharLiteralOrText(TokenDumpTokenId.NEWLINE_CHAR, '\n'); 80 case 'u': 81 if ((c = input.read()) == '.') 82 return finishUnicodeOrText(); 83 } 84 } 85 input.backup(1); 86 return finishText(); 87 88 case 'e': if ((c = input.read()) == '.' 90 && (c = input.read()) == 'o' 91 && (c = input.read()) == '.' 92 && (c = input.read()) == 'f' 93 && (c = input.read()) == '.' 94 ) 95 return finishNewlineOrText(TokenDumpTokenId.EOF_VIRTUAL); 96 input.backup(1); 97 return finishText(); 98 99 case 't': if ((c = input.read()) == '.' 101 && (c = input.read()) == 'e' 102 && (c = input.read()) == '.' 103 && (c = input.read()) == 's' 104 && (c = input.read()) == '.' 105 && (c = input.read()) == 't' 106 && (c = input.read()) == '.' 107 ) { return finishTillNewline(TokenDumpTokenId.TEST_NAME); 109 } 110 input.backup(1); 111 return finishText(); 112 113 case EOF: 114 return token(TokenDumpTokenId.TEXT); 115 } 116 117 default: 118 return finishText(); 119 } 120 } 121 122 private Token<TokenDumpTokenId> finishText() { 123 return finishTillNewline(TokenDumpTokenId.TEXT); 124 } 125 126 private Token<TokenDumpTokenId> finishTillNewline(TokenDumpTokenId id) { 127 while (true) { 128 switch (input.read()) { 129 case '\r': 130 case '\n': 131 case EOF: 132 input.backup(1); 133 return token(id); 134 } 135 } 136 } 137 138 private Token<TokenDumpTokenId> finishNewlineOrText(TokenDumpTokenId id) { 139 switch (input.read()) { 141 case '\r': 142 case '\n': 143 case EOF: input.backup(1); 145 return token(id); 146 } 147 return finishText(); 148 } 149 150 private Token<TokenDumpTokenId> finishUnicodeOrText() { 151 int c; 153 int number = 0; 154 int hexDigit = 0; 155 for (int i = 4; i > 0; i--) { switch (c = input.read()) { 157 case '0': case '1': case '2': case '3': case '4': 158 case '5': case '6': case '7': case '8': case '9': 159 hexDigit = c - '0'; 160 break; 161 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': 162 hexDigit = c - 'a' + 10; 163 break; 164 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': 165 hexDigit = c - 'A' + 10; 166 break; 167 default: 168 input.backup(1); 169 return finishText(); 170 171 } 172 number = (number << 4) | hexDigit; 173 } 174 return finishCharLiteralOrText(TokenDumpTokenId.UNICODE_CHAR, (char)number); 175 } 176 177 private Token<TokenDumpTokenId> finishCharLiteralOrText(TokenDumpTokenId id, char ch) { 178 int c; 179 if ((c = input.read()) == '.') { 180 switch (c = input.read()) { 181 case '\r': 182 case '\n': 183 case EOF: 184 input.backup(1); 185 return tokenFactory.createPropertyToken(id, input.readLength(), 186 new UnicodeCharValueProvider(new Character (ch)), PartType.COMPLETE); 187 } 188 } 189 input.backup(1); 190 return finishText(); 191 } 192 193 private Token<TokenDumpTokenId> token(TokenDumpTokenId id) { 194 return tokenFactory.createToken(id); 195 } 196 197 public void release() { 198 } 199 200 private static final class UnicodeCharValueProvider implements TokenPropertyProvider { 201 202 private Character ch; 203 204 UnicodeCharValueProvider(Character ch) { 205 this.ch = ch; 206 } 207 208 public Object getValue(Token token, Object key) { 209 if (TokenDumpTokenId.UNICODE_CHAR_TOKEN_PROPERTY.equals(key)) 210 return ch; 211 return null; } 213 214 } 215 216 } 217 | Popular Tags |