1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 package org.jomc.util;
34
35
36
37
38
39
40 public class SimpleCharStream
41 {
42
43 public static final boolean staticFlag = false;
44 int bufsize;
45 int available;
46 int tokenBegin;
47
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
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
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
236
237
238
239 public int getColumn() {
240 return bufcolumn[bufpos];
241 }
242
243
244
245
246
247
248 public int getLine() {
249 return bufline[bufpos];
250 }
251
252
253 public int getEndColumn() {
254 return bufcolumn[bufpos];
255 }
256
257
258 public int getEndLine() {
259 return bufline[bufpos];
260 }
261
262
263 public int getBeginColumn() {
264 return bufcolumn[tokenBegin];
265 }
266
267
268 public int getBeginLine() {
269 return bufline[tokenBegin];
270 }
271
272
273 public void backup(int amount) {
274
275 inBuf += amount;
276 if ((bufpos -= amount) < 0)
277 bufpos += bufsize;
278 }
279
280
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
295 public SimpleCharStream(java.io.Reader dstream, int startline,
296 int startcolumn)
297 {
298 this(dstream, startline, startcolumn, 4096);
299 }
300
301
302 public SimpleCharStream(java.io.Reader dstream)
303 {
304 this(dstream, 1, 1, 4096);
305 }
306
307
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
328 public void ReInit(java.io.Reader dstream, int startline,
329 int startcolumn)
330 {
331 ReInit(dstream, startline, startcolumn, 4096);
332 }
333
334
335 public void ReInit(java.io.Reader dstream)
336 {
337 ReInit(dstream, 1, 1, 4096);
338 }
339
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
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
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
361 public SimpleCharStream(java.io.InputStream dstream, int startline,
362 int startcolumn)
363 {
364 this(dstream, startline, startcolumn, 4096);
365 }
366
367
368 public SimpleCharStream(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException
369 {
370 this(dstream, encoding, 1, 1, 4096);
371 }
372
373
374 public SimpleCharStream(java.io.InputStream dstream)
375 {
376 this(dstream, 1, 1, 4096);
377 }
378
379
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
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
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
400 public void ReInit(java.io.InputStream dstream)
401 {
402 ReInit(dstream, 1, 1, 4096);
403 }
404
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
411 public void ReInit(java.io.InputStream dstream, int startline,
412 int startcolumn)
413 {
414 ReInit(dstream, startline, startcolumn, 4096);
415 }
416
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
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
444 public void Done()
445 {
446 buffer = null;
447 bufline = null;
448 bufcolumn = null;
449 }
450
451
452
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