1 2 57 package org.objectstyle.cayenne.exp.parser; 58 59 63 64 class JavaCharStream { 65 public static final boolean staticFlag = false; 66 static final int hexval(char c) throws java.io.IOException { 67 switch (c) { 68 case '0' : 69 return 0; 70 case '1' : 71 return 1; 72 case '2' : 73 return 2; 74 case '3' : 75 return 3; 76 case '4' : 77 return 4; 78 case '5' : 79 return 5; 80 case '6' : 81 return 6; 82 case '7' : 83 return 7; 84 case '8' : 85 return 8; 86 case '9' : 87 return 9; 88 89 case 'a' : 90 case 'A' : 91 return 10; 92 case 'b' : 93 case 'B' : 94 return 11; 95 case 'c' : 96 case 'C' : 97 return 12; 98 case 'd' : 99 case 'D' : 100 return 13; 101 case 'e' : 102 case 'E' : 103 return 14; 104 case 'f' : 105 case 'F' : 106 return 15; 107 } 108 109 throw new java.io.IOException (); } 111 112 public int bufpos = -1; 113 int bufsize; 114 int available; 115 int tokenBegin; 116 protected int bufline[]; 117 protected int bufcolumn[]; 118 119 protected int column = 0; 120 protected int line = 1; 121 122 protected boolean prevCharIsCR = false; 123 protected boolean prevCharIsLF = false; 124 125 protected java.io.Reader inputStream; 126 127 protected char[] nextCharBuf; 128 protected char[] buffer; 129 protected int maxNextCharInd = 0; 130 protected int nextCharInd = -1; 131 protected int inBuf = 0; 132 133 protected void ExpandBuff(boolean wrapAround) { 134 char[] newbuffer = new char[bufsize + 2048]; 135 int newbufline[] = new int[bufsize + 2048]; 136 int newbufcolumn[] = new int[bufsize + 2048]; 137 138 try { 139 if (wrapAround) { 140 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin); 141 System.arraycopy(buffer, 0, newbuffer, bufsize - tokenBegin, bufpos); 142 buffer = newbuffer; 143 144 System.arraycopy( 145 bufline, 146 tokenBegin, 147 newbufline, 148 0, 149 bufsize - tokenBegin); 150 System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos); 151 bufline = newbufline; 152 153 System.arraycopy( 154 bufcolumn, 155 tokenBegin, 156 newbufcolumn, 157 0, 158 bufsize - tokenBegin); 159 System.arraycopy( 160 bufcolumn, 161 0, 162 newbufcolumn, 163 bufsize - tokenBegin, 164 bufpos); 165 bufcolumn = newbufcolumn; 166 167 bufpos += (bufsize - tokenBegin); 168 } 169 else { 170 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin); 171 buffer = newbuffer; 172 173 System.arraycopy( 174 bufline, 175 tokenBegin, 176 newbufline, 177 0, 178 bufsize - tokenBegin); 179 bufline = newbufline; 180 181 System.arraycopy( 182 bufcolumn, 183 tokenBegin, 184 newbufcolumn, 185 0, 186 bufsize - tokenBegin); 187 bufcolumn = newbufcolumn; 188 189 bufpos -= tokenBegin; 190 } 191 } 192 catch (Throwable t) { 193 throw new Error (t.getMessage()); 194 } 195 196 available = (bufsize += 2048); 197 tokenBegin = 0; 198 } 199 200 protected void FillBuff() throws java.io.IOException { 201 int i; 202 if (maxNextCharInd == 4096) 203 maxNextCharInd = nextCharInd = 0; 204 205 try { 206 if ((i = 207 inputStream.read(nextCharBuf, maxNextCharInd, 4096 - maxNextCharInd)) 208 == -1) { 209 inputStream.close(); 210 throw new java.io.IOException (); 211 } 212 else 213 maxNextCharInd += i; 214 return; 215 } 216 catch (java.io.IOException e) { 217 if (bufpos != 0) { 218 --bufpos; 219 backup(0); 220 } 221 else { 222 bufline[bufpos] = line; 223 bufcolumn[bufpos] = column; 224 } 225 throw e; 226 } 227 } 228 229 protected char ReadByte() throws java.io.IOException { 230 if (++nextCharInd >= maxNextCharInd) 231 FillBuff(); 232 233 return nextCharBuf[nextCharInd]; 234 } 235 236 public char BeginToken() throws java.io.IOException { 237 if (inBuf > 0) { 238 --inBuf; 239 240 if (++bufpos == bufsize) 241 bufpos = 0; 242 243 tokenBegin = bufpos; 244 return buffer[bufpos]; 245 } 246 247 tokenBegin = 0; 248 bufpos = -1; 249 250 return readChar(); 251 } 252 253 protected void AdjustBuffSize() { 254 if (available == bufsize) { 255 if (tokenBegin > 2048) { 256 bufpos = 0; 257 available = tokenBegin; 258 } 259 else 260 ExpandBuff(false); 261 } 262 else if (available > tokenBegin) 263 available = bufsize; 264 else if ((tokenBegin - available) < 2048) 265 ExpandBuff(true); 266 else 267 available = tokenBegin; 268 } 269 270 protected void UpdateLineColumn(char c) { 271 column++; 272 273 if (prevCharIsLF) { 274 prevCharIsLF = false; 275 line += (column = 1); 276 } 277 else if (prevCharIsCR) { 278 prevCharIsCR = false; 279 if (c == '\n') { 280 prevCharIsLF = true; 281 } 282 else 283 line += (column = 1); 284 } 285 286 switch (c) { 287 case '\r' : 288 prevCharIsCR = true; 289 break; 290 case '\n' : 291 prevCharIsLF = true; 292 break; 293 case '\t' : 294 column--; 295 column += (8 - (column & 07)); 296 break; 297 default : 298 break; 299 } 300 301 bufline[bufpos] = line; 302 bufcolumn[bufpos] = column; 303 } 304 305 public char readChar() throws java.io.IOException { 306 if (inBuf > 0) { 307 --inBuf; 308 309 if (++bufpos == bufsize) 310 bufpos = 0; 311 312 return buffer[bufpos]; 313 } 314 315 char c; 316 317 if (++bufpos == available) 318 AdjustBuffSize(); 319 320 if ((buffer[bufpos] = c = ReadByte()) == '\\') { 321 UpdateLineColumn(c); 322 323 int backSlashCnt = 1; 324 325 for (;;) { 327 if (++bufpos == available) 328 AdjustBuffSize(); 329 330 try { 331 if ((buffer[bufpos] = c = ReadByte()) != '\\') { 332 UpdateLineColumn(c); 333 if ((c == 'u') && ((backSlashCnt & 1) == 1)) { 335 if (--bufpos < 0) 336 bufpos = bufsize - 1; 337 338 break; 339 } 340 341 backup(backSlashCnt); 342 return '\\'; 343 } 344 } 345 catch (java.io.IOException e) { 346 if (backSlashCnt > 1) 347 backup(backSlashCnt); 348 349 return '\\'; 350 } 351 352 UpdateLineColumn(c); 353 backSlashCnt++; 354 } 355 356 try { 358 while ((c = ReadByte()) == 'u') 359 ++column; 360 361 buffer[bufpos] = 362 c = 363 (char) (hexval(c) 364 << 12 | hexval(ReadByte()) 365 << 8 | hexval(ReadByte()) 366 << 4 | hexval(ReadByte())); 367 368 column += 4; 369 } 370 catch (java.io.IOException e) { 371 throw new Error ( 372 "Invalid escape character at line " 373 + line 374 + " column " 375 + column 376 + "."); 377 } 378 379 if (backSlashCnt == 1) 380 return c; 381 else { 382 backup(backSlashCnt - 1); 383 return '\\'; 384 } 385 } 386 else { 387 UpdateLineColumn(c); 388 return (c); 389 } 390 } 391 392 396 397 public int getColumn() { 398 return bufcolumn[bufpos]; 399 } 400 401 405 406 public int getLine() { 407 return bufline[bufpos]; 408 } 409 410 public int getEndColumn() { 411 return bufcolumn[bufpos]; 412 } 413 414 public int getEndLine() { 415 return bufline[bufpos]; 416 } 417 418 public int getBeginColumn() { 419 return bufcolumn[tokenBegin]; 420 } 421 422 public int getBeginLine() { 423 return bufline[tokenBegin]; 424 } 425 426 public void backup(int amount) { 427 428 inBuf += amount; 429 if ((bufpos -= amount) < 0) 430 bufpos += bufsize; 431 } 432 433 public JavaCharStream( 434 java.io.Reader dstream, 435 int startline, 436 int startcolumn, 437 int buffersize) { 438 inputStream = dstream; 439 line = startline; 440 column = startcolumn - 1; 441 442 available = bufsize = buffersize; 443 buffer = new char[buffersize]; 444 bufline = new int[buffersize]; 445 bufcolumn = new int[buffersize]; 446 nextCharBuf = new char[4096]; 447 } 448 449 public JavaCharStream(java.io.Reader dstream, int startline, int startcolumn) { 450 this(dstream, startline, startcolumn, 4096); 451 } 452 453 public JavaCharStream(java.io.Reader dstream) { 454 this(dstream, 1, 1, 4096); 455 } 456 public void ReInit( 457 java.io.Reader dstream, 458 int startline, 459 int startcolumn, 460 int buffersize) { 461 inputStream = dstream; 462 line = startline; 463 column = startcolumn - 1; 464 465 if (buffer == null || buffersize != buffer.length) { 466 available = bufsize = buffersize; 467 buffer = new char[buffersize]; 468 bufline = new int[buffersize]; 469 bufcolumn = new int[buffersize]; 470 nextCharBuf = new char[4096]; 471 } 472 prevCharIsLF = prevCharIsCR = false; 473 tokenBegin = inBuf = maxNextCharInd = 0; 474 nextCharInd = bufpos = -1; 475 } 476 477 public void ReInit(java.io.Reader dstream, int startline, int startcolumn) { 478 ReInit(dstream, startline, startcolumn, 4096); 479 } 480 481 public void ReInit(java.io.Reader dstream) { 482 ReInit(dstream, 1, 1, 4096); 483 } 484 public JavaCharStream( 485 java.io.InputStream dstream, 486 int startline, 487 int startcolumn, 488 int buffersize) { 489 this(new java.io.InputStreamReader (dstream), startline, startcolumn, 4096); 490 } 491 492 public JavaCharStream(java.io.InputStream dstream, int startline, int startcolumn) { 493 this(dstream, startline, startcolumn, 4096); 494 } 495 496 public JavaCharStream(java.io.InputStream dstream) { 497 this(dstream, 1, 1, 4096); 498 } 499 500 public void ReInit( 501 java.io.InputStream dstream, 502 int startline, 503 int startcolumn, 504 int buffersize) { 505 ReInit(new java.io.InputStreamReader (dstream), startline, startcolumn, 4096); 506 } 507 public void ReInit(java.io.InputStream dstream, int startline, int startcolumn) { 508 ReInit(dstream, startline, startcolumn, 4096); 509 } 510 public void ReInit(java.io.InputStream dstream) { 511 ReInit(dstream, 1, 1, 4096); 512 } 513 514 public String GetImage() { 515 if (bufpos >= tokenBegin) 516 return new String (buffer, tokenBegin, bufpos - tokenBegin + 1); 517 else 518 return new String (buffer, tokenBegin, bufsize - tokenBegin) 519 + new String (buffer, 0, bufpos + 1); 520 } 521 522 public char[] GetSuffix(int len) { 523 char[] ret = new char[len]; 524 525 if ((bufpos + 1) >= len) 526 System.arraycopy(buffer, bufpos - len + 1, ret, 0, len); 527 else { 528 System.arraycopy( 529 buffer, 530 bufsize - (len - bufpos - 1), 531 ret, 532 0, 533 len - bufpos - 1); 534 System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1); 535 } 536 537 return ret; 538 } 539 540 public void Done() { 541 nextCharBuf = null; 542 buffer = null; 543 bufline = null; 544 bufcolumn = null; 545 } 546 547 550 public void adjustBeginLineColumn(int newLine, int newCol) { 551 int start = tokenBegin; 552 int len; 553 554 if (bufpos >= tokenBegin) { 555 len = bufpos - tokenBegin + inBuf + 1; 556 } 557 else { 558 len = bufsize - tokenBegin + bufpos + 1 + inBuf; 559 } 560 561 int i = 0, j = 0, k = 0; 562 int nextColDiff = 0, columnDiff = 0; 563 564 while (i < len && bufline[j = 565 start % bufsize] == bufline[k = ++start % bufsize]) { 566 bufline[j] = newLine; 567 nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j]; 568 bufcolumn[j] = newCol + columnDiff; 569 columnDiff = nextColDiff; 570 i++; 571 } 572 573 if (i < len) { 574 bufline[j] = newLine++; 575 bufcolumn[j] = newCol + columnDiff; 576 577 while (i++ < len) { 578 if (bufline[j = start % bufsize] != bufline[++start % bufsize]) 579 bufline[j] = newLine++; 580 else 581 bufline[j] = newLine; 582 } 583 } 584 585 line = bufline[j]; 586 column = bufcolumn[j]; 587 } 588 589 } 590 | Popular Tags |