1 19 20 package org.netbeans.modules.lexer.demo; 21 22 import javax.swing.event.DocumentEvent ; 23 import javax.swing.event.DocumentListener ; 24 import javax.swing.text.Segment ; 25 import javax.swing.text.Document ; 26 import javax.swing.text.BadLocationException ; 27 import org.netbeans.api.lexer.Token; 28 import org.netbeans.api.lexer.TokenId; 29 import org.netbeans.api.lexer.LexerInput; 30 import org.netbeans.api.lexer.SampleTextMatcher; 31 import org.netbeans.api.lexer.Language; 32 import org.netbeans.api.lexer.Lexer; 33 import org.netbeans.spi.lexer.util.Compatibility; 34 import org.netbeans.spi.lexer.inc.TextTokenUpdater; 35 import org.netbeans.spi.lexer.util.LexerUtilities; 36 37 43 44 public class DemoTokenUpdater extends TextTokenUpdater { 45 46 private Segment seg = new Segment (); 48 private final Document doc; 49 50 private final Language language; 51 52 private final boolean maintainLookbacks; 53 54 private boolean debugTokenChanges; 55 56 private Lexer sharedLexer; 58 public DemoTokenUpdater(Document doc, Language language) { 59 this(doc, language, true); 60 } 61 62 public DemoTokenUpdater(Document doc, Language language, boolean maintainLookbacks) { 63 this.doc = doc; 64 this.language = language; 65 this.maintainLookbacks = maintainLookbacks; 66 67 doc.addDocumentListener( 68 new DocumentListener () { 69 public void insertUpdate(DocumentEvent evt) { 70 if (debugTokenChanges) { 71 try { 72 System.out.println("\nDocument-insert \"" 73 + LexerUtilities.toSource( 74 (evt.getDocument()).getText(evt.getOffset(), 75 evt.getLength())) 76 + "\"" 77 ); 78 } catch (BadLocationException e) { 79 e.printStackTrace(); 80 } 81 } 82 83 update(evt.getOffset(), evt.getLength()); 84 } 85 86 public void removeUpdate(DocumentEvent evt) { 87 if (debugTokenChanges) { 88 System.out.println("\nDocument-remove at offset=" 89 + evt.getOffset() 90 + ", length=" 91 + evt.getLength() 92 ); 93 } 94 95 update(evt.getOffset(), -evt.getLength()); 96 } 97 98 public void changedUpdate(DocumentEvent evt) { 99 } 100 } 101 ); 102 } 103 104 public boolean getDebugTokenChanges() { 105 return debugTokenChanges; 106 } 107 108 public void setDebugTokenChanges(boolean debugTokenChanges) { 109 this.debugTokenChanges = debugTokenChanges; 110 } 111 112 protected final Document getDocument() { 113 return doc; 114 } 115 116 protected final Language getLanguage() { 117 return language; 118 } 119 120 public char textCharAt(int index) { 121 synchronized (seg) { 122 try { 123 doc.getText(index, 1, seg); 124 return seg.array[seg.offset]; 125 126 } catch (BadLocationException e) { 127 throw new IllegalStateException (e.toString()); 128 } 129 } 130 } 131 132 public int textLength() { 133 return doc.getLength(); 134 } 135 136 private String getDocumentText(int offset, int length) { 137 try { 138 return doc.getText(offset, length); 139 } catch (BadLocationException e) { 140 throw new IllegalStateException (e.getMessage()); 141 } 142 } 143 144 protected Token createToken(TokenId id, int index, int length) { 145 String sampleText = null; 146 if (Compatibility.charSequenceExists()) { 147 SampleTextMatcher matcher = id.getSampleTextMatcher(); 148 if (matcher != null) { 149 155 CharSequence recognizedText 156 = (CharSequence )(Object )getDocumentText(index, length); sampleText = matcher.match(recognizedText); 158 } 159 } 160 161 Token token; 162 if (sampleText != null) { 163 token = new DemoSampleToken(id, sampleText); 164 } else { 165 token = new DemoToken(this, id, index, length); 166 } 167 168 return token; 169 } 170 171 protected Lexer createLexer() { 172 if (sharedLexer == null) { 173 sharedLexer = language.createLexer(); 174 } 175 176 return sharedLexer; 177 } 178 179 protected void add(Token token, int lookahead, Object state) { 180 add(token); 181 182 if (token instanceof DemoSampleToken) { 183 DemoSampleToken dft = (DemoSampleToken)token; 184 dft.setLookahead(lookahead); 185 dft.setState(state); 186 } else { 187 DemoToken dt = (DemoToken)token; 188 dt.setLookahead(lookahead); 189 dt.setState(state); 190 } 191 192 if (debugTokenChanges) { 193 System.out.println("Added token: [" 194 + (getNextIndex() - 1) + "]=" 195 + tokenToString(token, false) 196 ); 197 } 198 } 199 200 protected void remove() { 201 if (debugTokenChanges) { 202 System.out.println("Removed token at index=" 203 + (getNextIndex() - 1) 204 ); 205 } 206 207 super.remove(); 208 } 209 210 public int getLookahead() { 211 Token token = getToken(getValidPreviousIndex()); 212 return (token instanceof DemoSampleToken) 213 ? ((DemoSampleToken)token).getLookahead() 214 : ((DemoToken)token).getLookahead(); 215 } 216 217 public Object getState() { 218 Token token = getToken(getValidPreviousIndex()); 219 return (token instanceof DemoSampleToken) 220 ? ((DemoSampleToken)token).getState() 221 : ((DemoToken)token).getState(); 222 } 223 224 public int getLookback() { 225 if (maintainLookbacks) { 226 Token token = getToken(getValidPreviousIndex()); 227 return (token instanceof DemoSampleToken) 228 ? ((DemoSampleToken)token).getLookback() 229 : ((DemoToken)token).getLookback(); 230 231 } else { return -1; 233 } 234 } 235 236 protected void setLookback(int lookback) { 237 if (maintainLookbacks) { 238 Token token = getToken(getValidPreviousIndex()); 239 if (token instanceof DemoSampleToken) { 240 ((DemoSampleToken)token).setLookback(lookback); 241 } else { 242 ((DemoToken)token).setLookback(lookback); 243 } 244 } 245 } 246 247 public boolean hasNext() { 248 return super.hasNext(); 249 } 250 251 public Token next() { 252 return super.next(); 253 } 254 255 public int relocate(int index) { 256 return super.relocate(index); 257 } 258 259 public String tokenToString(Token token, boolean extraInfo) { 260 StringBuffer sb = new StringBuffer (); 261 262 int length = org.netbeans.spi.lexer.util.Compatibility.getLength(token); 263 String text = org.netbeans.spi.lexer.util.Compatibility.toString(token); 264 sb.append("\"" 265 + org.netbeans.spi.lexer.util.LexerUtilities.toSource(text) 266 + "\", " + token.getId() 267 ); 268 269 if (token instanceof DemoToken) { 270 DemoToken dt = (DemoToken)token; 271 272 sb.append(", off="); 273 sb.append(getOffset(dt.getRawOffset())); 274 275 sb.append(", type=regular"); 276 if (extraInfo) { 277 sb.append(", la=" + dt.getLookahead() 278 + ", lb=" + dt.getLookback() 279 + ", st=" + dt.getState() 280 ); 281 } 282 283 } else { 284 DemoSampleToken dft = (DemoSampleToken)token; 285 sb.append(", type=sample"); 286 if (extraInfo) { 287 sb.append(", la=" + dft.getLookahead() 288 + ", lb=" + dft.getLookback() 289 + ", st=" + dft.getState() 290 ); 291 } 292 } 293 294 return sb.toString(); 295 } 296 297 public String allTokensToString() { 298 StringBuffer sb = new StringBuffer (); 299 int cnt = getTokenCount(); 300 sb.append("Dump of tokens (tokenCount=" + cnt + ") in token updater:\n"); 301 int offset = 0; 302 try { 303 for (int i = 0; i < cnt; i++) { 304 Token t = getToken(i); 305 int length = org.netbeans.spi.lexer.util.Compatibility.getLength(t); 306 String text = org.netbeans.spi.lexer.util.Compatibility.toString(t); 307 sb.append("[" + i + "] \"" 308 + org.netbeans.spi.lexer.util.LexerUtilities.toSource(text) 309 + "\", " + t.getId() 310 + ", off=" + offset 311 ); 312 313 if (t instanceof DemoToken) { 314 DemoToken dt = (DemoToken)t; 315 316 if (getOffset(dt.getRawOffset()) != offset) { 317 throw new IllegalStateException ("offsets differ"); 318 } 319 320 sb.append(", type=regular" 321 + ", la=" + dt.getLookahead() 322 + ", lb=" + dt.getLookback() 323 + ", st=" + dt.getState() 324 + "\n" 325 ); 326 327 } else { 328 DemoSampleToken dft = (DemoSampleToken)t; 329 sb.append(", type=sample" 330 + ", la=" + dft.getLookahead() 331 + ", lb=" + dft.getLookback() 332 + ", st=" + dft.getState() 333 + "\n" 334 ); 335 } 336 337 338 offset += length; 339 } 340 } catch (RuntimeException e) { 341 System.err.println(sb.toString()); 342 throw e; 343 } 344 345 return sb.toString(); 346 } 347 348 } 349 | Popular Tags |