001/* Generated By:JavaCC: Do not edit this line. ASCII_CharStream.java Version 0.7pre6 */
002
003package jtb.cparser;
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 final class ASCII_CharStream
011{
012  public static final boolean staticFlag = true;
013  static int bufsize;
014  static int available;
015  static int tokenBegin;
016  static public int bufpos = -1;
017  static private int bufline[];
018  static private int bufcolumn[];
019
020  static private int column = 0;
021  static private int line = 1;
022
023  static private boolean prevCharIsCR = false;
024  static private boolean prevCharIsLF = false;
025
026  static private java.io.Reader inputStream;
027
028  static private char[] buffer;
029  static private int maxNextCharInd = 0;
030  static private int inBuf = 0;
031
032  static private final void ExpandBuff(boolean wrapAround)
033  {
034     char[] newbuffer = new char[bufsize + 2048];
035     int newbufline[] = new int[bufsize + 2048];
036     int newbufcolumn[] = new int[bufsize + 2048];
037
038     try
039     {
040        if (wrapAround)
041        {
042           System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
043           System.arraycopy(buffer, 0, newbuffer,
044                                             bufsize - tokenBegin, bufpos);
045           buffer = newbuffer;
046
047           System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
048           System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);
049           bufline = newbufline;
050
051           System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
052           System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);
053           bufcolumn = newbufcolumn;
054
055           maxNextCharInd = (bufpos += (bufsize - tokenBegin));
056        }
057        else
058        {
059           System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
060           buffer = newbuffer;
061
062           System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
063           bufline = newbufline;
064
065           System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
066           bufcolumn = newbufcolumn;
067
068           maxNextCharInd = (bufpos -= tokenBegin);
069        }
070     }
071     catch (Throwable t)
072     {
073        throw new Error(t.getMessage());
074     }
075
076
077     bufsize += 2048;
078     available = bufsize;
079     tokenBegin = 0;
080  }
081
082  static private final void FillBuff() throws java.io.IOException
083  {
084     if (maxNextCharInd == available)
085     {
086        if (available == bufsize)
087        {
088           if (tokenBegin > 2048)
089           {
090              bufpos = maxNextCharInd = 0;
091              available = tokenBegin;
092           }
093           else if (tokenBegin < 0)
094              bufpos = maxNextCharInd = 0;
095           else
096              ExpandBuff(false);
097        }
098        else if (available > tokenBegin)
099           available = bufsize;
100        else if ((tokenBegin - available) < 2048)
101           ExpandBuff(true);
102        else
103           available = tokenBegin;
104     }
105
106     int i;
107     try {
108        if ((i = inputStream.read(buffer, maxNextCharInd,
109                                    available - maxNextCharInd)) == -1)
110        {
111           inputStream.close();
112           throw new java.io.IOException();
113        }
114        else
115           maxNextCharInd += i;
116        return;
117     }
118     catch(java.io.IOException e) {
119        --bufpos;
120        backup(0);
121        if (tokenBegin == -1)
122           tokenBegin = bufpos;
123        throw e;
124     }
125  }
126
127  static public final char BeginToken() throws java.io.IOException
128  {
129     tokenBegin = -1;
130     char c = readChar();
131     tokenBegin = bufpos;
132
133     return c;
134  }
135
136  static private final void UpdateLineColumn(char c)
137  {
138     column++;
139
140     if (prevCharIsLF)
141     {
142        prevCharIsLF = false;
143        line += (column = 1);
144     }
145     else if (prevCharIsCR)
146     {
147        prevCharIsCR = false;
148        if (c == '\n')
149        {
150           prevCharIsLF = true;
151        }
152        else
153           line += (column = 1);
154     }
155
156     switch (c)
157     {
158        case '\r' :
159           prevCharIsCR = true;
160           break;
161        case '\n' :
162           prevCharIsLF = true;
163           break;
164        case '\t' :
165           column--;
166           column += (8 - (column & 07));
167           break;
168        default :
169           break;
170     }
171
172     bufline[bufpos] = line;
173     bufcolumn[bufpos] = column;
174  }
175
176  static public final char readChar() throws java.io.IOException
177  {
178     if (inBuf > 0)
179     {
180        --inBuf;
181        return (char)((char)0xff & buffer[(bufpos == bufsize - 1) ? (bufpos = 0) : ++bufpos]);
182     }
183
184     if (++bufpos >= maxNextCharInd)
185        FillBuff();
186
187     char c = (char)((char)0xff & buffer[bufpos]);
188
189     UpdateLineColumn(c);
190     return (c);
191  }
192
193  /**
194   * @deprecated
195   * @see #getEndColumn
196   */
197  @Deprecated
198  static public final int getColumn() {
199     return bufcolumn[bufpos];
200  }
201
202  /**
203   * @deprecated
204   * @see #getEndLine
205   */
206  @Deprecated
207  static public final int getLine() {
208     return bufline[bufpos];
209  }
210
211  static public final int getEndColumn() {
212     return bufcolumn[bufpos];
213  }
214
215  static public final int getEndLine() {
216     return bufline[bufpos];
217  }
218
219  static public final int getBeginColumn() {
220     return bufcolumn[tokenBegin];
221  }
222
223  static public final int getBeginLine() {
224     return bufline[tokenBegin];
225  }
226
227  static public final void backup(int amount) {
228
229    inBuf += amount;
230    if ((bufpos -= amount) < 0)
231       bufpos += bufsize;
232  }
233
234  public ASCII_CharStream(java.io.Reader dstream, int startline,
235  int startcolumn, int buffersize)
236  {
237    if (inputStream != null)
238       throw new Error("\n   ERROR: Second call to the constructor of a static ASCII_CharStream.  You must\n" +
239       "       either use ReInit() or set the JavaCC option STATIC to false\n" +
240       "       during the generation of this class.");
241    inputStream = dstream;
242    line = startline;
243    column = startcolumn - 1;
244
245    available = bufsize = buffersize;
246    buffer = new char[buffersize];
247    bufline = new int[buffersize];
248    bufcolumn = new int[buffersize];
249  }
250
251  public ASCII_CharStream(java.io.Reader dstream, int startline,
252                                                           int startcolumn)
253  {
254     this(dstream, startline, startcolumn, 4096);
255  }
256  static public void ReInit(java.io.Reader dstream, int startline,
257  int startcolumn, int buffersize)
258  {
259    inputStream = dstream;
260    line = startline;
261    column = startcolumn - 1;
262
263    if (buffer == null || buffersize != buffer.length)
264    {
265      available = bufsize = buffersize;
266      buffer = new char[buffersize];
267      bufline = new int[buffersize];
268      bufcolumn = new int[buffersize];
269    }
270    prevCharIsLF = prevCharIsCR = false;
271    tokenBegin = inBuf = maxNextCharInd = 0;
272    bufpos = -1;
273  }
274
275  static public void ReInit(java.io.Reader dstream, int startline,
276                                                           int startcolumn)
277  {
278     ReInit(dstream, startline, startcolumn, 4096);
279  }
280  public ASCII_CharStream(java.io.InputStream dstream, int startline,
281  int startcolumn, int buffersize)
282  {
283     this(new java.io.InputStreamReader(dstream), startline, startcolumn, 4096);
284  }
285
286  public ASCII_CharStream(java.io.InputStream dstream, int startline,
287                                                           int startcolumn)
288  {
289     this(dstream, startline, startcolumn, 4096);
290  }
291
292  static public void ReInit(java.io.InputStream dstream, int startline,
293  int startcolumn, int buffersize)
294  {
295     ReInit(new java.io.InputStreamReader(dstream), startline, startcolumn, 4096);
296  }
297  static public void ReInit(java.io.InputStream dstream, int startline,
298                                                           int startcolumn)
299  {
300     ReInit(dstream, startline, startcolumn, 4096);
301  }
302  static public final String GetImage()
303  {
304     if (bufpos >= tokenBegin)
305        return new String(buffer, tokenBegin, bufpos - tokenBegin + 1);
306     else
307        return new String(buffer, tokenBegin, bufsize - tokenBegin) +
308                              new String(buffer, 0, bufpos + 1);
309  }
310
311  static public final char[] GetSuffix(int len)
312  {
313     char[] ret = new char[len];
314
315     if ((bufpos + 1) >= len)
316        System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
317     else
318     {
319        System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0,
320                                                          len - bufpos - 1);
321        System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
322     }
323
324     return ret;
325  }
326
327  static public void Done()
328  {
329     buffer = null;
330     bufline = null;
331     bufcolumn = null;
332  }
333
334  /**
335   * Method to adjust line and column numbers for the start of a token.<BR>
336   */
337  static public void adjustBeginLineColumn(int newLine, int newCol)
338  {
339     int start = tokenBegin;
340     int len;
341
342     if (bufpos >= tokenBegin)
343     {
344        len = bufpos - tokenBegin + inBuf + 1;
345     }
346     else
347     {
348        len = bufsize - tokenBegin + bufpos + 1 + inBuf;
349     }
350
351     int i = 0, j = 0, k = 0;
352     int nextColDiff = 0, columnDiff = 0;
353
354     while (i < len &&
355            bufline[j = start % bufsize] == bufline[k = ++start % bufsize])
356     {
357        bufline[j] = newLine;
358        nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
359        bufcolumn[j] = newCol + columnDiff;
360        columnDiff = nextColDiff;
361        i++;
362     }
363
364     if (i < len)
365     {
366        bufline[j] = newLine++;
367        bufcolumn[j] = newCol + columnDiff;
368
369        while (i++ < len)
370        {
371           if (bufline[j = start % bufsize] != bufline[++start % bufsize])
372              bufline[j] = newLine++;
373           else
374              bufline[j] = newLine;
375        }
376     }
377
378     line = bufline[j];
379     column = bufcolumn[j];
380  }
381
382}