KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejen > util > arl > SimpleCharStream


1 /* Generated By:JavaCC: Do not edit this line. SimpleCharStream.java Version 2.1 */
2 package org.ejen.util.arl;
3
4 /**
5  * An implementation of interface CharStream, where the stream is assumed to
6  * contain only ASCII characters (without unicode processing).
7  */

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

171     static public final int getColumn() {
172         return bufcolumn[bufpos];
173     }
174
175     /**
176      * @deprecated
177      * @see #getEndLine
178      */

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

317     static public void adjustBeginLineColumn(int newLine, int newCol) {
318         int start = tokenBegin;
319         int len;
320
321         if (bufpos >= tokenBegin) {
322             len = bufpos - tokenBegin + inBuf + 1;
323         } else {
324             len = bufsize - tokenBegin + bufpos + 1 + inBuf;
325         }
326         int i = 0, j = 0, k = 0;
327         int nextColDiff = 0, columnDiff = 0;
328
329         while (i < len
330                 && bufline[j = start % bufsize]
331                         == bufline[k = ++start % bufsize]) {
332             bufline[j] = newLine;
333             nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
334             bufcolumn[j] = newCol + columnDiff;
335             columnDiff = nextColDiff;
336             i++;
337         }
338         if (i < len) {
339             bufline[j] = newLine++;
340             bufcolumn[j] = newCol + columnDiff;
341             while (i++ < len) {
342                 if (bufline[j = start % bufsize] != bufline[++start % bufsize]) {
343                     bufline[j] = newLine++;
344                 } else {
345                     bufline[j] = newLine;
346                 }
347             }
348         }
349         line = bufline[j];
350         column = bufcolumn[j];
351     }
352 }
353
Popular Tags