001: /* Generated By:JavaCC: Do not edit this line. JavaCharStream.java Version 4.0 */
002: package org.openrdf.query.parser.sparql.ast;
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: try {
260: if ((buffer[bufpos] = c = ReadByte()) != '\\') {
261: UpdateLineColumn(c);
262: // found a non-backslash char.
263: if ((c == 'u') && ((backSlashCnt & 1) == 1)) {
264: if (--bufpos < 0)
265: bufpos = bufsize - 1;
266:
267: break;
268: }
269:
270: backup(backSlashCnt);
271: return '\\';
272: }
273: } catch (java.io.IOException e) {
274: if (backSlashCnt > 1)
275: backup(backSlashCnt);
276:
277: return '\\';
278: }
279:
280: UpdateLineColumn(c);
281: backSlashCnt++;
282: }
283:
284: // Here, we have seen an odd number of backslash's followed by a 'u'
285: try {
286: while ((c = ReadByte()) == 'u')
287: ++column;
288:
289: buffer[bufpos] = c = (char) (hexval(c) << 12
290: | hexval(ReadByte()) << 8
291: | hexval(ReadByte()) << 4 | hexval(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: public int getColumn() {
317: return bufcolumn[bufpos];
318: }
319:
320: /**
321: * @deprecated
322: * @see #getEndLine
323: */
324:
325: public int getLine() {
326: return bufline[bufpos];
327: }
328:
329: public int getEndColumn() {
330: return bufcolumn[bufpos];
331: }
332:
333: public int getEndLine() {
334: return bufline[bufpos];
335: }
336:
337: public int getBeginColumn() {
338: return bufcolumn[tokenBegin];
339: }
340:
341: public int getBeginLine() {
342: return bufline[tokenBegin];
343: }
344:
345: public void backup(int amount) {
346:
347: inBuf += amount;
348: if ((bufpos -= amount) < 0)
349: bufpos += bufsize;
350: }
351:
352: public JavaCharStream(java.io.Reader dstream, int startline,
353: int startcolumn, int buffersize) {
354: inputStream = dstream;
355: line = startline;
356: column = startcolumn - 1;
357:
358: available = bufsize = buffersize;
359: buffer = new char[buffersize];
360: bufline = new int[buffersize];
361: bufcolumn = new int[buffersize];
362: nextCharBuf = new char[4096];
363: }
364:
365: public JavaCharStream(java.io.Reader dstream, int startline,
366: int startcolumn) {
367: this (dstream, startline, startcolumn, 4096);
368: }
369:
370: public JavaCharStream(java.io.Reader dstream) {
371: this (dstream, 1, 1, 4096);
372: }
373:
374: public void ReInit(java.io.Reader dstream, int startline,
375: int startcolumn, int buffersize) {
376: inputStream = dstream;
377: line = startline;
378: column = startcolumn - 1;
379:
380: if (buffer == null || buffersize != buffer.length) {
381: available = bufsize = buffersize;
382: buffer = new char[buffersize];
383: bufline = new int[buffersize];
384: bufcolumn = new int[buffersize];
385: nextCharBuf = new char[4096];
386: }
387: prevCharIsLF = prevCharIsCR = false;
388: tokenBegin = inBuf = maxNextCharInd = 0;
389: nextCharInd = bufpos = -1;
390: }
391:
392: public void ReInit(java.io.Reader dstream, int startline,
393: int startcolumn) {
394: ReInit(dstream, startline, startcolumn, 4096);
395: }
396:
397: public void ReInit(java.io.Reader dstream) {
398: ReInit(dstream, 1, 1, 4096);
399: }
400:
401: public JavaCharStream(java.io.InputStream dstream, String encoding,
402: int startline, int startcolumn, int buffersize)
403: throws java.io.UnsupportedEncodingException {
404: this (encoding == null ? new java.io.InputStreamReader(dstream)
405: : new java.io.InputStreamReader(dstream, encoding),
406: startline, startcolumn, buffersize);
407: }
408:
409: public JavaCharStream(java.io.InputStream dstream, int startline,
410: int startcolumn, int buffersize) {
411: this (new java.io.InputStreamReader(dstream), startline,
412: startcolumn, 4096);
413: }
414:
415: public JavaCharStream(java.io.InputStream dstream, String encoding,
416: int startline, int startcolumn)
417: throws java.io.UnsupportedEncodingException {
418: this (dstream, encoding, startline, startcolumn, 4096);
419: }
420:
421: public JavaCharStream(java.io.InputStream dstream, int startline,
422: int startcolumn) {
423: this (dstream, startline, startcolumn, 4096);
424: }
425:
426: public JavaCharStream(java.io.InputStream dstream, String encoding)
427: throws java.io.UnsupportedEncodingException {
428: this (dstream, encoding, 1, 1, 4096);
429: }
430:
431: public JavaCharStream(java.io.InputStream dstream) {
432: this (dstream, 1, 1, 4096);
433: }
434:
435: public void ReInit(java.io.InputStream dstream, String encoding,
436: int startline, int startcolumn, int buffersize)
437: throws java.io.UnsupportedEncodingException {
438: ReInit(
439: encoding == null ? new java.io.InputStreamReader(
440: dstream) : new java.io.InputStreamReader(
441: dstream, encoding), startline, startcolumn,
442: buffersize);
443: }
444:
445: public void ReInit(java.io.InputStream dstream, int startline,
446: int startcolumn, int buffersize) {
447: ReInit(new java.io.InputStreamReader(dstream), startline,
448: startcolumn, buffersize);
449: }
450:
451: public void ReInit(java.io.InputStream dstream, String encoding,
452: int startline, int startcolumn)
453: throws java.io.UnsupportedEncodingException {
454: ReInit(dstream, encoding, startline, startcolumn, 4096);
455: }
456:
457: public void ReInit(java.io.InputStream dstream, int startline,
458: int startcolumn) {
459: ReInit(dstream, startline, startcolumn, 4096);
460: }
461:
462: public void ReInit(java.io.InputStream dstream, String encoding)
463: throws java.io.UnsupportedEncodingException {
464: ReInit(dstream, encoding, 1, 1, 4096);
465: }
466:
467: public void ReInit(java.io.InputStream dstream) {
468: ReInit(dstream, 1, 1, 4096);
469: }
470:
471: public String GetImage() {
472: if (bufpos >= tokenBegin)
473: return new String(buffer, tokenBegin, bufpos - tokenBegin
474: + 1);
475: else
476: return new String(buffer, tokenBegin, bufsize - tokenBegin)
477: + new String(buffer, 0, bufpos + 1);
478: }
479:
480: public char[] GetSuffix(int len) {
481: char[] ret = new char[len];
482:
483: if ((bufpos + 1) >= len)
484: System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
485: else {
486: System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret,
487: 0, len - bufpos - 1);
488: System.arraycopy(buffer, 0, ret, len - bufpos - 1,
489: bufpos + 1);
490: }
491:
492: return ret;
493: }
494:
495: public void Done() {
496: nextCharBuf = null;
497: buffer = null;
498: bufline = null;
499: bufcolumn = null;
500: }
501:
502: /**
503: * Method to adjust line and column numbers for the start of a token.
504: */
505: public void adjustBeginLineColumn(int newLine, int newCol) {
506: int start = tokenBegin;
507: int len;
508:
509: if (bufpos >= tokenBegin) {
510: len = bufpos - tokenBegin + inBuf + 1;
511: } else {
512: len = bufsize - tokenBegin + bufpos + 1 + inBuf;
513: }
514:
515: int i = 0, j = 0, k = 0;
516: int nextColDiff = 0, columnDiff = 0;
517:
518: while (i < len
519: && bufline[j = start % bufsize] == bufline[k = ++start
520: % bufsize]) {
521: bufline[j] = newLine;
522: nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
523: bufcolumn[j] = newCol + columnDiff;
524: columnDiff = nextColDiff;
525: i++;
526: }
527:
528: if (i < len) {
529: bufline[j] = newLine++;
530: bufcolumn[j] = newCol + columnDiff;
531:
532: while (i++ < len) {
533: if (bufline[j = start % bufsize] != bufline[++start
534: % bufsize])
535: bufline[j] = newLine++;
536: else
537: bufline[j] = newLine;
538: }
539: }
540:
541: line = bufline[j];
542: column = bufcolumn[j];
543: }
544:
545: }
|