00001
00002 package plp.imperative2.parser;
00003
00009 public class JavaCharStream
00010 {
00011 public static final boolean staticFlag = true;
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 static public int bufpos = -1;
00060 static int bufsize;
00061 static int available;
00062 static int tokenBegin;
00063 static protected int bufline[];
00064 static protected int bufcolumn[];
00065
00066 static protected int column = 0;
00067 static protected int line = 1;
00068
00069 static protected boolean prevCharIsCR = false;
00070 static protected boolean prevCharIsLF = false;
00071
00072 static protected java.io.Reader inputStream;
00073
00074 static protected char[] nextCharBuf;
00075 static protected char[] buffer;
00076 static protected int maxNextCharInd = 0;
00077 static protected int nextCharInd = -1;
00078 static protected int inBuf = 0;
00079 static protected int tabSize = 8;
00080
00081 static protected void setTabSize(int i) { tabSize = i; }
00082 static protected int getTabSize(int i) { return tabSize; }
00083
00084 static 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 static 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 static protected char ReadByte() throws java.io.IOException
00165 {
00166 if (++nextCharInd >= maxNextCharInd)
00167 FillBuff();
00168
00169 return nextCharBuf[nextCharInd];
00170 }
00171
00172 static 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 static 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 static 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 static 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 static public int getColumn() {
00349 return bufcolumn[bufpos];
00350 }
00351
00357 static public int getLine() {
00358 return bufline[bufpos];
00359 }
00360
00361 static public int getEndColumn() {
00362 return bufcolumn[bufpos];
00363 }
00364
00365 static public int getEndLine() {
00366 return bufline[bufpos];
00367 }
00368
00369 static public int getBeginColumn() {
00370 return bufcolumn[tokenBegin];
00371 }
00372
00373 static public int getBeginLine() {
00374 return bufline[tokenBegin];
00375 }
00376
00377 static 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 if (inputStream != null)
00388 throw new Error("\n ERROR: Second call to the constructor of a static JavaCharStream. You must\n" +
00389 " either use ReInit() or set the JavaCC option STATIC to false\n" +
00390 " during the generation of this class.");
00391 inputStream = dstream;
00392 line = startline;
00393 column = startcolumn - 1;
00394
00395 available = bufsize = buffersize;
00396 buffer = new char[buffersize];
00397 bufline = new int[buffersize];
00398 bufcolumn = new int[buffersize];
00399 nextCharBuf = new char[4096];
00400 }
00401
00402 public JavaCharStream(java.io.Reader dstream,
00403 int startline, int startcolumn)
00404 {
00405 this(dstream, startline, startcolumn, 4096);
00406 }
00407
00408 public JavaCharStream(java.io.Reader dstream)
00409 {
00410 this(dstream, 1, 1, 4096);
00411 }
00412 public void ReInit(java.io.Reader dstream,
00413 int startline, int startcolumn, int buffersize)
00414 {
00415 inputStream = dstream;
00416 line = startline;
00417 column = startcolumn - 1;
00418
00419 if (buffer == null || buffersize != buffer.length)
00420 {
00421 available = bufsize = buffersize;
00422 buffer = new char[buffersize];
00423 bufline = new int[buffersize];
00424 bufcolumn = new int[buffersize];
00425 nextCharBuf = new char[4096];
00426 }
00427 prevCharIsLF = prevCharIsCR = false;
00428 tokenBegin = inBuf = maxNextCharInd = 0;
00429 nextCharInd = bufpos = -1;
00430 }
00431
00432 public void ReInit(java.io.Reader dstream,
00433 int startline, int startcolumn)
00434 {
00435 ReInit(dstream, startline, startcolumn, 4096);
00436 }
00437
00438 public void ReInit(java.io.Reader dstream)
00439 {
00440 ReInit(dstream, 1, 1, 4096);
00441 }
00442 public JavaCharStream(java.io.InputStream dstream, String encoding, int startline,
00443 int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException
00444 {
00445 this(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);
00446 }
00447
00448 public JavaCharStream(java.io.InputStream dstream, int startline,
00449 int startcolumn, int buffersize)
00450 {
00451 this(new java.io.InputStreamReader(dstream), startline, startcolumn, 4096);
00452 }
00453
00454 public JavaCharStream(java.io.InputStream dstream, String encoding, int startline,
00455 int startcolumn) throws java.io.UnsupportedEncodingException
00456 {
00457 this(dstream, encoding, startline, startcolumn, 4096);
00458 }
00459
00460 public JavaCharStream(java.io.InputStream dstream, int startline,
00461 int startcolumn)
00462 {
00463 this(dstream, startline, startcolumn, 4096);
00464 }
00465
00466 public JavaCharStream(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException
00467 {
00468 this(dstream, encoding, 1, 1, 4096);
00469 }
00470
00471 public JavaCharStream(java.io.InputStream dstream)
00472 {
00473 this(dstream, 1, 1, 4096);
00474 }
00475
00476 public void ReInit(java.io.InputStream dstream, String encoding, int startline,
00477 int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException
00478 {
00479 ReInit(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);
00480 }
00481
00482 public void ReInit(java.io.InputStream dstream, int startline,
00483 int startcolumn, int buffersize)
00484 {
00485 ReInit(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize);
00486 }
00487 public void ReInit(java.io.InputStream dstream, String encoding, int startline,
00488 int startcolumn) throws java.io.UnsupportedEncodingException
00489 {
00490 ReInit(dstream, encoding, startline, startcolumn, 4096);
00491 }
00492 public void ReInit(java.io.InputStream dstream, int startline,
00493 int startcolumn)
00494 {
00495 ReInit(dstream, startline, startcolumn, 4096);
00496 }
00497 public void ReInit(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException
00498 {
00499 ReInit(dstream, encoding, 1, 1, 4096);
00500 }
00501
00502 public void ReInit(java.io.InputStream dstream)
00503 {
00504 ReInit(dstream, 1, 1, 4096);
00505 }
00506
00507 static public String GetImage()
00508 {
00509 if (bufpos >= tokenBegin)
00510 return new String(buffer, tokenBegin, bufpos - tokenBegin + 1);
00511 else
00512 return new String(buffer, tokenBegin, bufsize - tokenBegin) +
00513 new String(buffer, 0, bufpos + 1);
00514 }
00515
00516 static public char[] GetSuffix(int len)
00517 {
00518 char[] ret = new char[len];
00519
00520 if ((bufpos + 1) >= len)
00521 System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
00522 else
00523 {
00524 System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0,
00525 len - bufpos - 1);
00526 System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
00527 }
00528
00529 return ret;
00530 }
00531
00532 static public void Done()
00533 {
00534 nextCharBuf = null;
00535 buffer = null;
00536 bufline = null;
00537 bufcolumn = null;
00538 }
00539
00543 static public void adjustBeginLineColumn(int newLine, int newCol)
00544 {
00545 int start = tokenBegin;
00546 int len;
00547
00548 if (bufpos >= tokenBegin)
00549 {
00550 len = bufpos - tokenBegin + inBuf + 1;
00551 }
00552 else
00553 {
00554 len = bufsize - tokenBegin + bufpos + 1 + inBuf;
00555 }
00556
00557 int i = 0, j = 0, k = 0;
00558 int nextColDiff = 0, columnDiff = 0;
00559
00560 while (i < len &&
00561 bufline[j = start % bufsize] == bufline[k = ++start % bufsize])
00562 {
00563 bufline[j] = newLine;
00564 nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
00565 bufcolumn[j] = newCol + columnDiff;
00566 columnDiff = nextColDiff;
00567 i++;
00568 }
00569
00570 if (i < len)
00571 {
00572 bufline[j] = newLine++;
00573 bufcolumn[j] = newCol + columnDiff;
00574
00575 while (i++ < len)
00576 {
00577 if (bufline[j = start % bufsize] != bufline[++start % bufsize])
00578 bufline[j] = newLine++;
00579 else
00580 bufline[j] = newLine;
00581 }
00582 }
00583
00584 line = bufline[j];
00585 column = bufcolumn[j];
00586 }
00587
00588 }