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