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