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