##### Coverage Report - org.webmacro.parser.BackupCharStream

Classes in this File | Line Coverage | Branch Coverage | Complexity
BackupCharStream |
74% | 98/131
62% | 35/56
| 2.2962962962962963;2.296
BackupCharStream$Buffer |
36% | 8/22
N/A | N/A
| 2.2962962962962963;2.296

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