1 11 12 package org.eclipse.jface.text.rules; 13 14 15 import org.eclipse.core.runtime.Assert; 16 17 import org.eclipse.jface.text.BadLocationException; 18 import org.eclipse.jface.text.IDocument; 19 20 21 33 public class RuleBasedScanner implements ICharacterScanner, ITokenScanner { 34 35 36 protected IRule[] fRules; 37 38 protected IToken fDefaultReturnToken; 39 40 protected IDocument fDocument; 41 42 protected char[][] fDelimiters; 43 44 protected int fOffset; 45 46 protected int fRangeEnd; 47 48 protected int fTokenOffset; 49 50 protected int fColumn; 51 52 protected static final int UNDEFINED= -1; 53 54 57 public RuleBasedScanner() { 58 } 59 60 65 public void setRules(IRule[] rules) { 66 if (rules != null) { 67 fRules= new IRule[rules.length]; 68 System.arraycopy(rules, 0, fRules, 0, rules.length); 69 } else 70 fRules= null; 71 } 72 73 81 public void setDefaultReturnToken(IToken defaultReturnToken) { 82 Assert.isNotNull(defaultReturnToken.getData()); 83 fDefaultReturnToken= defaultReturnToken; 84 } 85 86 89 public void setRange(final IDocument document, int offset, int length) { 90 Assert.isLegal(document != null); 91 final int documentLength= document.getLength(); 92 checkRange(offset, length, documentLength); 93 94 fDocument= document; 95 fOffset= offset; 96 fColumn= UNDEFINED; 97 fRangeEnd= offset + length; 98 99 String [] delimiters= fDocument.getLegalLineDelimiters(); 100 fDelimiters= new char[delimiters.length][]; 101 for (int i= 0; i < delimiters.length; i++) 102 fDelimiters[i]= delimiters[i].toCharArray(); 103 104 if (fDefaultReturnToken == null) 105 fDefaultReturnToken= new Token(null); 106 } 107 108 117 private void checkRange(int offset, int length, int documentLength) { 118 Assert.isLegal(offset > -1); 119 Assert.isLegal(length > -1); 120 Assert.isLegal(offset + length <= documentLength); 121 } 122 123 126 public int getTokenOffset() { 127 return fTokenOffset; 128 } 129 130 133 public int getTokenLength() { 134 if (fOffset < fRangeEnd) 135 return fOffset - getTokenOffset(); 136 return fRangeEnd - getTokenOffset(); 137 } 138 139 140 143 public int getColumn() { 144 if (fColumn == UNDEFINED) { 145 try { 146 int line= fDocument.getLineOfOffset(fOffset); 147 int start= fDocument.getLineOffset(line); 148 149 fColumn= fOffset - start; 150 151 } catch (BadLocationException ex) { 152 } 153 } 154 return fColumn; 155 } 156 157 160 public char[][] getLegalLineDelimiters() { 161 return fDelimiters; 162 } 163 164 167 public IToken nextToken() { 168 169 fTokenOffset= fOffset; 170 fColumn= UNDEFINED; 171 172 if (fRules != null) { 173 for (int i= 0; i < fRules.length; i++) { 174 IToken token= (fRules[i].evaluate(this)); 175 if (!token.isUndefined()) 176 return token; 177 } 178 } 179 180 if (read() == EOF) 181 return Token.EOF; 182 return fDefaultReturnToken; 183 } 184 185 188 public int read() { 189 190 try { 191 192 if (fOffset < fRangeEnd) { 193 try { 194 return fDocument.getChar(fOffset); 195 } catch (BadLocationException e) { 196 } 197 } 198 199 return EOF; 200 201 } finally { 202 ++ fOffset; 203 fColumn= UNDEFINED; 204 } 205 } 206 207 210 public void unread() { 211 --fOffset; 212 } 213 } 214 215 216 | Popular Tags |