~ubuntu-branches/ubuntu/natty/cobertura/natty

« back to all changes in this revision

Viewing changes to src/net/sourceforge/cobertura/javancss/parser/JavaCharStream.java

  • Committer: Bazaar Package Importer
  • Author(s): Miguel Landaeta
  • Date: 2010-05-11 19:21:46 UTC
  • mfrom: (0.1.4 sid) (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20100511192146-j742v5jsl89ztndu
Tags: 1.9.4.1+dfsg-2
* Now Build-Depends on libservlet2.5-java and add a missing Depends
  on the same package. (Closes: #580842). 
* Simplify list of JRE dependences for cobertura and drop JRE dependences for
  libcobertura-java as Java libraries are no longer required to depend on a
  JVM.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Cobertura - http://cobertura.sourceforge.net/
 
3
 *
 
4
 * This file was taken from JavaNCSS
 
5
 * http://www.kclee.com/clemens/java/javancss/
 
6
 * Copyright (C) 2000 Chr. Clemens Lee <clemens a.t kclee d.o.t com>
 
7
 *
 
8
 * Cobertura is free software; you can redistribute it and/or modify
 
9
 * it under the terms of the GNU General Public License as published
 
10
 * by the Free Software Foundation; either version 2 of the License,
 
11
 * or (at your option) any later version.
 
12
 *
 
13
 * Cobertura is distributed in the hope that it will be useful, but
 
14
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 
16
 * General Public License for more details.
 
17
 *
 
18
 * You should have received a copy of the GNU General Public License
 
19
 * along with Cobertura; if not, write to the Free Software
 
20
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 
21
 * USA
 
22
 */
 
23
 
 
24
 
 
25
/*
 
26
 *
 
27
 * WARNING   WARNING   WARNING   WARNING   WARNING   WARNING   WARNING   WARNING   WARNING  
 
28
 *
 
29
 * WARNING TO COBERTURA DEVELOPERS
 
30
 *
 
31
 * DO NOT MODIFY THIS FILE!
 
32
 *
 
33
 * MODIFY THE FILES UNDER THE JAVANCSS DIRECTORY LOCATED AT THE ROOT OF THE COBERTURA PROJECT.
 
34
 *
 
35
 * FOLLOW THE PROCEDURE FOR MERGING THE LATEST JAVANCSS INTO COBERTURA LOCATED AT
 
36
 * javancss/coberturaREADME.txt
 
37
 *
 
38
 * WARNING   WARNING   WARNING   WARNING   WARNING   WARNING   WARNING   WARNING   WARNING   
 
39
 */
 
40
/* Generated By:JavaCC: Do not edit this line. JavaCharStream.java Version 4.1 */
 
41
/* JavaCCOptions:STATIC=false */
 
42
package net.sourceforge.cobertura.javancss.parser;
 
43
 
 
44
/**
 
45
 * An implementation of interface CharStream, where the stream is assumed to
 
46
 * contain only ASCII characters (with java-like unicode escape processing).
 
47
 */
 
48
 
 
49
public class JavaCharStream
 
50
{
 
51
/** Whether parser is static. */
 
52
  public static final boolean staticFlag = false;
 
53
  static final int hexval(char c) throws java.io.IOException {
 
54
    switch(c)
 
55
    {
 
56
       case '0' :
 
57
          return 0;
 
58
       case '1' :
 
59
          return 1;
 
60
       case '2' :
 
61
          return 2;
 
62
       case '3' :
 
63
          return 3;
 
64
       case '4' :
 
65
          return 4;
 
66
       case '5' :
 
67
          return 5;
 
68
       case '6' :
 
69
          return 6;
 
70
       case '7' :
 
71
          return 7;
 
72
       case '8' :
 
73
          return 8;
 
74
       case '9' :
 
75
          return 9;
 
76
 
 
77
       case 'a' :
 
78
       case 'A' :
 
79
          return 10;
 
80
       case 'b' :
 
81
       case 'B' :
 
82
          return 11;
 
83
       case 'c' :
 
84
       case 'C' :
 
85
          return 12;
 
86
       case 'd' :
 
87
       case 'D' :
 
88
          return 13;
 
89
       case 'e' :
 
90
       case 'E' :
 
91
          return 14;
 
92
       case 'f' :
 
93
       case 'F' :
 
94
          return 15;
 
95
    }
 
96
 
 
97
    throw new java.io.IOException(); // Should never come here
 
98
  }
 
99
 
 
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) */