View Javadoc

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