blob: 59bde074e0459d5768584db8a1d129c8f81256e2 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1999-2004 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26/* Generated By:JavaCC: Do not edit this line. ASCII_UCodeESC_CharStream.java Version 0.7pre6 */
27
28package com.sun.tools.example.debug.expr;
29
30/**
31 * An implementation of interface CharStream, where the stream is assumed to
32 * contain only ASCII characters (with java-like unicode escape processing).
33 */
34
35public final class ASCII_UCodeESC_CharStream
36{
37 public static final boolean staticFlag = false;
38 static final int hexval(char c) throws java.io.IOException {
39 switch(c)
40 {
41 case '0' :
42 return 0;
43 case '1' :
44 return 1;
45 case '2' :
46 return 2;
47 case '3' :
48 return 3;
49 case '4' :
50 return 4;
51 case '5' :
52 return 5;
53 case '6' :
54 return 6;
55 case '7' :
56 return 7;
57 case '8' :
58 return 8;
59 case '9' :
60 return 9;
61
62 case 'a' :
63 case 'A' :
64 return 10;
65 case 'b' :
66 case 'B' :
67 return 11;
68 case 'c' :
69 case 'C' :
70 return 12;
71 case 'd' :
72 case 'D' :
73 return 13;
74 case 'e' :
75 case 'E' :
76 return 14;
77 case 'f' :
78 case 'F' :
79 return 15;
80 }
81
82 throw new java.io.IOException(); // Should never come here
83 }
84
85 public int bufpos = -1;
86 int bufsize;
87 int available;
88 int tokenBegin;
89 private int bufline[];
90 private int bufcolumn[];
91
92 private int column = 0;
93 private int line = 1;
94
95 private java.io.InputStream inputStream;
96
97 private boolean prevCharIsCR = false;
98 private boolean prevCharIsLF = false;
99
100 private byte[] nextCharBuf;
101 private char[] buffer;
102 private int maxNextCharInd = 0;
103 private int nextCharInd = -1;
104 private int inBuf = 0;
105
106 private final void ExpandBuff(boolean wrapAround)
107 {
108 char[] newbuffer = new char[bufsize + 2048];
109 int newbufline[] = new int[bufsize + 2048];
110 int newbufcolumn[] = new int[bufsize + 2048];
111
112 try
113 {
114 if (wrapAround)
115 {
116 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
117 System.arraycopy(buffer, 0, newbuffer,
118 bufsize - tokenBegin, bufpos);
119 buffer = newbuffer;
120
121 System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
122 System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);
123 bufline = newbufline;
124
125 System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
126 System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);
127 bufcolumn = newbufcolumn;
128
129 bufpos += (bufsize - tokenBegin);
130 }
131 else
132 {
133 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
134 buffer = newbuffer;
135
136 System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
137 bufline = newbufline;
138
139 System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
140 bufcolumn = newbufcolumn;
141
142 bufpos -= tokenBegin;
143 }
144 }
145 catch (Throwable t)
146 {
147 throw new Error(t.getMessage());
148 }
149
150 available = (bufsize += 2048);
151 tokenBegin = 0;
152 }
153
154 private final void FillBuff() throws java.io.IOException
155 {
156 int i;
157 if (maxNextCharInd == 4096)
158 maxNextCharInd = nextCharInd = 0;
159
160 try {
161 if ((i = inputStream.read(nextCharBuf, maxNextCharInd,
162 4096 - maxNextCharInd)) == -1)
163 {
164 inputStream.close();
165 throw new java.io.IOException();
166 }
167 else
168 maxNextCharInd += i;
169 return;
170 }
171 catch(java.io.IOException e) {
172 if (bufpos != 0)
173 {
174 --bufpos;
175 backup(0);
176 }
177 else
178 {
179 bufline[bufpos] = line;
180 bufcolumn[bufpos] = column;
181 }
182 throw e;
183 }
184 }
185
186 private final byte ReadByte() throws java.io.IOException
187 {
188 if (++nextCharInd >= maxNextCharInd)
189 FillBuff();
190
191 return nextCharBuf[nextCharInd];
192 }
193
194 public final char BeginToken() throws java.io.IOException
195 {
196 if (inBuf > 0)
197 {
198 --inBuf;
199 return buffer[tokenBegin = (bufpos == bufsize - 1) ? (bufpos = 0)
200 : ++bufpos];
201 }
202
203 tokenBegin = 0;
204 bufpos = -1;
205
206 return readChar();
207 }
208
209 private final void AdjustBuffSize()
210 {
211 if (available == bufsize)
212 {
213 if (tokenBegin > 2048)
214 {
215 bufpos = 0;
216 available = tokenBegin;
217 }
218 else
219 ExpandBuff(false);
220 }
221 else if (available > tokenBegin)
222 available = bufsize;
223 else if ((tokenBegin - available) < 2048)
224 ExpandBuff(true);
225 else
226 available = tokenBegin;
227 }
228
229 private final void UpdateLineColumn(char c)
230 {
231 column++;
232
233 if (prevCharIsLF)
234 {
235 prevCharIsLF = false;
236 line += (column = 1);
237 }
238 else if (prevCharIsCR)
239 {
240 prevCharIsCR = false;
241 if (c == '\n')
242 {
243 prevCharIsLF = true;
244 }
245 else
246 line += (column = 1);
247 }
248
249 switch (c)
250 {
251 case '\r' :
252 prevCharIsCR = true;
253 break;
254 case '\n' :
255 prevCharIsLF = true;
256 break;
257 case '\t' :
258 column--;
259 column += (8 - (column & 07));
260 break;
261 default :
262 break;
263 }
264
265 bufline[bufpos] = line;
266 bufcolumn[bufpos] = column;
267 }
268
269 public final char readChar() throws java.io.IOException
270 {
271 if (inBuf > 0)
272 {
273 --inBuf;
274 return buffer[(bufpos == bufsize - 1) ? (bufpos = 0) : ++bufpos];
275 }
276
277 char c;
278
279 if (++bufpos == available)
280 AdjustBuffSize();
281
282 if (((buffer[bufpos] = c = (char)((char)0xff & ReadByte())) == '\\'))
283 {
284 UpdateLineColumn(c);
285
286 int backSlashCnt = 1;
287
288 for (;;) // Read all the backslashes
289 {
290 if (++bufpos == available)
291 AdjustBuffSize();
292
293 try
294 {
295 if ((buffer[bufpos] = c = (char)((char)0xff & ReadByte())) != '\\')
296 {
297 UpdateLineColumn(c);
298 // found a non-backslash char.
299 if ((c == 'u') && ((backSlashCnt & 1) == 1))
300 {
301 if (--bufpos < 0)
302 bufpos = bufsize - 1;
303
304 break;
305 }
306
307 backup(backSlashCnt);
308 return '\\';
309 }
310 }
311 catch(java.io.IOException e)
312 {
313 if (backSlashCnt > 1)
314 backup(backSlashCnt);
315
316 return '\\';
317 }
318
319 UpdateLineColumn(c);
320 backSlashCnt++;
321 }
322
323 // Here, we have seen an odd number of backslash's followed by a 'u'
324 try
325 {
326 while ((c = (char)((char)0xff & ReadByte())) == 'u')
327 ++column;
328
329 buffer[bufpos] = c = (char)(hexval(c) << 12 |
330 hexval((char)((char)0xff & ReadByte())) << 8 |
331 hexval((char)((char)0xff & ReadByte())) << 4 |
332 hexval((char)((char)0xff & ReadByte())));
333
334 column += 4;
335 }
336 catch(java.io.IOException e)
337 {
338 throw new Error("Invalid escape character at line " + line +
339 " column " + column + ".");
340 }
341
342 if (backSlashCnt == 1)
343 return c;
344 else
345 {
346 backup(backSlashCnt - 1);
347 return '\\';
348 }
349 }
350 else
351 {
352 UpdateLineColumn(c);
353 return (c);
354 }
355 }
356
357 /**
358 * @deprecated
359 * @see #getEndColumn
360 */
361 @Deprecated
362 public final int getColumn() {
363 return bufcolumn[bufpos];
364 }
365
366 /**
367 * @deprecated
368 * @see #getEndLine
369 */
370 @Deprecated
371 public final int getLine() {
372 return bufline[bufpos];
373 }
374
375 public final int getEndColumn() {
376 return bufcolumn[bufpos];
377 }
378
379 public final int getEndLine() {
380 return bufline[bufpos];
381 }
382
383 public final int getBeginColumn() {
384 return bufcolumn[tokenBegin];
385 }
386
387 public final int getBeginLine() {
388 return bufline[tokenBegin];
389 }
390
391 public final void backup(int amount) {
392
393 inBuf += amount;
394 if ((bufpos -= amount) < 0)
395 bufpos += bufsize;
396 }
397
398 public ASCII_UCodeESC_CharStream(java.io.InputStream dstream,
399 int startline, int startcolumn, int buffersize)
400 {
401 inputStream = dstream;
402 line = startline;
403 column = startcolumn - 1;
404
405 available = bufsize = buffersize;
406 buffer = new char[buffersize];
407 bufline = new int[buffersize];
408 bufcolumn = new int[buffersize];
409 nextCharBuf = new byte[4096];
410 }
411
412 public ASCII_UCodeESC_CharStream(java.io.InputStream dstream,
413 int startline, int startcolumn)
414 {
415 this(dstream, startline, startcolumn, 4096);
416 }
417 public void ReInit(java.io.InputStream dstream,
418 int startline, int startcolumn, int buffersize)
419 {
420 inputStream = dstream;
421 line = startline;
422 column = startcolumn - 1;
423
424 if (buffer == null || buffersize != buffer.length)
425 {
426 available = bufsize = buffersize;
427 buffer = new char[buffersize];
428 bufline = new int[buffersize];
429 bufcolumn = new int[buffersize];
430 nextCharBuf = new byte[4096];
431 }
432 prevCharIsLF = prevCharIsCR = false;
433 tokenBegin = inBuf = maxNextCharInd = 0;
434 nextCharInd = bufpos = -1;
435 }
436
437 public void ReInit(java.io.InputStream dstream,
438 int startline, int startcolumn)
439 {
440 ReInit(dstream, startline, startcolumn, 4096);
441 }
442
443 public final String GetImage()
444 {
445 if (bufpos >= tokenBegin)
446 return new String(buffer, tokenBegin, bufpos - tokenBegin + 1);
447 else
448 return new String(buffer, tokenBegin, bufsize - tokenBegin) +
449 new String(buffer, 0, bufpos + 1);
450 }
451
452 public final char[] GetSuffix(int len)
453 {
454 char[] ret = new char[len];
455
456 if ((bufpos + 1) >= len)
457 System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
458 else
459 {
460 System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0,
461 len - bufpos - 1);
462 System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
463 }
464
465 return ret;
466 }
467
468 public void Done()
469 {
470 nextCharBuf = null;
471 buffer = null;
472 bufline = null;
473 bufcolumn = null;
474 }
475
476 /**
477 * Method to adjust line and column numbers for the start of a token.<BR>
478 */
479 public void adjustBeginLineColumn(int newLine, int newCol)
480 {
481 int start = tokenBegin;
482 int len;
483
484 if (bufpos >= tokenBegin)
485 {
486 len = bufpos - tokenBegin + inBuf + 1;
487 }
488 else
489 {
490 len = bufsize - tokenBegin + bufpos + 1 + inBuf;
491 }
492
493 int i = 0, j = 0, k = 0;
494 int nextColDiff = 0, columnDiff = 0;
495
496 while (i < len &&
497 bufline[j = start % bufsize] == bufline[k = ++start % bufsize])
498 {
499 bufline[j] = newLine;
500 nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
501 bufcolumn[j] = newCol + columnDiff;
502 columnDiff = nextColDiff;
503 i++;
504 }
505
506 if (i < len)
507 {
508 bufline[j] = newLine++;
509 bufcolumn[j] = newCol + columnDiff;
510
511 while (i++ < len)
512 {
513 if (bufline[j = start % bufsize] != bufline[++start % bufsize])
514 bufline[j] = newLine++;
515 else
516 bufline[j] = newLine;
517 }
518 }
519
520 line = bufline[j];
521 column = bufcolumn[j];
522 }
523
524}