1 package org.jedit.syntax; 2 3 11 12 import javax.swing.text.Segment ; 13 import java.util.*; 14 15 30 public abstract class TokenMarker 31 { 32 38 public Token markTokens(Segment line, int lineIndex) 39 { 40 if(lineIndex >= length) 41 { 42 throw new IllegalArgumentException ("Tokenizing invalid line: " 43 + lineIndex); 44 } 45 46 lastToken = null; 47 48 LineInfo info = lineInfo[lineIndex]; 49 LineInfo prev; 50 if(lineIndex == 0) 51 prev = null; 52 else 53 prev = lineInfo[lineIndex - 1]; 54 55 byte oldToken = info.token; 56 byte token = markTokensImpl(prev == null ? 57 Token.NULL : prev.token,line,lineIndex); 58 59 info.token = token; 60 61 99 if(!(lastLine == lineIndex && nextLineRequested)) 100 nextLineRequested = (oldToken != token); 101 102 lastLine = lineIndex; 103 104 addToken(0,Token.END); 105 106 return firstToken; 107 } 108 109 126 protected abstract byte markTokensImpl(byte token, Segment line, 127 int lineIndex); 128 129 138 public boolean supportsMultilineTokens() 139 { 140 return true; 141 } 142 143 150 public void insertLines(int index, int lines) 151 { 152 if(lines <= 0) 153 return; 154 length += lines; 155 ensureCapacity(length); 156 int len = index + lines; 157 System.arraycopy(lineInfo,index,lineInfo,len, 158 lineInfo.length - len); 159 160 for(int i = index + lines - 1; i >= index; i--) 161 { 162 lineInfo[i] = new LineInfo(); 163 } 164 } 165 166 173 public void deleteLines(int index, int lines) 174 { 175 if (lines <= 0) 176 return; 177 int len = index + lines; 178 length -= lines; 179 System.arraycopy(lineInfo,len,lineInfo, 180 index,lineInfo.length - len); 181 } 182 183 186 public int getLineCount() 187 { 188 return length; 189 } 190 191 196 public boolean isNextLineRequested() 197 { 198 return nextLineRequested; 199 } 200 201 203 207 protected Token firstToken; 208 209 213 protected Token lastToken; 214 215 220 protected LineInfo[] lineInfo; 221 222 226 protected int length; 227 228 231 protected int lastLine; 232 233 236 protected boolean nextLineRequested; 237 238 243 protected TokenMarker() 244 { 245 lastLine = -1; 246 } 247 248 259 protected void ensureCapacity(int index) 260 { 261 if(lineInfo == null) 262 lineInfo = new LineInfo[index + 1]; 263 else if(lineInfo.length <= index) 264 { 265 LineInfo[] lineInfoN = new LineInfo[(index + 1) * 2]; 266 System.arraycopy(lineInfo,0,lineInfoN,0, 267 lineInfo.length); 268 lineInfo = lineInfoN; 269 } 270 } 271 272 277 protected void addToken(int length, byte id) 278 { 279 if(id >= Token.INTERNAL_FIRST && id <= Token.INTERNAL_LAST) 280 throw new InternalError ("Invalid id: " + id); 281 282 if(length == 0 && id != Token.END) 283 return; 284 285 if(firstToken == null) 286 { 287 firstToken = new Token(length,id); 288 lastToken = firstToken; 289 } 290 else if(lastToken == null) 291 { 292 lastToken = firstToken; 293 firstToken.length = length; 294 firstToken.id = id; 295 } 296 else if(lastToken.next == null) 297 { 298 lastToken.next = new Token(length,id); 299 lastToken = lastToken.next; 300 } 301 else 302 { 303 lastToken = lastToken.next; 304 lastToken.length = length; 305 lastToken.id = id; 306 } 307 } 308 309 312 public class LineInfo 313 { 314 318 public LineInfo() 319 { 320 } 321 322 326 public LineInfo(byte token, Object obj) 327 { 328 this.token = token; 329 this.obj = obj; 330 } 331 332 335 public byte token; 336 337 343 public Object obj; 344 } 345 } 346 | Popular Tags |