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