00001 package edu.ksu.cis.bandera.abstraction.predicate.parser;
00002
00003
00004
00005
00006
00007
00008 public final class ASCII_UCodeESC_CharStream
00009 {
00010 public static final boolean staticFlag = false;
00011 public int bufpos = -1;
00012 int bufsize;
00013 int available;
00014 int tokenBegin;
00015 private int bufline[];
00016 private int bufcolumn[];
00017
00018 private int column = 0;
00019 private int line = 1;
00020
00021 private java.io.Reader inputStream;
00022
00023 private boolean prevCharIsCR = false;
00024 private boolean prevCharIsLF = false;
00025
00026 private char[] nextCharBuf;
00027 private char[] buffer;
00028 private int maxNextCharInd = 0;
00029 private int nextCharInd = -1;
00030 private int inBuf = 0;
00031
00032 public ASCII_UCodeESC_CharStream(java.io.InputStream dstream, int startline,
00033 int startcolumn)
00034 {
00035 this(dstream, startline, startcolumn, 4096);
00036 }
00037 public ASCII_UCodeESC_CharStream(java.io.InputStream dstream, int startline,
00038 int startcolumn, int buffersize)
00039 {
00040 this(new java.io.InputStreamReader(dstream), startline, startcolumn, 4096);
00041 }
00042 public ASCII_UCodeESC_CharStream(java.io.Reader dstream,
00043 int startline, int startcolumn)
00044 {
00045 this(dstream, startline, startcolumn, 4096);
00046 }
00047 public ASCII_UCodeESC_CharStream(java.io.Reader dstream,
00048 int startline, int startcolumn, int buffersize)
00049 {
00050 inputStream = dstream;
00051 line = startline;
00052 column = startcolumn - 1;
00053
00054 available = bufsize = buffersize;
00055 buffer = new char[buffersize];
00056 bufline = new int[buffersize];
00057 bufcolumn = new int[buffersize];
00058 nextCharBuf = new char[4096];
00059 }
00060
00061
00062
00063 public void adjustBeginLineColumn(int newLine, int newCol)
00064 {
00065 int start = tokenBegin;
00066 int len;
00067
00068 if (bufpos >= tokenBegin)
00069 {
00070 len = bufpos - tokenBegin + inBuf + 1;
00071 }
00072 else
00073 {
00074 len = bufsize - tokenBegin + bufpos + 1 + inBuf;
00075 }
00076
00077 int i = 0, j = 0, k = 0;
00078 int nextColDiff = 0, columnDiff = 0;
00079
00080 while (i < len &&
00081 bufline[j = start % bufsize] == bufline[k = ++start % bufsize])
00082 {
00083 bufline[j] = newLine;
00084 nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
00085 bufcolumn[j] = newCol + columnDiff;
00086 columnDiff = nextColDiff;
00087 i++;
00088 }
00089
00090 if (i < len)
00091 {
00092 bufline[j] = newLine++;
00093 bufcolumn[j] = newCol + columnDiff;
00094
00095 while (i++ < len)
00096 {
00097 if (bufline[j = start % bufsize] != bufline[++start % bufsize])
00098 bufline[j] = newLine++;
00099 else
00100 bufline[j] = newLine;
00101 }
00102 }
00103
00104 line = bufline[j];
00105 column = bufcolumn[j];
00106 }
00107 private final void AdjustBuffSize()
00108 {
00109 if (available == bufsize)
00110 {
00111 if (tokenBegin > 2048)
00112 {
00113 bufpos = 0;
00114 available = tokenBegin;
00115 }
00116 else
00117 ExpandBuff(false);
00118 }
00119 else if (available > tokenBegin)
00120 available = bufsize;
00121 else if ((tokenBegin - available) < 2048)
00122 ExpandBuff(true);
00123 else
00124 available = tokenBegin;
00125 }
00126 public final void backup(int amount) {
00127
00128 inBuf += amount;
00129 if ((bufpos -= amount) < 0)
00130 bufpos += bufsize;
00131 }
00132 public final char BeginToken() throws java.io.IOException
00133 {
00134 if (inBuf > 0)
00135 {
00136 --inBuf;
00137 return buffer[tokenBegin = (bufpos == bufsize - 1) ? (bufpos = 0)
00138 : ++bufpos];
00139 }
00140
00141 tokenBegin = 0;
00142 bufpos = -1;
00143
00144 return readChar();
00145 }
00146 public void Done()
00147 {
00148 nextCharBuf = null;
00149 buffer = null;
00150 bufline = null;
00151 bufcolumn = null;
00152 }
00153 private final void ExpandBuff(boolean wrapAround)
00154 {
00155 char[] newbuffer = new char[bufsize + 2048];
00156 int newbufline[] = new int[bufsize + 2048];
00157 int newbufcolumn[] = new int[bufsize + 2048];
00158
00159 try
00160 {
00161 if (wrapAround)
00162 {
00163 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
00164 System.arraycopy(buffer, 0, newbuffer,
00165 bufsize - tokenBegin, bufpos);
00166 buffer = newbuffer;
00167
00168 System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
00169 System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);
00170 bufline = newbufline;
00171
00172 System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
00173 System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);
00174 bufcolumn = newbufcolumn;
00175
00176 bufpos += (bufsize - tokenBegin);
00177 }
00178 else
00179 {
00180 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
00181 buffer = newbuffer;
00182
00183 System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
00184 bufline = newbufline;
00185
00186 System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
00187 bufcolumn = newbufcolumn;
00188
00189 bufpos -= tokenBegin;
00190 }
00191 }
00192 catch (Throwable t)
00193 {
00194 throw new Error(t.getMessage());
00195 }
00196
00197 available = (bufsize += 2048);
00198 tokenBegin = 0;
00199 }
00200 private final void FillBuff() throws java.io.IOException
00201 {
00202 int i;
00203 if (maxNextCharInd == 4096)
00204 maxNextCharInd = nextCharInd = 0;
00205
00206 try {
00207 if ((i = inputStream.read(nextCharBuf, maxNextCharInd,
00208 4096 - maxNextCharInd)) == -1)
00209 {
00210 inputStream.close();
00211 throw new java.io.IOException();
00212 }
00213 else
00214 maxNextCharInd += i;
00215 return;
00216 }
00217 catch(java.io.IOException e) {
00218 if (bufpos != 0)
00219 {
00220 --bufpos;
00221 backup(0);
00222 }
00223 else
00224 {
00225 bufline[bufpos] = line;
00226 bufcolumn[bufpos] = column;
00227 }
00228 throw e;
00229 }
00230 }
00231 public final int getBeginColumn() {
00232 return bufcolumn[tokenBegin];
00233 }
00234 public final int getBeginLine() {
00235 return bufline[tokenBegin];
00236 }
00237
00238
00239
00240
00241
00242 public final int getColumn() {
00243 return bufcolumn[bufpos];
00244 }
00245 public final int getEndColumn() {
00246 return bufcolumn[bufpos];
00247 }
00248 public final int getEndLine() {
00249 return bufline[bufpos];
00250 }
00251 public final String GetImage()
00252 {
00253 if (bufpos >= tokenBegin)
00254 return new String(buffer, tokenBegin, bufpos - tokenBegin + 1);
00255 else
00256 return new String(buffer, tokenBegin, bufsize - tokenBegin) +
00257 new String(buffer, 0, bufpos + 1);
00258 }
00259
00260
00261
00262
00263
00264 public final int getLine() {
00265 return bufline[bufpos];
00266 }
00267 public final char[] GetSuffix(int len)
00268 {
00269 char[] ret = new char[len];
00270
00271 if ((bufpos + 1) >= len)
00272 System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
00273 else
00274 {
00275 System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0,
00276 len - bufpos - 1);
00277 System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
00278 }
00279
00280 return ret;
00281 }
00282 static final int hexval(char c) throws java.io.IOException {
00283 switch(c)
00284 {
00285 case '0' :
00286 return 0;
00287 case '1' :
00288 return 1;
00289 case '2' :
00290 return 2;
00291 case '3' :
00292 return 3;
00293 case '4' :
00294 return 4;
00295 case '5' :
00296 return 5;
00297 case '6' :
00298 return 6;
00299 case '7' :
00300 return 7;
00301 case '8' :
00302 return 8;
00303 case '9' :
00304 return 9;
00305
00306 case 'a' :
00307 case 'A' :
00308 return 10;
00309 case 'b' :
00310 case 'B' :
00311 return 11;
00312 case 'c' :
00313 case 'C' :
00314 return 12;
00315 case 'd' :
00316 case 'D' :
00317 return 13;
00318 case 'e' :
00319 case 'E' :
00320 return 14;
00321 case 'f' :
00322 case 'F' :
00323 return 15;
00324 }
00325
00326 throw new java.io.IOException();
00327 }
00328 private final char ReadByte() throws java.io.IOException
00329 {
00330 if (++nextCharInd >= maxNextCharInd)
00331 FillBuff();
00332
00333 return nextCharBuf[nextCharInd];
00334 }
00335 public final char readChar() throws java.io.IOException
00336 {
00337 if (inBuf > 0)
00338 {
00339 --inBuf;
00340 return buffer[(bufpos == bufsize - 1) ? (bufpos = 0) : ++bufpos];
00341 }
00342
00343 char c;
00344
00345 if (++bufpos == available)
00346 AdjustBuffSize();
00347
00348 if (((buffer[bufpos] = c = (char)((char)0xff & ReadByte())) == '\\'))
00349 {
00350 UpdateLineColumn(c);
00351
00352 int backSlashCnt = 1;
00353
00354 for (;;)
00355 {
00356 if (++bufpos == available)
00357 AdjustBuffSize();
00358
00359 try
00360 {
00361 if ((buffer[bufpos] = c = (char)((char)0xff & ReadByte())) != '\\')
00362 {
00363 UpdateLineColumn(c);
00364
00365 if ((c == 'u') && ((backSlashCnt & 1) == 1))
00366 {
00367 if (--bufpos < 0)
00368 bufpos = bufsize - 1;
00369
00370 break;
00371 }
00372
00373 backup(backSlashCnt);
00374 return '\\';
00375 }
00376 }
00377 catch(java.io.IOException e)
00378 {
00379 if (backSlashCnt > 1)
00380 backup(backSlashCnt);
00381
00382 return '\\';
00383 }
00384
00385 UpdateLineColumn(c);
00386 backSlashCnt++;
00387 }
00388
00389
00390 try
00391 {
00392 while ((c = (char)((char)0xff & ReadByte())) == 'u')
00393 ++column;
00394
00395 buffer[bufpos] = c = (char)(hexval(c) << 12 |
00396 hexval((char)((char)0xff & ReadByte())) << 8 |
00397 hexval((char)((char)0xff & ReadByte())) << 4 |
00398 hexval((char)((char)0xff & ReadByte())));
00399
00400 column += 4;
00401 }
00402 catch(java.io.IOException e)
00403 {
00404 throw new Error("Invalid escape character at line " + line +
00405 " column " + column + ".");
00406 }
00407
00408 if (backSlashCnt == 1)
00409 return c;
00410 else
00411 {
00412 backup(backSlashCnt - 1);
00413 return '\\';
00414 }
00415 }
00416 else
00417 {
00418 UpdateLineColumn(c);
00419 return (c);
00420 }
00421 }
00422 public void ReInit(java.io.InputStream dstream, int startline,
00423 int startcolumn)
00424 {
00425 ReInit(dstream, startline, startcolumn, 4096);
00426 }
00427 public void ReInit(java.io.InputStream dstream, int startline,
00428 int startcolumn, int buffersize)
00429 {
00430 ReInit(new java.io.InputStreamReader(dstream), startline, startcolumn, 4096);
00431 }
00432 public void ReInit(java.io.Reader dstream,
00433 int startline, int startcolumn)
00434 {
00435 ReInit(dstream, startline, startcolumn, 4096);
00436 }
00437 public void ReInit(java.io.Reader dstream,
00438 int startline, int startcolumn, int buffersize)
00439 {
00440 inputStream = dstream;
00441 line = startline;
00442 column = startcolumn - 1;
00443
00444 if (buffer == null || buffersize != buffer.length)
00445 {
00446 available = bufsize = buffersize;
00447 buffer = new char[buffersize];
00448 bufline = new int[buffersize];
00449 bufcolumn = new int[buffersize];
00450 nextCharBuf = new char[4096];
00451 }
00452 prevCharIsLF = prevCharIsCR = false;
00453 tokenBegin = inBuf = maxNextCharInd = 0;
00454 nextCharInd = bufpos = -1;
00455 }
00456 private final void UpdateLineColumn(char c)
00457 {
00458 column++;
00459
00460 if (prevCharIsLF)
00461 {
00462 prevCharIsLF = false;
00463 line += (column = 1);
00464 }
00465 else if (prevCharIsCR)
00466 {
00467 prevCharIsCR = false;
00468 if (c == '\n')
00469 {
00470 prevCharIsLF = true;
00471 }
00472 else
00473 line += (column = 1);
00474 }
00475
00476 switch (c)
00477 {
00478 case '\r' :
00479 prevCharIsCR = true;
00480 break;
00481 case '\n' :
00482 prevCharIsLF = true;
00483 break;
00484 case '\t' :
00485 column--;
00486 column += (8 - (column & 07));
00487 break;
00488 default :
00489 break;
00490 }
00491
00492 bufline[bufpos] = line;
00493 bufcolumn[bufpos] = column;
00494 }
00495 }