1 | |
|
2 | |
|
3 | |
package org.webmacro.parser; |
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
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 | 3532 | { |
26 | 3532 | size = n; |
27 | 3532 | dataLen = 0; |
28 | 3532 | curPos = -1; |
29 | 3532 | buffer = new char[n]; |
30 | 3532 | bufline = new int[n]; |
31 | 3532 | bufcolumn = new int[n]; |
32 | 3532 | } |
33 | |
|
34 | |
public void expand (int n) |
35 | |
{ |
36 | 8 | char[] newbuffer = new char[size + n]; |
37 | 8 | int newbufline[] = new int[size + n]; |
38 | 8 | int newbufcolumn[] = new int[size + n]; |
39 | |
|
40 | |
try |
41 | |
{ |
42 | 8 | System.arraycopy(buffer, 0, newbuffer, 0, size); |
43 | 8 | buffer = newbuffer; |
44 | 8 | System.arraycopy(bufline, 0, newbufline, 0, size); |
45 | 8 | bufline = newbufline; |
46 | 8 | System.arraycopy(bufcolumn, 0, newbufcolumn, 0, size); |
47 | 8 | bufcolumn = newbufcolumn; |
48 | |
} |
49 | 0 | catch (Throwable t) |
50 | |
{ |
51 | 0 | throw new Error(t.getMessage()); |
52 | 8 | } |
53 | |
|
54 | 8 | size += n; |
55 | 8 | } |
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 | 1766 | private int column = 0; |
65 | 1766 | private int line = 1; |
66 | 1766 | private boolean prevCharIsCR = false; |
67 | 1766 | private boolean prevCharIsLF = false; |
68 | |
|
69 | |
private java.io.Reader inputStream; |
70 | 1766 | private boolean inputStreamClosed = false; |
71 | |
|
72 | |
private final void swapBuf () |
73 | |
{ |
74 | 2820 | Buffer tmp = curBuf; |
75 | 2820 | curBuf = otherBuf; |
76 | 2820 | otherBuf = tmp; |
77 | 2820 | } |
78 | |
|
79 | |
private final void FillBuff () throws java.io.IOException |
80 | |
{ |
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | 6477 | if (curBuf.size - curBuf.dataLen < 2048) |
91 | |
{ |
92 | 1058 | if (tokenBeginPos >= 0 |
93 | |
&& ((tokenBeginBuf == curBuf && tokenBeginPos < 2048) |
94 | |
|| tokenBeginBuf != curBuf)) |
95 | |
{ |
96 | 8 | curBuf.expand(2048); |
97 | |
} |
98 | |
else |
99 | |
{ |
100 | 1050 | swapBuf(); |
101 | 1050 | curBuf.curPos = curBuf.dataLen = 0; |
102 | |
} |
103 | |
} |
104 | |
|
105 | |
try |
106 | |
{ |
107 | 6477 | int i = inputStream.read(curBuf.buffer, curBuf.dataLen, |
108 | |
curBuf.size - curBuf.dataLen); |
109 | 4410 | if (i == -1) |
110 | |
{ |
111 | 1766 | inputStream.close(); |
112 | 1766 | inputStreamClosed = true; |
113 | 1766 | throw new java.io.IOException(); |
114 | |
} |
115 | |
else |
116 | 2644 | curBuf.dataLen += i; |
117 | 2644 | return; |
118 | |
} |
119 | 3833 | catch (java.io.IOException e) |
120 | |
{ |
121 | 3833 | if (curBuf.curPos > 0) |
122 | 3659 | --curBuf.curPos; |
123 | 3833 | if (tokenBeginPos == -1) |
124 | |
{ |
125 | 3243 | tokenBeginPos = curBuf.curPos; |
126 | 3243 | tokenBeginBuf = curBuf; |
127 | |
} |
128 | 3833 | if (e.getClass().getName().equals("sun.io.MalformedInputException")) |
129 | |
{ |
130 | |
|
131 | |
|
132 | |
|
133 | 0 | throw new ParserRuntimeException("MalformedInput", e); |
134 | |
} |
135 | 3833 | throw e; |
136 | |
} |
137 | |
} |
138 | |
|
139 | |
public final char BeginToken () throws java.io.IOException |
140 | |
{ |
141 | 1558341 | tokenBeginPos = -1; |
142 | 1558341 | char c = readChar(); |
143 | 1555106 | tokenBeginBuf = curBuf; |
144 | 1555106 | tokenBeginPos = curBuf.curPos; |
145 | |
|
146 | 1555106 | return c; |
147 | |
} |
148 | |
|
149 | |
private final void UpdateLineColumn (char c) |
150 | |
{ |
151 | 4187491 | column++; |
152 | |
|
153 | 4187491 | if (prevCharIsLF) |
154 | |
{ |
155 | 68994 | prevCharIsLF = false; |
156 | 68994 | line += (column = 1); |
157 | |
} |
158 | 4118497 | else if (prevCharIsCR) |
159 | |
{ |
160 | 491 | prevCharIsCR = false; |
161 | 491 | if (c == '\n') |
162 | |
{ |
163 | 366 | prevCharIsLF = true; |
164 | |
} |
165 | |
else |
166 | 125 | line += (column = 1); |
167 | |
} |
168 | |
|
169 | 4187491 | switch (c) |
170 | |
{ |
171 | |
case '\r': |
172 | 492 | prevCharIsCR = true; |
173 | 492 | break; |
174 | |
case '\n': |
175 | 69259 | prevCharIsLF = true; |
176 | 69259 | break; |
177 | |
case '\t': |
178 | 3123 | column--; |
179 | 3123 | column += (8 - (column & 07)); |
180 | 3123 | break; |
181 | |
default : |
182 | |
break; |
183 | |
} |
184 | |
|
185 | 4187491 | curBuf.bufline[curBuf.curPos] = line; |
186 | 4187491 | curBuf.bufcolumn[curBuf.curPos] = column; |
187 | 4187491 | } |
188 | |
|
189 | |
public final char readChar () throws java.io.IOException |
190 | |
{ |
191 | |
|
192 | |
|
193 | 8035572 | if (++curBuf.curPos >= curBuf.dataLen) |
194 | |
{ |
195 | 7344 | if (backupChars > 0) |
196 | |
{ |
197 | 867 | --curBuf.curPos; |
198 | 867 | swapBuf(); |
199 | |
} |
200 | |
else |
201 | 6477 | FillBuff(); |
202 | |
} |
203 | |
; |
204 | |
|
205 | |
|
206 | 8031739 | char c = curBuf.buffer[curBuf.curPos]; |
207 | |
|
208 | |
|
209 | 8031739 | if (backupChars > 0) |
210 | 3844248 | --backupChars; |
211 | |
else |
212 | 4187491 | UpdateLineColumn(c); |
213 | |
|
214 | 8031739 | return (c); |
215 | |
} |
216 | |
|
217 | |
|
218 | |
|
219 | |
|
220 | |
|
221 | |
|
222 | |
public final int getColumn () |
223 | |
{ |
224 | 0 | return curBuf.bufcolumn[curBuf.curPos]; |
225 | |
} |
226 | |
|
227 | |
|
228 | |
|
229 | |
|
230 | |
|
231 | |
|
232 | |
public final int getLine () |
233 | |
{ |
234 | 0 | return curBuf.bufline[curBuf.curPos]; |
235 | |
} |
236 | |
|
237 | |
public final int getEndColumn () |
238 | |
{ |
239 | 116830 | return curBuf.bufcolumn[curBuf.curPos]; |
240 | |
} |
241 | |
|
242 | |
public final int getEndLine () |
243 | |
{ |
244 | 116830 | return curBuf.bufline[curBuf.curPos]; |
245 | |
} |
246 | |
|
247 | |
public final int getBeginColumn () |
248 | |
{ |
249 | 116830 | return tokenBeginBuf.bufcolumn[tokenBeginPos]; |
250 | |
} |
251 | |
|
252 | |
public final int getBeginLine () |
253 | |
{ |
254 | 116830 | return tokenBeginBuf.bufline[tokenBeginPos]; |
255 | |
} |
256 | |
|
257 | |
public final void backup (int amount) |
258 | |
{ |
259 | 1519693 | backupChars += amount; |
260 | 1519693 | if (curBuf.curPos - amount < 0) |
261 | |
{ |
262 | 903 | int addlChars = amount - (inputStreamClosed ? 0 : 1) - curBuf.curPos; |
263 | 903 | curBuf.curPos = 0; |
264 | 903 | swapBuf(); |
265 | 903 | curBuf.curPos = curBuf.dataLen - addlChars - 1; |
266 | 903 | } |
267 | |
else |
268 | |
{ |
269 | 1518790 | curBuf.curPos -= amount; |
270 | |
} |
271 | 1519693 | } |
272 | |
|
273 | |
public BackupCharStream (java.io.Reader dstream) |
274 | |
{ |
275 | 1766 | this(dstream, 1, 1, 4096); |
276 | 1766 | } |
277 | |
|
278 | |
public BackupCharStream (java.io.Reader dstream, int startline, |
279 | |
int startcolumn, int buffersize) |
280 | 1766 | { |
281 | 1766 | ReInit(dstream, startline, startcolumn, buffersize); |
282 | 1766 | } |
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 | 1766 | inputStream = dstream; |
294 | 1766 | inputStreamClosed = false; |
295 | 1766 | line = startline; |
296 | 1766 | column = startcolumn - 1; |
297 | |
|
298 | 1766 | if (bufA == null || bufA.size != buffersize) |
299 | 1766 | bufA = new Buffer(buffersize); |
300 | 1766 | if (bufB == null || bufB.size != buffersize) |
301 | 1766 | bufB = new Buffer(buffersize); |
302 | 1766 | curBuf = bufA; |
303 | 1766 | otherBuf = bufB; |
304 | 1766 | curBuf.curPos = otherBuf.dataLen = -1; |
305 | 1766 | curBuf.dataLen = otherBuf.dataLen = 0; |
306 | |
|
307 | 1766 | prevCharIsLF = prevCharIsCR = false; |
308 | 1766 | tokenBeginPos = -1; |
309 | 1766 | tokenBeginBuf = null; |
310 | 1766 | backupChars = 0; |
311 | 1766 | } |
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 | 63990 | if (tokenBeginBuf == curBuf) |
354 | |
{ |
355 | 63958 | ret = new String(curBuf.buffer, tokenBeginPos, |
356 | |
curBuf.curPos - tokenBeginPos + 1); |
357 | |
} |
358 | |
else |
359 | |
{ |
360 | 32 | ret = new String(otherBuf.buffer, tokenBeginPos, |
361 | |
otherBuf.dataLen - tokenBeginPos); |
362 | 32 | if (curBuf.curPos < curBuf.dataLen) |
363 | 2 | ret += new String(curBuf.buffer, 0, curBuf.curPos + 1); |
364 | |
} |
365 | |
|
366 | 63990 | 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 | |
|