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