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