1 19 20 package org.netbeans.spi.lexer; 21 22 import org.netbeans.lib.editor.util.AbstractCharSequence; 23 import org.netbeans.lib.lexer.CharProvider; 24 import org.netbeans.lib.lexer.IntegerCache; 25 import org.netbeans.lib.lexer.LexerUtilsConstants; 26 27 48 49 public final class LexerInput { 50 51 60 public static final int EOF = -1; 61 62 66 private CharProvider charProvider; 67 68 73 private ReadText readText; 74 75 78 private int eof; 79 80 85 LexerInput(CharProvider charProvider) { 86 this.charProvider = charProvider; 87 } 88 89 97 public int read() { 98 int c = charProvider.read(); 99 if (c == EOF) { 100 eof = 1; 101 } 102 return c; 103 } 104 105 130 public void backup(int count) { 131 if (count < 0) { 132 throw new IndexOutOfBoundsException ("count=" + count + " <0"); } 134 LexerUtilsConstants.checkValidBackup(count, readLengthEOF()); 136 if (eof != 0) { 137 eof = 0; count--; 139 } 140 charProvider.backup(count); 141 } 142 143 159 public int readLength() { 160 return charProvider.readIndex(); 161 } 162 163 167 public int readLengthEOF() { 168 return readLength() + eof; 169 } 170 171 228 public CharSequence readText(int start, int end) { 229 assert (start >= 0 && end >= start && end <= readLength()) 230 : "start=" + start + ", end=" + end + ", readLength()=" + readLength(); 232 if (readText == null) { 233 readText = new ReadText(); 234 } 235 readText.reinit(start, end); 236 return readText; 237 } 238 239 243 public CharSequence readText() { 244 return readText(0, readLength()); 245 } 246 247 269 public boolean consumeNewline() { 270 if (read() == '\n') { 271 return true; 272 } else { 273 backup(1); 274 return false; 275 } 276 } 277 278 288 public static Integer integerState(int state) { 289 return IntegerCache.integer(state); 290 } 291 292 295 private final class ReadText extends AbstractCharSequence.StringLike { 296 297 private int start; 298 299 private int length; 300 301 private void reinit(int start, int end) { 302 this.start = start; 303 this.length = (end - start); 304 } 305 306 public int length() { 307 return length; 308 } 309 310 public char charAt(int index) { 311 if (index < 0 || index >= length) { 312 throw new IndexOutOfBoundsException ("index=" + index + ", length=" + length); } 314 return charProvider.readExisting(index); 315 } 316 317 } 318 319 } 320 | Popular Tags |