001 /* Generated By:JavaCC: Do not edit this line. SimpleCharStream.java Version 4.0 */ 002 package org.hackystat.telemetry.analyzer.language.parser.impl; 003 004 /** 005 * An implementation of interface CharStream, where the stream is assumed to 006 * contain only ASCII characters (without unicode processing). 007 */ 008 009 public class SimpleCharStream 010 { 011 public static final boolean staticFlag = false; 012 int bufsize; 013 int available; 014 int tokenBegin; 015 public int bufpos = -1; 016 protected int bufline[]; 017 protected int bufcolumn[]; 018 019 protected int column = 0; 020 protected int line = 1; 021 022 protected boolean prevCharIsCR = false; 023 protected boolean prevCharIsLF = false; 024 025 protected java.io.Reader inputStream; 026 027 protected char[] buffer; 028 protected int maxNextCharInd = 0; 029 protected int inBuf = 0; 030 protected int tabSize = 8; 031 032 protected void setTabSize(int i) { tabSize = i; } 033 protected int getTabSize(int i) { return tabSize; } 034 035 036 protected void ExpandBuff(boolean wrapAround) 037 { 038 char[] newbuffer = new char[bufsize + 2048]; 039 int newbufline[] = new int[bufsize + 2048]; 040 int newbufcolumn[] = new int[bufsize + 2048]; 041 042 try 043 { 044 if (wrapAround) 045 { 046 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin); 047 System.arraycopy(buffer, 0, newbuffer, 048 bufsize - tokenBegin, bufpos); 049 buffer = newbuffer; 050 051 System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin); 052 System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos); 053 bufline = newbufline; 054 055 System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin); 056 System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos); 057 bufcolumn = newbufcolumn; 058 059 maxNextCharInd = (bufpos += (bufsize - tokenBegin)); 060 } 061 else 062 { 063 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin); 064 buffer = newbuffer; 065 066 System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin); 067 bufline = newbufline; 068 069 System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin); 070 bufcolumn = newbufcolumn; 071 072 maxNextCharInd = (bufpos -= tokenBegin); 073 } 074 } 075 catch (Throwable t) 076 { 077 throw new Error(t.getMessage()); 078 } 079 080 081 bufsize += 2048; 082 available = bufsize; 083 tokenBegin = 0; 084 } 085 086 protected void FillBuff() throws java.io.IOException 087 { 088 if (maxNextCharInd == available) 089 { 090 if (available == bufsize) 091 { 092 if (tokenBegin > 2048) 093 { 094 bufpos = maxNextCharInd = 0; 095 available = tokenBegin; 096 } 097 else if (tokenBegin < 0) 098 bufpos = maxNextCharInd = 0; 099 else 100 ExpandBuff(false); 101 } 102 else if (available > tokenBegin) 103 available = bufsize; 104 else if ((tokenBegin - available) < 2048) 105 ExpandBuff(true); 106 else 107 available = tokenBegin; 108 } 109 110 int i; 111 try { 112 if ((i = inputStream.read(buffer, maxNextCharInd, 113 available - maxNextCharInd)) == -1) 114 { 115 inputStream.close(); 116 throw new java.io.IOException(); 117 } 118 else 119 maxNextCharInd += i; 120 return; 121 } 122 catch(java.io.IOException e) { 123 --bufpos; 124 backup(0); 125 if (tokenBegin == -1) 126 tokenBegin = bufpos; 127 throw e; 128 } 129 } 130 131 public char BeginToken() throws java.io.IOException 132 { 133 tokenBegin = -1; 134 char c = readChar(); 135 tokenBegin = bufpos; 136 137 return c; 138 } 139 140 protected void UpdateLineColumn(char c) 141 { 142 column++; 143 144 if (prevCharIsLF) 145 { 146 prevCharIsLF = false; 147 line += (column = 1); 148 } 149 else if (prevCharIsCR) 150 { 151 prevCharIsCR = false; 152 if (c == '\n') 153 { 154 prevCharIsLF = true; 155 } 156 else 157 line += (column = 1); 158 } 159 160 switch (c) 161 { 162 case '\r' : 163 prevCharIsCR = true; 164 break; 165 case '\n' : 166 prevCharIsLF = true; 167 break; 168 case '\t' : 169 column--; 170 column += (tabSize - (column % tabSize)); 171 break; 172 default : 173 break; 174 } 175 176 bufline[bufpos] = line; 177 bufcolumn[bufpos] = column; 178 } 179 180 public char readChar() throws java.io.IOException 181 { 182 if (inBuf > 0) 183 { 184 --inBuf; 185 186 if (++bufpos == bufsize) 187 bufpos = 0; 188 189 return buffer[bufpos]; 190 } 191 192 if (++bufpos >= maxNextCharInd) 193 FillBuff(); 194 195 char c = buffer[bufpos]; 196 197 UpdateLineColumn(c); 198 return (c); 199 } 200 201 // /** 202 // * @Deprecated 203 // * @see #getEndColumn 204 // */ 205 // public int getColumn() { 206 // return bufcolumn[bufpos]; 207 // } 208 // 209 // /** 210 // * @Deprecated 211 // * @see #getEndLine 212 // */ 213 // public int getLine() { 214 // return bufline[bufpos]; 215 // } 216 217 public int getEndColumn() { 218 return bufcolumn[bufpos]; 219 } 220 221 public int getEndLine() { 222 return bufline[bufpos]; 223 } 224 225 public int getBeginColumn() { 226 return bufcolumn[tokenBegin]; 227 } 228 229 public int getBeginLine() { 230 return bufline[tokenBegin]; 231 } 232 233 public void backup(int amount) { 234 235 inBuf += amount; 236 if ((bufpos -= amount) < 0) 237 bufpos += bufsize; 238 } 239 240 public SimpleCharStream(java.io.Reader dstream, int startline, 241 int startcolumn, int buffersize) 242 { 243 inputStream = dstream; 244 line = startline; 245 column = startcolumn - 1; 246 247 available = bufsize = buffersize; 248 buffer = new char[buffersize]; 249 bufline = new int[buffersize]; 250 bufcolumn = new int[buffersize]; 251 } 252 253 public SimpleCharStream(java.io.Reader dstream, int startline, 254 int startcolumn) 255 { 256 this(dstream, startline, startcolumn, 4096); 257 } 258 259 public SimpleCharStream(java.io.Reader dstream) 260 { 261 this(dstream, 1, 1, 4096); 262 } 263 public void ReInit(java.io.Reader dstream, int startline, 264 int startcolumn, int buffersize) 265 { 266 inputStream = dstream; 267 line = startline; 268 column = startcolumn - 1; 269 270 if (buffer == null || buffersize != buffer.length) 271 { 272 available = bufsize = buffersize; 273 buffer = new char[buffersize]; 274 bufline = new int[buffersize]; 275 bufcolumn = new int[buffersize]; 276 } 277 prevCharIsLF = prevCharIsCR = false; 278 tokenBegin = inBuf = maxNextCharInd = 0; 279 bufpos = -1; 280 } 281 282 public void ReInit(java.io.Reader dstream, int startline, 283 int startcolumn) 284 { 285 ReInit(dstream, startline, startcolumn, 4096); 286 } 287 288 public void ReInit(java.io.Reader dstream) 289 { 290 ReInit(dstream, 1, 1, 4096); 291 } 292 public SimpleCharStream(java.io.InputStream dstream, String encoding, int startline, 293 int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException 294 { 295 this(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize); 296 } 297 298 public SimpleCharStream(java.io.InputStream dstream, int startline, 299 int startcolumn, int buffersize) 300 { 301 this(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize); 302 } 303 304 public SimpleCharStream(java.io.InputStream dstream, String encoding, int startline, 305 int startcolumn) throws java.io.UnsupportedEncodingException 306 { 307 this(dstream, encoding, startline, startcolumn, 4096); 308 } 309 310 public SimpleCharStream(java.io.InputStream dstream, int startline, 311 int startcolumn) 312 { 313 this(dstream, startline, startcolumn, 4096); 314 } 315 316 public SimpleCharStream(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException 317 { 318 this(dstream, encoding, 1, 1, 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, String encoding, int startline, 327 int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException 328 { 329 ReInit(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize); 330 } 331 332 public void ReInit(java.io.InputStream dstream, int startline, 333 int startcolumn, int buffersize) 334 { 335 ReInit(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize); 336 } 337 338 public void ReInit(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException 339 { 340 ReInit(dstream, encoding, 1, 1, 4096); 341 } 342 343 public void ReInit(java.io.InputStream dstream) 344 { 345 ReInit(dstream, 1, 1, 4096); 346 } 347 public void ReInit(java.io.InputStream dstream, String encoding, int startline, 348 int startcolumn) throws java.io.UnsupportedEncodingException 349 { 350 ReInit(dstream, encoding, startline, startcolumn, 4096); 351 } 352 public void ReInit(java.io.InputStream dstream, int startline, 353 int startcolumn) 354 { 355 ReInit(dstream, startline, startcolumn, 4096); 356 } 357 public String GetImage() 358 { 359 if (bufpos >= tokenBegin) 360 return new String(buffer, tokenBegin, bufpos - tokenBegin + 1); 361 else 362 return new String(buffer, tokenBegin, bufsize - tokenBegin) + 363 new String(buffer, 0, bufpos + 1); 364 } 365 366 public char[] GetSuffix(int len) 367 { 368 char[] ret = new char[len]; 369 370 if ((bufpos + 1) >= len) 371 System.arraycopy(buffer, bufpos - len + 1, ret, 0, len); 372 else 373 { 374 System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0, 375 len - bufpos - 1); 376 System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1); 377 } 378 379 return ret; 380 } 381 382 public void Done() 383 { 384 buffer = null; 385 bufline = null; 386 bufcolumn = null; 387 } 388 389 /** 390 * Method to adjust line and column numbers for the start of a token. 391 */ 392 public void adjustBeginLineColumn(int newLine, int newCol) 393 { 394 int start = tokenBegin; 395 int len; 396 397 if (bufpos >= tokenBegin) 398 { 399 len = bufpos - tokenBegin + inBuf + 1; 400 } 401 else 402 { 403 len = bufsize - tokenBegin + bufpos + 1 + inBuf; 404 } 405 406 int i = 0, j = 0, k = 0; 407 int nextColDiff = 0, columnDiff = 0; 408 409 while (i < len && 410 bufline[j = start % bufsize] == bufline[k = ++start % bufsize]) 411 { 412 bufline[j] = newLine; 413 nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j]; 414 bufcolumn[j] = newCol + columnDiff; 415 columnDiff = nextColDiff; 416 i++; 417 } 418 419 if (i < len) 420 { 421 bufline[j] = newLine++; 422 bufcolumn[j] = newCol + columnDiff; 423 424 while (i++ < len) 425 { 426 if (bufline[j = start % bufsize] != bufline[++start % bufsize]) 427 bufline[j] = newLine++; 428 else 429 bufline[j] = newLine; 430 } 431 } 432 433 line = bufline[j]; 434 column = bufcolumn[j]; 435 } 436 437 }