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