1 | /* Generated By:JavaCC: Do not edit this line. SimpleCharStream.java Version 5.0 */ |
2 | /* JavaCCOptions:STATIC=false,SUPPORT_CLASS_VISIBILITY_PUBLIC=true */ |
3 | /* |
4 | * Copyright (C) Christian Schulte, 2005-206 |
5 | * All rights reserved. |
6 | * |
7 | * Redistribution and use in source and binary forms, with or without |
8 | * modification, are permitted provided that the following conditions |
9 | * are met: |
10 | * |
11 | * o Redistributions of source code must retain the above copyright |
12 | * notice, this list of conditions and the following disclaimer. |
13 | * |
14 | * o Redistributions in binary form must reproduce the above copyright |
15 | * notice, this list of conditions and the following disclaimer in |
16 | * the documentation and/or other materials provided with the |
17 | * distribution. |
18 | * |
19 | * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
20 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY |
21 | * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
22 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT, |
23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
24 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
28 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 | * |
30 | * $JOMC: VersionParser.jj 4613 2012-09-22 10:07:08Z schulte $ |
31 | * |
32 | */ |
33 | package org.jomc.util; |
34 | |
35 | /** |
36 | * An implementation of interface CharStream, where the stream is assumed to |
37 | * contain only ASCII characters (without unicode processing). |
38 | */ |
39 | |
40 | public class SimpleCharStream |
41 | { |
42 | /** Whether parser is static. */ |
43 | public static final boolean staticFlag = false; |
44 | int bufsize; |
45 | int available; |
46 | int tokenBegin; |
47 | /** Position in buffer. */ |
48 | public int bufpos = -1; |
49 | protected int bufline[]; |
50 | protected int bufcolumn[]; |
51 | |
52 | protected int column = 0; |
53 | protected int line = 1; |
54 | |
55 | protected boolean prevCharIsCR = false; |
56 | protected boolean prevCharIsLF = false; |
57 | |
58 | protected java.io.Reader inputStream; |
59 | |
60 | protected char[] buffer; |
61 | protected int maxNextCharInd = 0; |
62 | protected int inBuf = 0; |
63 | protected int tabSize = 8; |
64 | |
65 | protected void setTabSize(int i) { tabSize = i; } |
66 | protected int getTabSize(int i) { return tabSize; } |
67 | |
68 | |
69 | protected void ExpandBuff(boolean wrapAround) |
70 | { |
71 | char[] newbuffer = new char[bufsize + 2048]; |
72 | int newbufline[] = new int[bufsize + 2048]; |
73 | int newbufcolumn[] = new int[bufsize + 2048]; |
74 | |
75 | try |
76 | { |
77 | if (wrapAround) |
78 | { |
79 | System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin); |
80 | System.arraycopy(buffer, 0, newbuffer, bufsize - tokenBegin, bufpos); |
81 | buffer = newbuffer; |
82 | |
83 | System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin); |
84 | System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos); |
85 | bufline = newbufline; |
86 | |
87 | System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin); |
88 | System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos); |
89 | bufcolumn = newbufcolumn; |
90 | |
91 | maxNextCharInd = (bufpos += (bufsize - tokenBegin)); |
92 | } |
93 | else |
94 | { |
95 | System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin); |
96 | buffer = newbuffer; |
97 | |
98 | System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin); |
99 | bufline = newbufline; |
100 | |
101 | System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin); |
102 | bufcolumn = newbufcolumn; |
103 | |
104 | maxNextCharInd = (bufpos -= tokenBegin); |
105 | } |
106 | } |
107 | catch (Throwable t) |
108 | { |
109 | throw new Error(t.getMessage()); |
110 | } |
111 | |
112 | |
113 | bufsize += 2048; |
114 | available = bufsize; |
115 | tokenBegin = 0; |
116 | } |
117 | |
118 | protected void FillBuff() throws java.io.IOException |
119 | { |
120 | if (maxNextCharInd == available) |
121 | { |
122 | if (available == bufsize) |
123 | { |
124 | if (tokenBegin > 2048) |
125 | { |
126 | bufpos = maxNextCharInd = 0; |
127 | available = tokenBegin; |
128 | } |
129 | else if (tokenBegin < 0) |
130 | bufpos = maxNextCharInd = 0; |
131 | else |
132 | ExpandBuff(false); |
133 | } |
134 | else if (available > tokenBegin) |
135 | available = bufsize; |
136 | else if ((tokenBegin - available) < 2048) |
137 | ExpandBuff(true); |
138 | else |
139 | available = tokenBegin; |
140 | } |
141 | |
142 | int i; |
143 | try { |
144 | if ((i = inputStream.read(buffer, maxNextCharInd, available - maxNextCharInd)) == -1) |
145 | { |
146 | inputStream.close(); |
147 | throw new java.io.IOException(); |
148 | } |
149 | else |
150 | maxNextCharInd += i; |
151 | return; |
152 | } |
153 | catch(java.io.IOException e) { |
154 | --bufpos; |
155 | backup(0); |
156 | if (tokenBegin == -1) |
157 | tokenBegin = bufpos; |
158 | throw e; |
159 | } |
160 | } |
161 | |
162 | /** Start. */ |
163 | public char BeginToken() throws java.io.IOException |
164 | { |
165 | tokenBegin = -1; |
166 | char c = readChar(); |
167 | tokenBegin = bufpos; |
168 | |
169 | return c; |
170 | } |
171 | |
172 | protected void UpdateLineColumn(char c) |
173 | { |
174 | column++; |
175 | |
176 | if (prevCharIsLF) |
177 | { |
178 | prevCharIsLF = false; |
179 | line += (column = 1); |
180 | } |
181 | else if (prevCharIsCR) |
182 | { |
183 | prevCharIsCR = false; |
184 | if (c == '\n') |
185 | { |
186 | prevCharIsLF = true; |
187 | } |
188 | else |
189 | line += (column = 1); |
190 | } |
191 | |
192 | switch (c) |
193 | { |
194 | case '\r' : |
195 | prevCharIsCR = true; |
196 | break; |
197 | case '\n' : |
198 | prevCharIsLF = true; |
199 | break; |
200 | case '\t' : |
201 | column--; |
202 | column += (tabSize - (column % tabSize)); |
203 | break; |
204 | default : |
205 | break; |
206 | } |
207 | |
208 | bufline[bufpos] = line; |
209 | bufcolumn[bufpos] = column; |
210 | } |
211 | |
212 | /** Read a character. */ |
213 | public char readChar() throws java.io.IOException |
214 | { |
215 | if (inBuf > 0) |
216 | { |
217 | --inBuf; |
218 | |
219 | if (++bufpos == bufsize) |
220 | bufpos = 0; |
221 | |
222 | return buffer[bufpos]; |
223 | } |
224 | |
225 | if (++bufpos >= maxNextCharInd) |
226 | FillBuff(); |
227 | |
228 | char c = buffer[bufpos]; |
229 | |
230 | UpdateLineColumn(c); |
231 | return c; |
232 | } |
233 | |
234 | /** |
235 | * @deprecated |
236 | * @see #getEndColumn |
237 | */ |
238 | |
239 | public int getColumn() { |
240 | return bufcolumn[bufpos]; |
241 | } |
242 | |
243 | /** |
244 | * @deprecated |
245 | * @see #getEndLine |
246 | */ |
247 | |
248 | public int getLine() { |
249 | return bufline[bufpos]; |
250 | } |
251 | |
252 | /** Get token end column number. */ |
253 | public int getEndColumn() { |
254 | return bufcolumn[bufpos]; |
255 | } |
256 | |
257 | /** Get token end line number. */ |
258 | public int getEndLine() { |
259 | return bufline[bufpos]; |
260 | } |
261 | |
262 | /** Get token beginning column number. */ |
263 | public int getBeginColumn() { |
264 | return bufcolumn[tokenBegin]; |
265 | } |
266 | |
267 | /** Get token beginning line number. */ |
268 | public int getBeginLine() { |
269 | return bufline[tokenBegin]; |
270 | } |
271 | |
272 | /** Backup a number of characters. */ |
273 | public void backup(int amount) { |
274 | |
275 | inBuf += amount; |
276 | if ((bufpos -= amount) < 0) |
277 | bufpos += bufsize; |
278 | } |
279 | |
280 | /** Constructor. */ |
281 | public SimpleCharStream(java.io.Reader dstream, int startline, |
282 | int startcolumn, int buffersize) |
283 | { |
284 | inputStream = dstream; |
285 | line = startline; |
286 | column = startcolumn - 1; |
287 | |
288 | available = bufsize = buffersize; |
289 | buffer = new char[buffersize]; |
290 | bufline = new int[buffersize]; |
291 | bufcolumn = new int[buffersize]; |
292 | } |
293 | |
294 | /** Constructor. */ |
295 | public SimpleCharStream(java.io.Reader dstream, int startline, |
296 | int startcolumn) |
297 | { |
298 | this(dstream, startline, startcolumn, 4096); |
299 | } |
300 | |
301 | /** Constructor. */ |
302 | public SimpleCharStream(java.io.Reader dstream) |
303 | { |
304 | this(dstream, 1, 1, 4096); |
305 | } |
306 | |
307 | /** Reinitialise. */ |
308 | public void ReInit(java.io.Reader dstream, int startline, |
309 | int startcolumn, int buffersize) |
310 | { |
311 | inputStream = dstream; |
312 | line = startline; |
313 | column = startcolumn - 1; |
314 | |
315 | if (buffer == null || buffersize != buffer.length) |
316 | { |
317 | available = bufsize = buffersize; |
318 | buffer = new char[buffersize]; |
319 | bufline = new int[buffersize]; |
320 | bufcolumn = new int[buffersize]; |
321 | } |
322 | prevCharIsLF = prevCharIsCR = false; |
323 | tokenBegin = inBuf = maxNextCharInd = 0; |
324 | bufpos = -1; |
325 | } |
326 | |
327 | /** Reinitialise. */ |
328 | public void ReInit(java.io.Reader dstream, int startline, |
329 | int startcolumn) |
330 | { |
331 | ReInit(dstream, startline, startcolumn, 4096); |
332 | } |
333 | |
334 | /** Reinitialise. */ |
335 | public void ReInit(java.io.Reader dstream) |
336 | { |
337 | ReInit(dstream, 1, 1, 4096); |
338 | } |
339 | /** Constructor. */ |
340 | public SimpleCharStream(java.io.InputStream dstream, String encoding, int startline, |
341 | int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException |
342 | { |
343 | this(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize); |
344 | } |
345 | |
346 | /** Constructor. */ |
347 | public SimpleCharStream(java.io.InputStream dstream, int startline, |
348 | int startcolumn, int buffersize) |
349 | { |
350 | this(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize); |
351 | } |
352 | |
353 | /** Constructor. */ |
354 | public SimpleCharStream(java.io.InputStream dstream, String encoding, int startline, |
355 | int startcolumn) throws java.io.UnsupportedEncodingException |
356 | { |
357 | this(dstream, encoding, startline, startcolumn, 4096); |
358 | } |
359 | |
360 | /** Constructor. */ |
361 | public SimpleCharStream(java.io.InputStream dstream, int startline, |
362 | int startcolumn) |
363 | { |
364 | this(dstream, startline, startcolumn, 4096); |
365 | } |
366 | |
367 | /** Constructor. */ |
368 | public SimpleCharStream(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException |
369 | { |
370 | this(dstream, encoding, 1, 1, 4096); |
371 | } |
372 | |
373 | /** Constructor. */ |
374 | public SimpleCharStream(java.io.InputStream dstream) |
375 | { |
376 | this(dstream, 1, 1, 4096); |
377 | } |
378 | |
379 | /** Reinitialise. */ |
380 | public void ReInit(java.io.InputStream dstream, String encoding, int startline, |
381 | int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException |
382 | { |
383 | ReInit(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize); |
384 | } |
385 | |
386 | /** Reinitialise. */ |
387 | public void ReInit(java.io.InputStream dstream, int startline, |
388 | int startcolumn, int buffersize) |
389 | { |
390 | ReInit(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize); |
391 | } |
392 | |
393 | /** Reinitialise. */ |
394 | public void ReInit(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException |
395 | { |
396 | ReInit(dstream, encoding, 1, 1, 4096); |
397 | } |
398 | |
399 | /** Reinitialise. */ |
400 | public void ReInit(java.io.InputStream dstream) |
401 | { |
402 | ReInit(dstream, 1, 1, 4096); |
403 | } |
404 | /** Reinitialise. */ |
405 | public void ReInit(java.io.InputStream dstream, String encoding, int startline, |
406 | int startcolumn) throws java.io.UnsupportedEncodingException |
407 | { |
408 | ReInit(dstream, encoding, startline, startcolumn, 4096); |
409 | } |
410 | /** Reinitialise. */ |
411 | public void ReInit(java.io.InputStream dstream, int startline, |
412 | int startcolumn) |
413 | { |
414 | ReInit(dstream, startline, startcolumn, 4096); |
415 | } |
416 | /** Get token literal value. */ |
417 | public String GetImage() |
418 | { |
419 | if (bufpos >= tokenBegin) |
420 | return new String(buffer, tokenBegin, bufpos - tokenBegin + 1); |
421 | else |
422 | return new String(buffer, tokenBegin, bufsize - tokenBegin) + |
423 | new String(buffer, 0, bufpos + 1); |
424 | } |
425 | |
426 | /** Get the suffix. */ |
427 | public char[] GetSuffix(int len) |
428 | { |
429 | char[] ret = new char[len]; |
430 | |
431 | if ((bufpos + 1) >= len) |
432 | System.arraycopy(buffer, bufpos - len + 1, ret, 0, len); |
433 | else |
434 | { |
435 | System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0, |
436 | len - bufpos - 1); |
437 | System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1); |
438 | } |
439 | |
440 | return ret; |
441 | } |
442 | |
443 | /** Reset buffer when finished. */ |
444 | public void Done() |
445 | { |
446 | buffer = null; |
447 | bufline = null; |
448 | bufcolumn = null; |
449 | } |
450 | |
451 | /** |
452 | * Method to adjust line and column numbers for the start of a token. |
453 | */ |
454 | public void adjustBeginLineColumn(int newLine, int newCol) |
455 | { |
456 | int start = tokenBegin; |
457 | int len; |
458 | |
459 | if (bufpos >= tokenBegin) |
460 | { |
461 | len = bufpos - tokenBegin + inBuf + 1; |
462 | } |
463 | else |
464 | { |
465 | len = bufsize - tokenBegin + bufpos + 1 + inBuf; |
466 | } |
467 | |
468 | int i = 0, j = 0, k = 0; |
469 | int nextColDiff = 0, columnDiff = 0; |
470 | |
471 | while (i < len && bufline[j = start % bufsize] == bufline[k = ++start % bufsize]) |
472 | { |
473 | bufline[j] = newLine; |
474 | nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j]; |
475 | bufcolumn[j] = newCol + columnDiff; |
476 | columnDiff = nextColDiff; |
477 | i++; |
478 | } |
479 | |
480 | if (i < len) |
481 | { |
482 | bufline[j] = newLine++; |
483 | bufcolumn[j] = newCol + columnDiff; |
484 | |
485 | while (i++ < len) |
486 | { |
487 | if (bufline[j = start % bufsize] != bufline[++start % bufsize]) |
488 | bufline[j] = newLine++; |
489 | else |
490 | bufline[j] = newLine; |
491 | } |
492 | } |
493 | |
494 | line = bufline[j]; |
495 | column = bufcolumn[j]; |
496 | } |
497 | |
498 | } |
499 | /* JavaCC - OriginalChecksum=7b6663c1e6a32a3c479a2844bb509874 (do not edit this line) */ |