rev version
[lwes-java.git] / src / org / lwes / db / SimpleCharStream.java
blob179d34a0892f911dff185c30f62b2d293ccdf4f6
1 /* Generated By:JavaCC: Do not edit this line. SimpleCharStream.java Version 4.0 */
2 package org.lwes.db;
4 /**
5 * An implementation of interface CharStream, where the stream is assumed to
6 * contain only ASCII characters (without unicode processing).
7 */
9 public class SimpleCharStream
11 public static final boolean staticFlag = false;
12 int bufsize;
13 int available;
14 int tokenBegin;
15 public int bufpos = -1;
16 protected int bufline[];
17 protected int bufcolumn[];
19 protected int column = 0;
20 protected int line = 1;
22 protected boolean prevCharIsCR = false;
23 protected boolean prevCharIsLF = false;
25 protected java.io.Reader inputStream;
27 protected char[] buffer;
28 protected int maxNextCharInd = 0;
29 protected int inBuf = 0;
30 protected int tabSize = 8;
32 protected void setTabSize(int i) { tabSize = i; }
33 protected int getTabSize(int i) { return tabSize; }
36 protected void expandBuff(boolean wrapAround)
38 char[] newbuffer = new char[bufsize + 2048];
39 int newbufline[] = new int[bufsize + 2048];
40 int newbufcolumn[] = new int[bufsize + 2048];
42 try
44 if (wrapAround)
46 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
47 System.arraycopy(buffer, 0, newbuffer,
48 bufsize - tokenBegin, bufpos);
49 buffer = newbuffer;
51 System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
52 System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);
53 bufline = newbufline;
55 System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
56 System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);
57 bufcolumn = newbufcolumn;
59 maxNextCharInd = (bufpos += (bufsize - tokenBegin));
61 else
63 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
64 buffer = newbuffer;
66 System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
67 bufline = newbufline;
69 System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
70 bufcolumn = newbufcolumn;
72 maxNextCharInd = (bufpos -= tokenBegin);
75 catch (Throwable t)
77 throw new Error(t.getMessage());
81 bufsize += 2048;
82 available = bufsize;
83 tokenBegin = 0;
86 protected void fillBuff() throws java.io.IOException
88 if (maxNextCharInd == available)
90 if (available == bufsize)
92 if (tokenBegin > 2048)
94 bufpos = maxNextCharInd = 0;
95 available = tokenBegin;
97 else if (tokenBegin < 0)
98 bufpos = maxNextCharInd = 0;
99 else
100 expandBuff(false);
102 else if (available > tokenBegin)
103 available = bufsize;
104 else if ((tokenBegin - available) < 2048)
105 expandBuff(true);
106 else
107 available = tokenBegin;
110 int i;
111 try {
112 if ((i = inputStream.read(buffer, maxNextCharInd,
113 available - maxNextCharInd)) == -1)
115 inputStream.close();
116 throw new java.io.IOException();
118 else
119 maxNextCharInd += i;
120 return;
122 catch(java.io.IOException e) {
123 --bufpos;
124 backup(0);
125 if (tokenBegin == -1)
126 tokenBegin = bufpos;
127 throw e;
131 public char beginToken() throws java.io.IOException
133 tokenBegin = -1;
134 char c = readChar();
135 tokenBegin = bufpos;
137 return c;
140 protected void updateLineColumn(char c)
142 column++;
144 if (prevCharIsLF)
146 prevCharIsLF = false;
147 line += (column = 1);
149 else if (prevCharIsCR)
151 prevCharIsCR = false;
152 if (c == '\n')
154 prevCharIsLF = true;
156 else
157 line += (column = 1);
160 switch (c)
162 case '\r' :
163 prevCharIsCR = true;
164 break;
165 case '\n' :
166 prevCharIsLF = true;
167 break;
168 case '\t' :
169 column--;
170 column += (tabSize - (column % tabSize));
171 break;
172 default :
173 break;
176 bufline[bufpos] = line;
177 bufcolumn[bufpos] = column;
180 public char readChar() throws java.io.IOException
182 if (inBuf > 0)
184 --inBuf;
186 if (++bufpos == bufsize)
187 bufpos = 0;
189 return buffer[bufpos];
192 if (++bufpos >= maxNextCharInd)
193 fillBuff();
195 char c = buffer[bufpos];
197 updateLineColumn(c);
198 return (c);
202 * @deprecated
203 * @see #getEndColumn
206 public int getColumn() {
207 return bufcolumn[bufpos];
211 * @deprecated
212 * @see #getEndLine
215 public int getLine() {
216 return bufline[bufpos];
219 public int getEndColumn() {
220 return bufcolumn[bufpos];
223 public int getEndLine() {
224 return bufline[bufpos];
227 public int getBeginColumn() {
228 return bufcolumn[tokenBegin];
231 public int getBeginLine() {
232 return bufline[tokenBegin];
235 public void backup(int amount) {
237 inBuf += amount;
238 if ((bufpos -= amount) < 0)
239 bufpos += bufsize;
242 public SimpleCharStream(java.io.Reader dstream, int startline,
243 int startcolumn, int buffersize)
245 inputStream = dstream;
246 line = startline;
247 column = startcolumn - 1;
249 available = bufsize = buffersize;
250 buffer = new char[buffersize];
251 bufline = new int[buffersize];
252 bufcolumn = new int[buffersize];
255 public SimpleCharStream(java.io.Reader dstream, int startline,
256 int startcolumn)
258 this(dstream, startline, startcolumn, 4096);
261 public SimpleCharStream(java.io.Reader dstream)
263 this(dstream, 1, 1, 4096);
265 public void reInit(java.io.Reader dstream, int startline,
266 int startcolumn, int buffersize)
268 inputStream = dstream;
269 line = startline;
270 column = startcolumn - 1;
272 if (buffer == null || buffersize != buffer.length)
274 available = bufsize = buffersize;
275 buffer = new char[buffersize];
276 bufline = new int[buffersize];
277 bufcolumn = new int[buffersize];
279 prevCharIsLF = prevCharIsCR = false;
280 tokenBegin = inBuf = maxNextCharInd = 0;
281 bufpos = -1;
284 public void reInit(java.io.Reader dstream, int startline,
285 int startcolumn)
287 reInit(dstream, startline, startcolumn, 4096);
290 public void reInit(java.io.Reader dstream)
292 reInit(dstream, 1, 1, 4096);
294 public SimpleCharStream(java.io.InputStream dstream, String encoding, int startline,
295 int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException
297 this(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);
300 public SimpleCharStream(java.io.InputStream dstream, int startline,
301 int startcolumn, int buffersize)
303 this(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize);
306 public SimpleCharStream(java.io.InputStream dstream, String encoding, int startline,
307 int startcolumn) throws java.io.UnsupportedEncodingException
309 this(dstream, encoding, startline, startcolumn, 4096);
312 public SimpleCharStream(java.io.InputStream dstream, int startline,
313 int startcolumn)
315 this(dstream, startline, startcolumn, 4096);
318 public SimpleCharStream(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException
320 this(dstream, encoding, 1, 1, 4096);
323 public SimpleCharStream(java.io.InputStream dstream)
325 this(dstream, 1, 1, 4096);
328 public void reInit(java.io.InputStream dstream, String encoding, int startline,
329 int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException
331 reInit(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);
334 public void reInit(java.io.InputStream dstream, int startline,
335 int startcolumn, int buffersize)
337 reInit(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize);
340 public void reInit(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException
342 reInit(dstream, encoding, 1, 1, 4096);
345 public void reInit(java.io.InputStream dstream)
347 reInit(dstream, 1, 1, 4096);
349 public void reInit(java.io.InputStream dstream, String encoding, int startline,
350 int startcolumn) throws java.io.UnsupportedEncodingException
352 reInit(dstream, encoding, startline, startcolumn, 4096);
354 public void reInit(java.io.InputStream dstream, int startline,
355 int startcolumn)
357 reInit(dstream, startline, startcolumn, 4096);
359 public String getImage()
361 if (bufpos >= tokenBegin)
362 return new String(buffer, tokenBegin, bufpos - tokenBegin + 1);
363 else
364 return new String(buffer, tokenBegin, bufsize - tokenBegin) +
365 new String(buffer, 0, bufpos + 1);
368 public char[] getSuffix(int len)
370 char[] ret = new char[len];
372 if ((bufpos + 1) >= len)
373 System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
374 else
376 System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0,
377 len - bufpos - 1);
378 System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
381 return ret;
384 public void done()
386 buffer = null;
387 bufline = null;
388 bufcolumn = null;
392 * Method to adjust line and column numbers for the start of a token.
394 public void adjustBeginLineColumn(int newLine, int newCol)
396 int start = tokenBegin;
397 int len;
399 if (bufpos >= tokenBegin)
401 len = bufpos - tokenBegin + inBuf + 1;
403 else
405 len = bufsize - tokenBegin + bufpos + 1 + inBuf;
408 int i = 0, j = 0, k = 0;
409 int nextColDiff = 0, columnDiff = 0;
411 while (i < len &&
412 bufline[j = start % bufsize] == bufline[k = ++start % bufsize])
414 bufline[j] = newLine;
415 nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
416 bufcolumn[j] = newCol + columnDiff;
417 columnDiff = nextColDiff;
418 i++;
421 if (i < len)
423 bufline[j] = newLine++;
424 bufcolumn[j] = newCol + columnDiff;
426 while (i++ < len)
428 if (bufline[j = start % bufsize] != bufline[++start % bufsize])
429 bufline[j] = newLine++;
430 else
431 bufline[j] = newLine;
435 line = bufline[j];
436 column = bufcolumn[j];