View Javadoc

1   /* Generated By:JavaCC: Do not edit this line. SimpleCharStream.java Version 3.0 */
2   /***************************************************************************************
3    * Copyright (c) Jonas Bonér, Alexandre Vasseur. All rights reserved.                 *
4    * http://aspectwerkz.codehaus.org                                                    *
5    * ---------------------------------------------------------------------------------- *
6    * The software in this package is published under the terms of the LGPL license      *
7    * a copy of which has been included with this distribution in the license.txt file.  *
8    **************************************************************************************/
9   package org.codehaus.aspectwerkz.expression.ast;
10  
11  /***
12   * An implementation of interface CharStream, where the stream is assumed to contain only ASCII characters (without
13   * unicode processing).
14   */
15  
16  public class SimpleCharStream {
17      public static final boolean staticFlag = true;
18  
19      static int bufsize;
20  
21      static int available;
22  
23      static int tokenBegin;
24  
25      static public int bufpos = -1;
26  
27      static protected int bufline[];
28  
29      static protected int bufcolumn[];
30  
31      static protected int column = 0;
32  
33      static protected int line = 1;
34  
35      static protected boolean prevCharIsCR = false;
36  
37      static protected boolean prevCharIsLF = false;
38  
39      static protected java.io.Reader inputStream;
40  
41      static protected char[] buffer;
42  
43      static protected int maxNextCharInd = 0;
44  
45      static protected int inBuf = 0;
46  
47      static protected void ExpandBuff(boolean wrapAround) {
48          char[] newbuffer = new char[bufsize + 2048];
49          int newbufline[] = new int[bufsize + 2048];
50          int newbufcolumn[] = new int[bufsize + 2048];
51  
52          try {
53              if (wrapAround) {
54                  System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
55                  System.arraycopy(buffer, 0, newbuffer, bufsize - tokenBegin, bufpos);
56                  buffer = newbuffer;
57  
58                  System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
59                  System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);
60                  bufline = newbufline;
61  
62                  System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
63                  System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);
64                  bufcolumn = newbufcolumn;
65  
66                  maxNextCharInd = (bufpos += (bufsize - tokenBegin));
67              } else {
68                  System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
69                  buffer = newbuffer;
70  
71                  System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
72                  bufline = newbufline;
73  
74                  System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
75                  bufcolumn = newbufcolumn;
76  
77                  maxNextCharInd = (bufpos -= tokenBegin);
78              }
79          } catch (Throwable t) {
80              throw new Error(t.getMessage());
81          }
82  
83          bufsize += 2048;
84          available = bufsize;
85          tokenBegin = 0;
86      }
87  
88      static protected void FillBuff() throws java.io.IOException {
89          if (maxNextCharInd == available) {
90              if (available == bufsize) {
91                  if (tokenBegin > 2048) {
92                      bufpos = maxNextCharInd = 0;
93                      available = tokenBegin;
94                  } else if (tokenBegin < 0)
95                      bufpos = maxNextCharInd = 0;
96                  else
97                      ExpandBuff(false);
98              } else if (available > tokenBegin)
99                  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, available - maxNextCharInd)) == -1) {
109                 inputStream.close();
110                 throw new java.io.IOException();
111             } else
112                 maxNextCharInd += i;
113             return;
114         } catch (java.io.IOException e) {
115             --bufpos;
116             backup(0);
117             if (tokenBegin == -1)
118                 tokenBegin = bufpos;
119             throw e;
120         }
121     }
122 
123     static public char BeginToken() throws java.io.IOException {
124         tokenBegin = -1;
125         char c = readChar();
126         tokenBegin = bufpos;
127 
128         return c;
129     }
130 
131     static protected void UpdateLineColumn(char c) {
132         column++;
133 
134         if (prevCharIsLF) {
135             prevCharIsLF = false;
136             line += (column = 1);
137         } else if (prevCharIsCR) {
138             prevCharIsCR = false;
139             if (c == '\n') {
140                 prevCharIsLF = true;
141             } else
142                 line += (column = 1);
143         }
144 
145         switch (c) {
146             case '\r':
147                 prevCharIsCR = true;
148                 break;
149             case '\n':
150                 prevCharIsLF = true;
151                 break;
152             case '\t':
153                 column--;
154                 column += (8 - (column & 07));
155                 break;
156             default:
157                 break;
158         }
159 
160         bufline[bufpos] = line;
161         bufcolumn[bufpos] = column;
162     }
163 
164     static public char readChar() throws java.io.IOException {
165         if (inBuf > 0) {
166             --inBuf;
167 
168             if (++bufpos == bufsize)
169                 bufpos = 0;
170 
171             return buffer[bufpos];
172         }
173 
174         if (++bufpos >= maxNextCharInd)
175             FillBuff();
176 
177         char c = buffer[bufpos];
178 
179         UpdateLineColumn(c);
180         return (c);
181     }
182 
183     /***
184      * @deprecated @see #getEndColumn
185      */
186 
187     static public int getColumn() {
188         return bufcolumn[bufpos];
189     }
190 
191     /***
192      * @deprecated @see #getEndLine
193      */
194 
195     static public int getLine() {
196         return bufline[bufpos];
197     }
198 
199     static public int getEndColumn() {
200         return bufcolumn[bufpos];
201     }
202 
203     static public int getEndLine() {
204         return bufline[bufpos];
205     }
206 
207     static public int getBeginColumn() {
208         return bufcolumn[tokenBegin];
209     }
210 
211     static public int getBeginLine() {
212         return bufline[tokenBegin];
213     }
214 
215     static public void backup(int amount) {
216 
217         inBuf += amount;
218         if ((bufpos -= amount) < 0)
219             bufpos += bufsize;
220     }
221 
222     public SimpleCharStream(java.io.Reader dstream, int startline, int startcolumn, int buffersize) {
223         if (inputStream != null)
224             throw new Error("\n   ERROR: Second call to the constructor of a static SimpleCharStream.  You must\n"
225                 + "       either use ReInit() or set the JavaCC option STATIC to false\n"
226                 + "       during the generation of this class.");
227         inputStream = dstream;
228         line = startline;
229         column = startcolumn - 1;
230 
231         available = bufsize = buffersize;
232         buffer = new char[buffersize];
233         bufline = new int[buffersize];
234         bufcolumn = new int[buffersize];
235     }
236 
237     public SimpleCharStream(java.io.Reader dstream, int startline, int startcolumn) {
238         this(dstream, startline, startcolumn, 4096);
239     }
240 
241     public SimpleCharStream(java.io.Reader dstream) {
242         this(dstream, 1, 1, 4096);
243     }
244 
245     public void ReInit(java.io.Reader dstream, int startline, int startcolumn, int buffersize) {
246         inputStream = dstream;
247         line = startline;
248         column = startcolumn - 1;
249 
250         if (buffer == null || buffersize != buffer.length) {
251             available = bufsize = buffersize;
252             buffer = new char[buffersize];
253             bufline = new int[buffersize];
254             bufcolumn = new int[buffersize];
255         }
256         prevCharIsLF = prevCharIsCR = false;
257         tokenBegin = inBuf = maxNextCharInd = 0;
258         bufpos = -1;
259     }
260 
261     public void ReInit(java.io.Reader dstream, int startline, int startcolumn) {
262         ReInit(dstream, startline, startcolumn, 4096);
263     }
264 
265     public void ReInit(java.io.Reader dstream) {
266         ReInit(dstream, 1, 1, 4096);
267     }
268 
269     public SimpleCharStream(java.io.InputStream dstream, int startline, int startcolumn, int buffersize) {
270         this(new java.io.InputStreamReader(dstream), startline, startcolumn, 4096);
271     }
272 
273     public SimpleCharStream(java.io.InputStream dstream, int startline, int startcolumn) {
274         this(dstream, startline, startcolumn, 4096);
275     }
276 
277     public SimpleCharStream(java.io.InputStream dstream) {
278         this(dstream, 1, 1, 4096);
279     }
280 
281     public void ReInit(java.io.InputStream dstream, int startline, int startcolumn, int buffersize) {
282         ReInit(new java.io.InputStreamReader(dstream), startline, startcolumn, 4096);
283     }
284 
285     public void ReInit(java.io.InputStream dstream) {
286         ReInit(dstream, 1, 1, 4096);
287     }
288 
289     public void ReInit(java.io.InputStream dstream, int startline, int startcolumn) {
290         ReInit(dstream, startline, startcolumn, 4096);
291     }
292 
293     static public String GetImage() {
294         if (bufpos >= tokenBegin)
295             return new String(buffer, tokenBegin, bufpos - tokenBegin + 1);
296         else
297             return new String(buffer, tokenBegin, bufsize - tokenBegin) + new String(buffer, 0, bufpos + 1);
298     }
299 
300     static public char[] GetSuffix(int len) {
301         char[] ret = new char[len];
302 
303         if ((bufpos + 1) >= len)
304             System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
305         else {
306             System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0, len - bufpos - 1);
307             System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
308         }
309 
310         return ret;
311     }
312 
313     static public void Done() {
314         buffer = null;
315         bufline = null;
316         bufcolumn = null;
317     }
318 
319     /***
320      * Method to adjust line and column numbers for the start of a token.
321      */
322     static public void adjustBeginLineColumn(int newLine, int newCol) {
323         int start = tokenBegin;
324         int len;
325 
326         if (bufpos >= tokenBegin) {
327             len = bufpos - tokenBegin + inBuf + 1;
328         } else {
329             len = bufsize - tokenBegin + bufpos + 1 + inBuf;
330         }
331 
332         int i = 0, j = 0, k = 0;
333         int nextColDiff = 0, columnDiff = 0;
334 
335         while (i < len && bufline[j = start % bufsize] == bufline[k = ++start % bufsize]) {
336             bufline[j] = newLine;
337             nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
338             bufcolumn[j] = newCol + columnDiff;
339             columnDiff = nextColDiff;
340             i++;
341         }
342 
343         if (i < len) {
344             bufline[j] = newLine++;
345             bufcolumn[j] = newCol + columnDiff;
346 
347             while (i++ < len) {
348                 if (bufline[j = start % bufsize] != bufline[++start % bufsize])
349                     bufline[j] = newLine++;
350                 else
351                     bufline[j] = newLine;
352             }
353         }
354 
355         line = bufline[j];
356         column = bufcolumn[j];
357     }
358 
359 }