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