1 2 package JSci.maths.symbolic; 3 4 8 9 class SimpleCharStream 10 { 11 public static final boolean staticFlag = true; 12 static int bufsize; 13 static int available; 14 static int tokenBegin; 15 static public int bufpos = -1; 16 static protected int bufline[]; 17 static protected int bufcolumn[]; 18 19 static protected int column = 0; 20 static protected int line = 1; 21 22 static protected boolean prevCharIsCR = false; 23 static protected boolean prevCharIsLF = false; 24 25 static protected java.io.Reader inputStream; 26 27 static protected char[] buffer; 28 static protected int maxNextCharInd = 0; 29 static protected int inBuf = 0; 30 31 static protected void ExpandBuff(boolean wrapAround) 32 { 33 char[] newbuffer = new char[bufsize + 2048]; 34 int newbufline[] = new int[bufsize + 2048]; 35 int newbufcolumn[] = new int[bufsize + 2048]; 36 37 try 38 { 39 if (wrapAround) 40 { 41 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin); 42 System.arraycopy(buffer, 0, newbuffer, 43 bufsize - tokenBegin, bufpos); 44 buffer = newbuffer; 45 46 System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin); 47 System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos); 48 bufline = newbufline; 49 50 System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin); 51 System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos); 52 bufcolumn = newbufcolumn; 53 54 maxNextCharInd = (bufpos += (bufsize - tokenBegin)); 55 } 56 else 57 { 58 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin); 59 buffer = newbuffer; 60 61 System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin); 62 bufline = newbufline; 63 64 System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin); 65 bufcolumn = newbufcolumn; 66 67 maxNextCharInd = (bufpos -= tokenBegin); 68 } 69 } 70 catch (Throwable t) 71 { 72 throw new Error (t.getMessage()); 73 } 74 75 76 bufsize += 2048; 77 available = bufsize; 78 tokenBegin = 0; 79 } 80 81 static protected void FillBuff() throws java.io.IOException 82 { 83 if (maxNextCharInd == available) 84 { 85 if (available == bufsize) 86 { 87 if (tokenBegin > 2048) 88 { 89 bufpos = maxNextCharInd = 0; 90 available = tokenBegin; 91 } 92 else if (tokenBegin < 0) 93 bufpos = maxNextCharInd = 0; 94 else 95 ExpandBuff(false); 96 } 97 else if (available > tokenBegin) 98 available = bufsize; 99 else if ((tokenBegin - available) < 2048) 100 ExpandBuff(true); 101 else 102 available = tokenBegin; 103 } 104 105 int i; 106 try { 107 if ((i = inputStream.read(buffer, maxNextCharInd, 108 available - maxNextCharInd)) == -1) 109 { 110 inputStream.close(); 111 throw new java.io.IOException (); 112 } 113 else 114 maxNextCharInd += i; 115 return; 116 } 117 catch(java.io.IOException e) { 118 --bufpos; 119 backup(0); 120 if (tokenBegin == -1) 121 tokenBegin = bufpos; 122 throw e; 123 } 124 } 125 126 static public char BeginToken() throws java.io.IOException 127 { 128 tokenBegin = -1; 129 char c = readChar(); 130 tokenBegin = bufpos; 131 132 return c; 133 } 134 135 static protected void UpdateLineColumn(char c) 136 { 137 column++; 138 139 if (prevCharIsLF) 140 { 141 prevCharIsLF = false; 142 line += (column = 1); 143 } 144 else if (prevCharIsCR) 145 { 146 prevCharIsCR = false; 147 if (c == '\n') 148 { 149 prevCharIsLF = true; 150 } 151 else 152 line += (column = 1); 153 } 154 155 switch (c) 156 { 157 case '\r' : 158 prevCharIsCR = true; 159 break; 160 case '\n' : 161 prevCharIsLF = true; 162 break; 163 case '\t' : 164 column--; 165 column += (8 - (column & 07)); 166 break; 167 default : 168 break; 169 } 170 171 bufline[bufpos] = line; 172 bufcolumn[bufpos] = column; 173 } 174 175 static public char readChar() throws java.io.IOException 176 { 177 if (inBuf > 0) 178 { 179 --inBuf; 180 181 if (++bufpos == bufsize) 182 bufpos = 0; 183 184 return buffer[bufpos]; 185 } 186 187 if (++bufpos >= maxNextCharInd) 188 FillBuff(); 189 190 char c = buffer[bufpos]; 191 192 UpdateLineColumn(c); 193 return (c); 194 } 195 196 200 201 static public int getColumn() { 202 return bufcolumn[bufpos]; 203 } 204 205 209 210 static public int getLine() { 211 return bufline[bufpos]; 212 } 213 214 static public int getEndColumn() { 215 return bufcolumn[bufpos]; 216 } 217 218 static public int getEndLine() { 219 return bufline[bufpos]; 220 } 221 222 static public int getBeginColumn() { 223 return bufcolumn[tokenBegin]; 224 } 225 226 static public int getBeginLine() { 227 return bufline[tokenBegin]; 228 } 229 230 static public void backup(int amount) { 231 232 inBuf += amount; 233 if ((bufpos -= amount) < 0) 234 bufpos += bufsize; 235 } 236 237 public SimpleCharStream(java.io.Reader dstream, int startline, 238 int startcolumn, int buffersize) 239 { 240 if (inputStream != null) 241 throw new Error ("\n ERROR: Second call to the constructor of a static SimpleCharStream. You must\n" + 242 " either use ReInit() or set the JavaCC option STATIC to false\n" + 243 " during the generation of this class."); 244 inputStream = dstream; 245 line = startline; 246 column = startcolumn - 1; 247 248 available = bufsize = buffersize; 249 buffer = new char[buffersize]; 250 bufline = new int[buffersize]; 251 bufcolumn = new int[buffersize]; 252 } 253 254 public SimpleCharStream(java.io.Reader dstream, int startline, 255 int startcolumn) 256 { 257 this(dstream, startline, startcolumn, 4096); 258 } 259 260 public SimpleCharStream(java.io.Reader dstream) 261 { 262 this(dstream, 1, 1, 4096); 263 } 264 public void ReInit(java.io.Reader dstream, int startline, 265 int startcolumn, int buffersize) 266 { 267 inputStream = dstream; 268 line = startline; 269 column = startcolumn - 1; 270 271 if (buffer == null || buffersize != buffer.length) 272 { 273 available = bufsize = buffersize; 274 buffer = new char[buffersize]; 275 bufline = new int[buffersize]; 276 bufcolumn = new int[buffersize]; 277 } 278 prevCharIsLF = prevCharIsCR = false; 279 tokenBegin = inBuf = maxNextCharInd = 0; 280 bufpos = -1; 281 } 282 283 public void ReInit(java.io.Reader dstream, int startline, 284 int startcolumn) 285 { 286 ReInit(dstream, startline, startcolumn, 4096); 287 } 288 289 public void ReInit(java.io.Reader dstream) 290 { 291 ReInit(dstream, 1, 1, 4096); 292 } 293 public SimpleCharStream(java.io.InputStream dstream, int startline, 294 int startcolumn, int buffersize) 295 { 296 this(new java.io.InputStreamReader (dstream), startline, startcolumn, 4096); 297 } 298 299 public SimpleCharStream(java.io.InputStream dstream, int startline, 300 int startcolumn) 301 { 302 this(dstream, startline, startcolumn, 4096); 303 } 304 305 public SimpleCharStream(java.io.InputStream dstream) 306 { 307 this(dstream, 1, 1, 4096); 308 } 309 310 public void ReInit(java.io.InputStream dstream, int startline, 311 int startcolumn, int buffersize) 312 { 313 ReInit(new java.io.InputStreamReader (dstream), startline, startcolumn, 4096); 314 } 315 316 public void ReInit(java.io.InputStream dstream) 317 { 318 ReInit(dstream, 1, 1, 4096); 319 } 320 public void ReInit(java.io.InputStream dstream, int startline, 321 int startcolumn) 322 { 323 ReInit(dstream, startline, startcolumn, 4096); 324 } 325 static public String GetImage() 326 { 327 if (bufpos >= tokenBegin) 328 return new String (buffer, tokenBegin, bufpos - tokenBegin + 1); 329 else 330 return new String (buffer, tokenBegin, bufsize - tokenBegin) + 331 new String (buffer, 0, bufpos + 1); 332 } 333 334 static public char[] GetSuffix(int len) 335 { 336 char[] ret = new char[len]; 337 338 if ((bufpos + 1) >= len) 339 System.arraycopy(buffer, bufpos - len + 1, ret, 0, len); 340 else 341 { 342 System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0, 343 len - bufpos - 1); 344 System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1); 345 } 346 347 return ret; 348 } 349 350 static public void Done() 351 { 352 buffer = null; 353 bufline = null; 354 bufcolumn = null; 355 } 356 357 360 static public void adjustBeginLineColumn(int newLine, int newCol) 361 { 362 int start = tokenBegin; 363 int len; 364 365 if (bufpos >= tokenBegin) 366 { 367 len = bufpos - tokenBegin + inBuf + 1; 368 } 369 else 370 { 371 len = bufsize - tokenBegin + bufpos + 1 + inBuf; 372 } 373 374 int i = 0, j = 0, k = 0; 375 int nextColDiff = 0, columnDiff = 0; 376 377 while (i < len && 378 bufline[j = start % bufsize] == bufline[k = ++start % bufsize]) 379 { 380 bufline[j] = newLine; 381 nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j]; 382 bufcolumn[j] = newCol + columnDiff; 383 columnDiff = nextColDiff; 384 i++; 385 } 386 387 if (i < len) 388 { 389 bufline[j] = newLine++; 390 bufcolumn[j] = newCol + columnDiff; 391 392 while (i++ < len) 393 { 394 if (bufline[j = start % bufsize] != bufline[++start % bufsize]) 395 bufline[j] = newLine++; 396 else 397 bufline[j] = newLine; 398 } 399 } 400 401 line = bufline[j]; 402 column = bufcolumn[j]; 403 } 404 405 } 406 | Popular Tags |