001: /*
002:
003: Derby - Class org.apache.derby.impl.sql.compile.UCode_CharStream
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: /* Generated By:JavaCC: Do not edit this line. UCode_CharStream.java Version 0.7pre6 */
023: package org.apache.derby.impl.sql.compile;
024:
025: /**
026: * An implementation of interface CharStream, where the stream is assumed to
027: * contain only Unicode characters.
028: */
029:
030: // NOTE: This class was modified to support the ability to get all the
031: // characters in the input stream between two tokens. - Jeff
032: public final class UCode_CharStream implements CharStream {
033: // The next two lines are added to support ability to get the input
034: // between two tokens.
035: int charCnt;
036: int[] charOffset;
037:
038: public static final boolean staticFlag = false;
039: public int bufpos = -1;
040: int bufsize;
041: int available;
042: int tokenBegin;
043: private int bufline[];
044: private int bufcolumn[];
045:
046: private int column = 0;
047: private int line = 1;
048:
049: private boolean prevCharIsCR = false;
050: private boolean prevCharIsLF = false;
051:
052: private java.io.Reader inputStream;
053:
054: private char[] nextCharBuf;
055: private char[] buffer;
056: private int maxNextCharInd = 0;
057: private int nextCharInd = -1;
058:
059: private final void ExpandBuff(boolean wrapAround) {
060: char[] newbuffer = new char[bufsize + 2048];
061: int newbufline[] = new int[bufsize + 2048];
062: int newbufcolumn[] = new int[bufsize + 2048];
063:
064: // The next line was added to support ability to get the input
065: // between two tokens.
066: int newcharOffset[] = new int[bufsize + 2048];
067:
068: try {
069: if (wrapAround) {
070: System.arraycopy(buffer, tokenBegin, newbuffer, 0,
071: bufsize - tokenBegin);
072: System.arraycopy(buffer, 0, newbuffer, bufsize
073: - tokenBegin, bufpos);
074: buffer = newbuffer;
075:
076: System.arraycopy(bufline, tokenBegin, newbufline, 0,
077: bufsize - tokenBegin);
078: System.arraycopy(bufline, 0, newbufline, bufsize
079: - tokenBegin, bufpos);
080: bufline = newbufline;
081:
082: System.arraycopy(bufcolumn, tokenBegin, newbufcolumn,
083: 0, bufsize - tokenBegin);
084: System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize
085: - tokenBegin, bufpos);
086: bufcolumn = newbufcolumn;
087:
088: // The next three lines were added to support ability to get input
089: // between two tokens.
090: System.arraycopy(charOffset, tokenBegin, newcharOffset,
091: 0, bufsize - tokenBegin);
092: System.arraycopy(charOffset, 0, newcharOffset, bufsize
093: - tokenBegin, bufpos);
094: charOffset = newcharOffset;
095:
096: bufpos += (bufsize - tokenBegin);
097: } else {
098: System.arraycopy(buffer, tokenBegin, newbuffer, 0,
099: bufsize - tokenBegin);
100: buffer = newbuffer;
101:
102: System.arraycopy(bufline, tokenBegin, newbufline, 0,
103: bufsize - tokenBegin);
104: bufline = newbufline;
105:
106: System.arraycopy(bufcolumn, tokenBegin, newbufcolumn,
107: 0, bufsize - tokenBegin);
108: bufcolumn = newbufcolumn;
109:
110: // The next two lines were added to support ability to get input
111: // between two tokens.
112: System.arraycopy(charOffset, tokenBegin, newcharOffset,
113: 0, bufsize - tokenBegin);
114: charOffset = newcharOffset;
115:
116: bufpos -= tokenBegin;
117: }
118: } catch (Throwable t) {
119: throw new Error(t.getMessage());
120: }
121:
122: available = (bufsize += 2048);
123: tokenBegin = 0;
124: }
125:
126: private final void FillBuff() throws java.io.IOException {
127: if (maxNextCharInd == nextCharBuf.length)
128: maxNextCharInd = nextCharInd = 0;
129:
130: int i;
131: try {
132: if ((i = inputStream.read(nextCharBuf, maxNextCharInd,
133: nextCharBuf.length - maxNextCharInd)) == -1) {
134: inputStream.close();
135: throw new java.io.IOException();
136: } else
137: maxNextCharInd += i;
138: return;
139: } catch (java.io.IOException e) {
140: if (bufpos != 0) {
141: --bufpos;
142: backup(0);
143: } else {
144: bufline[bufpos] = line;
145: bufcolumn[bufpos] = column;
146: }
147: if (tokenBegin == -1)
148: tokenBegin = bufpos;
149: throw e;
150: }
151: }
152:
153: private final char ReadChar() 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: return buffer[tokenBegin = (bufpos == bufsize - 1) ? (bufpos = 0)
164: : ++bufpos];
165: }
166:
167: tokenBegin = 0;
168: bufpos = -1;
169: char c = readChar();
170:
171: return c;
172: }
173:
174: private final void UpdateLineColumn(char c) {
175: column++;
176:
177: if (prevCharIsLF) {
178: prevCharIsLF = false;
179: line += (column = 1);
180: } else if (prevCharIsCR) {
181: prevCharIsCR = false;
182: if (c == '\n') {
183: prevCharIsLF = true;
184: } else
185: line += (column = 1);
186: }
187:
188: switch (c) {
189: case '\r':
190: prevCharIsCR = true;
191: break;
192: case '\n':
193: prevCharIsLF = true;
194: break;
195: case '\t':
196: column--;
197: column += (8 - (column & 07));
198: break;
199: default:
200: break;
201: }
202:
203: bufline[bufpos] = line;
204: bufcolumn[bufpos] = column;
205: }
206:
207: private int inBuf = 0;
208:
209: public final char readChar() throws java.io.IOException {
210: if (inBuf > 0) {
211: --inBuf;
212: return (char) buffer[(bufpos == bufsize - 1) ? (bufpos = 0)
213: : ++bufpos];
214: }
215:
216: if (++bufpos == available) {
217: if (available == bufsize) {
218: if (tokenBegin > 2048) {
219: bufpos = 0;
220: available = tokenBegin;
221: } else if (tokenBegin < 0)
222: bufpos = 0;
223: else
224: ExpandBuff(false);
225: } else if (available > tokenBegin)
226: available = bufsize;
227: else if ((tokenBegin - available) < 2048)
228: ExpandBuff(true);
229: else
230: available = tokenBegin;
231: }
232:
233: char c = ReadChar();
234:
235: UpdateLineColumn(c);
236:
237: // The next line was added to support ability to get the input
238: // between two tokens.
239: charOffset[bufpos] = charCnt++;
240:
241: return (buffer[bufpos] = c);
242: }
243:
244: /**
245: * @deprecated
246: * @see #getEndColumn
247: */
248:
249: public final int getColumn() {
250: return bufcolumn[bufpos];
251: }
252:
253: /**
254: * @deprecated
255: * @see #getEndLine
256: */
257:
258: public final int getLine() {
259: return bufline[bufpos];
260: }
261:
262: public final int getEndColumn() {
263: return bufcolumn[bufpos];
264: }
265:
266: public final int getEndLine() {
267: return bufline[bufpos];
268: }
269:
270: public final int getBeginColumn() {
271: return bufcolumn[tokenBegin];
272: }
273:
274: public final int getBeginLine() {
275: return bufline[tokenBegin];
276: }
277:
278: // This method was added to support ability to get the input
279: // between two tokens.
280: public final int getBeginOffset() {
281: return charOffset[tokenBegin];
282: }
283:
284: // This method was added to support ability to get the input
285: // between two tokens.
286: public final int getEndOffset() {
287: return charOffset[bufpos];
288: }
289:
290: public final void backup(int amount) {
291:
292: inBuf += amount;
293: if ((bufpos -= amount) < 0)
294: bufpos += bufsize;
295: }
296:
297: public UCode_CharStream(java.io.Reader dstream, int startline,
298: int startcolumn, int buffersize) {
299: inputStream = dstream;
300: line = startline;
301: column = startcolumn - 1;
302:
303: available = bufsize = buffersize;
304: buffer = new char[buffersize];
305: nextCharBuf = new char[buffersize];
306: bufline = new int[buffersize];
307: bufcolumn = new int[buffersize];
308:
309: // The next line was added to support ability to get the input
310: // between two tokens.
311: charOffset = new int[buffersize];
312: }
313:
314: public UCode_CharStream(java.io.Reader dstream, int startline,
315: int startcolumn) {
316: this (dstream, startline, startcolumn, 4096);
317: }
318:
319: public void ReInit(java.io.Reader dstream, int startline,
320: int startcolumn, int buffersize) {
321: inputStream = dstream;
322: line = startline;
323: column = startcolumn - 1;
324:
325: if (buffer == null || buffersize != buffer.length) {
326: available = bufsize = buffersize;
327: buffer = new char[buffersize];
328: nextCharBuf = new char[buffersize];
329: bufline = new int[buffersize];
330: bufcolumn = new int[buffersize];
331: }
332:
333: // The next line was added to support ability to get the input
334: // between two tokens.
335: inBuf = maxNextCharInd = charCnt = tokenBegin = 0;
336: nextCharInd = bufpos = -1;
337: }
338:
339: public void ReInit(java.io.Reader dstream, int startline,
340: int startcolumn) {
341: ReInit(dstream, startline, startcolumn, 4096);
342: }
343:
344: public UCode_CharStream(java.io.InputStream dstream, int startline,
345: int startcolumn, int buffersize) {
346: this (new java.io.InputStreamReader(dstream), startline,
347: startcolumn, 4096);
348: }
349:
350: public UCode_CharStream(java.io.InputStream dstream, int startline,
351: int startcolumn) {
352: this (dstream, startline, startcolumn, 4096);
353: }
354:
355: public void ReInit(java.io.InputStream dstream, int startline,
356: int startcolumn, int buffersize) {
357: ReInit(new java.io.InputStreamReader(dstream), startline,
358: startcolumn, 4096);
359: }
360:
361: public void ReInit(java.io.InputStream dstream, int startline,
362: int startcolumn) {
363: ReInit(dstream, startline, startcolumn, 4096);
364: }
365:
366: public final String GetImage() {
367: if (bufpos >= tokenBegin)
368: return new String(buffer, tokenBegin, bufpos - tokenBegin
369: + 1);
370: else
371: return new String(buffer, tokenBegin, bufsize - tokenBegin)
372: + new String(buffer, 0, bufpos + 1);
373: }
374:
375: public final char[] GetSuffix(int len) {
376: char[] ret = new char[len];
377:
378: if ((bufpos + 1) >= len)
379: System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
380: else {
381: System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret,
382: 0, len - bufpos - 1);
383: System.arraycopy(buffer, 0, ret, len - bufpos - 1,
384: bufpos + 1);
385: }
386:
387: return ret;
388: }
389:
390: public void Done() {
391: nextCharBuf = null;
392: buffer = null;
393: bufline = null;
394: bufcolumn = null;
395:
396: // The next line was added to support ability to get the input
397: // between two tokens.
398: charOffset = null;
399: }
400:
401: /**
402: * Method to adjust line and column numbers for the start of a token.<BR>
403: */
404: public void adjustBeginLineColumn(int newLine, int newCol) {
405: int start = tokenBegin;
406: int len;
407:
408: if (bufpos >= tokenBegin) {
409: len = bufpos - tokenBegin + inBuf + 1;
410: } else {
411: len = bufsize - tokenBegin + bufpos + 1 + inBuf;
412: }
413:
414: int i = 0, j = 0, k = 0;
415: int nextColDiff = 0, columnDiff = 0;
416:
417: while (i < len
418: && bufline[j = start % bufsize] == bufline[k = ++start
419: % bufsize]) {
420: bufline[j] = newLine;
421: nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
422: bufcolumn[j] = newCol + columnDiff;
423: columnDiff = nextColDiff;
424: i++;
425: }
426:
427: if (i < len) {
428: bufline[j] = newLine++;
429: bufcolumn[j] = newCol + columnDiff;
430:
431: while (i++ < len) {
432: if (bufline[j = start % bufsize] != bufline[++start
433: % bufsize])
434: bufline[j] = newLine++;
435: else
436: bufline[j] = newLine;
437: }
438: }
439:
440: line = bufline[j];
441: column = bufcolumn[j];
442: }
443:
444: }
|