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