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