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