001package jtb; 002 003/* Generated By:JavaCC: Do not edit this line. JavaCharStream.java Version 3.0 */ 004/** 005 * An implementation of interface CharStream, where the stream is assumed to 006 * contain only ASCII characters (with java-like unicode escape processing). 007 */ 008 009public class JavaCharStream 010{ 011 public static final boolean staticFlag = false; 012 static final int hexval(char c) throws java.io.IOException { 013 switch(c) 014 { 015 case '0' : 016 return 0; 017 case '1' : 018 return 1; 019 case '2' : 020 return 2; 021 case '3' : 022 return 3; 023 case '4' : 024 return 4; 025 case '5' : 026 return 5; 027 case '6' : 028 return 6; 029 case '7' : 030 return 7; 031 case '8' : 032 return 8; 033 case '9' : 034 return 9; 035 036 case 'a' : 037 case 'A' : 038 return 10; 039 case 'b' : 040 case 'B' : 041 return 11; 042 case 'c' : 043 case 'C' : 044 return 12; 045 case 'd' : 046 case 'D' : 047 return 13; 048 case 'e' : 049 case 'E' : 050 return 14; 051 case 'f' : 052 case 'F' : 053 return 15; 054 } 055 056 throw new java.io.IOException(); // Should never come here 057 } 058 059 public int bufpos = -1; 060 int bufsize; 061 int available; 062 int tokenBegin; 063 protected int bufline[]; 064 protected int bufcolumn[]; 065 066 protected int column = 0; 067 protected int line = 1; 068 069 protected boolean prevCharIsCR = false; 070 protected boolean prevCharIsLF = false; 071 072 protected java.io.Reader inputStream; 073 074 protected char[] nextCharBuf; 075 protected char[] buffer; 076 protected int maxNextCharInd = 0; 077 protected int nextCharInd = -1; 078 protected int inBuf = 0; 079 080 protected void ExpandBuff(boolean wrapAround) 081 { 082 char[] newbuffer = new char[bufsize + 2048]; 083 int newbufline[] = new int[bufsize + 2048]; 084 int newbufcolumn[] = new int[bufsize + 2048]; 085 086 try 087 { 088 if (wrapAround) 089 { 090 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin); 091 System.arraycopy(buffer, 0, newbuffer, 092 bufsize - tokenBegin, bufpos); 093 buffer = newbuffer; 094 095 System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin); 096 System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos); 097 bufline = newbufline; 098 099 System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin); 100 System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos); 101 bufcolumn = newbufcolumn; 102 103 bufpos += (bufsize - tokenBegin); 104 } 105 else 106 { 107 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin); 108 buffer = newbuffer; 109 110 System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin); 111 bufline = newbufline; 112 113 System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin); 114 bufcolumn = newbufcolumn; 115 116 bufpos -= tokenBegin; 117 } 118 } 119 catch (Throwable t) 120 { 121 throw new Error(t.getMessage()); 122 } 123 124 available = (bufsize += 2048); 125 tokenBegin = 0; 126 } 127 128 protected void FillBuff() throws java.io.IOException 129 { 130 int i; 131 if (maxNextCharInd == 4096) 132 maxNextCharInd = nextCharInd = 0; 133 134 try { 135 if ((i = inputStream.read(nextCharBuf, maxNextCharInd, 136 4096 - maxNextCharInd)) == -1) 137 { 138 inputStream.close(); 139 throw new java.io.IOException(); 140 } 141 else 142 maxNextCharInd += i; 143 return; 144 } 145 catch(java.io.IOException e) { 146 if (bufpos != 0) 147 { 148 --bufpos; 149 backup(0); 150 } 151 else 152 { 153 bufline[bufpos] = line; 154 bufcolumn[bufpos] = column; 155 } 156 throw e; 157 } 158 } 159 160 protected char ReadByte() throws java.io.IOException 161 { 162 if (++nextCharInd >= maxNextCharInd) 163 FillBuff(); 164 165 return nextCharBuf[nextCharInd]; 166 } 167 168 public char BeginToken() throws java.io.IOException 169 { 170 if (inBuf > 0) 171 { 172 --inBuf; 173 174 if (++bufpos == bufsize) 175 bufpos = 0; 176 177 tokenBegin = bufpos; 178 return buffer[bufpos]; 179 } 180 181 tokenBegin = 0; 182 bufpos = -1; 183 184 return readChar(); 185 } 186 187 protected void AdjustBuffSize() 188 { 189 if (available == bufsize) 190 { 191 if (tokenBegin > 2048) 192 { 193 bufpos = 0; 194 available = tokenBegin; 195 } 196 else 197 ExpandBuff(false); 198 } 199 else if (available > tokenBegin) 200 available = bufsize; 201 else if ((tokenBegin - available) < 2048) 202 ExpandBuff(true); 203 else 204 available = tokenBegin; 205 } 206 207 protected void UpdateLineColumn(char c) 208 { 209 column++; 210 211 if (prevCharIsLF) 212 { 213 prevCharIsLF = false; 214 line += (column = 1); 215 } 216 else if (prevCharIsCR) 217 { 218 prevCharIsCR = false; 219 if (c == '\n') 220 { 221 prevCharIsLF = true; 222 } 223 else 224 line += (column = 1); 225 } 226 227 switch (c) 228 { 229 case '\r' : 230 prevCharIsCR = true; 231 break; 232 case '\n' : 233 prevCharIsLF = true; 234 break; 235 case '\t' : 236 column--; 237 column += (8 - (column & 07)); 238 break; 239 default : 240 break; 241 } 242 243 bufline[bufpos] = line; 244 bufcolumn[bufpos] = column; 245 } 246 247 public char readChar() throws java.io.IOException 248 { 249 if (inBuf > 0) 250 { 251 --inBuf; 252 253 if (++bufpos == bufsize) 254 bufpos = 0; 255 256 return buffer[bufpos]; 257 } 258 259 char c; 260 261 if (++bufpos == available) 262 AdjustBuffSize(); 263 264 if ((buffer[bufpos] = c = ReadByte()) == '\\') 265 { 266 UpdateLineColumn(c); 267 268 int backSlashCnt = 1; 269 270 for (;;) // Read all the backslashes 271 { 272 if (++bufpos == available) 273 AdjustBuffSize(); 274 275 try 276 { 277 if ((buffer[bufpos] = c = ReadByte()) != '\\') 278 { 279 UpdateLineColumn(c); 280 // found a non-backslash char. 281 if ((c == 'u') && ((backSlashCnt & 1) == 1)) 282 { 283 if (--bufpos < 0) 284 bufpos = bufsize - 1; 285 286 break; 287 } 288 289 backup(backSlashCnt); 290 return '\\'; 291 } 292 } 293 catch(java.io.IOException e) 294 { 295 if (backSlashCnt > 1) 296 backup(backSlashCnt); 297 298 return '\\'; 299 } 300 301 UpdateLineColumn(c); 302 backSlashCnt++; 303 } 304 305 // Here, we have seen an odd number of backslash's followed by a 'u' 306 try 307 { 308 while ((c = ReadByte()) == 'u') 309 ++column; 310 311 buffer[bufpos] = c = (char)(hexval(c) << 12 | 312 hexval(ReadByte()) << 8 | 313 hexval(ReadByte()) << 4 | 314 hexval(ReadByte())); 315 316 column += 4; 317 } 318 catch(java.io.IOException e) 319 { 320 throw new Error("Invalid escape character at line " + line + 321 " column " + column + "."); 322 } 323 324 if (backSlashCnt == 1) 325 return c; 326 else 327 { 328 backup(backSlashCnt - 1); 329 return '\\'; 330 } 331 } 332 else 333 { 334 UpdateLineColumn(c); 335 return (c); 336 } 337 } 338 339 /** 340 * @deprecated 341 * @see #getEndColumn 342 */ 343 // Added by hand to eliminate Enumeration warnings. (markro) 344 @Deprecated 345 public int getColumn() { 346 return bufcolumn[bufpos]; 347 } 348 349 /** 350 * @deprecated 351 * @see #getEndLine 352 */ 353 // Added by hand to eliminate Enumeration warnings. (markro) 354 @Deprecated 355 public int getLine() { 356 return bufline[bufpos]; 357 } 358 359 public int getEndColumn() { 360 return bufcolumn[bufpos]; 361 } 362 363 public int getEndLine() { 364 return bufline[bufpos]; 365 } 366 367 public int getBeginColumn() { 368 return bufcolumn[tokenBegin]; 369 } 370 371 public int getBeginLine() { 372 return bufline[tokenBegin]; 373 } 374 375 public void backup(int amount) { 376 377 inBuf += amount; 378 if ((bufpos -= amount) < 0) 379 bufpos += bufsize; 380 } 381 382 public JavaCharStream(java.io.Reader dstream, 383 int startline, int startcolumn, int buffersize) 384 { 385 inputStream = dstream; 386 line = startline; 387 column = startcolumn - 1; 388 389 available = bufsize = buffersize; 390 buffer = new char[buffersize]; 391 bufline = new int[buffersize]; 392 bufcolumn = new int[buffersize]; 393 nextCharBuf = new char[4096]; 394 } 395 396 public JavaCharStream(java.io.Reader dstream, 397 int startline, int startcolumn) 398 { 399 this(dstream, startline, startcolumn, 4096); 400 } 401 402 public JavaCharStream(java.io.Reader dstream) 403 { 404 this(dstream, 1, 1, 4096); 405 } 406 public void ReInit(java.io.Reader dstream, 407 int startline, int startcolumn, int buffersize) 408 { 409 inputStream = dstream; 410 line = startline; 411 column = startcolumn - 1; 412 413 if (buffer == null || buffersize != buffer.length) 414 { 415 available = bufsize = buffersize; 416 buffer = new char[buffersize]; 417 bufline = new int[buffersize]; 418 bufcolumn = new int[buffersize]; 419 nextCharBuf = new char[4096]; 420 } 421 prevCharIsLF = prevCharIsCR = false; 422 tokenBegin = inBuf = maxNextCharInd = 0; 423 nextCharInd = bufpos = -1; 424 } 425 426 public void ReInit(java.io.Reader dstream, 427 int startline, int startcolumn) 428 { 429 ReInit(dstream, startline, startcolumn, 4096); 430 } 431 432 public void ReInit(java.io.Reader dstream) 433 { 434 ReInit(dstream, 1, 1, 4096); 435 } 436 public JavaCharStream(java.io.InputStream dstream, int startline, 437 int startcolumn, int buffersize) 438 { 439 this(new java.io.InputStreamReader(dstream), startline, startcolumn, 4096); 440 } 441 442 public JavaCharStream(java.io.InputStream dstream, int startline, 443 int startcolumn) 444 { 445 this(dstream, startline, startcolumn, 4096); 446 } 447 448 public JavaCharStream(java.io.InputStream dstream) 449 { 450 this(dstream, 1, 1, 4096); 451 } 452 453 public void ReInit(java.io.InputStream dstream, int startline, 454 int startcolumn, int buffersize) 455 { 456 ReInit(new java.io.InputStreamReader(dstream), startline, startcolumn, 4096); 457 } 458 public void ReInit(java.io.InputStream dstream, int startline, 459 int startcolumn) 460 { 461 ReInit(dstream, startline, startcolumn, 4096); 462 } 463 public void ReInit(java.io.InputStream dstream) 464 { 465 ReInit(dstream, 1, 1, 4096); 466 } 467 468 public String GetImage() 469 { 470 if (bufpos >= tokenBegin) 471 return new String(buffer, tokenBegin, bufpos - tokenBegin + 1); 472 else 473 return new String(buffer, tokenBegin, bufsize - tokenBegin) + 474 new String(buffer, 0, bufpos + 1); 475 } 476 477 public char[] GetSuffix(int len) 478 { 479 char[] ret = new char[len]; 480 481 if ((bufpos + 1) >= len) 482 System.arraycopy(buffer, bufpos - len + 1, ret, 0, len); 483 else 484 { 485 System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0, 486 len - bufpos - 1); 487 System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1); 488 } 489 490 return ret; 491 } 492 493 public void Done() 494 { 495 nextCharBuf = null; 496 buffer = null; 497 bufline = null; 498 bufcolumn = null; 499 } 500 501 /** 502 * Method to adjust line and column numbers for the start of a token. 503 */ 504 public void adjustBeginLineColumn(int newLine, int newCol) 505 { 506 int start = tokenBegin; 507 int len; 508 509 if (bufpos >= tokenBegin) 510 { 511 len = bufpos - tokenBegin + inBuf + 1; 512 } 513 else 514 { 515 len = bufsize - tokenBegin + bufpos + 1 + inBuf; 516 } 517 518 int i = 0, j = 0, k = 0; 519 int nextColDiff = 0, columnDiff = 0; 520 521 while (i < len && 522 bufline[j = start % bufsize] == bufline[k = ++start % bufsize]) 523 { 524 bufline[j] = newLine; 525 nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j]; 526 bufcolumn[j] = newCol + columnDiff; 527 columnDiff = nextColDiff; 528 i++; 529 } 530 531 if (i < len) 532 { 533 bufline[j] = newLine++; 534 bufcolumn[j] = newCol + columnDiff; 535 536 while (i++ < len) 537 { 538 if (bufline[j = start % bufsize] != bufline[++start % bufsize]) 539 bufline[j] = newLine++; 540 else 541 bufline[j] = newLine; 542 } 543 } 544 545 line = bufline[j]; 546 column = bufcolumn[j]; 547 } 548 549}