1 | /* |
2 | |
3 | Derby - Class org.apache.derby.impl.tools.ij.UCode_CharStream |
4 | |
5 | Copyright 2000, 2004 The Apache Software Foundation or its licensors, as applicable. |
6 | |
7 | Licensed under the Apache License, Version 2.0 (the "License"); |
8 | you may not use this file except in compliance with the License. |
9 | You may obtain a copy of the License at |
10 | |
11 | http://www.apache.org/licenses/LICENSE-2.0 |
12 | |
13 | Unless required by applicable law or agreed to in writing, software |
14 | distributed under the License is distributed on an "AS IS" BASIS, |
15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | See the License for the specific language governing permissions and |
17 | limitations under the License. |
18 | |
19 | */ |
20 | |
21 | /* Generated By:JavaCC: Do not edit this line. UCode_CharStream.java Version 0.7pre6 */ |
22 | package org.apache.derby.impl.tools.ij; |
23 | |
24 | /** |
25 | * An implementation of interface CharStream, where the stream is assumed to |
26 | * contain only Unicode characters. |
27 | */ |
28 | |
29 | public final class UCode_CharStream implements CharStream |
30 | { |
31 | public static final boolean staticFlag = false; |
32 | public int bufpos = -1; |
33 | int bufsize; |
34 | int available; |
35 | int tokenBegin; |
36 | private int bufline[]; |
37 | private int bufcolumn[]; |
38 | |
39 | private int column = 0; |
40 | private int line = 1; |
41 | |
42 | private boolean prevCharIsCR = false; |
43 | private boolean prevCharIsLF = false; |
44 | |
45 | private java.io.Reader inputStream; |
46 | |
47 | private char[] nextCharBuf; |
48 | private char[] buffer; |
49 | private int maxNextCharInd = 0; |
50 | private int nextCharInd = -1; |
51 | |
52 | private final void ExpandBuff(boolean wrapAround) |
53 | { |
54 | char[] newbuffer = new char[bufsize + 2048]; |
55 | int newbufline[] = new int[bufsize + 2048]; |
56 | int newbufcolumn[] = new int[bufsize + 2048]; |
57 | |
58 | try |
59 | { |
60 | if (wrapAround) |
61 | { |
62 | System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin); |
63 | System.arraycopy(buffer, 0, newbuffer, |
64 | bufsize - tokenBegin, bufpos); |
65 | buffer = newbuffer; |
66 | |
67 | System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin); |
68 | System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos); |
69 | bufline = newbufline; |
70 | |
71 | System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin); |
72 | System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos); |
73 | bufcolumn = newbufcolumn; |
74 | |
75 | bufpos += (bufsize - tokenBegin); |
76 | } |
77 | else |
78 | { |
79 | System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin); |
80 | buffer = newbuffer; |
81 | |
82 | System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin); |
83 | bufline = newbufline; |
84 | |
85 | System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin); |
86 | bufcolumn = newbufcolumn; |
87 | |
88 | bufpos -= tokenBegin; |
89 | } |
90 | } |
91 | catch (Throwable t) |
92 | { |
93 | throw new Error(t.getMessage()); |
94 | } |
95 | |
96 | available = (bufsize += 2048); |
97 | tokenBegin = 0; |
98 | } |
99 | |
100 | private final void FillBuff() throws java.io.IOException |
101 | { |
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 | { |
110 | inputStream.close(); |
111 | throw new java.io.IOException(); |
112 | } |
113 | else |
114 | maxNextCharInd += i; |
115 | return; |
116 | } |
117 | catch(java.io.IOException e) { |
118 | if (bufpos != 0) |
119 | { |
120 | --bufpos; |
121 | backup(0); |
122 | } |
123 | else |
124 | { |
125 | bufline[bufpos] = line; |
126 | bufcolumn[bufpos] = column; |
127 | } |
128 | if (tokenBegin == -1) |
129 | tokenBegin = bufpos; |
130 | throw e; |
131 | } |
132 | } |
133 | |
134 | private final char ReadChar() throws java.io.IOException |
135 | { |
136 | if (++nextCharInd >= maxNextCharInd) |
137 | FillBuff(); |
138 | |
139 | return nextCharBuf[nextCharInd]; |
140 | } |
141 | |
142 | public char BeginToken() throws java.io.IOException |
143 | { |
144 | tokenBegin = -1; |
145 | char c = readChar(); |
146 | tokenBegin = bufpos; |
147 | |
148 | return c; |
149 | } |
150 | |
151 | private final void UpdateLineColumn(char c) |
152 | { |
153 | column++; |
154 | |
155 | if (prevCharIsLF) |
156 | { |
157 | prevCharIsLF = false; |
158 | line += (column = 1); |
159 | } |
160 | else if (prevCharIsCR) |
161 | { |
162 | prevCharIsCR = false; |
163 | if (c == '\n') |
164 | { |
165 | prevCharIsLF = true; |
166 | } |
167 | else |
168 | line += (column = 1); |
169 | } |
170 | |
171 | switch (c) |
172 | { |
173 | case '\r' : |
174 | prevCharIsCR = true; |
175 | break; |
176 | case '\n' : |
177 | prevCharIsLF = true; |
178 | break; |
179 | case '\t' : |
180 | column--; |
181 | column += (8 - (column & 07)); |
182 | break; |
183 | default : |
184 | break; |
185 | } |
186 | } |
187 | |
188 | private int inBuf = 0; |
189 | public final char readChar() throws java.io.IOException |
190 | { |
191 | if (inBuf > 0) |
192 | { |
193 | --inBuf; |
194 | return (char)buffer[(bufpos == bufsize - 1) ? (bufpos = 0) : ++bufpos]; |
195 | } |
196 | |
197 | bufpos++; |
198 | char c = ReadChar(); |
199 | UpdateLineColumn(c); |
200 | |
201 | if (bufpos == available) |
202 | { |
203 | if (available == bufsize) |
204 | { |
205 | if (tokenBegin > 2048) |
206 | { |
207 | bufpos = 0; |
208 | available = tokenBegin; |
209 | } |
210 | else if (tokenBegin < 0) |
211 | bufpos = 0; |
212 | else |
213 | ExpandBuff(false); |
214 | } |
215 | else if (available > tokenBegin) |
216 | available = bufsize; |
217 | else if ((tokenBegin - available) < 2048) |
218 | ExpandBuff(true); |
219 | else |
220 | available = tokenBegin; |
221 | } |
222 | |
223 | return (buffer[bufpos] = c); |
224 | } |
225 | |
226 | /** |
227 | * @deprecated |
228 | * @see #getEndColumn |
229 | */ |
230 | |
231 | public final int getColumn() { |
232 | return bufcolumn[bufpos]; |
233 | } |
234 | |
235 | /** |
236 | * @deprecated |
237 | * @see #getEndLine |
238 | */ |
239 | |
240 | public final int getLine() { |
241 | return bufline[bufpos]; |
242 | } |
243 | |
244 | public final int getEndColumn() { |
245 | return bufcolumn[bufpos]; |
246 | } |
247 | |
248 | public final int getEndLine() { |
249 | return bufline[bufpos]; |
250 | } |
251 | |
252 | public final int getBeginColumn() { |
253 | return bufcolumn[tokenBegin]; |
254 | } |
255 | |
256 | public final int getBeginLine() { |
257 | return bufline[tokenBegin]; |
258 | } |
259 | |
260 | public final void backup(int amount) { |
261 | |
262 | inBuf += amount; |
263 | if ((bufpos -= amount) < 0) |
264 | bufpos += bufsize; |
265 | } |
266 | |
267 | public UCode_CharStream(java.io.Reader dstream, |
268 | int startline, int startcolumn, int buffersize) |
269 | { |
270 | inputStream = dstream; |
271 | line = startline; |
272 | column = startcolumn - 1; |
273 | |
274 | available = bufsize = buffersize; |
275 | buffer = new char[buffersize]; |
276 | nextCharBuf = new char[buffersize]; |
277 | bufline = new int[buffersize]; |
278 | bufcolumn = new int[buffersize]; |
279 | } |
280 | |
281 | |
282 | public UCode_CharStream(java.io.Reader dstream, |
283 | int startline, int startcolumn) |
284 | { |
285 | this(dstream, startline, startcolumn, 4096); |
286 | } |
287 | |
288 | public void ReInit(java.io.Reader dstream, |
289 | int startline, int startcolumn, int buffersize) |
290 | { |
291 | inputStream = dstream; |
292 | line = startline; |
293 | column = startcolumn - 1; |
294 | |
295 | if (buffer == null || buffersize != buffer.length) |
296 | { |
297 | available = bufsize = buffersize; |
298 | buffer = new char[buffersize]; |
299 | nextCharBuf = new char[buffersize]; |
300 | bufline = new int[buffersize]; |
301 | bufcolumn = new int[buffersize]; |
302 | } |
303 | tokenBegin = inBuf = maxNextCharInd = 0; |
304 | nextCharInd = bufpos = -1; |
305 | } |
306 | |
307 | public void ReInit(java.io.Reader dstream, |
308 | int startline, int startcolumn) |
309 | { |
310 | ReInit(dstream, startline, startcolumn, 4096); |
311 | } |
312 | public UCode_CharStream(java.io.InputStream dstream, int startline, |
313 | int startcolumn, int buffersize) |
314 | { |
315 | this(new java.io.InputStreamReader(dstream), startline, startcolumn, 4096); |
316 | } |
317 | |
318 | public UCode_CharStream(java.io.InputStream dstream, int startline, |
319 | int startcolumn) |
320 | { |
321 | this(dstream, startline, startcolumn, 4096); |
322 | } |
323 | |
324 | public void ReInit(java.io.InputStream dstream, int startline, |
325 | int startcolumn, int buffersize) |
326 | { |
327 | ReInit(new java.io.InputStreamReader(dstream), startline, startcolumn, 4096); |
328 | } |
329 | public void ReInit(java.io.InputStream dstream, int startline, |
330 | int startcolumn) |
331 | { |
332 | ReInit(dstream, startline, startcolumn, 4096); |
333 | } |
334 | |
335 | public final String GetImage() |
336 | { |
337 | if (bufpos >= tokenBegin) |
338 | return new String(buffer, tokenBegin, bufpos - tokenBegin + 1); |
339 | else |
340 | return new String(buffer, tokenBegin, bufsize - tokenBegin) + |
341 | new String(buffer, 0, bufpos + 1); |
342 | } |
343 | |
344 | public final char[] GetSuffix(int len) |
345 | { |
346 | char[] ret = new char[len]; |
347 | |
348 | if ((bufpos + 1) >= len) |
349 | System.arraycopy(buffer, bufpos - len + 1, ret, 0, len); |
350 | else |
351 | { |
352 | System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0, |
353 | len - bufpos - 1); |
354 | System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1); |
355 | } |
356 | |
357 | return ret; |
358 | } |
359 | |
360 | public void Done() |
361 | { |
362 | nextCharBuf = null; |
363 | buffer = null; |
364 | bufline = null; |
365 | bufcolumn = null; |
366 | } |
367 | |
368 | /** |
369 | * Method to adjust line and column numbers for the start of a token.<BR> |
370 | */ |
371 | public void adjustBeginLineColumn(int newLine, int newCol) |
372 | { |
373 | int start = tokenBegin; |
374 | int len; |
375 | |
376 | if (bufpos >= tokenBegin) |
377 | { |
378 | len = bufpos - tokenBegin + inBuf + 1; |
379 | } |
380 | else |
381 | { |
382 | len = bufsize - tokenBegin + bufpos + 1 + inBuf; |
383 | } |
384 | |
385 | int i = 0, j = 0, k = 0; |
386 | int nextColDiff = 0, columnDiff = 0; |
387 | |
388 | while (i < len && |
389 | bufline[j = start % bufsize] == bufline[k = ++start % bufsize]) |
390 | { |
391 | bufline[j] = newLine; |
392 | nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j]; |
393 | bufcolumn[j] = newCol + columnDiff; |
394 | columnDiff = nextColDiff; |
395 | i++; |
396 | } |
397 | |
398 | if (i < len) |
399 | { |
400 | bufline[j] = newLine++; |
401 | bufcolumn[j] = newCol + columnDiff; |
402 | |
403 | while (i++ < len) |
404 | { |
405 | if (bufline[j = start % bufsize] != bufline[++start % bufsize]) |
406 | bufline[j] = newLine++; |
407 | else |
408 | bufline[j] = newLine; |
409 | } |
410 | } |
411 | |
412 | line = bufline[j]; |
413 | column = bufcolumn[j]; |
414 | } |
415 | |
416 | } |