1 19 20 package org.netbeans.editor; 21 22 82 83 public class Syntax { 84 85 86 public static final int EQUAL_STATE = 0; 87 88 89 public static final int DIFFERENT_STATE = 1; 90 91 92 93 public static final int INIT = -1; 94 95 96 97 101 protected int state = INIT; 102 103 104 protected char buffer[]; 105 106 107 protected int offset; 108 109 110 protected int tokenOffset; 111 112 113 protected int tokenLength; 114 115 121 protected TokenContextPath tokenContextPath; 122 123 135 protected boolean lastBuffer; 136 137 138 protected int stopOffset; 139 140 148 protected int stopPosition; 149 150 163 protected TokenID supposedTokenID; 164 165 170 public TokenID nextToken() { 171 if (tokenOffset >= stopOffset) { 173 tokenLength = 0; 174 return null; } 176 177 supposedTokenID = null; 179 TokenID tokenID = parseToken(); 180 if (tokenID != null) { tokenLength = offset - tokenOffset; 182 tokenOffset = offset; if (tokenLength == 0) { return nextToken(); } 186 } else { tokenLength = 0; 188 } 189 190 return tokenID; 191 } 192 193 202 protected TokenID parseToken() { 203 return null; 204 } 205 206 233 public void load(StateInfo stateInfo, char buffer[], int offset, int len, 234 boolean lastBuffer, int stopPosition) { 235 this.buffer = buffer; 236 this.offset = offset; 237 this.tokenOffset = offset; 238 this.stopOffset = offset + len; 239 this.lastBuffer = lastBuffer; 240 this.stopPosition = stopPosition; 241 242 if (stateInfo != null) { 243 loadState(stateInfo); 244 } else { 245 loadInitState(); 246 } 247 } 248 249 280 public void relocate(char buffer[], int offset, int len, 281 boolean lastBuffer, int stopPosition) { 282 this.buffer = buffer; 283 this.lastBuffer = lastBuffer; 284 285 int delta = offset - this.offset; this.offset += delta; 287 this.tokenOffset += delta; 288 this.stopOffset = offset + len; 289 this.stopPosition = stopPosition; 290 } 291 292 293 public char[] getBuffer() { 294 return buffer; 295 } 296 297 298 public int getOffset() { 299 return offset; 300 } 301 302 303 public int getTokenOffset() { 304 return offset - tokenLength; 305 } 306 307 308 public int getTokenLength() { 309 return tokenLength; 310 } 311 312 313 public TokenContextPath getTokenContextPath() { 314 return tokenContextPath; 315 } 316 317 public TokenID getSupposedTokenID() { 318 return supposedTokenID; 319 } 320 321 334 public int getPreScan() { 335 return offset - tokenOffset; 336 } 337 338 343 public void loadInitState() { 344 state = INIT; 345 } 346 347 public void reset() { 348 tokenLength = stopOffset = tokenOffset = offset = 0; 349 loadInitState(); 350 } 351 352 358 public void loadState(StateInfo stateInfo) { 359 state = stateInfo.getState(); 360 tokenOffset -= stateInfo.getPreScan(); 361 } 362 363 364 public void storeState(StateInfo stateInfo) { 365 stateInfo.setState(state); 366 stateInfo.setPreScan(getPreScan()); 367 } 368 369 370 public int compareState(StateInfo stateInfo) { 371 if (stateInfo != null) { 372 return ((stateInfo.getState() == state) && stateInfo.getPreScan() == getPreScan()) 373 ? EQUAL_STATE : DIFFERENT_STATE; 374 } else { 375 return DIFFERENT_STATE; 376 } 377 } 378 379 380 public StateInfo createStateInfo() { 381 return new BaseStateInfo(); 382 } 383 384 389 public String getStateName(int stateNumber) { 390 switch(stateNumber) { 391 case INIT: 392 return "INIT"; 394 default: 395 return "Unknown state " + stateNumber; } 397 } 398 399 400 public String toString() { 401 return "tokenOffset=" + tokenOffset + ", offset=" + offset + ", state=" + getStateName(state) + ", stopOffset=" + stopOffset + ", lastBuffer=" + lastBuffer; } 407 408 409 412 public interface StateInfo { 413 414 415 public int getState(); 416 417 418 public void setState(int state); 419 420 421 public int getPreScan(); 422 423 424 public void setPreScan(int preScan); 425 426 } 427 428 429 430 public static class BaseStateInfo implements StateInfo { 431 432 433 private int state; 434 435 436 private int preScan; 437 438 public int getState() { 439 return state; 440 } 441 442 public void setState(int state) { 443 this.state = state; 444 } 445 446 public int getPreScan() { 447 return preScan; 448 } 449 450 public void setPreScan(int preScan) { 451 this.preScan = preScan; 452 } 453 454 public String toString(Syntax syntax) { 455 return "state=" + ((syntax != null) 457 ? syntax.getStateName(getState()) 458 : Integer.toString(getState())) 459 + ", preScan=" + getPreScan(); } 461 462 public String toString() { 463 return toString(null); 464 } 465 466 } 467 468 } 469 | Popular Tags |