Debian Codegraph @

templates/JavaCharStream.template

raw
1 /**
2 * An implementation of interface CharStream, where the stream is assumed to
3 * contain only ASCII characters (with java-like unicode escape processing).
4 */
5
6 #if SUPPORT_CLASS_VISIBILITY_PUBLIC
7 public
8 #fi
9 class JavaCharStream
10 {
11 /** Whether parser is static. */
12 public static final boolean staticFlag = ${STATIC};
13
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(); // Should never come here
59 }
60
61 /** Position in buffer. */
62 ${PREFIX}public int bufpos = -1;
63 ${PREFIX}int bufsize;
64 ${PREFIX}int available;
65 ${PREFIX}int tokenBegin;
66 #if KEEP_LINE_COLUMN
67 ${PREFIX}protected int bufline[];
68 ${PREFIX}protected int bufcolumn[];
69
70 ${PREFIX}protected int column = 0;
71 ${PREFIX}protected int line = 1;
72
73 ${PREFIX}protected boolean prevCharIsCR = false;
74 ${PREFIX}protected boolean prevCharIsLF = false;
75 #fi
76
77 ${PREFIX}protected java.io.Reader inputStream;
78
79 ${PREFIX}protected char[] nextCharBuf;
80 ${PREFIX}protected char[] buffer;
81 ${PREFIX}protected int maxNextCharInd = 0;
82 ${PREFIX}protected int nextCharInd = -1;
83 ${PREFIX}protected int inBuf = 0;
84 ${PREFIX}protected int tabSize = 8;
85
86 ${PREFIX}protected void setTabSize(int i) { tabSize = i; }
87 ${PREFIX}protected int getTabSize(int i) { return tabSize; }
88
89 ${PREFIX}protected void ExpandBuff(boolean wrapAround)
90 {
91 char[] newbuffer = new char[bufsize + 2048];
92 #if KEEP_LINE_COLUMN
93 int newbufline[] = new int[bufsize + 2048];
94 int newbufcolumn[] = new int[bufsize + 2048];
95 #fi
96
97 try
98 {
99 if (wrapAround)
100 {
101 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
102 System.arraycopy(buffer, 0, newbuffer, bufsize - tokenBegin, bufpos);
103 buffer = newbuffer;
104 #if KEEP_LINE_COLUMN
105
106 System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
107 System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);
108 bufline = newbufline;
109
110 System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
111 System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);
112 bufcolumn = newbufcolumn;
113 #fi
114
115 bufpos += (bufsize - tokenBegin);
116 }
117 else
118 {
119 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
120 buffer = newbuffer;
121 #if KEEP_LINE_COLUMN
122
123 System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
124 bufline = newbufline;
125
126 System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
127 bufcolumn = newbufcolumn;
128 #fi
129
130 bufpos -= tokenBegin;
131 }
132 }
133 catch (Throwable t)
134 {
135 throw new Error(t.getMessage());
136 }
137
138 available = (bufsize += 2048);
139 tokenBegin = 0;
140 }
141
142 ${PREFIX}protected void FillBuff() throws java.io.IOException
143 {
144 int i;
145 if (maxNextCharInd == 4096)
146 maxNextCharInd = nextCharInd = 0;
147
148 try {
149 if ((i = inputStream.read(nextCharBuf, maxNextCharInd,
150 4096 - maxNextCharInd)) == -1)
151 {
152 inputStream.close();
153 throw new java.io.IOException();
154 }
155 else
156 maxNextCharInd += i;
157 return;
158 }
159 catch(java.io.IOException e) {
160 if (bufpos != 0)
161 {
162 --bufpos;
163 backup(0);
164 }
165 #if KEEP_LINE_COLUMN
166 else
167 {
168 bufline[bufpos] = line;
169 bufcolumn[bufpos] = column;
170 }
171 #fi
172 throw e;
173 }
174 }
175
176 ${PREFIX}protected char ReadByte() throws java.io.IOException
177 {
178 if (++nextCharInd >= maxNextCharInd)
179 FillBuff();
180
181 return nextCharBuf[nextCharInd];
182 }
183
184 /** @return starting character for token. */
185 ${PREFIX}public char BeginToken() throws java.io.IOException
186 {
187 if (inBuf > 0)
188 {
189 --inBuf;
190
191 if (++bufpos == bufsize)
192 bufpos = 0;
193
194 tokenBegin = bufpos;
195 return buffer[bufpos];
196 }
197
198 tokenBegin = 0;
199 bufpos = -1;
200
201 return readChar();
202 }
203
204 ${PREFIX}protected void AdjustBuffSize()
205 {
206 if (available == bufsize)
207 {
208 if (tokenBegin > 2048)
209 {
210 bufpos = 0;
211 available = tokenBegin;
212 }
213 else
214 ExpandBuff(false);
215 }
216 else if (available > tokenBegin)
217 available = bufsize;
218 else if ((tokenBegin - available) < 2048)
219 ExpandBuff(true);
220 else
221 available = tokenBegin;
222 }
223 #if KEEP_LINE_COLUMN
224
225 ${PREFIX}protected void UpdateLineColumn(char c)
226 {
227 column++;
228
229 if (prevCharIsLF)
230 {
231 prevCharIsLF = false;
232 line += (column = 1);
233 }
234 else if (prevCharIsCR)
235 {
236 prevCharIsCR = false;
237 if (c == '\n')
238 {
239 prevCharIsLF = true;
240 }
241 else
242 line += (column = 1);
243 }
244
245 switch (c)
246 {
247 case '\r' :
248 prevCharIsCR = true;
249 break;
250 case '\n' :
251 prevCharIsLF = true;
252 break;
253 case '\t' :
254 column--;
255 column += (tabSize - (column % tabSize));
256 break;
257 default :
258 break;
259 }
260
261 bufline[bufpos] = line;
262 bufcolumn[bufpos] = column;
263 }
264 #fi
265
266 /** Read a character. */
267 ${PREFIX}public char readChar() throws java.io.IOException
268 {
269 if (inBuf > 0)
270 {
271 --inBuf;
272
273 if (++bufpos == bufsize)
274 bufpos = 0;
275
276 return buffer[bufpos];
277 }
278
279 char c;
280
281 if (++bufpos == available)
282 AdjustBuffSize();
283
284 if ((buffer[bufpos] = c = ReadByte()) == '\\')
285 {
286 #if KEEP_LINE_COLUMN
287 UpdateLineColumn(c);
288 #fi
289
290 int backSlashCnt = 1;
291
292 for (;;) // Read all the backslashes
293 {
294 if (++bufpos == available)
295 AdjustBuffSize();
296
297 try
298 {
299 if ((buffer[bufpos] = c = ReadByte()) != '\\')
300 {
301 UpdateLineColumn(c);
302 // found a non-backslash char.
303 if ((c == 'u') && ((backSlashCnt & 1) == 1))
304 {
305 if (--bufpos < 0)
306 bufpos = bufsize - 1;
307
308 break;
309 }
310
311 backup(backSlashCnt);
312 return '\\';
313 }
314 }
315 catch(java.io.IOException e)
316 {
317 // We are returning one backslash so we should only backup (count-1)
318 if (backSlashCnt > 1)
319 backup(backSlashCnt-1);
320
321 return '\\';
322 }
323
324 #if KEEP_LINE_COLUMN
325 UpdateLineColumn(c);
326 #fi
327 backSlashCnt++;
328 }
329
330 // Here, we have seen an odd number of backslash's followed by a 'u'
331 try
332 {
333 while ((c = ReadByte()) == 'u')
334 #if KEEP_LINE_COLUMN
335 ++column;
336 #else
337 ;
338 #fi
339
340 buffer[bufpos] = c = (char)(hexval(c) << 12 |
341 hexval(ReadByte()) << 8 |
342 hexval(ReadByte()) << 4 |
343 hexval(ReadByte()));
344
345 #if KEEP_LINE_COLUMN
346 column += 4;
347 #fi
348 }
349 catch(java.io.IOException e)
350 {
351 #if KEEP_LINE_COLUMN
352 throw new Error("Invalid escape character at line " + line +
353 " column " + column + ".");
354 #else
355 throw new Error(\"Invalid escape character in input\");
356 #fi
357 }
358
359 if (backSlashCnt == 1)
360 return c;
361 else
362 {
363 backup(backSlashCnt - 1);
364 return '\\';
365 }
366 }
367 else
368 {
369 #if KEEP_LINE_COLUMN
370 UpdateLineColumn(c);
371 #fi
372 return c;
373 }
374 }
375
376 #if GENERATE_ANNOTATIONS
377 @Deprecated
378 #fi
379 /**
380 * @deprecated
381 * @see #getEndColumn
382 */
383 ${PREFIX}public int getColumn() {
384 #if KEEP_LINE_COLUMN
385 return bufcolumn[bufpos];
386 #else
387 return -1;
388 #fi
389 }
390
391 #if GENERATE_ANNOTATIONS
392 @Deprecated
393 #fi
394 /**
395 * @deprecated
396 * @see #getEndLine
397 */
398 ${PREFIX}public int getLine() {
399 #if KEEP_LINE_COLUMN
400 return bufline[bufpos];
401 #else
402 return -1;
403 #fi
404 }
405
406 /** Get end column. */
407 ${PREFIX}public int getEndColumn() {
408 #if KEEP_LINE_COLUMN
409 return bufcolumn[bufpos];
410 #else
411 return -1;
412 #fi
413 }
414
415 /** Get end line. */
416 ${PREFIX}public int getEndLine() {
417 #if KEEP_LINE_COLUMN
418 return bufline[bufpos];
419 #else
420 return -1;
421 #fi
422 }
423
424 /** @return column of token start */
425 ${PREFIX}public int getBeginColumn() {
426 #if KEEP_LINE_COLUMN
427 return bufcolumn[tokenBegin];
428 #else
429 return -1;
430 #fi
431 }
432
433 /** @return line number of token start */
434 ${PREFIX}public int getBeginLine() {
435 #if KEEP_LINE_COLUMN
436 return bufline[tokenBegin];
437 #else
438 return -1;
439 #fi
440 }
441
442 /** Retreat. */
443 ${PREFIX}public void backup(int amount) {
444
445 inBuf += amount;
446 if ((bufpos -= amount) < 0)
447 bufpos += bufsize;
448 }
449
450 /** Constructor. */
451 public JavaCharStream(java.io.Reader dstream,
452 int startline, int startcolumn, int buffersize)
453 {
454 #if STATIC
455 if (inputStream != null)
456 throw new Error("\n ERROR: Second call to the constructor of a static JavaCharStream.\n" +
457 " You must either use ReInit() or set the JavaCC option STATIC to false\n" +
458 " during the generation of this class.");
459 #fi
460 inputStream = dstream;
461 #if KEEP_LINE_COLUMN
462 line = startline;
463 column = startcolumn - 1;
464 #fi
465
466 available = bufsize = buffersize;
467 buffer = new char[buffersize];
468 #if KEEP_LINE_COLUMN
469 bufline = new int[buffersize];
470 bufcolumn = new int[buffersize];
471 #fi
472 nextCharBuf = new char[4096];
473 }
474
475 /** Constructor. */
476 public JavaCharStream(java.io.Reader dstream,
477 int startline, int startcolumn)
478 {
479 this(dstream, startline, startcolumn, 4096);
480 }
481
482 /** Constructor. */
483 public JavaCharStream(java.io.Reader dstream)
484 {
485 this(dstream, 1, 1, 4096);
486 }
487 /** Reinitialise. */
488 public void ReInit(java.io.Reader dstream,
489 int startline, int startcolumn, int buffersize)
490 {
491 inputStream = dstream;
492 #if KEEP_LINE_COLUMN
493 line = startline;
494 column = startcolumn - 1;
495 #fi
496
497 if (buffer == null || buffersize != buffer.length)
498 {
499 available = bufsize = buffersize;
500 buffer = new char[buffersize];
501 #if KEEP_LINE_COLUMN
502 bufline = new int[buffersize];
503 bufcolumn = new int[buffersize];
504 #fi
505 nextCharBuf = new char[4096];
506 }
507 #if KEEP_LINE_COLUMN
508 prevCharIsLF = prevCharIsCR = false;
509 #fi
510 tokenBegin = inBuf = maxNextCharInd = 0;
511 nextCharInd = bufpos = -1;
512 }
513
514 /** Reinitialise. */
515 public void ReInit(java.io.Reader dstream,
516 int startline, int startcolumn)
517 {
518 ReInit(dstream, startline, startcolumn, 4096);
519 }
520
521 /** Reinitialise. */
522 public void ReInit(java.io.Reader dstream)
523 {
524 ReInit(dstream, 1, 1, 4096);
525 }
526 /** Constructor. */
527 public JavaCharStream(java.io.InputStream dstream, String encoding, int startline,
528 int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException
529 {
530 this(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);
531 }
532
533 /** Constructor. */
534 public JavaCharStream(java.io.InputStream dstream, int startline,
535 int startcolumn, int buffersize)
536 {
537 this(new java.io.InputStreamReader(dstream), startline, startcolumn, 4096);
538 }
539
540 /** Constructor. */
541 public JavaCharStream(java.io.InputStream dstream, String encoding, int startline,
542 int startcolumn) throws java.io.UnsupportedEncodingException
543 {
544 this(dstream, encoding, startline, startcolumn, 4096);
545 }
546
547 /** Constructor. */
548 public JavaCharStream(java.io.InputStream dstream, int startline,
549 int startcolumn)
550 {
551 this(dstream, startline, startcolumn, 4096);
552 }
553
554 /** Constructor. */
555 public JavaCharStream(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException
556 {
557 this(dstream, encoding, 1, 1, 4096);
558 }
559
560 /** Constructor. */
561 public JavaCharStream(java.io.InputStream dstream)
562 {
563 this(dstream, 1, 1, 4096);
564 }
565
566 /** Reinitialise. */
567 public void ReInit(java.io.InputStream dstream, String encoding, int startline,
568 int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException
569 {
570 ReInit(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);
571 }
572
573 /** Reinitialise. */
574 public void ReInit(java.io.InputStream dstream, int startline,
575 int startcolumn, int buffersize)
576 {
577 ReInit(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize);
578 }
579 /** Reinitialise. */
580 public void ReInit(java.io.InputStream dstream, String encoding, int startline,
581 int startcolumn) throws java.io.UnsupportedEncodingException
582 {
583 ReInit(dstream, encoding, startline, startcolumn, 4096);
584 }
585 /** Reinitialise. */
586 public void ReInit(java.io.InputStream dstream, int startline,
587 int startcolumn)
588 {
589 ReInit(dstream, startline, startcolumn, 4096);
590 }
591 /** Reinitialise. */
592 public void ReInit(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException
593 {
594 ReInit(dstream, encoding, 1, 1, 4096);
595 }
596
597 /** Reinitialise. */
598 public void ReInit(java.io.InputStream dstream)
599 {
600 ReInit(dstream, 1, 1, 4096);
601 }
602
603 /** @return token image as String */
604 ${PREFIX}public String GetImage()
605 {
606 if (bufpos >= tokenBegin)
607 return new String(buffer, tokenBegin, bufpos - tokenBegin + 1);
608 else
609 return new String(buffer, tokenBegin, bufsize - tokenBegin) +
610 new String(buffer, 0, bufpos + 1);
611 }
612
613 /** @return suffix */
614 ${PREFIX}public char[] GetSuffix(int len)
615 {
616 char[] ret = new char[len];
617
618 if ((bufpos + 1) >= len)
619 System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
620 else
621 {
622 System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0,
623 len - bufpos - 1);
624 System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
625 }
626
627 return ret;
628 }
629
630 /** Set buffers back to null when finished. */
631 ${PREFIX}public void Done()
632 {
633 nextCharBuf = null;
634 buffer = null;
635 #if KEEP_LINE_COLUMN
636 bufline = null;
637 bufcolumn = null;
638 #fi
639 }
640 #if KEEP_LINE_COLUMN
641
642 /**
643 * Method to adjust line and column numbers for the start of a token.
644 */
645 ${PREFIX}public void adjustBeginLineColumn(int newLine, int newCol)
646 {
647 int start = tokenBegin;
648 int len;
649
650 if (bufpos >= tokenBegin)
651 {
652 len = bufpos - tokenBegin + inBuf + 1;
653 }
654 else
655 {
656 len = bufsize - tokenBegin + bufpos + 1 + inBuf;
657 }
658
659 int i = 0, j = 0, k = 0;
660 int nextColDiff = 0, columnDiff = 0;
661
662 while (i < len && bufline[j = start % bufsize] == bufline[k = ++start % bufsize])
663 {
664 bufline[j] = newLine;
665 nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
666 bufcolumn[j] = newCol + columnDiff;
667 columnDiff = nextColDiff;
668 i++;
669 }
670
671 if (i < len)
672 {
673 bufline[j] = newLine++;
674 bufcolumn[j] = newCol + columnDiff;
675
676 while (i++ < len)
677 {
678 if (bufline[j = start % bufsize] != bufline[++start % bufsize])
679 bufline[j] = newLine++;
680 else
681 bufline[j] = newLine;
682 }
683 }
684
685 line = bufline[j];
686 column = bufcolumn[j];
687 }
688
689 #fi
690 }