1 19 20 package org.netbeans.lib.java.lexer; 21 22 import org.netbeans.api.java.lexer.JavaStringTokenId; 23 import org.netbeans.api.java.lexer.JavaTokenId; 24 import org.netbeans.api.lexer.Token; 25 import org.netbeans.spi.lexer.Lexer; 26 import org.netbeans.spi.lexer.LexerInput; 27 import org.netbeans.spi.lexer.LexerRestartInfo; 28 import org.netbeans.spi.lexer.TokenFactory; 29 30 36 37 public class JavaStringLexer implements Lexer<JavaStringTokenId> { 38 39 private static final int EOF = LexerInput.EOF; 40 41 private LexerInput input; 42 43 private TokenFactory<JavaStringTokenId> tokenFactory; 44 45 public JavaStringLexer(LexerRestartInfo<JavaStringTokenId> info) { 46 this.input = info.input(); 47 this.tokenFactory = info.tokenFactory(); 48 assert (info.state() == null); } 50 51 public Object state() { 52 return null; 53 } 54 55 public Token<JavaStringTokenId> nextToken() { 56 while(true) { 57 int ch = input.read(); 58 switch (ch) { 59 case EOF: 60 if (input.readLength() > 0) 61 return token(JavaStringTokenId.TEXT); 62 else 63 return null; 64 case '\\': 65 if (input.readLength() > 1) { input.backup(1); 67 return tokenFactory.createToken(JavaStringTokenId.TEXT, input.readLength()); 68 } 69 switch (ch = input.read()) { 70 case 'b': 71 return token(JavaStringTokenId.BACKSPACE); 72 case 'f': 73 return token(JavaStringTokenId.FORM_FEED); 74 case 'n': 75 return token(JavaStringTokenId.NEWLINE); 76 case 'r': 77 return token(JavaStringTokenId.CR); 78 case 't': 79 return token(JavaStringTokenId.TAB); 80 case '\'': 81 return token(JavaStringTokenId.SINGLE_QUOTE); 82 case '"': 83 return token(JavaStringTokenId.DOUBLE_QUOTE); 84 case '\\': 85 return token(JavaStringTokenId.BACKSLASH); 86 case '0': case '1': case '2': case '3': 87 switch (input.read()) { 88 case '0': case '1': case '2': case '3': 89 case '4': case '5': case '6': case '7': 90 switch (input.read()) { 91 case '0': case '1': case '2': case '3': 92 case '4': case '5': case '6': case '7': 93 return token(JavaStringTokenId.OCTAL_ESCAPE); 94 } 95 return token(JavaStringTokenId.OCTAL_ESCAPE_INVALID); 96 } 97 return token(JavaStringTokenId.OCTAL_ESCAPE_INVALID); 98 } 99 return token(JavaStringTokenId.ESCAPE_SEQUENCE_INVALID); 100 } } } 103 104 private Token<JavaStringTokenId> token(JavaStringTokenId id) { 105 return tokenFactory.createToken(id); 106 } 107 108 public void release() { 109 } 110 111 } 112 | Popular Tags |