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