1 30 31 package org.syntax.jedit.tokenmarker; 32 33 import org.syntax.jedit.*; 34 35 import javax.swing.text.Segment ; 36 import java.util.*; 37 38 53 public abstract class TokenMarker 54 { 55 61 public Token markTokens(Segment line, int lineIndex) 62 { 63 if(lineIndex >= length) 64 { 65 throw new IllegalArgumentException ("Tokenizing invalid line: " 66 + lineIndex); 67 } 68 69 lastToken = null; 70 71 LineInfo info = lineInfo[lineIndex]; 72 LineInfo prev; 73 if(lineIndex == 0) 74 prev = null; 75 else 76 prev = lineInfo[lineIndex - 1]; 77 78 byte oldToken = info.token; 79 byte token = markTokensImpl(prev == null ? 80 Token.NULL : prev.token,line,lineIndex); 81 82 info.token = token; 83 84 122 if(!(lastLine == lineIndex && nextLineRequested)) 123 nextLineRequested = (oldToken != token); 124 125 lastLine = lineIndex; 126 127 addToken(0,Token.END); 128 129 return firstToken; 130 } 131 132 149 protected abstract byte markTokensImpl(byte token, Segment line, 150 int lineIndex); 151 152 161 public boolean supportsMultilineTokens() 162 { 163 return true; 164 } 165 166 173 public void insertLines(int index, int lines) 174 { 175 if(lines <= 0) 176 return; 177 length += lines; 178 ensureCapacity(length); 179 int len = index + lines; 180 System.arraycopy(lineInfo,index,lineInfo,len, 181 lineInfo.length - len); 182 183 for(int i = index + lines - 1; i >= index; i--) 184 { 185 lineInfo[i] = new LineInfo(); 186 } 187 } 188 189 196 public void deleteLines(int index, int lines) 197 { 198 if (lines <= 0) 199 return; 200 int len = index + lines; 201 length -= lines; 202 System.arraycopy(lineInfo,len,lineInfo, 203 index,lineInfo.length - len); 204 } 205 206 209 public int getLineCount() 210 { 211 return length; 212 } 213 214 219 public boolean isNextLineRequested() 220 { 221 return nextLineRequested; 222 } 223 224 226 230 protected Token firstToken; 231 232 236 protected Token lastToken; 237 238 243 protected LineInfo[] lineInfo; 244 245 249 protected int length; 250 251 254 protected int lastLine; 255 256 259 protected boolean nextLineRequested; 260 261 protected KeywordLookupIF keywordLookup; 262 267 protected TokenMarker() 268 { 269 lastLine = -1; 270 } 271 272 283 protected void ensureCapacity(int index) 284 { 285 if(lineInfo == null) 286 lineInfo = new LineInfo[index + 1]; 287 else if(lineInfo.length <= index) 288 { 289 LineInfo[] lineInfoN = new LineInfo[(index + 1) * 2]; 290 System.arraycopy(lineInfo,0,lineInfoN,0, 291 lineInfo.length); 292 lineInfo = lineInfoN; 293 } 294 } 295 296 301 protected void addToken(int length, byte id) 302 { 303 if(id >= Token.INTERNAL_FIRST && id <= Token.INTERNAL_LAST) 304 throw new InternalError ("Invalid id: " + id); 305 306 if(length == 0 && id != Token.END) 307 return; 308 309 if(firstToken == null) 310 { 311 firstToken = new Token(length,id); 312 lastToken = firstToken; 313 } 314 else if(lastToken == null) 315 { 316 lastToken = firstToken; 317 firstToken.length = length; 318 firstToken.id = id; 319 } 320 else if(lastToken.next == null) 321 { 322 lastToken.next = new Token(length,id); 323 lastToken = lastToken.next; 324 } 325 else 326 { 327 lastToken = lastToken.next; 328 lastToken.length = length; 329 lastToken.id = id; 330 } 331 } 332 333 336 public class LineInfo 337 { 338 342 public LineInfo() 343 { 344 } 345 346 350 public LineInfo(byte token, Object obj) 351 { 352 this.token = token; 353 this.obj = obj; 354 } 355 356 359 public byte token; 360 361 367 public Object obj; 368 } 369 370 372 public void setKeywordLookup(KeywordLookupIF keywordLookup) 373 { 374 this.keywordLookup = keywordLookup; 375 } 376 } 377 | Popular Tags |