Coverage Report - org.webmacro.parser.ASCII_CharStream
 
Classes in this File Line Coverage Branch Coverage Complexity
ASCII_CharStream
0%
0/119
0%
0/50
2.28
ASCII_CharStream$Buffer
0%
0/22
N/A
2.28
 
 1  
 /* Generated By:JavaCC: Do not edit this line. ASCII_CharStream.java Version 0.7pre6 */
 2  
 
 3  
 package org.webmacro.parser;
 4  
 
 5  
 /**
 6  
  * An implementation of interface CharStream, where the stream is assumed to
 7  
  * contain only ASCII characters (without unicode processing).
 8  
  * Modified extensively by Brian Goetz (Oct 2000) to support being able to
 9  
  * back up in the buffer.
 10  
  */
 11  
 
 12  
 public final class ASCII_CharStream
 13  
 {
 14  
 
 15  
     private static final class Buffer
 16  
     {
 17  
 
 18  
         int size;
 19  
         int dataLen, curPos;
 20  
         char[] buffer;
 21  
         int[] bufline, bufcolumn;
 22  
 
 23  
         public Buffer (int n)
 24  0
         {
 25  0
             size = n;
 26  0
             dataLen = 0;
 27  0
             curPos = -1;
 28  0
             buffer = new char[n];
 29  0
             bufline = new int[n];
 30  0
             bufcolumn = new int[n];
 31  0
         }
 32  
 
 33  
         public void expand (int n)
 34  
         {
 35  0
             char[] newbuffer = new char[size + n];
 36  0
             int newbufline[] = new int[size + n];
 37  0
             int newbufcolumn[] = new int[size + n];
 38  
 
 39  
             try
 40  
             {
 41  0
                 System.arraycopy(buffer, 0, newbuffer, 0, size);
 42  0
                 buffer = newbuffer;
 43  0
                 System.arraycopy(bufline, 0, newbufline, 0, size);
 44  0
                 bufline = newbufline;
 45  0
                 System.arraycopy(bufcolumn, 0, newbufcolumn, 0, size);
 46  0
                 bufcolumn = newbufcolumn;
 47  
             }
 48  0
             catch (Throwable t)
 49  
             {
 50  0
                 throw new Error(t.getMessage());
 51  0
             }
 52  
 
 53  0
             size += n;
 54  0
         }
 55  
     }
 56  
 
 57  
     private Buffer bufA, bufB, curBuf, otherBuf, tokenBeginBuf;
 58  
     private int tokenBeginPos;
 59  
     private int backupChars;
 60  
 
 61  
     public static final boolean staticFlag = false;
 62  
 
 63  0
     private int column = 0;
 64  0
     private int line = 1;
 65  0
     private boolean prevCharIsCR = false;
 66  0
     private boolean prevCharIsLF = false;
 67  
 
 68  
     private java.io.Reader inputStream;
 69  
 
 70  
     private final void swapBuf ()
 71  
     {
 72  0
         Buffer tmp = curBuf;
 73  0
         curBuf = otherBuf;
 74  0
         otherBuf = tmp;
 75  0
     }
 76  
 
 77  
     private final void FillBuff () throws java.io.IOException
 78  
     {
 79  
         // Buffer fill logic:
 80  
         // If there is at least 2K left in this buffer, just read some more
 81  
         // Otherwise, if we're processing a token and it either
 82  
         // (a) starts in the other buffer (meaning it's filled both part of
 83  
         //     the other buffer and some of this one, or
 84  
         // (b) starts in the first 2K of this buffer (meaning its taken up
 85  
         //     most of this buffer
 86  
         // we expand this buffer.  Otherwise, we swap buffers.
 87  
         // This guarantees we will be able to back up at least 2K characters.
 88  0
         if (curBuf.size - curBuf.dataLen < 2048)
 89  
         {
 90  0
             if (tokenBeginPos >= 0
 91  
                     && ((tokenBeginBuf == curBuf && tokenBeginPos < 2048)
 92  
                     || tokenBeginBuf != curBuf))
 93  
             {
 94  0
                 curBuf.expand(2048);
 95  
             }
 96  
             else
 97  
             {
 98  0
                 swapBuf();
 99  0
                 curBuf.curPos = curBuf.dataLen = 0;
 100  
             }
 101  
         }
 102  
 
 103  
         try
 104  
         {
 105  0
             int i = inputStream.read(curBuf.buffer, curBuf.dataLen,
 106  
                     curBuf.size - curBuf.dataLen);
 107  0
             if (i == -1)
 108  
             {
 109  0
                 inputStream.close();
 110  0
                 throw new java.io.IOException();
 111  
             }
 112  
             else
 113  0
                 curBuf.dataLen += i;
 114  0
             return;
 115  
         }
 116  0
         catch (java.io.IOException e)
 117  
         {
 118  0
             if (curBuf.curPos > 0)
 119  0
                 --curBuf.curPos;
 120  0
             if (tokenBeginPos == -1)
 121  
             {
 122  0
                 tokenBeginPos = curBuf.curPos;
 123  0
                 tokenBeginBuf = curBuf;
 124  
             }
 125  0
             throw e;
 126  
         }
 127  
     }
 128  
 
 129  
     public final char BeginToken () throws java.io.IOException
 130  
     {
 131  0
         tokenBeginPos = -1;
 132  0
         char c = readChar();
 133  0
         tokenBeginBuf = curBuf;
 134  0
         tokenBeginPos = curBuf.curPos;
 135  
 
 136  0
         return c;
 137  
     }
 138  
 
 139  
     private final void UpdateLineColumn (char c)
 140  
     {
 141  0
         column++;
 142  
 
 143  0
         if (prevCharIsLF)
 144  
         {
 145  0
             prevCharIsLF = false;
 146  0
             line += (column = 1);
 147  
         }
 148  0
         else if (prevCharIsCR)
 149  
         {
 150  0
             prevCharIsCR = false;
 151  0
             if (c == '\n')
 152  
             {
 153  0
                 prevCharIsLF = true;
 154  
             }
 155  
             else
 156  0
                 line += (column = 1);
 157  
         }
 158  
 
 159  0
         switch (c)
 160  
         {
 161  
             case '\r':
 162  0
                 prevCharIsCR = true;
 163  0
                 break;
 164  
             case '\n':
 165  0
                 prevCharIsLF = true;
 166  0
                 break;
 167  
             case '\t':
 168  0
                 column--;
 169  0
                 column += (8 - (column & 07));
 170  0
                 break;
 171  
             default :
 172  
                 break;
 173  
         }
 174  
 
 175  0
         curBuf.bufline[curBuf.curPos] = line;
 176  0
         curBuf.bufcolumn[curBuf.curPos] = column;
 177  0
     }
 178  
 
 179  
     public final char readChar () throws java.io.IOException
 180  
     {
 181  
         // When we hit the end of the buffer, if we're backing up, we just
 182  
         // swap, if we're not, we fill.
 183  0
         if (++curBuf.curPos >= curBuf.dataLen)
 184  
         {
 185  0
             if (backupChars > 0)
 186  
             {
 187  0
                 --curBuf.curPos;
 188  0
                 swapBuf();
 189  
             }
 190  
             else
 191  0
                 FillBuff();
 192  
         }
 193  
         ;
 194  
 
 195  0
         char c = (char) ((char) 0xff & curBuf.buffer[curBuf.curPos]);
 196  
 
 197  
         // No need to update line numbers if we've already processed this char
 198  0
         if (backupChars > 0)
 199  0
             --backupChars;
 200  
         else
 201  0
             UpdateLineColumn(c);
 202  
 
 203  0
         return (c);
 204  
     }
 205  
 
 206  
     /**
 207  
      * @deprecated
 208  
      * @see #getEndColumn
 209  
      */
 210  
 
 211  
     public final int getColumn ()
 212  
     {
 213  0
         return curBuf.bufcolumn[curBuf.curPos];
 214  
     }
 215  
 
 216  
     /**
 217  
      * @deprecated
 218  
      * @see #getEndLine
 219  
      */
 220  
 
 221  
     public final int getLine ()
 222  
     {
 223  0
         return curBuf.bufline[curBuf.curPos];
 224  
     }
 225  
 
 226  
     public final int getEndColumn ()
 227  
     {
 228  0
         return curBuf.bufcolumn[curBuf.curPos];
 229  
     }
 230  
 
 231  
     public final int getEndLine ()
 232  
     {
 233  0
         return curBuf.bufline[curBuf.curPos];
 234  
     }
 235  
 
 236  
     public final int getBeginColumn ()
 237  
     {
 238  0
         return tokenBeginBuf.bufcolumn[tokenBeginPos];
 239  
     }
 240  
 
 241  
     public final int getBeginLine ()
 242  
     {
 243  0
         return tokenBeginBuf.bufline[tokenBeginPos];
 244  
     }
 245  
 
 246  
     public final void backup (int amount)
 247  
     {
 248  0
         backupChars += amount;
 249  0
         if (curBuf.curPos - amount < 0)
 250  
         {
 251  0
             int addlChars = amount - 1 - curBuf.curPos;
 252  0
             curBuf.curPos = 0;
 253  0
             swapBuf();
 254  0
             curBuf.curPos = curBuf.dataLen - addlChars - 1;
 255  0
         }
 256  
         else
 257  
         {
 258  0
             curBuf.curPos -= amount;
 259  
         }
 260  0
     }
 261  
 
 262  
     public ASCII_CharStream (java.io.Reader dstream, int startline,
 263  
                              int startcolumn, int buffersize)
 264  0
     {
 265  0
         ReInit(dstream, startline, startcolumn, buffersize);
 266  0
     }
 267  
 
 268  
     public ASCII_CharStream (java.io.Reader dstream, int startline,
 269  
                              int startcolumn)
 270  
     {
 271  0
         this(dstream, startline, startcolumn, 4096);
 272  0
     }
 273  
 
 274  
     public void ReInit (java.io.Reader dstream, int startline,
 275  
                         int startcolumn, int buffersize)
 276  
     {
 277  0
         inputStream = dstream;
 278  0
         line = startline;
 279  0
         column = startcolumn - 1;
 280  
 
 281  0
         if (bufA == null || bufA.size != buffersize)
 282  0
             bufA = new Buffer(buffersize);
 283  0
         if (bufB == null || bufB.size != buffersize)
 284  0
             bufB = new Buffer(buffersize);
 285  0
         curBuf = bufA;
 286  0
         otherBuf = bufB;
 287  0
         curBuf.curPos = otherBuf.dataLen = -1;
 288  0
         curBuf.dataLen = otherBuf.dataLen = 0;
 289  
 
 290  0
         prevCharIsLF = prevCharIsCR = false;
 291  0
         tokenBeginPos = -1;
 292  0
         tokenBeginBuf = null;
 293  0
         backupChars = 0;
 294  0
     }
 295  
 
 296  
     public void ReInit (java.io.Reader dstream, int startline,
 297  
                         int startcolumn)
 298  
     {
 299  0
         ReInit(dstream, startline, startcolumn, 4096);
 300  0
     }
 301  
 
 302  
     public ASCII_CharStream (java.io.InputStream dstream, int startline,
 303  
                              int startcolumn, int buffersize)
 304  
     {
 305  0
         this(new java.io.InputStreamReader(dstream), startline, startcolumn, 4096);
 306  0
     }
 307  
 
 308  
     public ASCII_CharStream (java.io.InputStream dstream, int startline,
 309  
                              int startcolumn)
 310  
     {
 311  0
         this(dstream, startline, startcolumn, 4096);
 312  0
     }
 313  
 
 314  
     public void ReInit (java.io.InputStream dstream, int startline,
 315  
                         int startcolumn, int buffersize)
 316  
     {
 317  0
         ReInit(new java.io.InputStreamReader(dstream), startline, startcolumn,
 318  
                 4096);
 319  0
     }
 320  
 
 321  
     public void ReInit (java.io.InputStream dstream, int startline,
 322  
                         int startcolumn)
 323  
     {
 324  0
         ReInit(dstream, startline, startcolumn, 4096);
 325  0
     }
 326  
 
 327  
     public final String GetImage ()
 328  
     {
 329  0
         if (tokenBeginBuf == curBuf)
 330  0
             return new String(curBuf.buffer, tokenBeginPos,
 331  
                     curBuf.curPos - tokenBeginPos + 1);
 332  
         else
 333  0
             return new String(otherBuf.buffer, tokenBeginPos,
 334  
                     otherBuf.dataLen - tokenBeginPos)
 335  
                     + new String(curBuf.buffer, 0, curBuf.curPos + 1);
 336  
     }
 337  
 
 338  
     public final char[] GetSuffix (int len)
 339  
     {
 340  0
         char[] ret = new char[len];
 341  
 
 342  0
         if ((curBuf.curPos + 1) >= len)
 343  0
             System.arraycopy(curBuf.buffer, curBuf.curPos - len + 1, ret, 0, len);
 344  
         else
 345  
         {
 346  0
             if (otherBuf.dataLen >= len - curBuf.curPos - 1)
 347  0
                 System.arraycopy(otherBuf.buffer,
 348  
                         otherBuf.dataLen - (len - curBuf.curPos - 1), ret, 0,
 349  
                         len - curBuf.curPos - 1);
 350  0
             System.arraycopy(curBuf.buffer, 0, ret, len - curBuf.curPos - 1,
 351  
                     curBuf.curPos + 1);
 352  
         }
 353  
 
 354  0
         return null;
 355  
     }
 356  
 
 357  
     public void Done ()
 358  
     {
 359  0
         bufA = bufB = curBuf = otherBuf = null;
 360  0
     }
 361  
 
 362  
 }
 363