001: /* Generated By:JavaCC: Do not edit this line. SimpleCharStream.java Version 3.0 */
002: package org.josql.parser;
003:
004: /**
005: * An implementation of interface CharStream, where the stream is assumed to
006: * contain only ASCII characters (without unicode processing).
007: */
008:
009: public class SimpleCharStream {
010: public static final boolean staticFlag = false;
011: int bufsize;
012: int available;
013: int tokenBegin;
014: public int bufpos = -1;
015: protected int bufline[];
016: protected int bufcolumn[];
017:
018: protected int column = 0;
019: protected int line = 1;
020:
021: protected boolean prevCharIsCR = false;
022: protected boolean prevCharIsLF = false;
023:
024: protected java.io.Reader inputStream;
025:
026: protected char[] buffer;
027: protected int maxNextCharInd = 0;
028: protected int inBuf = 0;
029:
030: protected void ExpandBuff(boolean wrapAround) {
031: char[] newbuffer = new char[bufsize + 2048];
032: int newbufline[] = new int[bufsize + 2048];
033: int newbufcolumn[] = new int[bufsize + 2048];
034:
035: try {
036: if (wrapAround) {
037: System.arraycopy(buffer, tokenBegin, newbuffer, 0,
038: bufsize - tokenBegin);
039: System.arraycopy(buffer, 0, newbuffer, bufsize
040: - tokenBegin, bufpos);
041: buffer = newbuffer;
042:
043: System.arraycopy(bufline, tokenBegin, newbufline, 0,
044: bufsize - tokenBegin);
045: System.arraycopy(bufline, 0, newbufline, bufsize
046: - tokenBegin, bufpos);
047: bufline = newbufline;
048:
049: System.arraycopy(bufcolumn, tokenBegin, newbufcolumn,
050: 0, bufsize - tokenBegin);
051: System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize
052: - tokenBegin, bufpos);
053: bufcolumn = newbufcolumn;
054:
055: maxNextCharInd = (bufpos += (bufsize - tokenBegin));
056: } else {
057: System.arraycopy(buffer, tokenBegin, newbuffer, 0,
058: bufsize - tokenBegin);
059: buffer = newbuffer;
060:
061: System.arraycopy(bufline, tokenBegin, newbufline, 0,
062: bufsize - tokenBegin);
063: bufline = newbufline;
064:
065: System.arraycopy(bufcolumn, tokenBegin, newbufcolumn,
066: 0, bufsize - tokenBegin);
067: bufcolumn = newbufcolumn;
068:
069: maxNextCharInd = (bufpos -= tokenBegin);
070: }
071: } catch (Throwable t) {
072: throw new Error(t.getMessage());
073: }
074:
075: bufsize += 2048;
076: available = bufsize;
077: tokenBegin = 0;
078: }
079:
080: protected void FillBuff() throws java.io.IOException {
081: if (maxNextCharInd == available) {
082: if (available == bufsize) {
083: if (tokenBegin > 2048) {
084: bufpos = maxNextCharInd = 0;
085: available = tokenBegin;
086: } else if (tokenBegin < 0)
087: bufpos = maxNextCharInd = 0;
088: else
089: ExpandBuff(false);
090: } else if (available > tokenBegin)
091: available = bufsize;
092: else if ((tokenBegin - available) < 2048)
093: ExpandBuff(true);
094: else
095: available = tokenBegin;
096: }
097:
098: int i;
099: try {
100: if ((i = inputStream.read(buffer, maxNextCharInd, available
101: - maxNextCharInd)) == -1) {
102: inputStream.close();
103: throw new java.io.IOException();
104: } else
105: maxNextCharInd += i;
106: return;
107: } catch (java.io.IOException e) {
108: --bufpos;
109: backup(0);
110: if (tokenBegin == -1)
111: tokenBegin = bufpos;
112: throw e;
113: }
114: }
115:
116: public char BeginToken() throws java.io.IOException {
117: tokenBegin = -1;
118: char c = readChar();
119: tokenBegin = bufpos;
120:
121: return c;
122: }
123:
124: protected void UpdateLineColumn(char c) {
125: column++;
126:
127: if (prevCharIsLF) {
128: prevCharIsLF = false;
129: line += (column = 1);
130: } else if (prevCharIsCR) {
131: prevCharIsCR = false;
132: if (c == '\n') {
133: prevCharIsLF = true;
134: } else
135: line += (column = 1);
136: }
137:
138: switch (c) {
139: case '\r':
140: prevCharIsCR = true;
141: break;
142: case '\n':
143: prevCharIsLF = true;
144: break;
145: case '\t':
146: column--;
147: column += (8 - (column & 07));
148: break;
149: default:
150: break;
151: }
152:
153: bufline[bufpos] = line;
154: bufcolumn[bufpos] = column;
155: }
156:
157: public char readChar() throws java.io.IOException {
158: if (inBuf > 0) {
159: --inBuf;
160:
161: if (++bufpos == bufsize)
162: bufpos = 0;
163:
164: return buffer[bufpos];
165: }
166:
167: if (++bufpos >= maxNextCharInd)
168: FillBuff();
169:
170: char c = buffer[bufpos];
171:
172: UpdateLineColumn(c);
173: return (c);
174: }
175:
176: /**
177: * @deprecated
178: * @see #getEndColumn
179: */
180:
181: public int getColumn() {
182: return bufcolumn[bufpos];
183: }
184:
185: /**
186: * @deprecated
187: * @see #getEndLine
188: */
189:
190: public int getLine() {
191: return bufline[bufpos];
192: }
193:
194: public int getEndColumn() {
195: return bufcolumn[bufpos];
196: }
197:
198: public int getEndLine() {
199: return bufline[bufpos];
200: }
201:
202: public int getBeginColumn() {
203: return bufcolumn[tokenBegin];
204: }
205:
206: public int getBeginLine() {
207: return bufline[tokenBegin];
208: }
209:
210: public void backup(int amount) {
211:
212: inBuf += amount;
213: if ((bufpos -= amount) < 0)
214: bufpos += bufsize;
215: }
216:
217: public SimpleCharStream(java.io.Reader dstream, int startline,
218: int startcolumn, int buffersize) {
219: inputStream = dstream;
220: line = startline;
221: column = startcolumn - 1;
222:
223: available = bufsize = buffersize;
224: buffer = new char[buffersize];
225: bufline = new int[buffersize];
226: bufcolumn = new int[buffersize];
227: }
228:
229: public SimpleCharStream(java.io.Reader dstream, int startline,
230: int startcolumn) {
231: this (dstream, startline, startcolumn, 4096);
232: }
233:
234: public SimpleCharStream(java.io.Reader dstream) {
235: this (dstream, 1, 1, 4096);
236: }
237:
238: public void ReInit(java.io.Reader dstream, int startline,
239: int startcolumn, int buffersize) {
240: inputStream = dstream;
241: line = startline;
242: column = startcolumn - 1;
243:
244: if (buffer == null || buffersize != buffer.length) {
245: available = bufsize = buffersize;
246: buffer = new char[buffersize];
247: bufline = new int[buffersize];
248: bufcolumn = new int[buffersize];
249: }
250: prevCharIsLF = prevCharIsCR = false;
251: tokenBegin = inBuf = maxNextCharInd = 0;
252: bufpos = -1;
253: }
254:
255: public void ReInit(java.io.Reader dstream, int startline,
256: int startcolumn) {
257: ReInit(dstream, startline, startcolumn, 4096);
258: }
259:
260: public void ReInit(java.io.Reader dstream) {
261: ReInit(dstream, 1, 1, 4096);
262: }
263:
264: public SimpleCharStream(java.io.InputStream dstream, int startline,
265: int startcolumn, int buffersize) {
266: this (new java.io.InputStreamReader(dstream), startline,
267: startcolumn, 4096);
268: }
269:
270: public SimpleCharStream(java.io.InputStream dstream, int startline,
271: int startcolumn) {
272: this (dstream, startline, startcolumn, 4096);
273: }
274:
275: public SimpleCharStream(java.io.InputStream dstream) {
276: this (dstream, 1, 1, 4096);
277: }
278:
279: public void ReInit(java.io.InputStream dstream, int startline,
280: int startcolumn, int buffersize) {
281: ReInit(new java.io.InputStreamReader(dstream), startline,
282: startcolumn, 4096);
283: }
284:
285: public void ReInit(java.io.InputStream dstream) {
286: ReInit(dstream, 1, 1, 4096);
287: }
288:
289: public void ReInit(java.io.InputStream dstream, int startline,
290: int startcolumn) {
291: ReInit(dstream, startline, startcolumn, 4096);
292: }
293:
294: public String GetImage() {
295: if (bufpos >= tokenBegin)
296: return new String(buffer, tokenBegin, bufpos - tokenBegin
297: + 1);
298: else
299: return new String(buffer, tokenBegin, bufsize - tokenBegin)
300: + new String(buffer, 0, bufpos + 1);
301: }
302:
303: public char[] GetSuffix(int len) {
304: char[] ret = new char[len];
305:
306: if ((bufpos + 1) >= len)
307: System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
308: else {
309: System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret,
310: 0, len - bufpos - 1);
311: System.arraycopy(buffer, 0, ret, len - bufpos - 1,
312: bufpos + 1);
313: }
314:
315: return ret;
316: }
317:
318: public void Done() {
319: buffer = null;
320: bufline = null;
321: bufcolumn = null;
322: }
323:
324: /**
325: * Method to adjust line and column numbers for the start of a token.
326: */
327: public void adjustBeginLineColumn(int newLine, int newCol) {
328: int start = tokenBegin;
329: int len;
330:
331: if (bufpos >= tokenBegin) {
332: len = bufpos - tokenBegin + inBuf + 1;
333: } else {
334: len = bufsize - tokenBegin + bufpos + 1 + inBuf;
335: }
336:
337: int i = 0, j = 0, k = 0;
338: int nextColDiff = 0, columnDiff = 0;
339:
340: while (i < len
341: && bufline[j = start % bufsize] == bufline[k = ++start
342: % 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
356: % bufsize])
357: bufline[j] = newLine++;
358: else
359: bufline[j] = newLine;
360: }
361: }
362:
363: line = bufline[j];
364: column = bufcolumn[j];
365: }
366:
367: }
|