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