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