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