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