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