1 19 20 package org.netbeans.modules.lexer.demo.javacc; 21 22 import org.netbeans.api.lexer.Language; 23 import org.netbeans.api.lexer.Lexer; 24 import org.netbeans.api.lexer.LexerInput; 25 import org.netbeans.api.lexer.TokenId; 26 import org.netbeans.api.lexer.Token; 27 import org.netbeans.spi.lexer.AbstractLexer; 28 import org.netbeans.spi.lexer.javacc.LexerInputCharStream; 29 import org.netbeans.spi.lexer.javacc.TokenMgrError; 30 import org.netbeans.spi.lexer.util.IntegerCache; 31 32 50 51 final class CalcLexer extends AbstractLexer { 52 53 private static final CalcLanguage language = CalcLanguage.get(); 54 55 private static final Integer [] integerStates = IntegerCache.getTable(CalcLanguage.MAX_STATE); 56 57 private CalcTokenManager tokenManager; 58 59 private LexerInputCharStream charStream; 60 61 private LexerInput lexerInput; 62 63 public CalcLexer() { 64 this.charStream = new LexerInputCharStream(); 65 this.tokenManager = new CalcTokenManager(charStream); 66 } 67 68 protected Object getLexerState() { 69 int s = tokenManager.curLexState; 70 return (s == tokenManager.defaultLexState) 72 ? null 73 : integerStates[s]; 74 75 90 } 91 92 public void restart(LexerInput input, Object state) { 93 super.restart(input, state); 94 95 this.lexerInput = input; 96 101 charStream.setLexerInput(lexerInput); 102 103 tokenManager.ReInit(charStream, 105 (state != null) ? ((Integer )state).intValue() : tokenManager.defaultLexState ); 109 } 110 111 protected final LexerInput getLexerInput() { return lexerInput; 113 } 114 115 protected final Language getLanguage() { return language; 117 } 118 119 120 138 protected boolean fetchToken(TokenData tokenData) { 139 try { 140 org.netbeans.spi.lexer.javacc.Token javaccToken = tokenManager.getNextToken(); 142 if (javaccToken != null) { 143 int tokenKind = javaccToken.kind; 144 tokenData.setTokenIntId(tokenKind); 145 tokenData.setTokenLength(tokenData.getDefaultTokenLength()); 146 return (tokenKind != CalcConstants.EOF); 148 } else { return false; } 151 152 } catch (TokenMgrError e) { 153 if (e.getErrorCode() == TokenMgrError.LEXICAL_ERROR) { 154 if (tokenData.inExtendedToken()) { 155 switch (tokenData.getExtendedTokenIntId()) { 156 case CalcConstants.INCOMPLETE_ML_COMMENT: 157 tokenData.setTokenIntId(CalcConstants.EOF); 159 tokenData.setTokenLength(tokenData.getTextLookahead()); 162 return true; 164 } 165 } 166 167 throw new IllegalStateException ("Internal lexer error"); 170 171 } else { throw e; 173 } 174 } 175 } 176 177 178 192 protected void ordinaryToken(OrdinaryTokenData tokenData) { 193 194 198 int tokenIntId = tokenData.getTokenIntId(); 199 switch (tokenIntId) { case CalcConstants.ERROR: 201 204 tokenData.startExtendedToken(); 205 break; 206 207 case CalcConstants.INCOMPLETE_ML_COMMENT: 221 tokenData.startExtendedToken(); 222 break; 223 } 224 225 } 226 227 264 protected void extendedToken(ExtendedTokenData tokenData, 265 boolean fetchedTokenExists) { 266 267 int extendedTokenIntId = tokenData.getExtendedTokenIntId(); 268 int tokenIntId = tokenData.getTokenIntId(); 270 switch (extendedTokenIntId) { 271 case CalcConstants.ERROR: 272 if (!fetchedTokenExists 273 || tokenIntId != CalcConstants.ERROR 274 ) { 275 280 tokenData.finishExtendedToken(false); 281 } 282 break; 283 284 case CalcConstants.INCOMPLETE_ML_COMMENT: 285 308 309 if (fetchedTokenExists && tokenIntId == CalcConstants.ML_COMMENT) { tokenData.updateExtendedTokenIntId(tokenIntId); 311 } 312 tokenData.finishExtendedToken(fetchedTokenExists); 313 break; 314 315 default: throw new IllegalStateException ("Unsupported extended token"); 317 318 } 319 320 } 321 322 323 } 324 | Popular Tags |