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