1 /* Generated By:JavaCC: Do not edit this line. SimpleCharStream.java Version 3.0 */
5 * An implementation of interface CharStream, where the stream is assumed to
6 * contain only ASCII characters (without unicode processing).
9 public class SimpleCharStream
11 public static final boolean staticFlag = true;
14 static int tokenBegin;
15 static public int bufpos = -1;
16 static protected int bufline[];
17 static protected int bufcolumn[];
19 static protected int column = 0;
20 static protected int line = 1;
22 static protected boolean prevCharIsCR = false;
23 static protected boolean prevCharIsLF = false;
25 static protected java.io.Reader inputStream;
27 static protected char[] buffer;
28 static protected int maxNextCharInd = 0;
29 static protected int inBuf = 0;
31 static protected void ExpandBuff(boolean wrapAround)
33 char[] newbuffer = new char[bufsize + 2048];
34 int newbufline[] = new int[bufsize + 2048];
35 int newbufcolumn[] = new int[bufsize + 2048];
41 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
42 System.arraycopy(buffer, 0, newbuffer,
43 bufsize - tokenBegin, bufpos);
46 System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
47 System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);
50 System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
51 System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);
52 bufcolumn = newbufcolumn;
54 maxNextCharInd = (bufpos += (bufsize - tokenBegin));
58 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
61 System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
64 System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
65 bufcolumn = newbufcolumn;
67 maxNextCharInd = (bufpos -= tokenBegin);
72 throw new Error(t.getMessage());
81 static protected void FillBuff() throws java.io.IOException
83 if (maxNextCharInd == available)
85 if (available == bufsize)
87 if (tokenBegin > 2048)
89 bufpos = maxNextCharInd = 0;
90 available = tokenBegin;
92 else if (tokenBegin < 0)
93 bufpos = maxNextCharInd = 0;
97 else if (available > tokenBegin)
99 else if ((tokenBegin - available) < 2048)
102 available = tokenBegin;
107 if ((i = inputStream.read(buffer, maxNextCharInd,
108 available - maxNextCharInd)) == -1)
111 throw new java.io.IOException();
117 catch(java.io.IOException e) {
120 if (tokenBegin == -1)
126 static public char BeginToken() throws java.io.IOException
135 static protected void UpdateLineColumn(char c)
141 prevCharIsLF = false;
142 line += (column = 1);
144 else if (prevCharIsCR)
146 prevCharIsCR = false;
152 line += (column = 1);
165 column += (8 - (column & 07));
171 bufline[bufpos] = line;
172 bufcolumn[bufpos] = column;
175 static public char readChar() throws java.io.IOException
181 if (++bufpos == bufsize)
184 return buffer[bufpos];
187 if (++bufpos >= maxNextCharInd)
190 char c = buffer[bufpos];
201 static public int getColumn() {
202 return bufcolumn[bufpos];
210 static public int getLine() {
211 return bufline[bufpos];
214 static public int getEndColumn() {
215 return bufcolumn[bufpos];
218 static public int getEndLine() {
219 return bufline[bufpos];
222 static public int getBeginColumn() {
223 return bufcolumn[tokenBegin];
226 static public int getBeginLine() {
227 return bufline[tokenBegin];
230 static public void backup(int amount) {
233 if ((bufpos -= amount) < 0)
237 public SimpleCharStream(java.io.Reader dstream, int startline,
238 int startcolumn, int buffersize)
240 if (inputStream != null)
241 throw new Error("\n ERROR: Second call to the constructor of a static SimpleCharStream. You must\n" +
242 " either use ReInit() or set the JavaCC option STATIC to false\n" +
243 " during the generation of this class.");
244 inputStream = dstream;
246 column = startcolumn - 1;
248 available = bufsize = buffersize;
249 buffer = new char[buffersize];
250 bufline = new int[buffersize];
251 bufcolumn = new int[buffersize];
254 public SimpleCharStream(java.io.Reader dstream, int startline,
257 this(dstream, startline, startcolumn, 4096);
260 public SimpleCharStream(java.io.Reader dstream)
262 this(dstream, 1, 1, 4096);
264 public void ReInit(java.io.Reader dstream, int startline,
265 int startcolumn, int buffersize)
267 inputStream = dstream;
269 column = startcolumn - 1;
271 if (buffer == null || buffersize != buffer.length)
273 available = bufsize = buffersize;
274 buffer = new char[buffersize];
275 bufline = new int[buffersize];
276 bufcolumn = new int[buffersize];
278 prevCharIsLF = prevCharIsCR = false;
279 tokenBegin = inBuf = maxNextCharInd = 0;
283 public void ReInit(java.io.Reader dstream, int startline,
286 ReInit(dstream, startline, startcolumn, 4096);
289 public void ReInit(java.io.Reader dstream)
291 ReInit(dstream, 1, 1, 4096);
293 public SimpleCharStream(java.io.InputStream dstream, int startline,
294 int startcolumn, int buffersize)
296 this(new java.io.InputStreamReader(dstream), startline, startcolumn, 4096);
299 public SimpleCharStream(java.io.InputStream dstream, int startline,
302 this(dstream, startline, startcolumn, 4096);
305 public SimpleCharStream(java.io.InputStream dstream)
307 this(dstream, 1, 1, 4096);
310 public void ReInit(java.io.InputStream dstream, int startline,
311 int startcolumn, int buffersize)
313 ReInit(new java.io.InputStreamReader(dstream), startline, startcolumn, 4096);
316 public void ReInit(java.io.InputStream dstream)
318 ReInit(dstream, 1, 1, 4096);
320 public void ReInit(java.io.InputStream dstream, int startline,
323 ReInit(dstream, startline, startcolumn, 4096);
325 static public String GetImage()
327 if (bufpos >= tokenBegin)
328 return new String(buffer, tokenBegin, bufpos - tokenBegin + 1);
330 return new String(buffer, tokenBegin, bufsize - tokenBegin) +
331 new String(buffer, 0, bufpos + 1);
334 static public char[] GetSuffix(int len)
336 char[] ret = new char[len];
338 if ((bufpos + 1) >= len)
339 System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
342 System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0,
344 System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
350 static public void Done()
358 * Method to adjust line and column numbers for the start of a token.<BR>
360 static public void adjustBeginLineColumn(int newLine, int newCol)
362 int start = tokenBegin;
365 if (bufpos >= tokenBegin)
367 len = bufpos - tokenBegin + inBuf + 1;
371 len = bufsize - tokenBegin + bufpos + 1 + inBuf;
374 int i = 0, j = 0, k = 0;
375 int nextColDiff = 0, columnDiff = 0;
378 bufline[j = start % bufsize] == bufline[k = ++start % bufsize])
380 bufline[j] = newLine;
381 nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
382 bufcolumn[j] = newCol + columnDiff;
383 columnDiff = nextColDiff;
389 bufline[j] = newLine++;
390 bufcolumn[j] = newCol + columnDiff;
394 if (bufline[j = start % bufsize] != bufline[++start % bufsize])
395 bufline[j] = newLine++;
397 bufline[j] = newLine;
402 column = bufcolumn[j];