1/* 2 * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25/* 26 * Copyright (C) 2004-2011 27 * 28 * Permission is hereby granted, free of charge, to any person obtaining a copy 29 * of this software and associated documentation files (the "Software"), to deal 30 * in the Software without restriction, including without limitation the rights 31 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 32 * copies of the Software, and to permit persons to whom the Software is 33 * furnished to do so, subject to the following conditions: 34 * 35 * The above copyright notice and this permission notice shall be included in 36 * all copies or substantial portions of the Software. 37 * 38 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 39 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 40 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 41 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 42 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 43 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 44 * THE SOFTWARE. 45 */ 46/* Generated By:JavaCC: Do not edit this line. JavaCharStream.java Version 5.0 */ 47/* JavaCCOptions:STATIC=false,SUPPORT_CLASS_VISIBILITY_PUBLIC=true */ 48package com.sun.xml.internal.rngom.parse.compact; 49 50/** 51 * An implementation of interface CharStream, where the stream is assumed to 52 * contain only ASCII characters (with java-like unicode escape processing). 53 */ 54 55public 56class JavaCharStream 57{ 58 /** Whether parser is static. */ 59 public static final boolean staticFlag = false; 60 61 static final int hexval(char c) throws java.io.IOException { 62 switch(c) 63 { 64 case '0' : 65 return 0; 66 case '1' : 67 return 1; 68 case '2' : 69 return 2; 70 case '3' : 71 return 3; 72 case '4' : 73 return 4; 74 case '5' : 75 return 5; 76 case '6' : 77 return 6; 78 case '7' : 79 return 7; 80 case '8' : 81 return 8; 82 case '9' : 83 return 9; 84 85 case 'a' : 86 case 'A' : 87 return 10; 88 case 'b' : 89 case 'B' : 90 return 11; 91 case 'c' : 92 case 'C' : 93 return 12; 94 case 'd' : 95 case 'D' : 96 return 13; 97 case 'e' : 98 case 'E' : 99 return 14; 100 case 'f' : 101 case 'F' : 102 return 15; 103 } 104 105 throw new java.io.IOException(); // Should never come here 106 } 107 108/** Position in buffer. */ 109 public int bufpos = -1; 110 int bufsize; 111 int available; 112 int tokenBegin; 113 protected int bufline[]; 114 protected int bufcolumn[]; 115 116 protected int column = 0; 117 protected int line = 1; 118 119 protected boolean prevCharIsCR = false; 120 protected boolean prevCharIsLF = false; 121 122 protected java.io.Reader inputStream; 123 124 protected char[] nextCharBuf; 125 protected char[] buffer; 126 protected int maxNextCharInd = 0; 127 protected int nextCharInd = -1; 128 protected int inBuf = 0; 129 protected int tabSize = 8; 130 131 protected void setTabSize(int i) { tabSize = i; } 132 protected int getTabSize(int i) { return tabSize; } 133 134 protected void ExpandBuff(boolean wrapAround) 135 { 136 char[] newbuffer = new char[bufsize + 2048]; 137 int newbufline[] = new int[bufsize + 2048]; 138 int newbufcolumn[] = new int[bufsize + 2048]; 139 140 try 141 { 142 if (wrapAround) 143 { 144 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin); 145 System.arraycopy(buffer, 0, newbuffer, bufsize - tokenBegin, bufpos); 146 buffer = newbuffer; 147 148 System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin); 149 System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos); 150 bufline = newbufline; 151 152 System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin); 153 System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos); 154 bufcolumn = newbufcolumn; 155 156 bufpos += (bufsize - tokenBegin); 157 } 158 else 159 { 160 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin); 161 buffer = newbuffer; 162 163 System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin); 164 bufline = newbufline; 165 166 System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin); 167 bufcolumn = newbufcolumn; 168 169 bufpos -= tokenBegin; 170 } 171 } 172 catch (Throwable t) 173 { 174 throw new Error(t.getMessage()); 175 } 176 177 available = (bufsize += 2048); 178 tokenBegin = 0; 179 } 180 181 protected void FillBuff() throws java.io.IOException 182 { 183 int i; 184 if (maxNextCharInd == 4096) 185 maxNextCharInd = nextCharInd = 0; 186 187 try { 188 if ((i = inputStream.read(nextCharBuf, maxNextCharInd, 189 4096 - maxNextCharInd)) == -1) 190 { 191 inputStream.close(); 192 throw new java.io.IOException(); 193 } 194 else 195 maxNextCharInd += i; 196 return; 197 } 198 catch(java.io.IOException e) { 199 if (bufpos != 0) 200 { 201 --bufpos; 202 backup(0); 203 } 204 else 205 { 206 bufline[bufpos] = line; 207 bufcolumn[bufpos] = column; 208 } 209 throw e; 210 } 211 } 212 213 protected char ReadByte() throws java.io.IOException 214 { 215 if (++nextCharInd >= maxNextCharInd) 216 FillBuff(); 217 218 return nextCharBuf[nextCharInd]; 219 } 220 221/** @return starting character for token. */ 222 public char BeginToken() throws java.io.IOException 223 { 224 if (inBuf > 0) 225 { 226 --inBuf; 227 228 if (++bufpos == bufsize) 229 bufpos = 0; 230 231 tokenBegin = bufpos; 232 return buffer[bufpos]; 233 } 234 235 tokenBegin = 0; 236 bufpos = -1; 237 238 return readChar(); 239 } 240 241 protected void AdjustBuffSize() 242 { 243 if (available == bufsize) 244 { 245 if (tokenBegin > 2048) 246 { 247 bufpos = 0; 248 available = tokenBegin; 249 } 250 else 251 ExpandBuff(false); 252 } 253 else if (available > tokenBegin) 254 available = bufsize; 255 else if ((tokenBegin - available) < 2048) 256 ExpandBuff(true); 257 else 258 available = tokenBegin; 259 } 260 261 protected void UpdateLineColumn(char c) 262 { 263 column++; 264 265 if (prevCharIsLF) 266 { 267 prevCharIsLF = false; 268 line += (column = 1); 269 } 270 else if (prevCharIsCR) 271 { 272 prevCharIsCR = false; 273 if (c == '\n') 274 { 275 prevCharIsLF = true; 276 } 277 else 278 line += (column = 1); 279 } 280 281 switch (c) 282 { 283 case '\r' : 284 prevCharIsCR = true; 285 break; 286 case '\n' : 287 prevCharIsLF = true; 288 break; 289 case '\t' : 290 column--; 291 column += (tabSize - (column % tabSize)); 292 break; 293 default : 294 break; 295 } 296 297 bufline[bufpos] = line; 298 bufcolumn[bufpos] = column; 299 } 300 301/** Read a character. */ 302 public char readChar() throws java.io.IOException 303 { 304 if (inBuf > 0) 305 { 306 --inBuf; 307 308 if (++bufpos == bufsize) 309 bufpos = 0; 310 311 return buffer[bufpos]; 312 } 313 314 char c; 315 316 if (++bufpos == available) 317 AdjustBuffSize(); 318 319 if ((buffer[bufpos] = c = ReadByte()) == '\\') 320 { 321 UpdateLineColumn(c); 322 323 int backSlashCnt = 1; 324 325 for (;;) // Read all the backslashes 326 { 327 if (++bufpos == available) 328 AdjustBuffSize(); 329 330 try 331 { 332 if ((buffer[bufpos] = c = ReadByte()) != '\\') 333 { 334 UpdateLineColumn(c); 335 // found a non-backslash char. 336 if ((c == 'u') && ((backSlashCnt & 1) == 1)) 337 { 338 if (--bufpos < 0) 339 bufpos = bufsize - 1; 340 341 break; 342 } 343 344 backup(backSlashCnt); 345 return '\\'; 346 } 347 } 348 catch(java.io.IOException e) 349 { 350 // We are returning one backslash so we should only backup (count-1) 351 if (backSlashCnt > 1) 352 backup(backSlashCnt-1); 353 354 return '\\'; 355 } 356 357 UpdateLineColumn(c); 358 backSlashCnt++; 359 } 360 361 // Here, we have seen an odd number of backslash's followed by a 'u' 362 try 363 { 364 while ((c = ReadByte()) == 'u') 365 ++column; 366 367 buffer[bufpos] = c = (char)(hexval(c) << 12 | 368 hexval(ReadByte()) << 8 | 369 hexval(ReadByte()) << 4 | 370 hexval(ReadByte())); 371 372 column += 4; 373 } 374 catch(java.io.IOException e) 375 { 376 throw new Error("Invalid escape character at line " + line + 377 " column " + column + "."); 378 } 379 380 if (backSlashCnt == 1) 381 return c; 382 else 383 { 384 backup(backSlashCnt - 1); 385 return '\\'; 386 } 387 } 388 else 389 { 390 UpdateLineColumn(c); 391 return c; 392 } 393 } 394 395 @Deprecated 396 /** 397 * @deprecated 398 * @see #getEndColumn 399 */ 400 public int getColumn() { 401 return bufcolumn[bufpos]; 402 } 403 404 @Deprecated 405 /** 406 * @deprecated 407 * @see #getEndLine 408 */ 409 public int getLine() { 410 return bufline[bufpos]; 411 } 412 413/** Get end column. */ 414 public int getEndColumn() { 415 return bufcolumn[bufpos]; 416 } 417 418/** Get end line. */ 419 public int getEndLine() { 420 return bufline[bufpos]; 421 } 422 423/** @return column of token start */ 424 public int getBeginColumn() { 425 return bufcolumn[tokenBegin]; 426 } 427 428/** @return line number of token start */ 429 public int getBeginLine() { 430 return bufline[tokenBegin]; 431 } 432 433/** Retreat. */ 434 public void backup(int amount) { 435 436 inBuf += amount; 437 if ((bufpos -= amount) < 0) 438 bufpos += bufsize; 439 } 440 441/** Constructor. */ 442 public JavaCharStream(java.io.Reader dstream, 443 int startline, int startcolumn, int buffersize) 444 { 445 inputStream = dstream; 446 line = startline; 447 column = startcolumn - 1; 448 449 available = bufsize = buffersize; 450 buffer = new char[buffersize]; 451 bufline = new int[buffersize]; 452 bufcolumn = new int[buffersize]; 453 nextCharBuf = new char[4096]; 454 } 455 456/** Constructor. */ 457 public JavaCharStream(java.io.Reader dstream, 458 int startline, int startcolumn) 459 { 460 this(dstream, startline, startcolumn, 4096); 461 } 462 463/** Constructor. */ 464 public JavaCharStream(java.io.Reader dstream) 465 { 466 this(dstream, 1, 1, 4096); 467 } 468/** Reinitialise. */ 469 public void ReInit(java.io.Reader dstream, 470 int startline, int startcolumn, int buffersize) 471 { 472 inputStream = dstream; 473 line = startline; 474 column = startcolumn - 1; 475 476 if (buffer == null || buffersize != buffer.length) 477 { 478 available = bufsize = buffersize; 479 buffer = new char[buffersize]; 480 bufline = new int[buffersize]; 481 bufcolumn = new int[buffersize]; 482 nextCharBuf = new char[4096]; 483 } 484 prevCharIsLF = prevCharIsCR = false; 485 tokenBegin = inBuf = maxNextCharInd = 0; 486 nextCharInd = bufpos = -1; 487 } 488 489/** Reinitialise. */ 490 public void ReInit(java.io.Reader dstream, 491 int startline, int startcolumn) 492 { 493 ReInit(dstream, startline, startcolumn, 4096); 494 } 495 496/** Reinitialise. */ 497 public void ReInit(java.io.Reader dstream) 498 { 499 ReInit(dstream, 1, 1, 4096); 500 } 501/** Constructor. */ 502 public JavaCharStream(java.io.InputStream dstream, String encoding, int startline, 503 int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException 504 { 505 this(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize); 506 } 507 508/** Constructor. */ 509 public JavaCharStream(java.io.InputStream dstream, int startline, 510 int startcolumn, int buffersize) 511 { 512 this(new java.io.InputStreamReader(dstream), startline, startcolumn, 4096); 513 } 514 515/** Constructor. */ 516 public JavaCharStream(java.io.InputStream dstream, String encoding, int startline, 517 int startcolumn) throws java.io.UnsupportedEncodingException 518 { 519 this(dstream, encoding, startline, startcolumn, 4096); 520 } 521 522/** Constructor. */ 523 public JavaCharStream(java.io.InputStream dstream, int startline, 524 int startcolumn) 525 { 526 this(dstream, startline, startcolumn, 4096); 527 } 528 529/** Constructor. */ 530 public JavaCharStream(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException 531 { 532 this(dstream, encoding, 1, 1, 4096); 533 } 534 535/** Constructor. */ 536 public JavaCharStream(java.io.InputStream dstream) 537 { 538 this(dstream, 1, 1, 4096); 539 } 540 541/** Reinitialise. */ 542 public void ReInit(java.io.InputStream dstream, String encoding, int startline, 543 int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException 544 { 545 ReInit(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize); 546 } 547 548/** Reinitialise. */ 549 public void ReInit(java.io.InputStream dstream, int startline, 550 int startcolumn, int buffersize) 551 { 552 ReInit(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize); 553 } 554/** Reinitialise. */ 555 public void ReInit(java.io.InputStream dstream, String encoding, int startline, 556 int startcolumn) throws java.io.UnsupportedEncodingException 557 { 558 ReInit(dstream, encoding, startline, startcolumn, 4096); 559 } 560/** Reinitialise. */ 561 public void ReInit(java.io.InputStream dstream, int startline, 562 int startcolumn) 563 { 564 ReInit(dstream, startline, startcolumn, 4096); 565 } 566/** Reinitialise. */ 567 public void ReInit(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException 568 { 569 ReInit(dstream, encoding, 1, 1, 4096); 570 } 571 572/** Reinitialise. */ 573 public void ReInit(java.io.InputStream dstream) 574 { 575 ReInit(dstream, 1, 1, 4096); 576 } 577 578 /** @return token image as String */ 579 public String GetImage() 580 { 581 if (bufpos >= tokenBegin) 582 return new String(buffer, tokenBegin, bufpos - tokenBegin + 1); 583 else 584 return new String(buffer, tokenBegin, bufsize - tokenBegin) + 585 new String(buffer, 0, bufpos + 1); 586 } 587 588 /** @return suffix */ 589 public char[] GetSuffix(int len) 590 { 591 char[] ret = new char[len]; 592 593 if ((bufpos + 1) >= len) 594 System.arraycopy(buffer, bufpos - len + 1, ret, 0, len); 595 else 596 { 597 System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0, 598 len - bufpos - 1); 599 System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1); 600 } 601 602 return ret; 603 } 604 605 /** Set buffers back to null when finished. */ 606 public void Done() 607 { 608 nextCharBuf = null; 609 buffer = null; 610 bufline = null; 611 bufcolumn = null; 612 } 613 614 /** 615 * Method to adjust line and column numbers for the start of a token. 616 */ 617 public void adjustBeginLineColumn(int newLine, int newCol) 618 { 619 int start = tokenBegin; 620 int len; 621 622 if (bufpos >= tokenBegin) 623 { 624 len = bufpos - tokenBegin + inBuf + 1; 625 } 626 else 627 { 628 len = bufsize - tokenBegin + bufpos + 1 + inBuf; 629 } 630 631 int i = 0, j = 0, k = 0; 632 int nextColDiff = 0, columnDiff = 0; 633 634 while (i < len && bufline[j = start % bufsize] == bufline[k = ++start % bufsize]) 635 { 636 bufline[j] = newLine; 637 nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j]; 638 bufcolumn[j] = newCol + columnDiff; 639 columnDiff = nextColDiff; 640 i++; 641 } 642 643 if (i < len) 644 { 645 bufline[j] = newLine++; 646 bufcolumn[j] = newCol + columnDiff; 647 648 while (i++ < len) 649 { 650 if (bufline[j = start % bufsize] != bufline[++start % bufsize]) 651 bufline[j] = newLine++; 652 else 653 bufline[j] = newLine; 654 } 655 } 656 657 line = bufline[j]; 658 column = bufcolumn[j]; 659 } 660 661} 662/* JavaCC - OriginalChecksum=d4510307ecc248a96e4d3455ebf92ff3 (do not edit this line) */ 663