1 21 22 23 package org.apache.derby.impl.sql.compile; 24 25 29 30 33 public final class UCode_CharStream implements CharStream 34 { 35 int charCnt; 38 int[] charOffset; 39 40 public static final boolean staticFlag = false; 41 public int bufpos = -1; 42 int bufsize; 43 int available; 44 int tokenBegin; 45 private int bufline[]; 46 private int bufcolumn[]; 47 48 private int column = 0; 49 private int line = 1; 50 51 private boolean prevCharIsCR = false; 52 private boolean prevCharIsLF = false; 53 54 private java.io.Reader inputStream; 55 56 private char[] nextCharBuf; 57 private char[] buffer; 58 private int maxNextCharInd = 0; 59 private int nextCharInd = -1; 60 61 private final void ExpandBuff(boolean wrapAround) 62 { 63 char[] newbuffer = new char[bufsize + 2048]; 64 int newbufline[] = new int[bufsize + 2048]; 65 int newbufcolumn[] = new int[bufsize + 2048]; 66 67 int newcharOffset[] = new int[bufsize + 2048]; 70 71 try 72 { 73 if (wrapAround) 74 { 75 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin); 76 System.arraycopy(buffer, 0, newbuffer, 77 bufsize - tokenBegin, bufpos); 78 buffer = newbuffer; 79 80 System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin); 81 System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos); 82 bufline = newbufline; 83 84 System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin); 85 System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos); 86 bufcolumn = newbufcolumn; 87 88 System.arraycopy(charOffset, tokenBegin, newcharOffset, 0, bufsize - tokenBegin); 91 System.arraycopy(charOffset, 0, newcharOffset, bufsize - tokenBegin, bufpos); 92 charOffset = newcharOffset; 93 94 bufpos += (bufsize - tokenBegin); 95 } 96 else 97 { 98 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin); 99 buffer = newbuffer; 100 101 System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin); 102 bufline = newbufline; 103 104 System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin); 105 bufcolumn = newbufcolumn; 106 107 System.arraycopy(charOffset, tokenBegin, newcharOffset, 0, bufsize - tokenBegin); 110 charOffset = newcharOffset; 111 112 bufpos -= tokenBegin; 113 } 114 } 115 catch (Throwable t) 116 { 117 throw new Error (t.getMessage()); 118 } 119 120 available = (bufsize += 2048); 121 tokenBegin = 0; 122 } 123 124 private final void FillBuff() throws java.io.IOException 125 { 126 if (maxNextCharInd == nextCharBuf.length) 127 maxNextCharInd = nextCharInd = 0; 128 129 int i; 130 try { 131 if ((i = inputStream.read(nextCharBuf, maxNextCharInd, 132 nextCharBuf.length - maxNextCharInd)) == -1) 133 { 134 inputStream.close(); 135 throw new java.io.IOException (); 136 } 137 else 138 maxNextCharInd += i; 139 return; 140 } 141 catch(java.io.IOException e) { 142 if (bufpos != 0) 143 { 144 --bufpos; 145 backup(0); 146 } 147 else 148 { 149 bufline[bufpos] = line; 150 bufcolumn[bufpos] = column; 151 } 152 if (tokenBegin == -1) 153 tokenBegin = bufpos; 154 throw e; 155 } 156 } 157 158 private final char ReadChar() throws java.io.IOException 159 { 160 if (++nextCharInd >= maxNextCharInd) 161 FillBuff(); 162 163 return nextCharBuf[nextCharInd]; 164 } 165 166 public char BeginToken() throws java.io.IOException 167 { 168 if (inBuf > 0) 169 { 170 --inBuf; 171 return buffer[tokenBegin = (bufpos == bufsize - 1) ? (bufpos = 0) 172 : ++bufpos]; 173 } 174 175 tokenBegin = 0; 176 bufpos = -1; 177 char c = readChar(); 178 179 return c; 180 } 181 182 private final void UpdateLineColumn(char c) 183 { 184 column++; 185 186 if (prevCharIsLF) 187 { 188 prevCharIsLF = false; 189 line += (column = 1); 190 } 191 else if (prevCharIsCR) 192 { 193 prevCharIsCR = false; 194 if (c == '\n') 195 { 196 prevCharIsLF = true; 197 } 198 else 199 line += (column = 1); 200 } 201 202 switch (c) 203 { 204 case '\r' : 205 prevCharIsCR = true; 206 break; 207 case '\n' : 208 prevCharIsLF = true; 209 break; 210 case '\t' : 211 column--; 212 column += (8 - (column & 07)); 213 break; 214 default : 215 break; 216 } 217 218 bufline[bufpos] = line; 219 bufcolumn[bufpos] = column; 220 } 221 222 private int inBuf = 0; 223 public final char readChar() throws java.io.IOException 224 { 225 if (inBuf > 0) 226 { 227 --inBuf; 228 return (char)buffer[(bufpos == bufsize - 1) ? (bufpos = 0) : ++bufpos]; 229 } 230 231 if (++bufpos == available) 232 { 233 if (available == bufsize) 234 { 235 if (tokenBegin > 2048) 236 { 237 bufpos = 0; 238 available = tokenBegin; 239 } 240 else if (tokenBegin < 0) 241 bufpos = 0; 242 else 243 ExpandBuff(false); 244 } 245 else if (available > tokenBegin) 246 available = bufsize; 247 else if ((tokenBegin - available) < 2048) 248 ExpandBuff(true); 249 else 250 available = tokenBegin; 251 } 252 253 char c = ReadChar(); 254 255 UpdateLineColumn(c); 256 257 charOffset[bufpos] = charCnt++; 260 261 return (buffer[bufpos] = c); 262 } 263 264 268 269 public final int getColumn() { 270 return bufcolumn[bufpos]; 271 } 272 273 277 278 public final int getLine() { 279 return bufline[bufpos]; 280 } 281 282 public final int getEndColumn() { 283 return bufcolumn[bufpos]; 284 } 285 286 public final int getEndLine() { 287 return bufline[bufpos]; 288 } 289 290 public final int getBeginColumn() { 291 return bufcolumn[tokenBegin]; 292 } 293 294 public final int getBeginLine() { 295 return bufline[tokenBegin]; 296 } 297 298 public final int getBeginOffset() { 301 return charOffset[tokenBegin]; 302 } 303 304 public final int getEndOffset() { 307 return charOffset[bufpos]; 308 } 309 310 public final void backup(int amount) { 311 312 inBuf += amount; 313 if ((bufpos -= amount) < 0) 314 bufpos += bufsize; 315 } 316 317 public UCode_CharStream(java.io.Reader dstream, 318 int startline, int startcolumn, int buffersize) 319 { 320 inputStream = dstream; 321 line = startline; 322 column = startcolumn - 1; 323 324 available = bufsize = buffersize; 325 buffer = new char[buffersize]; 326 nextCharBuf = new char[buffersize]; 327 bufline = new int[buffersize]; 328 bufcolumn = new int[buffersize]; 329 330 charOffset = new int[buffersize]; 333 } 334 335 public UCode_CharStream(java.io.Reader dstream, 336 int startline, int startcolumn) 337 { 338 this(dstream, startline, startcolumn, 4096); 339 } 340 341 public void ReInit(java.io.Reader dstream, 342 int startline, int startcolumn, int buffersize) 343 { 344 inputStream = dstream; 345 line = startline; 346 column = startcolumn - 1; 347 348 if (buffer == null || buffersize != buffer.length) 349 { 350 available = bufsize = buffersize; 351 buffer = new char[buffersize]; 352 nextCharBuf = new char[buffersize]; 353 bufline = new int[buffersize]; 354 bufcolumn = new int[buffersize]; 355 } 356 357 inBuf = maxNextCharInd = charCnt = tokenBegin = 0; 360 nextCharInd = bufpos = -1; 361 } 362 363 public void ReInit(java.io.Reader dstream, 364 int startline, int startcolumn) 365 { 366 ReInit(dstream, startline, startcolumn, 4096); 367 } 368 public UCode_CharStream(java.io.InputStream dstream, int startline, 369 int startcolumn, int buffersize) 370 { 371 this(new java.io.InputStreamReader (dstream), startline, startcolumn, 4096); 372 } 373 374 public UCode_CharStream(java.io.InputStream dstream, int startline, 375 int startcolumn) 376 { 377 this(dstream, startline, startcolumn, 4096); 378 } 379 380 public void ReInit(java.io.InputStream dstream, int startline, 381 int startcolumn, int buffersize) 382 { 383 ReInit(new java.io.InputStreamReader (dstream), startline, startcolumn, 4096); 384 } 385 public void ReInit(java.io.InputStream dstream, int startline, 386 int startcolumn) 387 { 388 ReInit(dstream, startline, startcolumn, 4096); 389 } 390 391 public final String GetImage() 392 { 393 if (bufpos >= tokenBegin) 394 return new String (buffer, tokenBegin, bufpos - tokenBegin + 1); 395 else 396 return new String (buffer, tokenBegin, bufsize - tokenBegin) + 397 new String (buffer, 0, bufpos + 1); 398 } 399 400 public final char[] GetSuffix(int len) 401 { 402 char[] ret = new char[len]; 403 404 if ((bufpos + 1) >= len) 405 System.arraycopy(buffer, bufpos - len + 1, ret, 0, len); 406 else 407 { 408 System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0, 409 len - bufpos - 1); 410 System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1); 411 } 412 413 return ret; 414 } 415 416 public void Done() 417 { 418 nextCharBuf = null; 419 buffer = null; 420 bufline = null; 421 bufcolumn = null; 422 423 charOffset = null; 426 } 427 428 431 public void adjustBeginLineColumn(int newLine, int newCol) 432 { 433 int start = tokenBegin; 434 int len; 435 436 if (bufpos >= tokenBegin) 437 { 438 len = bufpos - tokenBegin + inBuf + 1; 439 } 440 else 441 { 442 len = bufsize - tokenBegin + bufpos + 1 + inBuf; 443 } 444 445 int i = 0, j = 0, k = 0; 446 int nextColDiff = 0, columnDiff = 0; 447 448 while (i < len && 449 bufline[j = start % bufsize] == bufline[k = ++start % bufsize]) 450 { 451 bufline[j] = newLine; 452 nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j]; 453 bufcolumn[j] = newCol + columnDiff; 454 columnDiff = nextColDiff; 455 i++; 456 } 457 458 if (i < len) 459 { 460 bufline[j] = newLine++; 461 bufcolumn[j] = newCol + columnDiff; 462 463 while (i++ < len) 464 { 465 if (bufline[j = start % bufsize] != bufline[++start % bufsize]) 466 bufline[j] = newLine++; 467 else 468 bufline[j] = newLine; 469 } 470 } 471 472 line = bufline[j]; 473 column = bufcolumn[j]; 474 } 475 476 } 477 | Popular Tags |