001 /*
002 * Cobertura - http://cobertura.sourceforge.net/
003 *
004 * This file was taken from JavaNCSS
005 * http://www.kclee.com/clemens/java/javancss/
006 * Copyright (C) 2000 Chr. Clemens Lee <clemens a.t kclee d.o.t com>
007 *
008 * Cobertura is free software; you can redistribute it and/or modify
009 * it under the terms of the GNU General Public License as published
010 * by the Free Software Foundation; either version 2 of the License,
011 * or (at your option) any later version.
012 *
013 * Cobertura is distributed in the hope that it will be useful, but
014 * WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016 * General Public License for more details.
017 *
018 * You should have received a copy of the GNU General Public License
019 * along with Cobertura; if not, write to the Free Software
020 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
021 * USA
022 */
023
024
025 /*
026 *
027 * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
028 *
029 * WARNING TO COBERTURA DEVELOPERS
030 *
031 * DO NOT MODIFY THIS FILE!
032 *
033 * MODIFY THE FILES UNDER THE JAVANCSS DIRECTORY LOCATED AT THE ROOT OF THE COBERTURA PROJECT.
034 *
035 * FOLLOW THE PROCEDURE FOR MERGING THE LATEST JAVANCSS INTO COBERTURA LOCATED AT
036 * javancss/coberturaREADME.txt
037 *
038 * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
039 */
040 /* Generated By:JavaCC: Do not edit this line. JavaCharStream.java Version 4.1 */
041 /* JavaCCOptions:STATIC=false */
042 package net.sourceforge.cobertura.javancss.parser;
043
044 /**
045 * An implementation of interface CharStream, where the stream is assumed to
046 * contain only ASCII characters (with java-like unicode escape processing).
047 */
048
049 public class JavaCharStream
050 {
051 /** Whether parser is static. */
052 public static final boolean staticFlag = false;
053 static final int hexval(char c) throws java.io.IOException {
054 switch(c)
055 {
056 case '0' :
057 return 0;
058 case '1' :
059 return 1;
060 case '2' :
061 return 2;
062 case '3' :
063 return 3;
064 case '4' :
065 return 4;
066 case '5' :
067 return 5;
068 case '6' :
069 return 6;
070 case '7' :
071 return 7;
072 case '8' :
073 return 8;
074 case '9' :
075 return 9;
076
077 case 'a' :
078 case 'A' :
079 return 10;
080 case 'b' :
081 case 'B' :
082 return 11;
083 case 'c' :
084 case 'C' :
085 return 12;
086 case 'd' :
087 case 'D' :
088 return 13;
089 case 'e' :
090 case 'E' :
091 return 14;
092 case 'f' :
093 case 'F' :
094 return 15;
095 }
096
097 throw new java.io.IOException(); // Should never come here
098 }
099
100 /** Position in buffer. */
101 public int bufpos = -1;
102 int bufsize;
103 int available;
104 int tokenBegin;
105 protected int bufline[];
106 protected int bufcolumn[];
107
108 protected int column = 0;
109 protected int line = 1;
110
111 protected boolean prevCharIsCR = false;
112 protected boolean prevCharIsLF = false;
113
114 protected java.io.Reader inputStream;
115
116 protected char[] nextCharBuf;
117 protected char[] buffer;
118 protected int maxNextCharInd = 0;
119 protected int nextCharInd = -1;
120 protected int inBuf = 0;
121 protected int tabSize = 8;
122
123 protected void setTabSize(int i) { tabSize = i; }
124 protected int getTabSize(int i) { return tabSize; }
125
126 protected void ExpandBuff(boolean wrapAround)
127 {
128 char[] newbuffer = new char[bufsize + 2048];
129 int newbufline[] = new int[bufsize + 2048];
130 int newbufcolumn[] = new int[bufsize + 2048];
131
132 try
133 {
134 if (wrapAround)
135 {
136 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
137 System.arraycopy(buffer, 0, newbuffer,
138 bufsize - tokenBegin, bufpos);
139 buffer = newbuffer;
140
141 System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
142 System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);
143 bufline = newbufline;
144
145 System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
146 System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);
147 bufcolumn = newbufcolumn;
148
149 bufpos += (bufsize - tokenBegin);
150 }
151 else
152 {
153 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
154 buffer = newbuffer;
155
156 System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
157 bufline = newbufline;
158
159 System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
160 bufcolumn = newbufcolumn;
161
162 bufpos -= tokenBegin;
163 }
164 }
165 catch (Throwable t)
166 {
167 throw new Error(t.getMessage());
168 }
169
170 available = (bufsize += 2048);
171 tokenBegin = 0;
172 }
173
174 protected void FillBuff() throws java.io.IOException
175 {
176 int i;
177 if (maxNextCharInd == 4096)
178 maxNextCharInd = nextCharInd = 0;
179
180 try {
181 if ((i = inputStream.read(nextCharBuf, maxNextCharInd,
182 4096 - maxNextCharInd)) == -1)
183 {
184 inputStream.close();
185 throw new java.io.IOException();
186 }
187 else
188 maxNextCharInd += i;
189 return;
190 }
191 catch(java.io.IOException e) {
192 if (bufpos != 0)
193 {
194 --bufpos;
195 backup(0);
196 }
197 else
198 {
199 bufline[bufpos] = line;
200 bufcolumn[bufpos] = column;
201 }
202 throw e;
203 }
204 }
205
206 protected char ReadByte() throws java.io.IOException
207 {
208 if (++nextCharInd >= maxNextCharInd)
209 FillBuff();
210
211 return nextCharBuf[nextCharInd];
212 }
213
214 /** @return starting character for token. */
215 public char BeginToken() throws java.io.IOException
216 {
217 if (inBuf > 0)
218 {
219 --inBuf;
220
221 if (++bufpos == bufsize)
222 bufpos = 0;
223
224 tokenBegin = bufpos;
225 return buffer[bufpos];
226 }
227
228 tokenBegin = 0;
229 bufpos = -1;
230
231 return readChar();
232 }
233
234 protected void AdjustBuffSize()
235 {
236 if (available == bufsize)
237 {
238 if (tokenBegin > 2048)
239 {
240 bufpos = 0;
241 available = tokenBegin;
242 }
243 else
244 ExpandBuff(false);
245 }
246 else if (available > tokenBegin)
247 available = bufsize;
248 else if ((tokenBegin - available) < 2048)
249 ExpandBuff(true);
250 else
251 available = tokenBegin;
252 }
253
254 protected void UpdateLineColumn(char c)
255 {
256 column++;
257
258 if (prevCharIsLF)
259 {
260 prevCharIsLF = false;
261 line += (column = 1);
262 }
263 else if (prevCharIsCR)
264 {
265 prevCharIsCR = false;
266 if (c == '\n')
267 {
268 prevCharIsLF = true;
269 }
270 else
271 line += (column = 1);
272 }
273
274 switch (c)
275 {
276 case '\r' :
277 prevCharIsCR = true;
278 break;
279 case '\n' :
280 prevCharIsLF = true;
281 break;
282 case '\t' :
283 column--;
284 column += (tabSize - (column % tabSize));
285 break;
286 default :
287 break;
288 }
289
290 bufline[bufpos] = line;
291 bufcolumn[bufpos] = column;
292 }
293
294 /** Read a character. */
295 public char readChar() throws java.io.IOException
296 {
297 if (inBuf > 0)
298 {
299 --inBuf;
300
301 if (++bufpos == bufsize)
302 bufpos = 0;
303
304 return buffer[bufpos];
305 }
306
307 char c;
308
309 if (++bufpos == available)
310 AdjustBuffSize();
311
312 if ((buffer[bufpos] = c = ReadByte()) == '\\')
313 {
314 UpdateLineColumn(c);
315
316 int backSlashCnt = 1;
317
318 for (;;) // Read all the backslashes
319 {
320 if (++bufpos == available)
321 AdjustBuffSize();
322
323 try
324 {
325 if ((buffer[bufpos] = c = ReadByte()) != '\\')
326 {
327 UpdateLineColumn(c);
328 // found a non-backslash char.
329 if ((c == 'u') && ((backSlashCnt & 1) == 1))
330 {
331 if (--bufpos < 0)
332 bufpos = bufsize - 1;
333
334 break;
335 }
336
337 backup(backSlashCnt);
338 return '\\';
339 }
340 }
341 catch(java.io.IOException e)
342 {
343 if (backSlashCnt > 1)
344 backup(backSlashCnt-1);
345
346 return '\\';
347 }
348
349 UpdateLineColumn(c);
350 backSlashCnt++;
351 }
352
353 // Here, we have seen an odd number of backslash's followed by a 'u'
354 try
355 {
356 while ((c = ReadByte()) == 'u')
357 ++column;
358
359 buffer[bufpos] = c = (char)(hexval(c) << 12 |
360 hexval(ReadByte()) << 8 |
361 hexval(ReadByte()) << 4 |
362 hexval(ReadByte()));
363
364 column += 4;
365 }
366 catch(java.io.IOException e)
367 {
368 throw new Error("Invalid escape character at line " + line +
369 " column " + column + ".");
370 }
371
372 if (backSlashCnt == 1)
373 return c;
374 else
375 {
376 backup(backSlashCnt - 1);
377 return '\\';
378 }
379 }
380 else
381 {
382 UpdateLineColumn(c);
383 return c;
384 }
385 }
386
387 /**
388 * @deprecated
389 * @see #getEndColumn
390 */
391 public int getColumn() {
392 return bufcolumn[bufpos];
393 }
394
395 /**
396 * @deprecated
397 * @see #getEndLine
398 */
399 public int getLine() {
400 return bufline[bufpos];
401 }
402
403 /** Get end column. */
404 public int getEndColumn() {
405 return bufcolumn[bufpos];
406 }
407
408 /** Get end line. */
409 public int getEndLine() {
410 return bufline[bufpos];
411 }
412
413 /** @return column of token start */
414 public int getBeginColumn() {
415 return bufcolumn[tokenBegin];
416 }
417
418 /** @return line number of token start */
419 public int getBeginLine() {
420 return bufline[tokenBegin];
421 }
422
423 /** Retreat. */
424 public void backup(int amount) {
425
426 inBuf += amount;
427 if ((bufpos -= amount) < 0)
428 bufpos += bufsize;
429 }
430
431 /** Constructor. */
432 public JavaCharStream(java.io.Reader dstream,
433 int startline, int startcolumn, int buffersize)
434 {
435 inputStream = dstream;
436 line = startline;
437 column = startcolumn - 1;
438
439 available = bufsize = buffersize;
440 buffer = new char[buffersize];
441 bufline = new int[buffersize];
442 bufcolumn = new int[buffersize];
443 nextCharBuf = new char[4096];
444 }
445
446 /** Constructor. */
447 public JavaCharStream(java.io.Reader dstream,
448 int startline, int startcolumn)
449 {
450 this(dstream, startline, startcolumn, 4096);
451 }
452
453 /** Constructor. */
454 public JavaCharStream(java.io.Reader dstream)
455 {
456 this(dstream, 1, 1, 4096);
457 }
458 /** Reinitialise. */
459 public void ReInit(java.io.Reader dstream,
460 int startline, int startcolumn, int buffersize)
461 {
462 inputStream = dstream;
463 line = startline;
464 column = startcolumn - 1;
465
466 if (buffer == null || buffersize != buffer.length)
467 {
468 available = bufsize = buffersize;
469 buffer = new char[buffersize];
470 bufline = new int[buffersize];
471 bufcolumn = new int[buffersize];
472 nextCharBuf = new char[4096];
473 }
474 prevCharIsLF = prevCharIsCR = false;
475 tokenBegin = inBuf = maxNextCharInd = 0;
476 nextCharInd = bufpos = -1;
477 }
478
479 /** Reinitialise. */
480 public void ReInit(java.io.Reader dstream,
481 int startline, int startcolumn)
482 {
483 ReInit(dstream, startline, startcolumn, 4096);
484 }
485
486 /** Reinitialise. */
487 public void ReInit(java.io.Reader dstream)
488 {
489 ReInit(dstream, 1, 1, 4096);
490 }
491 /** Constructor. */
492 public JavaCharStream(java.io.InputStream dstream, String encoding, int startline,
493 int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException
494 {
495 this(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);
496 }
497
498 /** Constructor. */
499 public JavaCharStream(java.io.InputStream dstream, int startline,
500 int startcolumn, int buffersize)
501 {
502 this(new java.io.InputStreamReader(dstream), startline, startcolumn, 4096);
503 }
504
505 /** Constructor. */
506 public JavaCharStream(java.io.InputStream dstream, String encoding, int startline,
507 int startcolumn) throws java.io.UnsupportedEncodingException
508 {
509 this(dstream, encoding, startline, startcolumn, 4096);
510 }
511
512 /** Constructor. */
513 public JavaCharStream(java.io.InputStream dstream, int startline,
514 int startcolumn)
515 {
516 this(dstream, startline, startcolumn, 4096);
517 }
518
519 /** Constructor. */
520 public JavaCharStream(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException
521 {
522 this(dstream, encoding, 1, 1, 4096);
523 }
524
525 /** Constructor. */
526 public JavaCharStream(java.io.InputStream dstream)
527 {
528 this(dstream, 1, 1, 4096);
529 }
530
531 /** Reinitialise. */
532 public void ReInit(java.io.InputStream dstream, String encoding, int startline,
533 int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException
534 {
535 ReInit(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);
536 }
537
538 /** Reinitialise. */
539 public void ReInit(java.io.InputStream dstream, int startline,
540 int startcolumn, int buffersize)
541 {
542 ReInit(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize);
543 }
544 /** Reinitialise. */
545 public void ReInit(java.io.InputStream dstream, String encoding, int startline,
546 int startcolumn) throws java.io.UnsupportedEncodingException
547 {
548 ReInit(dstream, encoding, startline, startcolumn, 4096);
549 }
550 /** Reinitialise. */
551 public void ReInit(java.io.InputStream dstream, int startline,
552 int startcolumn)
553 {
554 ReInit(dstream, startline, startcolumn, 4096);
555 }
556 /** Reinitialise. */
557 public void ReInit(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException
558 {
559 ReInit(dstream, encoding, 1, 1, 4096);
560 }
561
562 /** Reinitialise. */
563 public void ReInit(java.io.InputStream dstream)
564 {
565 ReInit(dstream, 1, 1, 4096);
566 }
567
568 /** @return token image as String */
569 public String GetImage()
570 {
571 if (bufpos >= tokenBegin)
572 return new String(buffer, tokenBegin, bufpos - tokenBegin + 1);
573 else
574 return new String(buffer, tokenBegin, bufsize - tokenBegin) +
575 new String(buffer, 0, bufpos + 1);
576 }
577
578 /** @return suffix */
579 public char[] GetSuffix(int len)
580 {
581 char[] ret = new char[len];
582
583 if ((bufpos + 1) >= len)
584 System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
585 else
586 {
587 System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0,
588 len - bufpos - 1);
589 System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
590 }
591
592 return ret;
593 }
594
595 /** Set buffers back to null when finished. */
596 public void Done()
597 {
598 nextCharBuf = null;
599 buffer = null;
600 bufline = null;
601 bufcolumn = null;
602 }
603
604 /**
605 * Method to adjust line and column numbers for the start of a token.
606 */
607 public void adjustBeginLineColumn(int newLine, int newCol)
608 {
609 int start = tokenBegin;
610 int len;
611
612 if (bufpos >= tokenBegin)
613 {
614 len = bufpos - tokenBegin + inBuf + 1;
615 }
616 else
617 {
618 len = bufsize - tokenBegin + bufpos + 1 + inBuf;
619 }
620
621 int i = 0, j = 0, k = 0;
622 int nextColDiff = 0, columnDiff = 0;
623
624 while (i < len &&
625 bufline[j = start % bufsize] == bufline[k = ++start % bufsize])
626 {
627 bufline[j] = newLine;
628 nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
629 bufcolumn[j] = newCol + columnDiff;
630 columnDiff = nextColDiff;
631 i++;
632 }
633
634 if (i < len)
635 {
636 bufline[j] = newLine++;
637 bufcolumn[j] = newCol + columnDiff;
638
639 while (i++ < len)
640 {
641 if (bufline[j = start % bufsize] != bufline[++start % bufsize])
642 bufline[j] = newLine++;
643 else
644 bufline[j] = newLine;
645 }
646 }
647
648 line = bufline[j];
649 column = bufcolumn[j];
650 }
651
652 }
653 /* JavaCC - OriginalChecksum=a71d76bc4e3e50f1cadfe216520f8634 (do not edit this line) */