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