001: /*
002: * Copyright (c) 1998 - 2005 Versant Corporation
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * Versant Corporation - initial API and implementation
010: */
011: /* Generated By:JavaCC: Do not edit this line. JavaCharStream.java Version 3.0 */
012: package com.versant.core.jdo.query;
013:
014: /**
015: * An implementation of interface CharStream, where the stream is assumed to
016: * contain only ASCII characters (with java-like unicode escape processing).
017: */
018:
019: public class JavaCharStream {
020: public static final boolean staticFlag = false;
021:
022: static final int hexval(char c) throws java.io.IOException {
023: switch (c) {
024: case '0':
025: return 0;
026: case '1':
027: return 1;
028: case '2':
029: return 2;
030: case '3':
031: return 3;
032: case '4':
033: return 4;
034: case '5':
035: return 5;
036: case '6':
037: return 6;
038: case '7':
039: return 7;
040: case '8':
041: return 8;
042: case '9':
043: return 9;
044:
045: case 'a':
046: case 'A':
047: return 10;
048: case 'b':
049: case 'B':
050: return 11;
051: case 'c':
052: case 'C':
053: return 12;
054: case 'd':
055: case 'D':
056: return 13;
057: case 'e':
058: case 'E':
059: return 14;
060: case 'f':
061: case 'F':
062: return 15;
063: }
064:
065: throw new java.io.IOException(); // Should never come here
066: }
067:
068: public int bufpos = -1;
069: int bufsize;
070: int available;
071: int tokenBegin;
072: protected int bufline[];
073: protected int bufcolumn[];
074:
075: protected int column = 0;
076: protected int line = 1;
077:
078: protected boolean prevCharIsCR = false;
079: protected boolean prevCharIsLF = false;
080:
081: protected java.io.Reader inputStream;
082:
083: protected char[] nextCharBuf;
084: protected char[] buffer;
085: protected int maxNextCharInd = 0;
086: protected int nextCharInd = -1;
087: protected int inBuf = 0;
088:
089: protected void ExpandBuff(boolean wrapAround) {
090: char[] newbuffer = new char[bufsize + 2048];
091: int newbufline[] = new int[bufsize + 2048];
092: int newbufcolumn[] = new int[bufsize + 2048];
093:
094: try {
095: if (wrapAround) {
096: System.arraycopy(buffer, tokenBegin, newbuffer, 0,
097: bufsize - tokenBegin);
098: System.arraycopy(buffer, 0, newbuffer, bufsize
099: - tokenBegin, bufpos);
100: buffer = newbuffer;
101:
102: System.arraycopy(bufline, tokenBegin, newbufline, 0,
103: bufsize - tokenBegin);
104: System.arraycopy(bufline, 0, newbufline, bufsize
105: - tokenBegin, bufpos);
106: bufline = newbufline;
107:
108: System.arraycopy(bufcolumn, tokenBegin, newbufcolumn,
109: 0, bufsize - tokenBegin);
110: System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize
111: - tokenBegin, bufpos);
112: bufcolumn = newbufcolumn;
113:
114: bufpos += (bufsize - tokenBegin);
115: } else {
116: System.arraycopy(buffer, tokenBegin, newbuffer, 0,
117: bufsize - tokenBegin);
118: buffer = newbuffer;
119:
120: System.arraycopy(bufline, tokenBegin, newbufline, 0,
121: bufsize - tokenBegin);
122: bufline = newbufline;
123:
124: System.arraycopy(bufcolumn, tokenBegin, newbufcolumn,
125: 0, bufsize - tokenBegin);
126: bufcolumn = newbufcolumn;
127:
128: bufpos -= tokenBegin;
129: }
130: } catch (Throwable t) {
131: throw new Error(t.getMessage());
132: }
133:
134: available = (bufsize += 2048);
135: tokenBegin = 0;
136: }
137:
138: protected void FillBuff() throws java.io.IOException {
139: int i;
140: if (maxNextCharInd == 4096)
141: maxNextCharInd = nextCharInd = 0;
142:
143: try {
144: if ((i = inputStream.read(nextCharBuf, maxNextCharInd,
145: 4096 - maxNextCharInd)) == -1) {
146: inputStream.close();
147: throw new java.io.IOException();
148: } else
149: maxNextCharInd += i;
150: return;
151: } catch (java.io.IOException e) {
152: if (bufpos != 0) {
153: --bufpos;
154: backup(0);
155: } else {
156: bufline[bufpos] = line;
157: bufcolumn[bufpos] = column;
158: }
159: throw e;
160: }
161: }
162:
163: protected char ReadByte() throws java.io.IOException {
164: if (++nextCharInd >= maxNextCharInd)
165: FillBuff();
166:
167: return nextCharBuf[nextCharInd];
168: }
169:
170: public char BeginToken() throws java.io.IOException {
171: if (inBuf > 0) {
172: --inBuf;
173:
174: if (++bufpos == bufsize)
175: bufpos = 0;
176:
177: tokenBegin = bufpos;
178: return buffer[bufpos];
179: }
180:
181: tokenBegin = 0;
182: bufpos = -1;
183:
184: return readChar();
185: }
186:
187: protected 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: protected 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: }
234:
235: public char readChar() throws java.io.IOException {
236: if (inBuf > 0) {
237: --inBuf;
238:
239: if (++bufpos == bufsize)
240: bufpos = 0;
241:
242: return buffer[bufpos];
243: }
244:
245: char c;
246:
247: if (++bufpos == available)
248: AdjustBuffSize();
249:
250: if ((buffer[bufpos] = c = ReadByte()) == '\\') {
251: UpdateLineColumn(c);
252:
253: int backSlashCnt = 1;
254:
255: for (;;) // Read all the backslashes
256: {
257: if (++bufpos == available)
258: AdjustBuffSize();
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:
317: public int getColumn() {
318: return bufcolumn[bufpos];
319: }
320:
321: /**
322: * @deprecated
323: * @see #getEndLine
324: */
325:
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, int startline,
403: int startcolumn, int buffersize) {
404: this (new java.io.InputStreamReader(dstream), startline,
405: startcolumn, 4096);
406: }
407:
408: public JavaCharStream(java.io.InputStream dstream, int startline,
409: int startcolumn) {
410: this (dstream, startline, startcolumn, 4096);
411: }
412:
413: public JavaCharStream(java.io.InputStream dstream) {
414: this (dstream, 1, 1, 4096);
415: }
416:
417: public void ReInit(java.io.InputStream dstream, int startline,
418: int startcolumn, int buffersize) {
419: ReInit(new java.io.InputStreamReader(dstream), startline,
420: startcolumn, 4096);
421: }
422:
423: public void ReInit(java.io.InputStream dstream, int startline,
424: int startcolumn) {
425: ReInit(dstream, startline, startcolumn, 4096);
426: }
427:
428: public void ReInit(java.io.InputStream dstream) {
429: ReInit(dstream, 1, 1, 4096);
430: }
431:
432: public 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: public 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: public void Done() {
457: nextCharBuf = null;
458: buffer = null;
459: bufline = null;
460: bufcolumn = null;
461: }
462:
463: /**
464: * Method to adjust line and column numbers for the start of a token.
465: */
466: public void adjustBeginLineColumn(int newLine, int newCol) {
467: int start = tokenBegin;
468: int len;
469:
470: if (bufpos >= tokenBegin) {
471: len = bufpos - tokenBegin + inBuf + 1;
472: } else {
473: len = bufsize - tokenBegin + bufpos + 1 + inBuf;
474: }
475:
476: int i = 0, j = 0, k = 0;
477: int nextColDiff = 0, columnDiff = 0;
478:
479: while (i < len
480: && bufline[j = start % bufsize] == bufline[k = ++start
481: % bufsize]) {
482: bufline[j] = newLine;
483: nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
484: bufcolumn[j] = newCol + columnDiff;
485: columnDiff = nextColDiff;
486: i++;
487: }
488:
489: if (i < len) {
490: bufline[j] = newLine++;
491: bufcolumn[j] = newCol + columnDiff;
492:
493: while (i++ < len) {
494: if (bufline[j = start % bufsize] != bufline[++start
495: % bufsize])
496: bufline[j] = newLine++;
497: else
498: bufline[j] = newLine;
499: }
500: }
501:
502: line = bufline[j];
503: column = bufcolumn[j];
504: }
505:
506: }
|