KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > aspectwerkz > expression > ast > SimpleCharStream


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.aspectwerkz.expression.ast;
5
6 /**
7  * An implementation of interface CharStream, where the stream is assumed to contain only ASCII characters (without
8  * unicode processing).
9  */

10
11 public class SimpleCharStream {
12   public static final boolean staticFlag = true;
13
14   static int bufsize;
15
16   static int available;
17
18   static int tokenBegin;
19
20   static public int bufpos = -1;
21
22   static protected int bufline[];
23
24   static protected int bufcolumn[];
25
26   static protected int column = 0;
27
28   static protected int line = 1;
29
30   static protected boolean prevCharIsCR = false;
31
32   static protected boolean prevCharIsLF = false;
33
34   static protected java.io.Reader JavaDoc inputStream;
35
36   static protected char[] buffer;
37
38   static protected int maxNextCharInd = 0;
39
40   static protected int inBuf = 0;
41
42   static protected void ExpandBuff(boolean wrapAround) {
43     char[] newbuffer = new char[bufsize + 2048];
44     int newbufline[] = new int[bufsize + 2048];
45     int newbufcolumn[] = new int[bufsize + 2048];
46
47     try {
48       if (wrapAround) {
49         System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
50         System.arraycopy(buffer, 0, newbuffer, bufsize - tokenBegin, bufpos);
51         buffer = newbuffer;
52
53         System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
54         System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);
55         bufline = newbufline;
56
57         System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
58         System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);
59         bufcolumn = newbufcolumn;
60
61         maxNextCharInd = (bufpos += (bufsize - tokenBegin));
62       } else {
63         System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
64         buffer = newbuffer;
65
66         System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
67         bufline = newbufline;
68
69         System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
70         bufcolumn = newbufcolumn;
71
72         maxNextCharInd = (bufpos -= tokenBegin);
73       }
74     } catch (Throwable JavaDoc t) {
75       throw new Error JavaDoc(t.getMessage());
76     }
77
78     bufsize += 2048;
79     available = bufsize;
80     tokenBegin = 0;
81   }
82
83   static protected void FillBuff() throws java.io.IOException JavaDoc {
84     if (maxNextCharInd == available) {
85       if (available == bufsize) {
86         if (tokenBegin > 2048) {
87           bufpos = maxNextCharInd = 0;
88           available = tokenBegin;
89         } else if (tokenBegin < 0) {
90           bufpos = maxNextCharInd = 0;
91         } else {
92           ExpandBuff(false);
93         }
94       } else if (available > tokenBegin) {
95         available = bufsize;
96       } else if ((tokenBegin - available) < 2048) {
97         ExpandBuff(true);
98       } else {
99         available = tokenBegin;
100       }
101     }
102
103     int i;
104     try {
105       if ((i = inputStream.read(buffer, maxNextCharInd, available - maxNextCharInd)) == -1) {
106         inputStream.close();
107         throw new java.io.IOException JavaDoc();
108       } else {
109         maxNextCharInd += i;
110       }
111       return;
112     } catch (java.io.IOException JavaDoc e) {
113       --bufpos;
114       backup(0);
115       if (tokenBegin == -1) {
116         tokenBegin = bufpos;
117       }
118       throw e;
119     }
120   }
121
122   static public char BeginToken() throws java.io.IOException JavaDoc {
123     tokenBegin = -1;
124     char c = readChar();
125     tokenBegin = bufpos;
126
127     return c;
128   }
129
130   static protected void UpdateLineColumn(char c) {
131     column++;
132
133     if (prevCharIsLF) {
134       prevCharIsLF = false;
135       line += (column = 1);
136     } else if (prevCharIsCR) {
137       prevCharIsCR = false;
138       if (c == '\n') {
139         prevCharIsLF = true;
140       } else {
141         line += (column = 1);
142       }
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 JavaDoc {
165     if (inBuf > 0) {
166       --inBuf;
167
168       if (++bufpos == bufsize) {
169         bufpos = 0;
170       }
171
172       return buffer[bufpos];
173     }
174
175     if (++bufpos >= maxNextCharInd) {
176       FillBuff();
177     }
178
179     char c = buffer[bufpos];
180
181     UpdateLineColumn(c);
182     return (c);
183   }
184
185   /**
186    * @deprecated @see #getEndColumn
187    */

188
189   static public int getColumn() {
190     return bufcolumn[bufpos];
191   }
192
193   /**
194    * @deprecated @see #getEndLine
195    */

196
197   static public int getLine() {
198     return bufline[bufpos];
199   }
200
201   static public int getEndColumn() {
202     return bufcolumn[bufpos];
203   }
204
205   static public int getEndLine() {
206     return bufline[bufpos];
207   }
208
209   static public int getBeginColumn() {
210     return bufcolumn[tokenBegin];
211   }
212
213   static public int getBeginLine() {
214     return bufline[tokenBegin];
215   }
216
217   static public void backup(int amount) {
218
219     inBuf += amount;
220     if ((bufpos -= amount) < 0) {
221       bufpos += bufsize;
222     }
223   }
224
225   public SimpleCharStream(java.io.Reader JavaDoc dstream, int startline, int startcolumn, int buffersize) {
226     if (inputStream != null) {
227       throw new Error JavaDoc(
228               "\n ERROR: Second call to the constructor of a static SimpleCharStream. You must\n"
229                       + " either use ReInit() or set the JavaCC option STATIC to false\n"
230                       + " during the generation of this class."
231       );
232     }
233     inputStream = dstream;
234     line = startline;
235     column = startcolumn - 1;
236
237     available = bufsize = buffersize;
238     buffer = new char[buffersize];
239     bufline = new int[buffersize];
240     bufcolumn = new int[buffersize];
241   }
242
243   public SimpleCharStream(java.io.Reader JavaDoc dstream, int startline, int startcolumn) {
244     this(dstream, startline, startcolumn, 4096);
245   }
246
247   public SimpleCharStream(java.io.Reader JavaDoc dstream) {
248     this(dstream, 1, 1, 4096);
249   }
250
251   public void ReInit(java.io.Reader JavaDoc dstream, int startline, int startcolumn, int buffersize) {
252     inputStream = dstream;
253     line = startline;
254     column = startcolumn - 1;
255
256     if (buffer == null || buffersize != buffer.length) {
257       available = bufsize = buffersize;
258       buffer = new char[buffersize];
259       bufline = new int[buffersize];
260       bufcolumn = new int[buffersize];
261     }
262     prevCharIsLF = prevCharIsCR = false;
263     tokenBegin = inBuf = maxNextCharInd = 0;
264     bufpos = -1;
265   }
266
267   public void ReInit(java.io.Reader JavaDoc dstream, int startline, int startcolumn) {
268     ReInit(dstream, startline, startcolumn, 4096);
269   }
270
271   public void ReInit(java.io.Reader JavaDoc dstream) {
272     ReInit(dstream, 1, 1, 4096);
273   }
274
275   public SimpleCharStream(java.io.InputStream JavaDoc dstream, int startline, int startcolumn, int buffersize) {
276     this(new java.io.InputStreamReader JavaDoc(dstream), startline, startcolumn, 4096);
277   }
278
279   public SimpleCharStream(java.io.InputStream JavaDoc dstream, int startline, int startcolumn) {
280     this(dstream, startline, startcolumn, 4096);
281   }
282
283   public SimpleCharStream(java.io.InputStream JavaDoc dstream) {
284     this(dstream, 1, 1, 4096);
285   }
286
287   public void ReInit(java.io.InputStream JavaDoc dstream, int startline, int startcolumn, int buffersize) {
288     ReInit(new java.io.InputStreamReader JavaDoc(dstream), startline, startcolumn, 4096);
289   }
290
291   public void ReInit(java.io.InputStream JavaDoc dstream) {
292     ReInit(dstream, 1, 1, 4096);
293   }
294
295   public void ReInit(java.io.InputStream JavaDoc dstream, int startline, int startcolumn) {
296     ReInit(dstream, startline, startcolumn, 4096);
297   }
298
299   static public String JavaDoc GetImage() {
300     if (bufpos >= tokenBegin) {
301       return new String JavaDoc(buffer, tokenBegin, bufpos - tokenBegin + 1);
302     } else {
303       return new String JavaDoc(buffer, tokenBegin, bufsize - tokenBegin) + new String JavaDoc(buffer, 0, bufpos + 1);
304     }
305   }
306
307   static public char[] GetSuffix(int len) {
308     char[] ret = new char[len];
309
310     if ((bufpos + 1) >= len) {
311       System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
312     } else {
313       System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0, len - bufpos - 1);
314       System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
315     }
316
317     return ret;
318   }
319
320   static public void Done() {
321     buffer = null;
322     bufline = null;
323     bufcolumn = null;
324   }
325
326   /**
327    * Method to adjust line and column numbers for the start of a token.
328    */

329   static public void adjustBeginLineColumn(int newLine, int newCol) {
330     int start = tokenBegin;
331     int len;
332
333     if (bufpos >= tokenBegin) {
334       len = bufpos - tokenBegin + inBuf + 1;
335     } else {
336       len = bufsize - tokenBegin + bufpos + 1 + inBuf;
337     }
338
339     int i = 0, j = 0, k = 0;
340     int nextColDiff = 0, columnDiff = 0;
341
342     while (i < len && bufline[j = start % bufsize] == bufline[k = ++start % bufsize]) {
343       bufline[j] = newLine;
344       nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
345       bufcolumn[j] = newCol + columnDiff;
346       columnDiff = nextColDiff;
347       i++;
348     }
349
350     if (i < len) {
351       bufline[j] = newLine++;
352       bufcolumn[j] = newCol + columnDiff;
353
354       while (i++ < len) {
355         if (bufline[j = start % bufsize] != bufline[++start % bufsize]) {
356           bufline[j] = newLine++;
357         } else {
358           bufline[j] = newLine;
359         }
360       }
361     }
362
363     line = bufline[j];
364     column = bufcolumn[j];
365   }
366
367 }
Popular Tags