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