1 19 20 package org.netbeans.lib.lexer.test.state; 21 22 import org.netbeans.api.lexer.InputAttributes; 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 29 34 final class StateLexer implements Lexer<StateTokenId> { 35 36 private static final int EOF = LexerInput.EOF; 38 39 static final Object AFTER_A = "after_a"; 40 static final Object AFTER_B = "after_b"; 41 42 static final Integer AFTER_A_INT = 1; 43 static final Integer AFTER_B_INT = 2; 44 45 private boolean useIntStates; 46 47 private Object state; 48 49 private LexerInput input; 50 51 private TokenFactory<StateTokenId> tokenFactory; 52 53 private LexerRestartInfo<StateTokenId> info; 54 55 private InputAttributes inputAttributes; 56 57 StateLexer(LexerRestartInfo<StateTokenId> info) { 58 this.input = info.input(); 59 this.tokenFactory = info.tokenFactory(); 60 this.state = info.state(); 61 this.info = info; 62 63 this.useIntStates = Boolean.TRUE.equals(info.getAttributeValue("states")); 64 Object expectedRestartState = info.getAttributeValue("restartState"); 65 if (expectedRestartState != null && !expectedRestartState.equals(state)) { 66 throw new IllegalStateException ("Expected restart state " + expectedRestartState + ", but real is " + state); 67 } 68 } 69 70 public Object state() { 71 return state; 72 } 73 74 public Token<StateTokenId> nextToken() { 75 boolean returnNullToken = Boolean.TRUE.equals(info.getAttributeValue("returnNullToken")); 76 while (true) { 77 int c = input.read(); 78 if (returnNullToken) return null; 80 switch (c) { 81 case 'a': 82 state = useIntStates ? AFTER_A_INT : AFTER_A; 83 return token(StateTokenId.A); 84 85 case 'b': 86 while (input.read() == 'b') {} 87 input.backup(1); 88 state = useIntStates ? AFTER_B_INT : AFTER_B; 89 return token(StateTokenId.BMULTI); 90 91 case EOF: return null; 94 default: 95 state = null; 97 return token(StateTokenId.ERROR); 98 } 99 } 100 } 101 102 private Token<StateTokenId> token(StateTokenId id) { 103 return tokenFactory.createToken(id); 104 } 105 106 public void release() { 107 InputAttributes attrs = info.inputAttributes(); 108 if (attrs != null) { 109 attrs.setValue(StateTokenId.language(), "lexerRelease", Boolean.TRUE, false); 110 } 111 } 112 113 } 114 | Popular Tags |