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;
17 //Added by Matthieu Casanova
18 public static int position = 0;
20 public static int beginOffset, endOffset;
22 static protected int bufline[];
23 static protected int bufcolumn[];
25 static protected int column = 0;
26 static protected int line = 1;
28 static protected boolean prevCharIsCR = false;
29 static protected boolean prevCharIsLF = false;
31 static protected java.io.Reader inputStream;
32 static protected StringBuffer currentBuffer = new StringBuffer();
34 static protected char[] buffer;
35 static protected int maxNextCharInd = 0;
36 static protected int inBuf = 0;
38 static protected void ExpandBuff(boolean wrapAround)
40 char[] newbuffer = new char[bufsize + 2048];
41 int newbufline[] = new int[bufsize + 2048];
42 int newbufcolumn[] = new int[bufsize + 2048];
48 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
49 System.arraycopy(buffer, 0, newbuffer,
50 bufsize - tokenBegin, bufpos);
53 System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
54 System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);
57 System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
58 System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);
59 bufcolumn = newbufcolumn;
61 maxNextCharInd = (bufpos += (bufsize - tokenBegin));
65 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
68 System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
71 System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
72 bufcolumn = newbufcolumn;
74 maxNextCharInd = (bufpos -= tokenBegin);
79 throw new Error(t.getMessage());
88 static protected void FillBuff() throws java.io.IOException
90 if (maxNextCharInd == available)
92 if (available == bufsize)
94 if (tokenBegin > 2048)
97 bufpos = maxNextCharInd = 0;
98 available = tokenBegin;
100 else if (tokenBegin < 0) {
102 bufpos = maxNextCharInd = 0;
106 else if (available > tokenBegin)
108 else if ((tokenBegin - available) < 2048)
111 available = tokenBegin;
116 if ((i = inputStream.read(buffer, maxNextCharInd,
117 available - maxNextCharInd)) == -1)
120 throw new java.io.IOException();
125 currentBuffer.append(buffer);
128 catch(java.io.IOException e) {
131 if (tokenBegin == -1)
137 static public char BeginToken() throws java.io.IOException
139 beginOffset = endOffset;
147 static protected void UpdateLineColumn(char c)
153 prevCharIsLF = false;
154 line += (column = 1);
156 else if (prevCharIsCR)
158 prevCharIsCR = false;
164 line += (column = 1);
177 column += (8 - (column & 07));
183 bufline[bufpos] = line;
184 bufcolumn[bufpos] = column;
188 static public char readChar() throws java.io.IOException
195 if (++bufpos == bufsize) {
200 return buffer[bufpos];
203 if (++bufpos >= maxNextCharInd) {
207 char c = buffer[bufpos];
218 static public int getColumn() {
219 return bufcolumn[bufpos];
227 static public int getLine() {
228 return bufline[bufpos];
231 static public int getEndColumn() {
232 return bufcolumn[bufpos];
235 static public int getEndLine() {
236 return bufline[bufpos];
239 static public int getBeginColumn() {
240 return bufcolumn[tokenBegin];
243 static public int getBeginLine() {
244 return bufline[tokenBegin];
247 static public void backup(int amount) {
250 if ((bufpos -= amount) < 0)
254 public SimpleCharStream(java.io.Reader dstream, int startline,
255 int startcolumn, int buffersize)
257 if (inputStream != null)
258 throw new Error("\n ERROR: Second call to the constructor of a static SimpleCharStream. You must\n" +
259 " either use ReInit() or set the JavaCC option STATIC to false\n" +
260 " during the generation of this class.");
261 inputStream = dstream;
262 currentBuffer = new StringBuffer();
264 column = startcolumn - 1;
266 available = bufsize = buffersize;
267 buffer = new char[buffersize];
268 bufline = new int[buffersize];
269 bufcolumn = new int[buffersize];
275 public SimpleCharStream(java.io.Reader dstream, int startline,
278 this(dstream, startline, startcolumn, 4096);
281 public SimpleCharStream(java.io.Reader dstream)
283 this(dstream, 1, 1, 4096);
285 public void ReInit(java.io.Reader dstream, int startline,
286 int startcolumn, int buffersize)
288 inputStream = dstream;
289 currentBuffer = new StringBuffer();
291 column = startcolumn - 1;
293 if (buffer == null || buffersize != buffer.length)
295 available = bufsize = buffersize;
296 buffer = new char[buffersize];
297 bufline = new int[buffersize];
298 bufcolumn = new int[buffersize];
300 prevCharIsLF = prevCharIsCR = false;
301 tokenBegin = inBuf = maxNextCharInd = 0;
308 public void ReInit(java.io.Reader dstream, int startline,
311 ReInit(dstream, startline, startcolumn, 4096);
314 public void ReInit(java.io.Reader dstream)
316 ReInit(dstream, 1, 1, 4096);
318 public SimpleCharStream(java.io.InputStream dstream, int startline,
319 int startcolumn, int buffersize)
321 this(new java.io.InputStreamReader(dstream), startline, startcolumn, 4096);
324 public SimpleCharStream(java.io.InputStream dstream, int startline,
327 this(dstream, startline, startcolumn, 4096);
330 public SimpleCharStream(java.io.InputStream dstream)
332 this(dstream, 1, 1, 4096);
335 public void ReInit(java.io.InputStream dstream, int startline,
336 int startcolumn, int buffersize)
338 ReInit(new java.io.InputStreamReader(dstream), startline, startcolumn, 4096);
341 public void ReInit(java.io.InputStream dstream)
343 ReInit(dstream, 1, 1, 4096);
345 public void ReInit(java.io.InputStream dstream, int startline,
348 ReInit(dstream, startline, startcolumn, 4096);
350 static public String GetImage()
352 if (bufpos >= tokenBegin)
353 return new String(buffer, tokenBegin, bufpos - tokenBegin + 1);
355 return new String(buffer, tokenBegin, bufsize - tokenBegin) +
356 new String(buffer, 0, bufpos + 1);
359 static public char[] GetSuffix(int len)
361 char[] ret = new char[len];
363 if ((bufpos + 1) >= len)
364 System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
367 System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0,
369 System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
375 static public void Done()
384 * Method to adjust line and column numbers for the start of a token.<BR>
386 static public void adjustBeginLineColumn(int newLine, int newCol)
388 int start = tokenBegin;
391 if (bufpos >= tokenBegin)
393 len = bufpos - tokenBegin + inBuf + 1;
397 len = bufsize - tokenBegin + bufpos + 1 + inBuf;
400 int i = 0, j = 0, k = 0;
401 int nextColDiff = 0, columnDiff = 0;
404 bufline[j = start % bufsize] == bufline[k = ++start % bufsize])
406 bufline[j] = newLine;
407 nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
408 bufcolumn[j] = newCol + columnDiff;
409 columnDiff = nextColDiff;
415 bufline[j] = newLine++;
416 bufcolumn[j] = newCol + columnDiff;
420 if (bufline[j = start % bufsize] != bufline[++start % bufsize]) {
421 bufline[j] = newLine++;
423 bufline[j] = newLine;
429 column = bufcolumn[j];
436 public static int getPosition() {
437 return position + bufpos;