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