001: /*
002:
003: Derby - Class org.apache.derby.impl.tools.ij.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.tools.ij;
024:
025: /**
026: * An implementation of interface CharStream, where the stream is assumed to
027: * contain only Unicode characters.
028: */
029:
030: public final class UCode_CharStream implements CharStream {
031: public static final boolean staticFlag = false;
032: public int bufpos = -1;
033: int bufsize;
034: int available;
035: int tokenBegin;
036: private int bufline[];
037: private int bufcolumn[];
038:
039: private int column = 0;
040: private int line = 1;
041:
042: private boolean prevCharIsCR = false;
043: private boolean prevCharIsLF = false;
044:
045: private java.io.Reader inputStream;
046:
047: private char[] nextCharBuf;
048: private char[] buffer;
049: private int maxNextCharInd = 0;
050: private int nextCharInd = -1;
051:
052: private final void ExpandBuff(boolean wrapAround) {
053: char[] newbuffer = new char[bufsize + 2048];
054: int newbufline[] = new int[bufsize + 2048];
055: int newbufcolumn[] = new int[bufsize + 2048];
056:
057: try {
058: if (wrapAround) {
059: System.arraycopy(buffer, tokenBegin, newbuffer, 0,
060: bufsize - tokenBegin);
061: System.arraycopy(buffer, 0, newbuffer, bufsize
062: - tokenBegin, bufpos);
063: buffer = newbuffer;
064:
065: System.arraycopy(bufline, tokenBegin, newbufline, 0,
066: bufsize - tokenBegin);
067: System.arraycopy(bufline, 0, newbufline, bufsize
068: - tokenBegin, bufpos);
069: bufline = newbufline;
070:
071: System.arraycopy(bufcolumn, tokenBegin, newbufcolumn,
072: 0, bufsize - tokenBegin);
073: System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize
074: - tokenBegin, bufpos);
075: bufcolumn = newbufcolumn;
076:
077: bufpos += (bufsize - tokenBegin);
078: } else {
079: System.arraycopy(buffer, tokenBegin, newbuffer, 0,
080: bufsize - tokenBegin);
081: buffer = newbuffer;
082:
083: System.arraycopy(bufline, tokenBegin, newbufline, 0,
084: bufsize - tokenBegin);
085: bufline = newbufline;
086:
087: System.arraycopy(bufcolumn, tokenBegin, newbufcolumn,
088: 0, bufsize - tokenBegin);
089: bufcolumn = newbufcolumn;
090:
091: bufpos -= tokenBegin;
092: }
093: } catch (Throwable t) {
094: throw new Error(t.getMessage());
095: }
096:
097: available = (bufsize += 2048);
098: tokenBegin = 0;
099: }
100:
101: private final void FillBuff() throws java.io.IOException {
102: if (maxNextCharInd == 4096)
103: maxNextCharInd = nextCharInd = 0;
104:
105: int i;
106: try {
107: if ((i = inputStream.read(nextCharBuf, maxNextCharInd,
108: 4096 - maxNextCharInd)) == -1) {
109: inputStream.close();
110: throw new java.io.IOException();
111: } else
112: maxNextCharInd += i;
113: return;
114: } catch (java.io.IOException e) {
115: if (bufpos != 0) {
116: --bufpos;
117: backup(0);
118: } else {
119: bufline[bufpos] = line;
120: bufcolumn[bufpos] = column;
121: }
122: if (tokenBegin == -1)
123: tokenBegin = bufpos;
124: throw e;
125: }
126: }
127:
128: private final char ReadChar() throws java.io.IOException {
129: if (++nextCharInd >= maxNextCharInd)
130: FillBuff();
131:
132: return nextCharBuf[nextCharInd];
133: }
134:
135: public char BeginToken() throws java.io.IOException {
136: tokenBegin = -1;
137: char c = readChar();
138: tokenBegin = bufpos;
139:
140: return c;
141: }
142:
143: private final void UpdateLineColumn(char c) {
144: column++;
145:
146: if (prevCharIsLF) {
147: prevCharIsLF = false;
148: line += (column = 1);
149: } else if (prevCharIsCR) {
150: prevCharIsCR = false;
151: if (c == '\n') {
152: prevCharIsLF = true;
153: } else
154: line += (column = 1);
155: }
156:
157: switch (c) {
158: case '\r':
159: prevCharIsCR = true;
160: break;
161: case '\n':
162: prevCharIsLF = true;
163: break;
164: case '\t':
165: column--;
166: column += (8 - (column & 07));
167: break;
168: default:
169: break;
170: }
171: }
172:
173: private int inBuf = 0;
174:
175: public final char readChar() throws java.io.IOException {
176: if (inBuf > 0) {
177: --inBuf;
178: return (char) buffer[(bufpos == bufsize - 1) ? (bufpos = 0)
179: : ++bufpos];
180: }
181:
182: bufpos++;
183: char c = ReadChar();
184: UpdateLineColumn(c);
185:
186: if (bufpos == available) {
187: if (available == bufsize) {
188: if (tokenBegin > 2048) {
189: bufpos = 0;
190: available = tokenBegin;
191: } else if (tokenBegin < 0)
192: bufpos = 0;
193: else
194: ExpandBuff(false);
195: } else if (available > tokenBegin)
196: available = bufsize;
197: else if ((tokenBegin - available) < 2048)
198: ExpandBuff(true);
199: else
200: available = tokenBegin;
201: }
202:
203: return (buffer[bufpos] = c);
204: }
205:
206: /**
207: * @deprecated
208: * @see #getEndColumn
209: */
210:
211: public final int getColumn() {
212: return bufcolumn[bufpos];
213: }
214:
215: /**
216: * @deprecated
217: * @see #getEndLine
218: */
219:
220: public final int getLine() {
221: return bufline[bufpos];
222: }
223:
224: public final int getEndColumn() {
225: return bufcolumn[bufpos];
226: }
227:
228: public final int getEndLine() {
229: return bufline[bufpos];
230: }
231:
232: public final int getBeginColumn() {
233: return bufcolumn[tokenBegin];
234: }
235:
236: public final int getBeginLine() {
237: return bufline[tokenBegin];
238: }
239:
240: public final void backup(int amount) {
241:
242: inBuf += amount;
243: if ((bufpos -= amount) < 0)
244: bufpos += bufsize;
245: }
246:
247: public UCode_CharStream(java.io.Reader dstream, int startline,
248: int startcolumn, int buffersize) {
249: inputStream = dstream;
250: line = startline;
251: column = startcolumn - 1;
252:
253: available = bufsize = buffersize;
254: buffer = new char[buffersize];
255: nextCharBuf = new char[buffersize];
256: bufline = new int[buffersize];
257: bufcolumn = new int[buffersize];
258: }
259:
260: public UCode_CharStream(java.io.Reader dstream, int startline,
261: int startcolumn) {
262: this (dstream, startline, startcolumn, 4096);
263: }
264:
265: public void ReInit(java.io.Reader dstream, int startline,
266: int startcolumn, int buffersize) {
267: inputStream = dstream;
268: line = startline;
269: column = startcolumn - 1;
270:
271: if (buffer == null || buffersize != buffer.length) {
272: available = bufsize = buffersize;
273: buffer = new char[buffersize];
274: nextCharBuf = new char[buffersize];
275: bufline = new int[buffersize];
276: bufcolumn = new int[buffersize];
277: }
278: tokenBegin = inBuf = maxNextCharInd = 0;
279: nextCharInd = bufpos = -1;
280: }
281:
282: public void ReInit(java.io.Reader dstream, int startline,
283: int startcolumn) {
284: ReInit(dstream, startline, startcolumn, 4096);
285: }
286:
287: public UCode_CharStream(java.io.InputStream dstream, int startline,
288: int startcolumn, int buffersize) {
289: this (new java.io.InputStreamReader(dstream), startline,
290: startcolumn, 4096);
291: }
292:
293: public UCode_CharStream(java.io.InputStream dstream, int startline,
294: int startcolumn) {
295: this (dstream, startline, startcolumn, 4096);
296: }
297:
298: public void ReInit(java.io.InputStream dstream, int startline,
299: int startcolumn, int buffersize) {
300: ReInit(new java.io.InputStreamReader(dstream), startline,
301: startcolumn, 4096);
302: }
303:
304: public void ReInit(java.io.InputStream dstream, int startline,
305: int startcolumn) {
306: ReInit(dstream, startline, startcolumn, 4096);
307: }
308:
309: public final String GetImage() {
310: if (bufpos >= tokenBegin)
311: return new String(buffer, tokenBegin, bufpos - tokenBegin
312: + 1);
313: else
314: return new String(buffer, tokenBegin, bufsize - tokenBegin)
315: + new String(buffer, 0, bufpos + 1);
316: }
317:
318: public final char[] GetSuffix(int len) {
319: char[] ret = new char[len];
320:
321: if ((bufpos + 1) >= len)
322: System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
323: else {
324: System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret,
325: 0, len - bufpos - 1);
326: System.arraycopy(buffer, 0, ret, len - bufpos - 1,
327: bufpos + 1);
328: }
329:
330: return ret;
331: }
332:
333: public void Done() {
334: nextCharBuf = null;
335: buffer = null;
336: bufline = null;
337: bufcolumn = null;
338: }
339:
340: /**
341: * Method to adjust line and column numbers for the start of a token.<BR>
342: */
343: public void adjustBeginLineColumn(int newLine, int newCol) {
344: int start = tokenBegin;
345: int len;
346:
347: if (bufpos >= tokenBegin) {
348: len = bufpos - tokenBegin + inBuf + 1;
349: } else {
350: len = bufsize - tokenBegin + bufpos + 1 + inBuf;
351: }
352:
353: int i = 0, j = 0, k = 0;
354: int nextColDiff = 0, columnDiff = 0;
355:
356: while (i < len
357: && bufline[j = start % bufsize] == bufline[k = ++start
358: % bufsize]) {
359: bufline[j] = newLine;
360: nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
361: bufcolumn[j] = newCol + columnDiff;
362: columnDiff = nextColDiff;
363: i++;
364: }
365:
366: if (i < len) {
367: bufline[j] = newLine++;
368: bufcolumn[j] = newCol + columnDiff;
369:
370: while (i++ < len) {
371: if (bufline[j = start % bufsize] != bufline[++start
372: % bufsize])
373: bufline[j] = newLine++;
374: else
375: bufline[j] = newLine;
376: }
377: }
378:
379: line = bufline[j];
380: column = bufcolumn[j];
381: }
382:
383: }
|