1 19 20 package org.netbeans.modules.lexer.demo; 21 22 import org.netbeans.api.lexer.Token; 23 import org.netbeans.api.lexer.TokenId; 24 import org.netbeans.spi.lexer.inc.RawOffsetToken; 25 import org.netbeans.spi.lexer.util.IntegerCache; 26 27 33 34 public class DemoToken implements RawOffsetToken, CharSequence { 35 36 private final DemoTokenUpdater tokenUpdater; 37 38 private final TokenId id; 39 40 private int rawOffset; 41 42 private final int length; 43 44 private int lookahead; 45 46 private int lookback; 47 48 private Object state; 49 50 51 DemoToken(DemoTokenUpdater tokenUpdater, TokenId id, int rawOffset, int length) { 52 if (tokenUpdater == null) { 53 throw new NullPointerException (); 54 } 55 56 if (id == null) { 57 throw new NullPointerException (); 58 } 59 60 if (rawOffset < 0) { 61 throw new IllegalArgumentException ("rawOffset=" + rawOffset); 62 } 63 64 if (length < 0) { 65 throw new IllegalArgumentException ("length=" + length); 66 } 67 68 this.tokenUpdater = tokenUpdater; 69 this.id = id; 70 this.rawOffset = rawOffset; 71 this.length = length; 72 } 73 74 protected final DemoTokenUpdater getTokenUpdater() { 75 return tokenUpdater; 76 } 77 78 public TokenId getId() { 79 return id; 80 } 81 82 public CharSequence getText() { 83 return this; 84 } 85 86 public int getOffset() { 87 return tokenUpdater.getOffset(rawOffset); 88 } 89 90 public int getRawOffset() { 91 return rawOffset; 92 } 93 94 public void setRawOffset(int rawOffset) { 95 this.rawOffset = rawOffset; 96 } 97 98 public void updateRawOffset(int diff) { 99 rawOffset += diff; 100 } 101 102 public int length() { 103 return length; 104 } 105 106 public char charAt(int index) { 107 if (index < 0 || index >= length) { 108 throw new IllegalStateException ("index=" + index + ", length=" + length); 109 } 110 111 return tokenUpdater.charAt(rawOffset, index); 112 } 113 114 public CharSequence subSequence(int start, int end) { 115 if (start < 0 || end < 0 || start > end || end > length()) { 116 throw new IndexOutOfBoundsException ( 117 "start=" + start + ", end=" + end + ", length()=" + length()); 118 } 119 120 return (CharSequence )(Object )toString().substring(start, end); } 122 123 public int getLookahead() { 124 return lookahead; 125 } 126 127 void setLookahead(int lookahead) { 128 this.lookahead = lookahead; 129 } 130 131 public int getLookback() { 132 return lookback; 133 } 134 135 void setLookback(int lookback) { 136 this.lookback = lookback; 137 } 138 139 public Object getState() { 140 return state; 141 } 142 143 void setState(Object state) { 144 this.state = state; 145 } 146 147 public String toString() { 148 StringBuffer sb = new StringBuffer (); 149 for (int i = 0; i < length(); i++) { 150 sb.append(charAt(i)); 151 } 152 return sb.toString(); 153 } 154 155 } 156 157 | Popular Tags |