001/* Generated By:JavaCC: Do not edit this line. SimpleCharStream.java Version 4.0 */ 002/** 003 * 004 * Licensed to the Apache Software Foundation (ASF) under one or more 005 * contributor license agreements. See the NOTICE file distributed with 006 * this work for additional information regarding copyright ownership. 007 * The ASF licenses this file to You under the Apache License, Version 2.0 008 * (the "License"); you may not use this file except in compliance with 009 * the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 */ 019 020package org.granite.gravity.selector; 021 022/** 023 * An implementation of interface CharStream, where the stream is assumed to 024 * contain only ASCII characters (without unicode processing). 025 */ 026 027public class SimpleCharStream 028{ 029 public static final boolean staticFlag = false; 030 int bufsize; 031 int available; 032 int tokenBegin; 033 public int bufpos = -1; 034 protected int bufline[]; 035 protected int bufcolumn[]; 036 037 protected int column = 0; 038 protected int line = 1; 039 040 protected boolean prevCharIsCR = false; 041 protected boolean prevCharIsLF = false; 042 043 protected java.io.Reader inputStream; 044 045 protected char[] buffer; 046 protected int maxNextCharInd = 0; 047 protected int inBuf = 0; 048 protected int tabSize = 8; 049 050 protected void setTabSize(int i) { tabSize = i; } 051 protected int getTabSize(int i) { return tabSize; } 052 053 054 protected void ExpandBuff(boolean wrapAround) 055 { 056 char[] newbuffer = new char[bufsize + 2048]; 057 int newbufline[] = new int[bufsize + 2048]; 058 int newbufcolumn[] = new int[bufsize + 2048]; 059 060 try 061 { 062 if (wrapAround) 063 { 064 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin); 065 System.arraycopy(buffer, 0, newbuffer, 066 bufsize - tokenBegin, bufpos); 067 buffer = newbuffer; 068 069 System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin); 070 System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos); 071 bufline = newbufline; 072 073 System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin); 074 System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos); 075 bufcolumn = newbufcolumn; 076 077 maxNextCharInd = (bufpos += (bufsize - tokenBegin)); 078 } 079 else 080 { 081 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin); 082 buffer = newbuffer; 083 084 System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin); 085 bufline = newbufline; 086 087 System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin); 088 bufcolumn = newbufcolumn; 089 090 maxNextCharInd = (bufpos -= tokenBegin); 091 } 092 } 093 catch (Throwable t) 094 { 095 throw new Error(t.getMessage()); 096 } 097 098 099 bufsize += 2048; 100 available = bufsize; 101 tokenBegin = 0; 102 } 103 104 protected void FillBuff() throws java.io.IOException 105 { 106 if (maxNextCharInd == available) 107 { 108 if (available == bufsize) 109 { 110 if (tokenBegin > 2048) 111 { 112 bufpos = maxNextCharInd = 0; 113 available = tokenBegin; 114 } 115 else if (tokenBegin < 0) 116 bufpos = maxNextCharInd = 0; 117 else 118 ExpandBuff(false); 119 } 120 else if (available > tokenBegin) 121 available = bufsize; 122 else if ((tokenBegin - available) < 2048) 123 ExpandBuff(true); 124 else 125 available = tokenBegin; 126 } 127 128 int i; 129 try { 130 if ((i = inputStream.read(buffer, maxNextCharInd, 131 available - maxNextCharInd)) == -1) 132 { 133 inputStream.close(); 134 throw new java.io.IOException(); 135 } 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 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 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 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 223 @SuppressWarnings("dep-ann") 224public int getColumn() { 225 return bufcolumn[bufpos]; 226 } 227 228 /** 229 * @deprecated 230 * @see #getEndLine 231 */ 232 233 @SuppressWarnings("dep-ann") 234public int getLine() { 235 return bufline[bufpos]; 236 } 237 238 public int getEndColumn() { 239 return bufcolumn[bufpos]; 240 } 241 242 public int getEndLine() { 243 return bufline[bufpos]; 244 } 245 246 public int getBeginColumn() { 247 return bufcolumn[tokenBegin]; 248 } 249 250 public int getBeginLine() { 251 return bufline[tokenBegin]; 252 } 253 254 public void backup(int amount) { 255 256 inBuf += amount; 257 if ((bufpos -= amount) < 0) 258 bufpos += bufsize; 259 } 260 261 public SimpleCharStream(java.io.Reader dstream, int startline, 262 int startcolumn, int buffersize) 263 { 264 inputStream = dstream; 265 line = startline; 266 column = startcolumn - 1; 267 268 available = bufsize = buffersize; 269 buffer = new char[buffersize]; 270 bufline = new int[buffersize]; 271 bufcolumn = new int[buffersize]; 272 } 273 274 public SimpleCharStream(java.io.Reader dstream, int startline, 275 int startcolumn) 276 { 277 this(dstream, startline, startcolumn, 4096); 278 } 279 280 public SimpleCharStream(java.io.Reader dstream) 281 { 282 this(dstream, 1, 1, 4096); 283 } 284 public void ReInit(java.io.Reader dstream, int startline, 285 int startcolumn, int buffersize) 286 { 287 inputStream = dstream; 288 line = startline; 289 column = startcolumn - 1; 290 291 if (buffer == null || buffersize != buffer.length) 292 { 293 available = bufsize = buffersize; 294 buffer = new char[buffersize]; 295 bufline = new int[buffersize]; 296 bufcolumn = new int[buffersize]; 297 } 298 prevCharIsLF = prevCharIsCR = false; 299 tokenBegin = inBuf = maxNextCharInd = 0; 300 bufpos = -1; 301 } 302 303 public void ReInit(java.io.Reader dstream, int startline, 304 int startcolumn) 305 { 306 ReInit(dstream, startline, startcolumn, 4096); 307 } 308 309 public void ReInit(java.io.Reader dstream) 310 { 311 ReInit(dstream, 1, 1, 4096); 312 } 313 public SimpleCharStream(java.io.InputStream dstream, String encoding, int startline, 314 int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException 315 { 316 this(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize); 317 } 318 319 public SimpleCharStream(java.io.InputStream dstream, int startline, 320 int startcolumn, int buffersize) 321 { 322 this(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize); 323 } 324 325 public SimpleCharStream(java.io.InputStream dstream, String encoding, int startline, 326 int startcolumn) throws java.io.UnsupportedEncodingException 327 { 328 this(dstream, encoding, startline, startcolumn, 4096); 329 } 330 331 public SimpleCharStream(java.io.InputStream dstream, int startline, 332 int startcolumn) 333 { 334 this(dstream, startline, startcolumn, 4096); 335 } 336 337 public SimpleCharStream(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException 338 { 339 this(dstream, encoding, 1, 1, 4096); 340 } 341 342 public SimpleCharStream(java.io.InputStream dstream) 343 { 344 this(dstream, 1, 1, 4096); 345 } 346 347 public void ReInit(java.io.InputStream dstream, String encoding, int startline, 348 int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException 349 { 350 ReInit(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize); 351 } 352 353 public void ReInit(java.io.InputStream dstream, int startline, 354 int startcolumn, int buffersize) 355 { 356 ReInit(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize); 357 } 358 359 public void ReInit(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException 360 { 361 ReInit(dstream, encoding, 1, 1, 4096); 362 } 363 364 public void ReInit(java.io.InputStream dstream) 365 { 366 ReInit(dstream, 1, 1, 4096); 367 } 368 public void ReInit(java.io.InputStream dstream, String encoding, int startline, 369 int startcolumn) throws java.io.UnsupportedEncodingException 370 { 371 ReInit(dstream, encoding, startline, startcolumn, 4096); 372 } 373 public void ReInit(java.io.InputStream dstream, int startline, 374 int startcolumn) 375 { 376 ReInit(dstream, startline, startcolumn, 4096); 377 } 378 public String GetImage() 379 { 380 if (bufpos >= tokenBegin) 381 return new String(buffer, tokenBegin, bufpos - tokenBegin + 1); 382 return new String(buffer, tokenBegin, bufsize - tokenBegin) + 383 new String(buffer, 0, bufpos + 1); 384 } 385 386 public char[] GetSuffix(int len) 387 { 388 char[] ret = new char[len]; 389 390 if ((bufpos + 1) >= len) 391 System.arraycopy(buffer, bufpos - len + 1, ret, 0, len); 392 else 393 { 394 System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0, 395 len - bufpos - 1); 396 System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1); 397 } 398 399 return ret; 400 } 401 402 public void Done() 403 { 404 buffer = null; 405 bufline = null; 406 bufcolumn = null; 407 } 408 409 /** 410 * Method to adjust line and column numbers for the start of a token. 411 */ 412 public void adjustBeginLineColumn(int newLine, int newCol) 413 { 414 int start = tokenBegin; 415 int len; 416 417 if (bufpos >= tokenBegin) 418 { 419 len = bufpos - tokenBegin + inBuf + 1; 420 } 421 else 422 { 423 len = bufsize - tokenBegin + bufpos + 1 + inBuf; 424 } 425 426 int i = 0, j = 0, k = 0; 427 int nextColDiff = 0, columnDiff = 0; 428 429 while (i < len && 430 bufline[j = start % bufsize] == bufline[k = ++start % bufsize]) 431 { 432 bufline[j] = newLine; 433 nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j]; 434 bufcolumn[j] = newCol + columnDiff; 435 columnDiff = nextColDiff; 436 i++; 437 } 438 439 if (i < len) 440 { 441 bufline[j] = newLine++; 442 bufcolumn[j] = newCol + columnDiff; 443 444 while (i++ < len) 445 { 446 if (bufline[j = start % bufsize] != bufline[++start % bufsize]) 447 bufline[j] = newLine++; 448 else 449 bufline[j] = newLine; 450 } 451 } 452 453 line = bufline[j]; 454 column = bufcolumn[j]; 455 } 456 457}