1 /* 2 * Copyright (c) 1999, 2011, 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 /* 27 * This source code is provided to illustrate the usage of a given feature 28 * or technique and has been deliberately simplified. Additional steps 29 * required for a production-quality application, such as security checks, 30 * input validation and proper error handling, might not be present in 31 * this sample code. 32 */ 33 34 35 /* Generated By:JavaCC: Do not edit this line. ASCII_UCodeESC_CharStream.java Version 0.7pre6 */ 36 37 package com.sun.tools.example.debug.expr; 38 39 /** 40 * An implementation of interface CharStream, where the stream is assumed to 41 * contain only ASCII characters (with java-like unicode escape processing). 42 */ 43 44 public final class ASCII_UCodeESC_CharStream 45 { 46 public static final boolean staticFlag = false; hexval(char c)47 static final int hexval(char c) throws java.io.IOException { 48 switch(c) 49 { 50 case '0' : 51 return 0; 52 case '1' : 53 return 1; 54 case '2' : 55 return 2; 56 case '3' : 57 return 3; 58 case '4' : 59 return 4; 60 case '5' : 61 return 5; 62 case '6' : 63 return 6; 64 case '7' : 65 return 7; 66 case '8' : 67 return 8; 68 case '9' : 69 return 9; 70 71 case 'a' : 72 case 'A' : 73 return 10; 74 case 'b' : 75 case 'B' : 76 return 11; 77 case 'c' : 78 case 'C' : 79 return 12; 80 case 'd' : 81 case 'D' : 82 return 13; 83 case 'e' : 84 case 'E' : 85 return 14; 86 case 'f' : 87 case 'F' : 88 return 15; 89 } 90 91 throw new java.io.IOException(); // Should never come here 92 } 93 94 public int bufpos = -1; 95 int bufsize; 96 int available; 97 int tokenBegin; 98 private int bufline[]; 99 private int bufcolumn[]; 100 101 private int column = 0; 102 private int line = 1; 103 104 private java.io.InputStream inputStream; 105 106 private boolean prevCharIsCR = false; 107 private boolean prevCharIsLF = false; 108 109 private byte[] nextCharBuf; 110 private char[] buffer; 111 private int maxNextCharInd = 0; 112 private int nextCharInd = -1; 113 private int inBuf = 0; 114 ExpandBuff(boolean wrapAround)115 private final void ExpandBuff(boolean wrapAround) 116 { 117 char[] newbuffer = new char[bufsize + 2048]; 118 int newbufline[] = new int[bufsize + 2048]; 119 int newbufcolumn[] = new int[bufsize + 2048]; 120 121 try 122 { 123 if (wrapAround) 124 { 125 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin); 126 System.arraycopy(buffer, 0, newbuffer, 127 bufsize - tokenBegin, bufpos); 128 buffer = newbuffer; 129 130 System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin); 131 System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos); 132 bufline = newbufline; 133 134 System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin); 135 System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos); 136 bufcolumn = newbufcolumn; 137 138 bufpos += (bufsize - tokenBegin); 139 } 140 else 141 { 142 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin); 143 buffer = newbuffer; 144 145 System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin); 146 bufline = newbufline; 147 148 System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin); 149 bufcolumn = newbufcolumn; 150 151 bufpos -= tokenBegin; 152 } 153 } 154 catch (Throwable t) 155 { 156 throw new Error(t.getMessage()); 157 } 158 159 available = (bufsize += 2048); 160 tokenBegin = 0; 161 } 162 FillBuff()163 private final void FillBuff() throws java.io.IOException 164 { 165 int i; 166 if (maxNextCharInd == 4096) 167 maxNextCharInd = nextCharInd = 0; 168 169 try { 170 if ((i = inputStream.read(nextCharBuf, maxNextCharInd, 171 4096 - maxNextCharInd)) == -1) 172 { 173 inputStream.close(); 174 throw new java.io.IOException(); 175 } 176 else 177 maxNextCharInd += i; 178 return; 179 } 180 catch(java.io.IOException e) { 181 if (bufpos != 0) 182 { 183 --bufpos; 184 backup(0); 185 } 186 else 187 { 188 bufline[bufpos] = line; 189 bufcolumn[bufpos] = column; 190 } 191 throw e; 192 } 193 } 194 ReadByte()195 private final byte ReadByte() throws java.io.IOException 196 { 197 if (++nextCharInd >= maxNextCharInd) 198 FillBuff(); 199 200 return nextCharBuf[nextCharInd]; 201 } 202 BeginToken()203 public final char BeginToken() throws java.io.IOException 204 { 205 if (inBuf > 0) 206 { 207 --inBuf; 208 return buffer[tokenBegin = (bufpos == bufsize - 1) ? (bufpos = 0) 209 : ++bufpos]; 210 } 211 212 tokenBegin = 0; 213 bufpos = -1; 214 215 return readChar(); 216 } 217 AdjustBuffSize()218 private final void AdjustBuffSize() 219 { 220 if (available == bufsize) 221 { 222 if (tokenBegin > 2048) 223 { 224 bufpos = 0; 225 available = tokenBegin; 226 } 227 else 228 ExpandBuff(false); 229 } 230 else if (available > tokenBegin) 231 available = bufsize; 232 else if ((tokenBegin - available) < 2048) 233 ExpandBuff(true); 234 else 235 available = tokenBegin; 236 } 237 UpdateLineColumn(char c)238 private final void UpdateLineColumn(char c) 239 { 240 column++; 241 242 if (prevCharIsLF) 243 { 244 prevCharIsLF = false; 245 line += (column = 1); 246 } 247 else if (prevCharIsCR) 248 { 249 prevCharIsCR = false; 250 if (c == '\n') 251 { 252 prevCharIsLF = true; 253 } 254 else 255 line += (column = 1); 256 } 257 258 switch (c) 259 { 260 case '\r' : 261 prevCharIsCR = true; 262 break; 263 case '\n' : 264 prevCharIsLF = true; 265 break; 266 case '\t' : 267 column--; 268 column += (8 - (column & 07)); 269 break; 270 default : 271 break; 272 } 273 274 bufline[bufpos] = line; 275 bufcolumn[bufpos] = column; 276 } 277 readChar()278 public final char readChar() throws java.io.IOException 279 { 280 if (inBuf > 0) 281 { 282 --inBuf; 283 return buffer[(bufpos == bufsize - 1) ? (bufpos = 0) : ++bufpos]; 284 } 285 286 char c; 287 288 if (++bufpos == available) 289 AdjustBuffSize(); 290 291 if (((buffer[bufpos] = c = (char)((char)0xff & ReadByte())) == '\\')) 292 { 293 UpdateLineColumn(c); 294 295 int backSlashCnt = 1; 296 297 for (;;) // Read all the backslashes 298 { 299 if (++bufpos == available) 300 AdjustBuffSize(); 301 302 try 303 { 304 if ((buffer[bufpos] = c = (char)((char)0xff & ReadByte())) != '\\') 305 { 306 UpdateLineColumn(c); 307 // found a non-backslash char. 308 if ((c == 'u') && ((backSlashCnt & 1) == 1)) 309 { 310 if (--bufpos < 0) 311 bufpos = bufsize - 1; 312 313 break; 314 } 315 316 backup(backSlashCnt); 317 return '\\'; 318 } 319 } 320 catch(java.io.IOException e) 321 { 322 if (backSlashCnt > 1) 323 backup(backSlashCnt); 324 325 return '\\'; 326 } 327 328 UpdateLineColumn(c); 329 backSlashCnt++; 330 } 331 332 // Here, we have seen an odd number of backslash's followed by a 'u' 333 try 334 { 335 while ((c = (char)((char)0xff & ReadByte())) == 'u') 336 ++column; 337 338 buffer[bufpos] = c = (char)(hexval(c) << 12 | 339 hexval((char)((char)0xff & ReadByte())) << 8 | 340 hexval((char)((char)0xff & ReadByte())) << 4 | 341 hexval((char)((char)0xff & ReadByte()))); 342 343 column += 4; 344 } 345 catch(java.io.IOException e) 346 { 347 throw new Error("Invalid escape character at line " + line + 348 " column " + column + "."); 349 } 350 351 if (backSlashCnt == 1) 352 return c; 353 else 354 { 355 backup(backSlashCnt - 1); 356 return '\\'; 357 } 358 } 359 else 360 { 361 UpdateLineColumn(c); 362 return (c); 363 } 364 } 365 366 /** 367 * @deprecated 368 * @see #getEndColumn 369 */ 370 @Deprecated getColumn()371 public final int getColumn() { 372 return bufcolumn[bufpos]; 373 } 374 375 /** 376 * @deprecated 377 * @see #getEndLine 378 */ 379 @Deprecated getLine()380 public final int getLine() { 381 return bufline[bufpos]; 382 } 383 getEndColumn()384 public final int getEndColumn() { 385 return bufcolumn[bufpos]; 386 } 387 getEndLine()388 public final int getEndLine() { 389 return bufline[bufpos]; 390 } 391 getBeginColumn()392 public final int getBeginColumn() { 393 return bufcolumn[tokenBegin]; 394 } 395 getBeginLine()396 public final int getBeginLine() { 397 return bufline[tokenBegin]; 398 } 399 backup(int amount)400 public final void backup(int amount) { 401 402 inBuf += amount; 403 if ((bufpos -= amount) < 0) 404 bufpos += bufsize; 405 } 406 ASCII_UCodeESC_CharStream(java.io.InputStream dstream, int startline, int startcolumn, int buffersize)407 public ASCII_UCodeESC_CharStream(java.io.InputStream dstream, 408 int startline, int startcolumn, int buffersize) 409 { 410 inputStream = dstream; 411 line = startline; 412 column = startcolumn - 1; 413 414 available = bufsize = buffersize; 415 buffer = new char[buffersize]; 416 bufline = new int[buffersize]; 417 bufcolumn = new int[buffersize]; 418 nextCharBuf = new byte[4096]; 419 } 420 ASCII_UCodeESC_CharStream(java.io.InputStream dstream, int startline, int startcolumn)421 public ASCII_UCodeESC_CharStream(java.io.InputStream dstream, 422 int startline, int startcolumn) 423 { 424 this(dstream, startline, startcolumn, 4096); 425 } ReInit(java.io.InputStream dstream, int startline, int startcolumn, int buffersize)426 public void ReInit(java.io.InputStream dstream, 427 int startline, int startcolumn, int buffersize) 428 { 429 inputStream = dstream; 430 line = startline; 431 column = startcolumn - 1; 432 433 if (buffer == null || buffersize != buffer.length) 434 { 435 available = bufsize = buffersize; 436 buffer = new char[buffersize]; 437 bufline = new int[buffersize]; 438 bufcolumn = new int[buffersize]; 439 nextCharBuf = new byte[4096]; 440 } 441 prevCharIsLF = prevCharIsCR = false; 442 tokenBegin = inBuf = maxNextCharInd = 0; 443 nextCharInd = bufpos = -1; 444 } 445 ReInit(java.io.InputStream dstream, int startline, int startcolumn)446 public void ReInit(java.io.InputStream dstream, 447 int startline, int startcolumn) 448 { 449 ReInit(dstream, startline, startcolumn, 4096); 450 } 451 GetImage()452 public final String GetImage() 453 { 454 if (bufpos >= tokenBegin) 455 return new String(buffer, tokenBegin, bufpos - tokenBegin + 1); 456 else 457 return new String(buffer, tokenBegin, bufsize - tokenBegin) + 458 new String(buffer, 0, bufpos + 1); 459 } 460 GetSuffix(int len)461 public final char[] GetSuffix(int len) 462 { 463 char[] ret = new char[len]; 464 465 if ((bufpos + 1) >= len) 466 System.arraycopy(buffer, bufpos - len + 1, ret, 0, len); 467 else 468 { 469 System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0, 470 len - bufpos - 1); 471 System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1); 472 } 473 474 return ret; 475 } 476 Done()477 public void Done() 478 { 479 nextCharBuf = null; 480 buffer = null; 481 bufline = null; 482 bufcolumn = null; 483 } 484 485 /** 486 * Method to adjust line and column numbers for the start of a token.<BR> 487 */ adjustBeginLineColumn(int newLine, int newCol)488 public void adjustBeginLineColumn(int newLine, int newCol) 489 { 490 int start = tokenBegin; 491 int len; 492 493 if (bufpos >= tokenBegin) 494 { 495 len = bufpos - tokenBegin + inBuf + 1; 496 } 497 else 498 { 499 len = bufsize - tokenBegin + bufpos + 1 + inBuf; 500 } 501 502 int i = 0, j = 0, k = 0; 503 int nextColDiff = 0, columnDiff = 0; 504 505 while (i < len && 506 bufline[j = start % bufsize] == bufline[k = ++start % bufsize]) 507 { 508 bufline[j] = newLine; 509 nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j]; 510 bufcolumn[j] = newCol + columnDiff; 511 columnDiff = nextColDiff; 512 i++; 513 } 514 515 if (i < len) 516 { 517 bufline[j] = newLine++; 518 bufcolumn[j] = newCol + columnDiff; 519 520 while (i++ < len) 521 { 522 if (bufline[j = start % bufsize] != bufline[++start % bufsize]) 523 bufline[j] = newLine++; 524 else 525 bufline[j] = newLine; 526 } 527 } 528 529 line = bufline[j]; 530 column = bufcolumn[j]; 531 } 532 533 } 534