1
2
3 package net.sf.zel.vm;
4
5
6
7
8
9
10 public class JavaCharStream
11 {
12
13 public static final boolean staticFlag = false;
14 static final int hexval(char c) throws java.io.IOException {
15 switch(c)
16 {
17 case '0' :
18 return 0;
19 case '1' :
20 return 1;
21 case '2' :
22 return 2;
23 case '3' :
24 return 3;
25 case '4' :
26 return 4;
27 case '5' :
28 return 5;
29 case '6' :
30 return 6;
31 case '7' :
32 return 7;
33 case '8' :
34 return 8;
35 case '9' :
36 return 9;
37
38 case 'a' :
39 case 'A' :
40 return 10;
41 case 'b' :
42 case 'B' :
43 return 11;
44 case 'c' :
45 case 'C' :
46 return 12;
47 case 'd' :
48 case 'D' :
49 return 13;
50 case 'e' :
51 case 'E' :
52 return 14;
53 case 'f' :
54 case 'F' :
55 return 15;
56 }
57
58 throw new java.io.IOException();
59 }
60
61
62 public int bufpos = -1;
63 int bufsize;
64 int available;
65 int tokenBegin;
66 protected int bufline[];
67 protected int bufcolumn[];
68
69 protected int column = 0;
70 protected int line = 1;
71
72 protected boolean prevCharIsCR = false;
73 protected boolean prevCharIsLF = false;
74
75 protected java.io.Reader inputStream;
76
77 protected char[] nextCharBuf;
78 protected char[] buffer;
79 protected int maxNextCharInd = 0;
80 protected int nextCharInd = -1;
81 protected int inBuf = 0;
82 protected int tabSize = 8;
83
84 protected void setTabSize(int i) { tabSize = i; }
85 protected int getTabSize(int i) { return tabSize; }
86
87 protected void ExpandBuff(boolean wrapAround)
88 {
89 char[] newbuffer = new char[bufsize + 2048];
90 int newbufline[] = new int[bufsize + 2048];
91 int newbufcolumn[] = new int[bufsize + 2048];
92
93 try
94 {
95 if (wrapAround)
96 {
97 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
98 System.arraycopy(buffer, 0, newbuffer,
99 bufsize - tokenBegin, bufpos);
100 buffer = newbuffer;
101
102 System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
103 System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);
104 bufline = newbufline;
105
106 System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
107 System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);
108 bufcolumn = newbufcolumn;
109
110 bufpos += (bufsize - tokenBegin);
111 }
112 else
113 {
114 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
115 buffer = newbuffer;
116
117 System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
118 bufline = newbufline;
119
120 System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
121 bufcolumn = newbufcolumn;
122
123 bufpos -= tokenBegin;
124 }
125 }
126 catch (Throwable t)
127 {
128 throw new Error(t.getMessage());
129 }
130
131 available = (bufsize += 2048);
132 tokenBegin = 0;
133 }
134
135 protected void FillBuff() throws java.io.IOException
136 {
137 int i;
138 if (maxNextCharInd == 4096)
139 maxNextCharInd = nextCharInd = 0;
140
141 try {
142 if ((i = inputStream.read(nextCharBuf, maxNextCharInd,
143 4096 - maxNextCharInd)) == -1)
144 {
145 inputStream.close();
146 throw new java.io.IOException();
147 }
148 else
149 maxNextCharInd += i;
150 return;
151 }
152 catch(java.io.IOException e) {
153 if (bufpos != 0)
154 {
155 --bufpos;
156 backup(0);
157 }
158 else
159 {
160 bufline[bufpos] = line;
161 bufcolumn[bufpos] = column;
162 }
163 throw e;
164 }
165 }
166
167 protected char ReadByte() throws java.io.IOException
168 {
169 if (++nextCharInd >= maxNextCharInd)
170 FillBuff();
171
172 return nextCharBuf[nextCharInd];
173 }
174
175
176 public char BeginToken() throws java.io.IOException
177 {
178 if (inBuf > 0)
179 {
180 --inBuf;
181
182 if (++bufpos == bufsize)
183 bufpos = 0;
184
185 tokenBegin = bufpos;
186 return buffer[bufpos];
187 }
188
189 tokenBegin = 0;
190 bufpos = -1;
191
192 return readChar();
193 }
194
195 protected void AdjustBuffSize()
196 {
197 if (available == bufsize)
198 {
199 if (tokenBegin > 2048)
200 {
201 bufpos = 0;
202 available = tokenBegin;
203 }
204 else
205 ExpandBuff(false);
206 }
207 else if (available > tokenBegin)
208 available = bufsize;
209 else if ((tokenBegin - available) < 2048)
210 ExpandBuff(true);
211 else
212 available = tokenBegin;
213 }
214
215 protected void UpdateLineColumn(char c)
216 {
217 column++;
218
219 if (prevCharIsLF)
220 {
221 prevCharIsLF = false;
222 line += (column = 1);
223 }
224 else if (prevCharIsCR)
225 {
226 prevCharIsCR = false;
227 if (c == '\n')
228 {
229 prevCharIsLF = true;
230 }
231 else
232 line += (column = 1);
233 }
234
235 switch (c)
236 {
237 case '\r' :
238 prevCharIsCR = true;
239 break;
240 case '\n' :
241 prevCharIsLF = true;
242 break;
243 case '\t' :
244 column--;
245 column += (tabSize - (column % tabSize));
246 break;
247 default :
248 break;
249 }
250
251 bufline[bufpos] = line;
252 bufcolumn[bufpos] = column;
253 }
254
255
256 public char readChar() throws java.io.IOException
257 {
258 if (inBuf > 0)
259 {
260 --inBuf;
261
262 if (++bufpos == bufsize)
263 bufpos = 0;
264
265 return buffer[bufpos];
266 }
267
268 char c;
269
270 if (++bufpos == available)
271 AdjustBuffSize();
272
273 if ((buffer[bufpos] = c = ReadByte()) == '\\')
274 {
275 UpdateLineColumn(c);
276
277 int backSlashCnt = 1;
278
279 for (;;)
280 {
281 if (++bufpos == available)
282 AdjustBuffSize();
283
284 try
285 {
286 if ((buffer[bufpos] = c = ReadByte()) != '\\')
287 {
288 UpdateLineColumn(c);
289
290 if ((c == 'u') && ((backSlashCnt & 1) == 1))
291 {
292 if (--bufpos < 0)
293 bufpos = bufsize - 1;
294
295 break;
296 }
297
298 backup(backSlashCnt);
299 return '\\';
300 }
301 }
302 catch(java.io.IOException e)
303 {
304 if (backSlashCnt > 1)
305 backup(backSlashCnt-1);
306
307 return '\\';
308 }
309
310 UpdateLineColumn(c);
311 backSlashCnt++;
312 }
313
314
315 try
316 {
317 while ((c = ReadByte()) == 'u')
318 ++column;
319
320 buffer[bufpos] = c = (char)(hexval(c) << 12 |
321 hexval(ReadByte()) << 8 |
322 hexval(ReadByte()) << 4 |
323 hexval(ReadByte()));
324
325 column += 4;
326 }
327 catch(java.io.IOException e)
328 {
329 throw new Error("Invalid escape character at line " + line +
330 " column " + column + ".");
331 }
332
333 if (backSlashCnt == 1)
334 return c;
335 else
336 {
337 backup(backSlashCnt - 1);
338 return '\\';
339 }
340 }
341 else
342 {
343 UpdateLineColumn(c);
344 return c;
345 }
346 }
347
348
349
350
351
352 public int getColumn() {
353 return bufcolumn[bufpos];
354 }
355
356
357
358
359
360 public int getLine() {
361 return bufline[bufpos];
362 }
363
364
365 public int getEndColumn() {
366 return bufcolumn[bufpos];
367 }
368
369
370 public int getEndLine() {
371 return bufline[bufpos];
372 }
373
374
375 public int getBeginColumn() {
376 return bufcolumn[tokenBegin];
377 }
378
379
380 public int getBeginLine() {
381 return bufline[tokenBegin];
382 }
383
384
385 public void backup(int amount) {
386
387 inBuf += amount;
388 if ((bufpos -= amount) < 0)
389 bufpos += bufsize;
390 }
391
392
393 public JavaCharStream(java.io.Reader dstream,
394 int startline, int startcolumn, int buffersize)
395 {
396 inputStream = dstream;
397 line = startline;
398 column = startcolumn - 1;
399
400 available = bufsize = buffersize;
401 buffer = new char[buffersize];
402 bufline = new int[buffersize];
403 bufcolumn = new int[buffersize];
404 nextCharBuf = new char[4096];
405 }
406
407
408 public JavaCharStream(java.io.Reader dstream,
409 int startline, int startcolumn)
410 {
411 this(dstream, startline, startcolumn, 4096);
412 }
413
414
415 public JavaCharStream(java.io.Reader dstream)
416 {
417 this(dstream, 1, 1, 4096);
418 }
419
420 public void ReInit(java.io.Reader dstream,
421 int startline, int startcolumn, int buffersize)
422 {
423 inputStream = dstream;
424 line = startline;
425 column = startcolumn - 1;
426
427 if (buffer == null || buffersize != buffer.length)
428 {
429 available = bufsize = buffersize;
430 buffer = new char[buffersize];
431 bufline = new int[buffersize];
432 bufcolumn = new int[buffersize];
433 nextCharBuf = new char[4096];
434 }
435 prevCharIsLF = prevCharIsCR = false;
436 tokenBegin = inBuf = maxNextCharInd = 0;
437 nextCharInd = bufpos = -1;
438 }
439
440
441 public void ReInit(java.io.Reader dstream,
442 int startline, int startcolumn)
443 {
444 ReInit(dstream, startline, startcolumn, 4096);
445 }
446
447
448 public void ReInit(java.io.Reader dstream)
449 {
450 ReInit(dstream, 1, 1, 4096);
451 }
452
453 public JavaCharStream(java.io.InputStream dstream, String encoding, int startline,
454 int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException
455 {
456 this(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);
457 }
458
459
460 public JavaCharStream(java.io.InputStream dstream, int startline,
461 int startcolumn, int buffersize)
462 {
463 this(new java.io.InputStreamReader(dstream), startline, startcolumn, 4096);
464 }
465
466
467 public JavaCharStream(java.io.InputStream dstream, String encoding, int startline,
468 int startcolumn) throws java.io.UnsupportedEncodingException
469 {
470 this(dstream, encoding, startline, startcolumn, 4096);
471 }
472
473
474 public JavaCharStream(java.io.InputStream dstream, int startline,
475 int startcolumn)
476 {
477 this(dstream, startline, startcolumn, 4096);
478 }
479
480
481 public JavaCharStream(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException
482 {
483 this(dstream, encoding, 1, 1, 4096);
484 }
485
486
487 public JavaCharStream(java.io.InputStream dstream)
488 {
489 this(dstream, 1, 1, 4096);
490 }
491
492
493 public void ReInit(java.io.InputStream dstream, String encoding, int startline,
494 int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException
495 {
496 ReInit(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);
497 }
498
499
500 public void ReInit(java.io.InputStream dstream, int startline,
501 int startcolumn, int buffersize)
502 {
503 ReInit(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize);
504 }
505
506 public void ReInit(java.io.InputStream dstream, String encoding, int startline,
507 int startcolumn) throws java.io.UnsupportedEncodingException
508 {
509 ReInit(dstream, encoding, startline, startcolumn, 4096);
510 }
511
512 public void ReInit(java.io.InputStream dstream, int startline,
513 int startcolumn)
514 {
515 ReInit(dstream, startline, startcolumn, 4096);
516 }
517
518 public void ReInit(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException
519 {
520 ReInit(dstream, encoding, 1, 1, 4096);
521 }
522
523
524 public void ReInit(java.io.InputStream dstream)
525 {
526 ReInit(dstream, 1, 1, 4096);
527 }
528
529
530 public String GetImage()
531 {
532 if (bufpos >= tokenBegin)
533 return new String(buffer, tokenBegin, bufpos - tokenBegin + 1);
534 else
535 return new String(buffer, tokenBegin, bufsize - tokenBegin) +
536 new String(buffer, 0, bufpos + 1);
537 }
538
539
540 public char[] GetSuffix(int len)
541 {
542 char[] ret = new char[len];
543
544 if ((bufpos + 1) >= len)
545 System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
546 else
547 {
548 System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0,
549 len - bufpos - 1);
550 System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
551 }
552
553 return ret;
554 }
555
556
557 public void Done()
558 {
559 nextCharBuf = null;
560 buffer = null;
561 bufline = null;
562 bufcolumn = null;
563 }
564
565
566
567
568 public void adjustBeginLineColumn(int newLine, int newCol)
569 {
570 int start = tokenBegin;
571 int len;
572
573 if (bufpos >= tokenBegin)
574 {
575 len = bufpos - tokenBegin + inBuf + 1;
576 }
577 else
578 {
579 len = bufsize - tokenBegin + bufpos + 1 + inBuf;
580 }
581
582 int i = 0, j = 0, k = 0;
583 int nextColDiff = 0, columnDiff = 0;
584
585 while (i < len &&
586 bufline[j = start % bufsize] == bufline[k = ++start % bufsize])
587 {
588 bufline[j] = newLine;
589 nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
590 bufcolumn[j] = newCol + columnDiff;
591 columnDiff = nextColDiff;
592 i++;
593 }
594
595 if (i < len)
596 {
597 bufline[j] = newLine++;
598 bufcolumn[j] = newCol + columnDiff;
599
600 while (i++ < len)
601 {
602 if (bufline[j = start % bufsize] != bufline[++start % bufsize])
603 bufline[j] = newLine++;
604 else
605 bufline[j] = newLine;
606 }
607 }
608
609 line = bufline[j];
610 column = bufcolumn[j];
611 }
612
613 }
614