1 19 20 package org.netbeans.modules.lexer.demo.antlr; 21 22 import antlr.LexerSharedInputState; 23 import antlr.CharStreamException; 24 import antlr.TokenStreamException; 25 import org.netbeans.api.lexer.Language; 26 import org.netbeans.api.lexer.Lexer; 27 import org.netbeans.api.lexer.LexerInput; 28 import org.netbeans.api.lexer.Token; 29 import org.netbeans.api.lexer.TokenId; 30 import org.netbeans.spi.lexer.AbstractLexer; 31 import org.netbeans.spi.lexer.antlr.AntlrToken; 32 import org.netbeans.spi.lexer.util.IntegerCache; 33 import org.netbeans.spi.lexer.util.LexerInputReader; 34 35 49 50 final class CalcLexer extends AbstractLexer { 51 52 private static final CalcLanguage language = CalcLanguage.get(); 53 54 private CalcScanner scanner; 55 56 private LexerInput lexerInput; 57 58 public CalcLexer() { 59 this.scanner = new CalcScanner((LexerSharedInputState)null); 60 } 61 62 69 70 public void restart(LexerInput input, Object state) { 71 super.restart(input, state); 72 73 this.lexerInput = input; 74 75 LexerSharedInputState inputState = null; 77 if (lexerInput != null) { 78 inputState = new LexerSharedInputState(new LexerInputReader(lexerInput)); 79 } 80 scanner.setInputState(inputState); 81 if (inputState != null) { 82 scanner.resetText(); 83 } 84 85 } 87 88 protected final LexerInput getLexerInput() { return lexerInput; 90 } 91 92 protected final Language getLanguage() { return language; 94 } 95 96 114 protected boolean fetchToken(TokenData tokenData) { 115 try { 116 antlr.Token antlrToken = scanner.nextToken(); 117 if (antlrToken != null) { 118 int intId = antlrToken.getType(); 119 if (intId == CalcScannerTokenTypes.EOF) { 120 return false; 121 } 122 tokenData.setTokenIntId(antlrToken.getType()); 123 124 int len; 125 if (antlrToken instanceof AntlrToken) { 126 len = ((AntlrToken)antlrToken).getLength(); 127 } else { 128 String text = antlrToken.getText(); 129 len = text.length(); 130 } 131 132 tokenData.setTokenLength(len); 133 134 } else { return false; } 137 138 } catch (TokenStreamException e) { 139 147 boolean useScannerTextTokenLength = true; 148 149 int incompleteIntId; 151 int state = scanner.getState(); 152 switch (state) { 153 case 0: 154 incompleteIntId = CalcLanguage.ERROR_INT; 155 break; 156 157 case CalcScannerTokenTypes.INCOMPLETE_ML_COMMENT: 158 incompleteIntId = state; 168 169 useScannerTextTokenLength = false; 175 break; 176 177 default: 178 throw new IllegalStateException (); 180 } 181 scanner.resetState(); 182 183 tokenData.setTokenIntId(incompleteIntId); 184 185 int scannerTextTokenLength = scanner.getText().length(); 186 int tokenLength = useScannerTextTokenLength 187 ? scannerTextTokenLength 188 : tokenData.getDefaultTokenLength(); 189 190 if (scannerTextTokenLength > tokenLength) { throw new IllegalStateException ("Internal lexer error"); } 194 while (scannerTextTokenLength < tokenLength) { 195 scannerConsumeChar(); 196 scannerTextTokenLength++; 197 } 198 199 tokenLength = increaseTokenLengthIfEmpty(tokenLength); 201 tokenData.setTokenLength(tokenLength); 202 203 scanner.resetText(); 204 } 205 206 return true; 207 } 208 209 private int increaseTokenLengthIfEmpty(int tokenLength) { 210 if (tokenLength == 0) { scannerConsumeChar(); 212 tokenLength++; 213 } 214 return tokenLength; 215 } 216 217 private void scannerConsumeChar() { 218 try { 219 scanner.consume(); 220 } catch (CharStreamException e) { 221 throw new IllegalStateException (); 222 } 223 } 224 225 239 protected void ordinaryToken(OrdinaryTokenData tokenData) { 240 241 247 int tokenIntId = tokenData.getTokenIntId(); 248 switch (tokenIntId) { case CalcLanguage.ERROR_INT: 250 tokenData.startExtendedToken(); 252 break; 253 254 } 255 256 } 257 258 295 protected void extendedToken(ExtendedTokenData tokenData, 296 boolean fetchedTokenExists) { 297 298 int extendedTokenIntId = tokenData.getExtendedTokenIntId(); 299 int tokenIntId = tokenData.getTokenIntId(); switch (extendedTokenIntId) { 301 case CalcLanguage.ERROR_INT: 302 if (!fetchedTokenExists 303 || tokenIntId != CalcLanguage.ERROR_INT 304 ) { 305 310 tokenData.finishExtendedToken(false); 311 } 312 break; 313 314 default: throw new IllegalStateException ("Unsupported extended token"); 316 317 } 318 319 } 320 321 public String toString() { 322 String scannerText = scanner.getText(); 323 return super.toString() + ", scannerText=\"" + scannerText 324 + "\";length=" + scannerText.length(); 325 } 326 327 } 328 | Popular Tags |