001    /* Generated By:JavaCC: Do not edit this line. SimpleCharStream.java Version 4.1 */
002    /* JavaCCOptions:STATIC=false */
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 573 2009-09-25 16:05:05Z 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,
083                                                 bufsize - tokenBegin, bufpos);
084               buffer = newbuffer;
085    
086               System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
087               System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);
088               bufline = newbufline;
089    
090               System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
091               System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);
092               bufcolumn = newbufcolumn;
093    
094               maxNextCharInd = (bufpos += (bufsize - tokenBegin));
095            }
096            else
097            {
098               System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
099               buffer = newbuffer;
100    
101               System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
102               bufline = newbufline;
103    
104               System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
105               bufcolumn = newbufcolumn;
106    
107               maxNextCharInd = (bufpos -= tokenBegin);
108            }
109         }
110         catch (Throwable t)
111         {
112            throw new Error(t.getMessage());
113         }
114    
115    
116         bufsize += 2048;
117         available = bufsize;
118         tokenBegin = 0;
119      }
120    
121      protected void FillBuff() throws java.io.IOException
122      {
123         if (maxNextCharInd == available)
124         {
125            if (available == bufsize)
126            {
127               if (tokenBegin > 2048)
128               {
129                  bufpos = maxNextCharInd = 0;
130                  available = tokenBegin;
131               }
132               else if (tokenBegin < 0)
133                  bufpos = maxNextCharInd = 0;
134               else
135                  ExpandBuff(false);
136            }
137            else if (available > tokenBegin)
138               available = bufsize;
139            else if ((tokenBegin - available) < 2048)
140               ExpandBuff(true);
141            else
142               available = tokenBegin;
143         }
144    
145         int i;
146         try {
147            if ((i = inputStream.read(buffer, maxNextCharInd,
148                                        available - maxNextCharInd)) == -1)
149            {
150               inputStream.close();
151               throw new java.io.IOException();
152            }
153            else
154               maxNextCharInd += i;
155            return;
156         }
157         catch(java.io.IOException e) {
158            --bufpos;
159            backup(0);
160            if (tokenBegin == -1)
161               tokenBegin = bufpos;
162            throw e;
163         }
164      }
165    
166    /** Start. */
167      public char BeginToken() throws java.io.IOException
168      {
169         tokenBegin = -1;
170         char c = readChar();
171         tokenBegin = bufpos;
172    
173         return c;
174      }
175    
176      protected void UpdateLineColumn(char c)
177      {
178         column++;
179    
180         if (prevCharIsLF)
181         {
182            prevCharIsLF = false;
183            line += (column = 1);
184         }
185         else if (prevCharIsCR)
186         {
187            prevCharIsCR = false;
188            if (c == '\n')
189            {
190               prevCharIsLF = true;
191            }
192            else
193               line += (column = 1);
194         }
195    
196         switch (c)
197         {
198            case '\r' :
199               prevCharIsCR = true;
200               break;
201            case '\n' :
202               prevCharIsLF = true;
203               break;
204            case '\t' :
205               column--;
206               column += (tabSize - (column % tabSize));
207               break;
208            default :
209               break;
210         }
211    
212         bufline[bufpos] = line;
213         bufcolumn[bufpos] = column;
214      }
215    
216    /** Read a character. */
217      public char readChar() throws java.io.IOException
218      {
219         if (inBuf > 0)
220         {
221            --inBuf;
222    
223            if (++bufpos == bufsize)
224               bufpos = 0;
225    
226            return buffer[bufpos];
227         }
228    
229         if (++bufpos >= maxNextCharInd)
230            FillBuff();
231    
232         char c = buffer[bufpos];
233    
234         UpdateLineColumn(c);
235         return c;
236      }
237    
238      /**
239       * @deprecated
240       * @see #getEndColumn
241       */
242    
243      public int getColumn() {
244         return bufcolumn[bufpos];
245      }
246    
247      /**
248       * @deprecated
249       * @see #getEndLine
250       */
251    
252      public int getLine() {
253         return bufline[bufpos];
254      }
255    
256      /** Get token end column number. */
257      public int getEndColumn() {
258         return bufcolumn[bufpos];
259      }
260    
261      /** Get token end line number. */
262      public int getEndLine() {
263         return bufline[bufpos];
264      }
265    
266      /** Get token beginning column number. */
267      public int getBeginColumn() {
268         return bufcolumn[tokenBegin];
269      }
270    
271      /** Get token beginning line number. */
272      public int getBeginLine() {
273         return bufline[tokenBegin];
274      }
275    
276    /** Backup a number of characters. */
277      public void backup(int amount) {
278    
279        inBuf += amount;
280        if ((bufpos -= amount) < 0)
281           bufpos += bufsize;
282      }
283    
284      /** Constructor. */
285      public SimpleCharStream(java.io.Reader dstream, int startline,
286      int startcolumn, int buffersize)
287      {
288        inputStream = dstream;
289        line = startline;
290        column = startcolumn - 1;
291    
292        available = bufsize = buffersize;
293        buffer = new char[buffersize];
294        bufline = new int[buffersize];
295        bufcolumn = new int[buffersize];
296      }
297    
298      /** Constructor. */
299      public SimpleCharStream(java.io.Reader dstream, int startline,
300                              int startcolumn)
301      {
302         this(dstream, startline, startcolumn, 4096);
303      }
304    
305      /** Constructor. */
306      public SimpleCharStream(java.io.Reader dstream)
307      {
308         this(dstream, 1, 1, 4096);
309      }
310    
311      /** Reinitialise. */
312      public void ReInit(java.io.Reader dstream, int startline,
313      int startcolumn, int buffersize)
314      {
315        inputStream = dstream;
316        line = startline;
317        column = startcolumn - 1;
318    
319        if (buffer == null || buffersize != buffer.length)
320        {
321          available = bufsize = buffersize;
322          buffer = new char[buffersize];
323          bufline = new int[buffersize];
324          bufcolumn = new int[buffersize];
325        }
326        prevCharIsLF = prevCharIsCR = false;
327        tokenBegin = inBuf = maxNextCharInd = 0;
328        bufpos = -1;
329      }
330    
331      /** Reinitialise. */
332      public void ReInit(java.io.Reader dstream, int startline,
333                         int startcolumn)
334      {
335         ReInit(dstream, startline, startcolumn, 4096);
336      }
337    
338      /** Reinitialise. */
339      public void ReInit(java.io.Reader dstream)
340      {
341         ReInit(dstream, 1, 1, 4096);
342      }
343      /** Constructor. */
344      public SimpleCharStream(java.io.InputStream dstream, String encoding, int startline,
345      int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException
346      {
347         this(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);
348      }
349    
350      /** Constructor. */
351      public SimpleCharStream(java.io.InputStream dstream, int startline,
352      int startcolumn, int buffersize)
353      {
354         this(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize);
355      }
356    
357      /** Constructor. */
358      public SimpleCharStream(java.io.InputStream dstream, String encoding, int startline,
359                              int startcolumn) throws java.io.UnsupportedEncodingException
360      {
361         this(dstream, encoding, startline, startcolumn, 4096);
362      }
363    
364      /** Constructor. */
365      public SimpleCharStream(java.io.InputStream dstream, int startline,
366                              int startcolumn)
367      {
368         this(dstream, startline, startcolumn, 4096);
369      }
370    
371      /** Constructor. */
372      public SimpleCharStream(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException
373      {
374         this(dstream, encoding, 1, 1, 4096);
375      }
376    
377      /** Constructor. */
378      public SimpleCharStream(java.io.InputStream dstream)
379      {
380         this(dstream, 1, 1, 4096);
381      }
382    
383      /** Reinitialise. */
384      public void ReInit(java.io.InputStream dstream, String encoding, int startline,
385                              int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException
386      {
387         ReInit(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);
388      }
389    
390      /** Reinitialise. */
391      public void ReInit(java.io.InputStream dstream, int startline,
392                              int startcolumn, int buffersize)
393      {
394         ReInit(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize);
395      }
396    
397      /** Reinitialise. */
398      public void ReInit(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException
399      {
400         ReInit(dstream, encoding, 1, 1, 4096);
401      }
402    
403      /** Reinitialise. */
404      public void ReInit(java.io.InputStream dstream)
405      {
406         ReInit(dstream, 1, 1, 4096);
407      }
408      /** Reinitialise. */
409      public void ReInit(java.io.InputStream dstream, String encoding, int startline,
410                         int startcolumn) throws java.io.UnsupportedEncodingException
411      {
412         ReInit(dstream, encoding, startline, startcolumn, 4096);
413      }
414      /** Reinitialise. */
415      public void ReInit(java.io.InputStream dstream, int startline,
416                         int startcolumn)
417      {
418         ReInit(dstream, startline, startcolumn, 4096);
419      }
420      /** Get token literal value. */
421      public String GetImage()
422      {
423         if (bufpos >= tokenBegin)
424            return new String(buffer, tokenBegin, bufpos - tokenBegin + 1);
425         else
426            return new String(buffer, tokenBegin, bufsize - tokenBegin) +
427                                  new String(buffer, 0, bufpos + 1);
428      }
429    
430      /** Get the suffix. */
431      public char[] GetSuffix(int len)
432      {
433         char[] ret = new char[len];
434    
435         if ((bufpos + 1) >= len)
436            System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
437         else
438         {
439            System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0,
440                                                              len - bufpos - 1);
441            System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
442         }
443    
444         return ret;
445      }
446    
447      /** Reset buffer when finished. */
448      public void Done()
449      {
450         buffer = null;
451         bufline = null;
452         bufcolumn = null;
453      }
454    
455      /**
456       * Method to adjust line and column numbers for the start of a token.
457       */
458      public void adjustBeginLineColumn(int newLine, int newCol)
459      {
460         int start = tokenBegin;
461         int len;
462    
463         if (bufpos >= tokenBegin)
464         {
465            len = bufpos - tokenBegin + inBuf + 1;
466         }
467         else
468         {
469            len = bufsize - tokenBegin + bufpos + 1 + inBuf;
470         }
471    
472         int i = 0, j = 0, k = 0;
473         int nextColDiff = 0, columnDiff = 0;
474    
475         while (i < len &&
476                bufline[j = start % bufsize] == bufline[k = ++start % bufsize])
477         {
478            bufline[j] = newLine;
479            nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
480            bufcolumn[j] = newCol + columnDiff;
481            columnDiff = nextColDiff;
482            i++;
483         }
484    
485         if (i < len)
486         {
487            bufline[j] = newLine++;
488            bufcolumn[j] = newCol + columnDiff;
489    
490            while (i++ < len)
491            {
492               if (bufline[j = start % bufsize] != bufline[++start % bufsize])
493                  bufline[j] = newLine++;
494               else
495                  bufline[j] = newLine;
496            }
497         }
498    
499         line = bufline[j];
500         column = bufcolumn[j];
501      }
502    
503    }
504    /* JavaCC - OriginalChecksum=06736fd2e6c2e9c37224359e190d6fa7 (do not edit this line) */