001: /* Generated By:JavaCC: Do not edit this line. ASCII_UCodeESC_CharStream.java Version 0.7pre6 */
002: package oscript.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 final class ASCII_UCodeESC_CharStream {
010: public static final boolean staticFlag = true;
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: static public int bufpos = -1;
059: static int bufsize;
060: static int available;
061: static int tokenBegin;
062: static private int bufline[];
063: static private int bufcolumn[];
064: static private int bufoff[];
065:
066: static private int off = 0;
067: static private int column = 0;
068: static private int line = 1;
069:
070: static private java.io.Reader inputStream;
071:
072: static private boolean prevCharIsCR = false;
073: static private boolean prevCharIsLF = false;
074:
075: static private char[] nextCharBuf;
076: static private char[] buffer;
077: static private int maxNextCharInd = 0;
078: static private int nextCharInd = -1;
079: static private int inBuf = 0;
080:
081: static private final void ExpandBuff(boolean wrapAround) {
082: char[] newbuffer = new char[bufsize + 2048];
083: int newbufline[] = new int[bufsize + 2048];
084: int newbufcolumn[] = new int[bufsize + 2048];
085: int newbufoff[] = new int[bufsize + 2048];
086:
087: try {
088: if (wrapAround) {
089: System.arraycopy(buffer, tokenBegin, newbuffer, 0,
090: bufsize - tokenBegin);
091: System.arraycopy(buffer, 0, newbuffer, bufsize
092: - tokenBegin, bufpos);
093: buffer = newbuffer;
094:
095: System.arraycopy(bufline, tokenBegin, newbufline, 0,
096: bufsize - tokenBegin);
097: System.arraycopy(bufline, 0, newbufline, bufsize
098: - tokenBegin, bufpos);
099: bufline = newbufline;
100:
101: System.arraycopy(bufcolumn, tokenBegin, newbufcolumn,
102: 0, bufsize - tokenBegin);
103: System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize
104: - tokenBegin, bufpos);
105: bufcolumn = newbufcolumn;
106:
107: System.arraycopy(bufoff, tokenBegin, newbufoff, 0,
108: bufsize - tokenBegin);
109: System.arraycopy(bufoff, 0, newbufoff, bufsize
110: - tokenBegin, bufpos);
111: bufoff = newbufoff;
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: System.arraycopy(bufoff, tokenBegin, newbufoff, 0,
128: bufsize - tokenBegin);
129: bufoff = newbufoff;
130:
131: bufpos -= tokenBegin;
132: }
133: } catch (Throwable t) {
134: throw new Error(t.getMessage());
135: }
136:
137: available = (bufsize += 2048);
138: tokenBegin = 0;
139: }
140:
141: static private final void FillBuff() throws java.io.IOException {
142: int i;
143: if (maxNextCharInd == 4096)
144: maxNextCharInd = nextCharInd = 0;
145:
146: try {
147: if ((i = inputStream.read(nextCharBuf, maxNextCharInd,
148: 4096 - maxNextCharInd)) == -1) {
149: inputStream.close();
150: throw new java.io.IOException();
151: } else
152: maxNextCharInd += i;
153: return;
154: } catch (java.io.IOException e) {
155: if (bufpos != 0) {
156: --bufpos;
157: backup(0);
158: } else {
159: bufline[bufpos] = line;
160: bufcolumn[bufpos] = column;
161: bufoff[bufpos] = off;
162: }
163: throw e;
164: }
165: }
166:
167: static private final char ReadByte() throws java.io.IOException {
168: if (++nextCharInd >= maxNextCharInd)
169: FillBuff();
170:
171: return nextCharBuf[nextCharInd];
172: }
173:
174: static public final char BeginToken() throws java.io.IOException {
175: if (inBuf > 0) {
176: --inBuf;
177: return buffer[tokenBegin = (bufpos == bufsize - 1) ? (bufpos = 0)
178: : ++bufpos];
179: }
180:
181: tokenBegin = 0;
182: bufpos = -1;
183:
184: return readChar();
185: }
186:
187: static private final void AdjustBuffSize() {
188: if (available == bufsize) {
189: if (tokenBegin > 2048) {
190: bufpos = 0;
191: available = tokenBegin;
192: } else
193: ExpandBuff(false);
194: } else if (available > tokenBegin)
195: available = bufsize;
196: else if ((tokenBegin - available) < 2048)
197: ExpandBuff(true);
198: else
199: available = tokenBegin;
200: }
201:
202: static private final void UpdateLineColumn(char c) {
203: column++;
204:
205: if (prevCharIsLF) {
206: prevCharIsLF = false;
207: line += (column = 1);
208: } else if (prevCharIsCR) {
209: prevCharIsCR = false;
210: if (c == '\n') {
211: prevCharIsLF = true;
212: } else
213: line += (column = 1);
214: }
215:
216: switch (c) {
217: case '\r':
218: prevCharIsCR = true;
219: break;
220: case '\n':
221: prevCharIsLF = true;
222: break;
223: case '\t':
224: column--;
225: column += (8 - (column & 07));
226: break;
227: default:
228: break;
229: }
230:
231: bufline[bufpos] = line;
232: bufcolumn[bufpos] = column;
233: bufoff[bufpos] = off++;
234: }
235:
236: static public final char readChar() throws java.io.IOException {
237: if (inBuf > 0) {
238: --inBuf;
239: return buffer[(bufpos == bufsize - 1) ? (bufpos = 0)
240: : ++bufpos];
241: }
242:
243: char c;
244:
245: if (++bufpos == available)
246: AdjustBuffSize();
247:
248: if (((buffer[bufpos] = c = (char) ((char) 0xff & ReadByte())) == '\\')) {
249: UpdateLineColumn(c);
250:
251: int backSlashCnt = 1;
252:
253: for (;;) // Read all the backslashes
254: {
255: if (++bufpos == available)
256: AdjustBuffSize();
257:
258: try {
259: if ((buffer[bufpos] = c = (char) ((char) 0xff & ReadByte())) != '\\') {
260: UpdateLineColumn(c);
261: // found a non-backslash char.
262: if ((c == 'u') && ((backSlashCnt & 1) == 1)) {
263: if (--bufpos < 0)
264: bufpos = bufsize - 1;
265:
266: break;
267: }
268:
269: backup(backSlashCnt);
270: return '\\';
271: }
272: } catch (java.io.IOException e) {
273: if (backSlashCnt > 1)
274: backup(backSlashCnt);
275:
276: return '\\';
277: }
278:
279: UpdateLineColumn(c);
280: backSlashCnt++;
281: }
282:
283: // Here, we have seen an odd number of backslash's followed by a 'u'
284: try {
285: // do we need to update offset here?
286: while ((c = (char) ((char) 0xff & ReadByte())) == 'u')
287: ++column;
288:
289: buffer[bufpos] = c = (char) (hexval(c) << 12
290: | hexval((char) ((char) 0xff & ReadByte())) << 8
291: | hexval((char) ((char) 0xff & ReadByte())) << 4 | hexval((char) ((char) 0xff & ReadByte())));
292:
293: column += 4;
294: } catch (java.io.IOException e) {
295: throw new Error("Invalid escape character at line "
296: + line + " column " + column + ".");
297: }
298:
299: if (backSlashCnt == 1)
300: return c;
301: else {
302: backup(backSlashCnt - 1);
303: return '\\';
304: }
305: } else {
306: UpdateLineColumn(c);
307: return (c);
308: }
309: }
310:
311: /**
312: * @deprecated
313: * @see #getEndColumn
314: */
315:
316: static public final int getColumn() {
317: return bufcolumn[bufpos];
318: }
319:
320: /**
321: * @deprecated
322: * @see #getEndLine
323: */
324:
325: static public final int getLine() {
326: return bufline[bufpos];
327: }
328:
329: static public final int getEndColumn() {
330: return bufcolumn[bufpos];
331: }
332:
333: static public final int getEndLine() {
334: return bufline[bufpos];
335: }
336:
337: static public final int getBeginColumn() {
338: return bufcolumn[tokenBegin];
339: }
340:
341: static public final int getBeginLine() {
342: return bufline[tokenBegin];
343: }
344:
345: static public final int getBeginOffset() {
346: return bufoff[tokenBegin];
347: }
348:
349: static public final int getEndOffset() {
350: return bufoff[bufpos];
351: }
352:
353: static public final void backup(int amount) {
354:
355: inBuf += amount;
356: if ((bufpos -= amount) < 0)
357: bufpos += bufsize;
358: }
359:
360: public ASCII_UCodeESC_CharStream(java.io.Reader dstream,
361: int startline, int startcolumn, int buffersize) {
362: if (inputStream != null)
363: throw new Error(
364: "\n ERROR: Second call to the constructor of a static ASCII_UCodeESC_CharStream. You must\n"
365: + " either use ReInit() or set the JavaCC option STATIC to false\n"
366: + " during the generation of this class.");
367: inputStream = dstream;
368: line = startline;
369: column = startcolumn - 1;
370: off = 0;
371:
372: available = bufsize = buffersize;
373: buffer = new char[buffersize];
374: bufline = new int[buffersize];
375: bufcolumn = new int[buffersize];
376: bufoff = new int[buffersize];
377: nextCharBuf = new char[4096];
378: }
379:
380: public ASCII_UCodeESC_CharStream(java.io.Reader dstream,
381: int startline, int startcolumn) {
382: this (dstream, startline, startcolumn, 4096);
383: }
384:
385: public void ReInit(java.io.Reader dstream, int startline,
386: int startcolumn, int buffersize) {
387: inputStream = dstream;
388: line = startline;
389: column = startcolumn - 1;
390: off = 0;
391:
392: if (buffer == null || buffersize != buffer.length) {
393: available = bufsize = buffersize;
394: buffer = new char[buffersize];
395: bufline = new int[buffersize];
396: bufcolumn = new int[buffersize];
397: bufoff = 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 ASCII_UCodeESC_CharStream(java.io.InputStream dstream,
411: int startline, int startcolumn, int buffersize) {
412: this (new java.io.InputStreamReader(dstream), startline,
413: startcolumn, 4096);
414: }
415:
416: public ASCII_UCodeESC_CharStream(java.io.InputStream dstream,
417: int startline, int startcolumn) {
418: this (dstream, startline, startcolumn, 4096);
419: }
420:
421: public void ReInit(java.io.InputStream dstream, int startline,
422: int startcolumn, int buffersize) {
423: ReInit(new java.io.InputStreamReader(dstream), startline,
424: startcolumn, 4096);
425: }
426:
427: public void ReInit(java.io.InputStream dstream, int startline,
428: int startcolumn) {
429: ReInit(dstream, startline, startcolumn, 4096);
430: }
431:
432: static public final String GetImage() {
433: if (bufpos >= tokenBegin)
434: return new String(buffer, tokenBegin, bufpos - tokenBegin
435: + 1);
436: else
437: return new String(buffer, tokenBegin, bufsize - tokenBegin)
438: + new String(buffer, 0, bufpos + 1);
439: }
440:
441: static public final char[] GetSuffix(int len) {
442: char[] ret = new char[len];
443:
444: if ((bufpos + 1) >= len)
445: System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
446: else {
447: System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret,
448: 0, len - bufpos - 1);
449: System.arraycopy(buffer, 0, ret, len - bufpos - 1,
450: bufpos + 1);
451: }
452:
453: return ret;
454: }
455:
456: static public void Done() {
457: nextCharBuf = null;
458: buffer = null;
459: bufline = null;
460: bufcolumn = null;
461: bufoff = null;
462: }
463:
464: /**
465: * Method to adjust line and column numbers for the start of a token.<BR>
466: */
467: static public void adjustBeginLineColumn(int newLine, int newCol) {
468: int start = tokenBegin;
469: int len;
470:
471: if (bufpos >= tokenBegin) {
472: len = bufpos - tokenBegin + inBuf + 1;
473: } else {
474: len = bufsize - tokenBegin + bufpos + 1 + inBuf;
475: }
476:
477: int i = 0, j = 0, k = 0;
478: int nextColDiff = 0, columnDiff = 0;
479:
480: while (i < len
481: && bufline[j = start % bufsize] == bufline[k = ++start
482: % bufsize]) {
483: bufline[j] = newLine;
484: nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
485: bufcolumn[j] = newCol + columnDiff;
486: columnDiff = nextColDiff;
487: i++;
488: }
489:
490: if (i < len) {
491: bufline[j] = newLine++;
492: bufcolumn[j] = newCol + columnDiff;
493:
494: while (i++ < len) {
495: if (bufline[j = start % bufsize] != bufline[++start
496: % bufsize])
497: bufline[j] = newLine++;
498: else
499: bufline[j] = newLine;
500: }
501: }
502:
503: line = bufline[j];
504: column = bufcolumn[j];
505: off = bufoff[j];
506: }
507:
508: }
|