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