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