1 /* 2 * Avis event router. 3 * 4 * Copyright (C) 2008 Matthew Phillips <avis@mattp.name> 5 * 6 * This program is free software: you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License 8 * version 3 as published by the Free Software Foundation. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 * General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 */ 18 /* Generated By:JavaCC: Do not edit this line. SimpleCharStream.java Version 4.0 */ 19 package org.avis.subscription.parser; 20 21 /** 22 * An implementation of interface CharStream, where the stream is assumed to 23 * contain only ASCII characters (without unicode processing). 24 */ 25 @SuppressWarnings("all") 26 public class SimpleCharStream 27 { 28 public static final boolean staticFlag = false; 29 int bufsize; 30 int available; 31 int tokenBegin; 32 public int bufpos = -1; 33 protected int bufline[]; 34 protected int bufcolumn[]; 35 36 protected int column = 0; 37 protected int line = 1; 38 39 protected boolean prevCharIsCR = false; 40 protected boolean prevCharIsLF = false; 41 42 protected java.io.Reader inputStream; 43 44 protected char[] buffer; 45 protected int maxNextCharInd = 0; 46 protected int inBuf = 0; 47 protected int tabSize = 8; 48 setTabSize(int i)49 protected void setTabSize(int i) { tabSize = i; } getTabSize(int i)50 protected int getTabSize(int i) { return tabSize; } 51 52 ExpandBuff(boolean wrapAround)53 protected void ExpandBuff(boolean wrapAround) 54 { 55 char[] newbuffer = new char[bufsize + 2048]; 56 int newbufline[] = new int[bufsize + 2048]; 57 int newbufcolumn[] = new int[bufsize + 2048]; 58 59 try 60 { 61 if (wrapAround) 62 { 63 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin); 64 System.arraycopy(buffer, 0, newbuffer, 65 bufsize - tokenBegin, bufpos); 66 buffer = newbuffer; 67 68 System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin); 69 System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos); 70 bufline = newbufline; 71 72 System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin); 73 System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos); 74 bufcolumn = newbufcolumn; 75 76 maxNextCharInd = (bufpos += (bufsize - tokenBegin)); 77 } 78 else 79 { 80 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin); 81 buffer = newbuffer; 82 83 System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin); 84 bufline = newbufline; 85 86 System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin); 87 bufcolumn = newbufcolumn; 88 89 maxNextCharInd = (bufpos -= tokenBegin); 90 } 91 } 92 catch (Throwable t) 93 { 94 throw new Error(t.getMessage()); 95 } 96 97 98 bufsize += 2048; 99 available = bufsize; 100 tokenBegin = 0; 101 } 102 FillBuff()103 protected void FillBuff() throws java.io.IOException 104 { 105 if (maxNextCharInd == available) 106 { 107 if (available == bufsize) 108 { 109 if (tokenBegin > 2048) 110 { 111 bufpos = maxNextCharInd = 0; 112 available = tokenBegin; 113 } 114 else if (tokenBegin < 0) 115 bufpos = maxNextCharInd = 0; 116 else 117 ExpandBuff(false); 118 } 119 else if (available > tokenBegin) 120 available = bufsize; 121 else if ((tokenBegin - available) < 2048) 122 ExpandBuff(true); 123 else 124 available = tokenBegin; 125 } 126 127 int i; 128 try { 129 if ((i = inputStream.read(buffer, maxNextCharInd, 130 available - maxNextCharInd)) == -1) 131 { 132 inputStream.close(); 133 throw new java.io.IOException(); 134 } 135 else 136 maxNextCharInd += i; 137 return; 138 } 139 catch(java.io.IOException e) { 140 --bufpos; 141 backup(0); 142 if (tokenBegin == -1) 143 tokenBegin = bufpos; 144 throw e; 145 } 146 } 147 BeginToken()148 public char BeginToken() throws java.io.IOException 149 { 150 tokenBegin = -1; 151 char c = readChar(); 152 tokenBegin = bufpos; 153 154 return c; 155 } 156 UpdateLineColumn(char c)157 protected void UpdateLineColumn(char c) 158 { 159 column++; 160 161 if (prevCharIsLF) 162 { 163 prevCharIsLF = false; 164 line += (column = 1); 165 } 166 else if (prevCharIsCR) 167 { 168 prevCharIsCR = false; 169 if (c == '\n') 170 { 171 prevCharIsLF = true; 172 } 173 else 174 line += (column = 1); 175 } 176 177 switch (c) 178 { 179 case '\r' : 180 prevCharIsCR = true; 181 break; 182 case '\n' : 183 prevCharIsLF = true; 184 break; 185 case '\t' : 186 column--; 187 column += (tabSize - (column % tabSize)); 188 break; 189 default : 190 break; 191 } 192 193 bufline[bufpos] = line; 194 bufcolumn[bufpos] = column; 195 } 196 readChar()197 public char readChar() throws java.io.IOException 198 { 199 if (inBuf > 0) 200 { 201 --inBuf; 202 203 if (++bufpos == bufsize) 204 bufpos = 0; 205 206 return buffer[bufpos]; 207 } 208 209 if (++bufpos >= maxNextCharInd) 210 FillBuff(); 211 212 char c = buffer[bufpos]; 213 214 UpdateLineColumn(c); 215 return (c); 216 } 217 218 /** 219 * @deprecated 220 * @see #getEndColumn 221 */ 222 getColumn()223 public int getColumn() { 224 return bufcolumn[bufpos]; 225 } 226 227 /** 228 * @deprecated 229 * @see #getEndLine 230 */ 231 getLine()232 public int getLine() { 233 return bufline[bufpos]; 234 } 235 getEndColumn()236 public int getEndColumn() { 237 return bufcolumn[bufpos]; 238 } 239 getEndLine()240 public int getEndLine() { 241 return bufline[bufpos]; 242 } 243 getBeginColumn()244 public int getBeginColumn() { 245 return bufcolumn[tokenBegin]; 246 } 247 getBeginLine()248 public int getBeginLine() { 249 return bufline[tokenBegin]; 250 } 251 backup(int amount)252 public void backup(int amount) { 253 254 inBuf += amount; 255 if ((bufpos -= amount) < 0) 256 bufpos += bufsize; 257 } 258 SimpleCharStream(java.io.Reader dstream, int startline, int startcolumn, int buffersize)259 public SimpleCharStream(java.io.Reader dstream, int startline, 260 int startcolumn, int buffersize) 261 { 262 inputStream = dstream; 263 line = startline; 264 column = startcolumn - 1; 265 266 available = bufsize = buffersize; 267 buffer = new char[buffersize]; 268 bufline = new int[buffersize]; 269 bufcolumn = new int[buffersize]; 270 } 271 SimpleCharStream(java.io.Reader dstream, int startline, int startcolumn)272 public SimpleCharStream(java.io.Reader dstream, int startline, 273 int startcolumn) 274 { 275 this(dstream, startline, startcolumn, 4096); 276 } 277 SimpleCharStream(java.io.Reader dstream)278 public SimpleCharStream(java.io.Reader dstream) 279 { 280 this(dstream, 1, 1, 4096); 281 } ReInit(java.io.Reader dstream, int startline, int startcolumn, int buffersize)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 ReInit(java.io.Reader dstream, int startline, int startcolumn)301 public void ReInit(java.io.Reader dstream, int startline, 302 int startcolumn) 303 { 304 ReInit(dstream, startline, startcolumn, 4096); 305 } 306 ReInit(java.io.Reader dstream)307 public void ReInit(java.io.Reader dstream) 308 { 309 ReInit(dstream, 1, 1, 4096); 310 } SimpleCharStream(java.io.InputStream dstream, String encoding, int startline, int startcolumn, int buffersize)311 public SimpleCharStream(java.io.InputStream dstream, String encoding, int startline, 312 int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException 313 { 314 this(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize); 315 } 316 SimpleCharStream(java.io.InputStream dstream, int startline, int startcolumn, int buffersize)317 public SimpleCharStream(java.io.InputStream dstream, int startline, 318 int startcolumn, int buffersize) 319 { 320 this(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize); 321 } 322 SimpleCharStream(java.io.InputStream dstream, String encoding, int startline, int startcolumn)323 public SimpleCharStream(java.io.InputStream dstream, String encoding, int startline, 324 int startcolumn) throws java.io.UnsupportedEncodingException 325 { 326 this(dstream, encoding, startline, startcolumn, 4096); 327 } 328 SimpleCharStream(java.io.InputStream dstream, int startline, int startcolumn)329 public SimpleCharStream(java.io.InputStream dstream, int startline, 330 int startcolumn) 331 { 332 this(dstream, startline, startcolumn, 4096); 333 } 334 SimpleCharStream(java.io.InputStream dstream, String encoding)335 public SimpleCharStream(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException 336 { 337 this(dstream, encoding, 1, 1, 4096); 338 } 339 SimpleCharStream(java.io.InputStream dstream)340 public SimpleCharStream(java.io.InputStream dstream) 341 { 342 this(dstream, 1, 1, 4096); 343 } 344 ReInit(java.io.InputStream dstream, String encoding, int startline, int startcolumn, int buffersize)345 public void ReInit(java.io.InputStream dstream, String encoding, int startline, 346 int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException 347 { 348 ReInit(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize); 349 } 350 ReInit(java.io.InputStream dstream, int startline, int startcolumn, int buffersize)351 public void ReInit(java.io.InputStream dstream, int startline, 352 int startcolumn, int buffersize) 353 { 354 ReInit(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize); 355 } 356 ReInit(java.io.InputStream dstream, String encoding)357 public void ReInit(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException 358 { 359 ReInit(dstream, encoding, 1, 1, 4096); 360 } 361 ReInit(java.io.InputStream dstream)362 public void ReInit(java.io.InputStream dstream) 363 { 364 ReInit(dstream, 1, 1, 4096); 365 } ReInit(java.io.InputStream dstream, String encoding, int startline, int startcolumn)366 public void ReInit(java.io.InputStream dstream, String encoding, int startline, 367 int startcolumn) throws java.io.UnsupportedEncodingException 368 { 369 ReInit(dstream, encoding, startline, startcolumn, 4096); 370 } ReInit(java.io.InputStream dstream, int startline, int startcolumn)371 public void ReInit(java.io.InputStream dstream, int startline, 372 int startcolumn) 373 { 374 ReInit(dstream, startline, startcolumn, 4096); 375 } GetImage()376 public String GetImage() 377 { 378 if (bufpos >= tokenBegin) 379 return new String(buffer, tokenBegin, bufpos - tokenBegin + 1); 380 else 381 return new String(buffer, tokenBegin, bufsize - tokenBegin) + 382 new String(buffer, 0, bufpos + 1); 383 } 384 GetSuffix(int len)385 public char[] GetSuffix(int len) 386 { 387 char[] ret = new char[len]; 388 389 if ((bufpos + 1) >= len) 390 System.arraycopy(buffer, bufpos - len + 1, ret, 0, len); 391 else 392 { 393 System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0, 394 len - bufpos - 1); 395 System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1); 396 } 397 398 return ret; 399 } 400 Done()401 public void Done() 402 { 403 buffer = null; 404 bufline = null; 405 bufcolumn = null; 406 } 407 408 /** 409 * Method to adjust line and column numbers for the start of a token. 410 */ adjustBeginLineColumn(int newLine, int newCol)411 public void adjustBeginLineColumn(int newLine, int newCol) 412 { 413 int start = tokenBegin; 414 int len; 415 416 if (bufpos >= tokenBegin) 417 { 418 len = bufpos - tokenBegin + inBuf + 1; 419 } 420 else 421 { 422 len = bufsize - tokenBegin + bufpos + 1 + inBuf; 423 } 424 425 int i = 0, j = 0, k = 0; 426 int nextColDiff = 0, columnDiff = 0; 427 428 while (i < len && 429 bufline[j = start % bufsize] == bufline[k = ++start % bufsize]) 430 { 431 bufline[j] = newLine; 432 nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j]; 433 bufcolumn[j] = newCol + columnDiff; 434 columnDiff = nextColDiff; 435 i++; 436 } 437 438 if (i < len) 439 { 440 bufline[j] = newLine++; 441 bufcolumn[j] = newCol + columnDiff; 442 443 while (i++ < len) 444 { 445 if (bufline[j = start % bufsize] != bufline[++start % bufsize]) 446 bufline[j] = newLine++; 447 else 448 bufline[j] = newLine; 449 } 450 } 451 452 line = bufline[j]; 453 column = bufcolumn[j]; 454 } 455 456 } 457