001: /* Generated By:JavaCC: Do not edit this line. JavaCharStream.java Version 3.0 */
002: package de.gulden.util.javasource.jjt;
003:
004: /**
005: * An implementation of interface CharStream, where the stream is assumed to
006: * contain only ASCII characters (with java-like unicode escape processing).
007: */
008:
009: public class JavaCharStream {
010:
011: /*--- Jens --- (MIND THE MODIFICATIONS THROUGHOUT THIS FILE!) */
012: TextImage text_image;
013:
014: public static final boolean staticFlag = false;
015:
016: static final int hexval(char c) throws java.io.IOException {
017: switch (c) {
018: case '0':
019: return 0;
020: case '1':
021: return 1;
022: case '2':
023: return 2;
024: case '3':
025: return 3;
026: case '4':
027: return 4;
028: case '5':
029: return 5;
030: case '6':
031: return 6;
032: case '7':
033: return 7;
034: case '8':
035: return 8;
036: case '9':
037: return 9;
038:
039: case 'a':
040: case 'A':
041: return 10;
042: case 'b':
043: case 'B':
044: return 11;
045: case 'c':
046: case 'C':
047: return 12;
048: case 'd':
049: case 'D':
050: return 13;
051: case 'e':
052: case 'E':
053: return 14;
054: case 'f':
055: case 'F':
056: return 15;
057: }
058:
059: throw new java.io.IOException(); // Should never come here
060: }
061:
062: public int bufpos = -1;
063: int bufsize;
064: int available;
065: int tokenBegin;
066: protected int bufline[];
067: protected int bufcolumn[];
068:
069: protected int column = 0;
070: protected int line = 1;
071:
072: protected boolean prevCharIsCR = false;
073: protected boolean prevCharIsLF = false;
074:
075: protected java.io.Reader inputStream;
076:
077: protected char[] nextCharBuf;
078: protected char[] buffer;
079: protected int maxNextCharInd = 0;
080: protected int nextCharInd = -1;
081: protected int inBuf = 0;
082:
083: protected void ExpandBuff(boolean wrapAround) {
084: char[] newbuffer = new char[bufsize + 2048];
085: int newbufline[] = new int[bufsize + 2048];
086: int newbufcolumn[] = new int[bufsize + 2048];
087:
088: try {
089: if (wrapAround) {
090: System.arraycopy(buffer, tokenBegin, newbuffer, 0,
091: bufsize - tokenBegin);
092: System.arraycopy(buffer, 0, newbuffer, bufsize
093: - tokenBegin, bufpos);
094: buffer = newbuffer;
095:
096: System.arraycopy(bufline, tokenBegin, newbufline, 0,
097: bufsize - tokenBegin);
098: System.arraycopy(bufline, 0, newbufline, bufsize
099: - tokenBegin, bufpos);
100: bufline = newbufline;
101:
102: System.arraycopy(bufcolumn, tokenBegin, newbufcolumn,
103: 0, bufsize - tokenBegin);
104: System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize
105: - tokenBegin, bufpos);
106: bufcolumn = newbufcolumn;
107:
108: bufpos += (bufsize - tokenBegin);
109: } else {
110: System.arraycopy(buffer, tokenBegin, newbuffer, 0,
111: bufsize - tokenBegin);
112: buffer = newbuffer;
113:
114: System.arraycopy(bufline, tokenBegin, newbufline, 0,
115: bufsize - tokenBegin);
116: bufline = newbufline;
117:
118: System.arraycopy(bufcolumn, tokenBegin, newbufcolumn,
119: 0, bufsize - tokenBegin);
120: bufcolumn = newbufcolumn;
121:
122: bufpos -= tokenBegin;
123: }
124: } catch (Throwable t) {
125: throw new Error(t.getMessage());
126: }
127:
128: available = (bufsize += 2048);
129: tokenBegin = 0;
130: }
131:
132: protected void FillBuff() throws java.io.IOException {
133: int i;
134: if (maxNextCharInd == 4096)
135: maxNextCharInd = nextCharInd = 0;
136:
137: try {
138: if ((i = inputStream.read(nextCharBuf, maxNextCharInd,
139: 4096 - maxNextCharInd)) == -1) {
140: inputStream.close();
141: throw new java.io.IOException();
142: } else
143: maxNextCharInd += i;
144: return;
145: } catch (java.io.IOException e) {
146: if (bufpos != 0) {
147: --bufpos;
148: backup(0);
149: } else {
150: bufline[bufpos] = line;
151: bufcolumn[bufpos] = column;
152: }
153: throw e;
154: }
155: }
156:
157: protected char ReadByte() throws java.io.IOException {
158: if (++nextCharInd >= maxNextCharInd)
159: FillBuff();
160:
161: return nextCharBuf[nextCharInd];
162: }
163:
164: public char BeginToken() throws java.io.IOException {
165: if (inBuf > 0) {
166: --inBuf;
167:
168: if (++bufpos == bufsize)
169: bufpos = 0;
170:
171: tokenBegin = bufpos;
172: return buffer[bufpos];
173: }
174:
175: tokenBegin = 0;
176: bufpos = -1;
177:
178: return readChar();
179: }
180:
181: protected void AdjustBuffSize() {
182: if (available == bufsize) {
183: if (tokenBegin > 2048) {
184: bufpos = 0;
185: available = tokenBegin;
186: } else
187: ExpandBuff(false);
188: } else if (available > tokenBegin)
189: available = bufsize;
190: else if ((tokenBegin - available) < 2048)
191: ExpandBuff(true);
192: else
193: available = tokenBegin;
194: }
195:
196: protected void UpdateLineColumn(char c) {
197: column++;
198:
199: if (prevCharIsLF) {
200: prevCharIsLF = false;
201: line += (column = 1);
202: /*--- Jens ---*/
203: text_image.nextLine();
204: } else if (prevCharIsCR) {
205: prevCharIsCR = false;
206: if (c == '\n') {
207: prevCharIsLF = true;
208: } else
209: line += (column = 1);
210: /*--- Jens ---*/
211: text_image.nextLine();
212: }
213:
214: switch (c) {
215: case '\r':
216: prevCharIsCR = true;
217: break;
218: case '\n':
219: prevCharIsLF = true;
220: break;
221: /* --- removed, Jens
222: case '\t' :
223: column--;
224: column += (8 - (column & 07));
225: break;
226: */
227: default:
228: break;
229: }
230:
231: bufline[bufpos] = line;
232: bufcolumn[bufpos] = column;
233:
234: /*--- Jens ---*/
235: text_image.write(c);
236: }
237:
238: public char readChar() throws java.io.IOException {
239: if (inBuf > 0) {
240: --inBuf;
241:
242: if (++bufpos == bufsize)
243: bufpos = 0;
244:
245: return buffer[bufpos];
246: }
247:
248: char c;
249:
250: if (++bufpos == available)
251: AdjustBuffSize();
252:
253: if ((buffer[bufpos] = c = ReadByte()) == '\\') {
254: UpdateLineColumn(c);
255:
256: int backSlashCnt = 1;
257:
258: for (;;) // Read all the backslashes
259: {
260: if (++bufpos == available)
261: AdjustBuffSize();
262:
263: try {
264: if ((buffer[bufpos] = c = ReadByte()) != '\\') {
265: UpdateLineColumn(c);
266: // found a non-backslash char.
267: if ((c == 'u') && ((backSlashCnt & 1) == 1)) {
268: if (--bufpos < 0)
269: bufpos = bufsize - 1;
270:
271: break;
272: }
273:
274: backup(backSlashCnt);
275: return '\\';
276: }
277: } catch (java.io.IOException e) {
278: if (backSlashCnt > 1)
279: backup(backSlashCnt);
280:
281: return '\\';
282: }
283:
284: UpdateLineColumn(c);
285: backSlashCnt++;
286: }
287:
288: // Here, we have seen an odd number of backslash's followed by a 'u'
289: /* --- Jens --- this disables unicode-processing - we want to preserve the original source
290: try
291: {
292: while ((c = ReadByte()) == 'u')
293: ++column;
294:
295: buffer[bufpos] = c = (char)(hexval(c) << 12 |
296: hexval(ReadByte()) << 8 |
297: hexval(ReadByte()) << 4 |
298: hexval(ReadByte()));
299:
300: column += 4;
301: }
302: catch(java.io.IOException e)
303: {
304: throw new Error("Invalid escape character at line " + line +
305: " column " + column + ".");
306: }
307: --- Jens --- */
308:
309: if (backSlashCnt == 1)
310: return c;
311: else {
312: backup(backSlashCnt - 1);
313: return '\\';
314: }
315: } else {
316: UpdateLineColumn(c);
317: return (c);
318: }
319: }
320:
321: /**
322: * @deprecated
323: * @see #getEndColumn
324: */
325:
326: public int getColumn() {
327: return bufcolumn[bufpos];
328: }
329:
330: /**
331: * @deprecated
332: * @see #getEndLine
333: */
334:
335: public int getLine() {
336: return bufline[bufpos];
337: }
338:
339: public int getEndColumn() {
340: return bufcolumn[bufpos];
341: }
342:
343: public int getEndLine() {
344: return bufline[bufpos];
345: }
346:
347: public int getBeginColumn() {
348: return bufcolumn[tokenBegin];
349: }
350:
351: public int getBeginLine() {
352: return bufline[tokenBegin];
353: }
354:
355: public void backup(int amount) {
356:
357: inBuf += amount;
358: if ((bufpos -= amount) < 0)
359: bufpos += bufsize;
360: }
361:
362: public JavaCharStream(java.io.Reader dstream, int startline,
363: int startcolumn, int buffersize) {
364: inputStream = dstream;
365: line = startline;
366: column = startcolumn - 1;
367:
368: available = bufsize = buffersize;
369: buffer = new char[buffersize];
370: bufline = new int[buffersize];
371: bufcolumn = new int[buffersize];
372: nextCharBuf = new char[4096];
373:
374: /*--- Jens ---*/
375: text_image = new TextImage();
376: }
377:
378: public JavaCharStream(java.io.Reader dstream, int startline,
379: int startcolumn) {
380: this (dstream, startline, startcolumn, 4096);
381: }
382:
383: public JavaCharStream(java.io.Reader dstream) {
384: this (dstream, 1, 1, 4096);
385: }
386:
387: public void ReInit(java.io.Reader dstream, int startline,
388: int startcolumn, int buffersize) {
389: inputStream = dstream;
390: line = startline;
391: column = startcolumn - 1;
392:
393: if (buffer == null || buffersize != buffer.length) {
394: available = bufsize = buffersize;
395: buffer = new char[buffersize];
396: bufline = new int[buffersize];
397: bufcolumn = new int[buffersize];
398: nextCharBuf = new char[4096];
399: }
400: prevCharIsLF = prevCharIsCR = false;
401: tokenBegin = inBuf = maxNextCharInd = 0;
402: nextCharInd = bufpos = -1;
403: }
404:
405: public void ReInit(java.io.Reader dstream, int startline,
406: int startcolumn) {
407: ReInit(dstream, startline, startcolumn, 4096);
408: }
409:
410: public void ReInit(java.io.Reader dstream) {
411: ReInit(dstream, 1, 1, 4096);
412: }
413:
414: public JavaCharStream(java.io.InputStream dstream, int startline,
415: int startcolumn, int buffersize) {
416: this (new java.io.InputStreamReader(dstream), startline,
417: startcolumn, 4096);
418: }
419:
420: public JavaCharStream(java.io.InputStream dstream, int startline,
421: int startcolumn) {
422: this (dstream, startline, startcolumn, 4096);
423: }
424:
425: public JavaCharStream(java.io.InputStream dstream) {
426: this (dstream, 1, 1, 4096);
427: }
428:
429: public void ReInit(java.io.InputStream dstream, int startline,
430: int startcolumn, int buffersize) {
431: ReInit(new java.io.InputStreamReader(dstream), startline,
432: startcolumn, 4096);
433: }
434:
435: public void ReInit(java.io.InputStream dstream, int startline,
436: int startcolumn) {
437: ReInit(dstream, startline, startcolumn, 4096);
438: }
439:
440: public void ReInit(java.io.InputStream dstream) {
441: ReInit(dstream, 1, 1, 4096);
442: }
443:
444: public String GetImage() {
445: if (bufpos >= tokenBegin)
446: return new String(buffer, tokenBegin, bufpos - tokenBegin
447: + 1);
448: else
449: return new String(buffer, tokenBegin, bufsize - tokenBegin)
450: + new String(buffer, 0, bufpos + 1);
451: }
452:
453: public char[] GetSuffix(int len) {
454: char[] ret = new char[len];
455:
456: if ((bufpos + 1) >= len)
457: System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
458: else {
459: System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret,
460: 0, len - bufpos - 1);
461: System.arraycopy(buffer, 0, ret, len - bufpos - 1,
462: bufpos + 1);
463: }
464:
465: return ret;
466: }
467:
468: public void Done() {
469: nextCharBuf = null;
470: buffer = null;
471: bufline = null;
472: bufcolumn = null;
473: }
474:
475: /**
476: * Method to adjust line and column numbers for the start of a token.
477: */
478: public void adjustBeginLineColumn(int newLine, int newCol) {
479: int start = tokenBegin;
480: int len;
481:
482: if (bufpos >= tokenBegin) {
483: len = bufpos - tokenBegin + inBuf + 1;
484: } else {
485: len = bufsize - tokenBegin + bufpos + 1 + inBuf;
486: }
487:
488: int i = 0, j = 0, k = 0;
489: int nextColDiff = 0, columnDiff = 0;
490:
491: while (i < len
492: && bufline[j = start % bufsize] == bufline[k = ++start
493: % bufsize]) {
494: bufline[j] = newLine;
495: nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
496: bufcolumn[j] = newCol + columnDiff;
497: columnDiff = nextColDiff;
498: i++;
499: }
500:
501: if (i < len) {
502: bufline[j] = newLine++;
503: bufcolumn[j] = newCol + columnDiff;
504:
505: while (i++ < len) {
506: if (bufline[j = start % bufsize] != bufline[++start
507: % bufsize])
508: bufline[j] = newLine++;
509: else
510: bufline[j] = newLine;
511: }
512: }
513:
514: line = bufline[j];
515: column = bufcolumn[j];
516: }
517:
518: }
|