Debian Codegraph @

mod/rng-parse/src/main/com/thaiopensource/relaxng/parse/compact/JavaCharStream.java

raw
1 /* Generated By:JavaCC: Do not edit this line. UCode_UCodeESC_CharStream.java Version 0.7pre6 */
2 /* The previous line keeps JavaCC quiet. In fact, the JavaCC generated file
3 has been edited to fix some bugs. */
4 package com.thaiopensource.relaxng.parse.compact;
5
6 import com.thaiopensource.util.Utf16;
7 import com.thaiopensource.relaxng.parse.BuildException;
8
9 import java.io.IOException;
10
11 /**
12 * An implementation of interface CharStream, where the stream is assumed to
13 * contain 16-bit unicode characters.
14 */
15 public final class JavaCharStream {
16 public static final boolean staticFlag = false;
17
18 static final int hexval(char c) {
19 switch (c) {
20 case '0':
21 return 0;
22 case '1':
23 return 1;
24 case '2':
25 return 2;
26 case '3':
27 return 3;
28 case '4':
29 return 4;
30 case '5':
31 return 5;
32 case '6':
33 return 6;
34 case '7':
35 return 7;
36 case '8':
37 return 8;
38 case '9':
39 return 9;
40
41 case 'a':
42 case 'A':
43 return 10;
44 case 'b':
45 case 'B':
46 return 11;
47 case 'c':
48 case 'C':
49 return 12;
50 case 'd':
51 case 'D':
52 return 13;
53 case 'e':
54 case 'E':
55 return 14;
56 case 'f':
57 case 'F':
58 return 15;
59 }
60 return -1;
61 }
62
63 public int bufpos = -1;
64 int bufsize;
65 int available;
66 int tokenBegin;
67 private int bufline[];
68 private int bufcolumn[];
69
70 private int column = 0;
71 private int line = 1;
72
73 private java.io.Reader inputStream;
74 private boolean closed = false;
75
76 private boolean prevCharIsLF = false;
77
78 private char[] nextCharBuf;
79 private char[] buffer;
80 private int maxNextCharInd = 0;
81 private int nextCharInd = -1;
82 private int inBuf = 0;
83 private int tabSize = 8;
84
85 protected void setTabSize(int i) { tabSize = i; }
86 protected int getTabSize(int i) { return tabSize; }
87
88 private final void ExpandBuff(boolean wrapAround) {
89 char[] newbuffer = new char[bufsize + 2048];
90 int newbufline[] = new int[bufsize + 2048];
91 int newbufcolumn[] = new int[bufsize + 2048];
92
93 if (wrapAround) {
94 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
95 System.arraycopy(buffer, 0, newbuffer,
96 bufsize - tokenBegin, bufpos);
97 buffer = newbuffer;
98
99 System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
100 System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);
101 bufline = newbufline;
102
103 System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
104 System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);
105 bufcolumn = newbufcolumn;
106
107 bufpos += (bufsize - tokenBegin);
108 }
109 else {
110 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
111 buffer = newbuffer;
112
113 System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
114 bufline = newbufline;
115
116 System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
117 bufcolumn = newbufcolumn;
118
119 bufpos -= tokenBegin;
120 }
121
122 available = (bufsize += 2048);
123 tokenBegin = 0;
124 }
125
126 private final void FillBuff() throws EOFException {
127 int i;
128 if (maxNextCharInd == 4096)
129 maxNextCharInd = nextCharInd = 0;
130
131 if (closed)
132 throw new EOFException();
133 try {
134 if ((i = inputStream.read(nextCharBuf, maxNextCharInd, 4096 - maxNextCharInd)) == -1) {
135 closed = true;
136 inputStream.close();
137 throw new EOFException();
138 }
139 else
140 maxNextCharInd += i;
141 }
142 catch (IOException e) {
143 throw new BuildException(e);
144 }
145 }
146
147 private final char ReadChar() throws EOFException {
148 if (++nextCharInd >= maxNextCharInd)
149 FillBuff();
150
151 return nextCharBuf[nextCharInd];
152 }
153
154 private final char PeekChar() throws EOFException {
155 char c = ReadChar();
156 --nextCharInd;
157 return c;
158 }
159
160 public final char BeginToken() throws EOFException {
161 if (inBuf > 0) {
162 --inBuf;
163 if (++bufpos == bufsize)
164 bufpos = 0;
165
166 tokenBegin = bufpos;
167 return buffer[bufpos];
168 }
169
170 tokenBegin = 0;
171 bufpos = -1;
172
173 return readChar();
174 }
175
176 private final void AdjustBuffSize() {
177 if (available == bufsize) {
178 if (tokenBegin > 2048) {
179 bufpos = 0;
180 available = tokenBegin;
181 }
182 else
183 ExpandBuff(false);
184 }
185 else if (available > tokenBegin)
186 available = bufsize;
187 else if ((tokenBegin - available) < 2048)
188 ExpandBuff(true);
189 else
190 available = tokenBegin;
191 }
192
193 private final void UpdateLineColumn(char c) {
194 column++;
195
196 if (prevCharIsLF) {
197 prevCharIsLF = false;
198 line += (column = 1);
199 }
200
201 switch (c) {
202 case NEWLINE_MARKER:
203 prevCharIsLF = true;
204 break;
205 case '\t':
206 column--;
207 column += (tabSize - (column % tabSize));
208 break;
209 default :
210 break;
211 }
212
213 bufline[bufpos] = line;
214 bufcolumn[bufpos] = column;
215 }
216
217 private final char NEWLINE_MARKER = '\u0000';
218
219 public final char readChar() throws EOFException {
220 if (inBuf > 0) {
221 --inBuf;
222 if (++bufpos == bufsize)
223 bufpos = 0;
224
225 return buffer[bufpos];
226 }
227
228 char c;
229 try {
230 c = ReadChar();
231 switch (c) {
232 case '\r':
233 c = NEWLINE_MARKER;
234 try {
235 if (PeekChar() == '\n')
236 ReadChar();
237 }
238 catch (EOFException e) {
239 }
240 break;
241 case '\n':
242 c = NEWLINE_MARKER;
243 break;
244 case '\t':
245 break;
246 default:
247 if (c >= 0x20) {
248 if (Utf16.isSurrogate(c)) {
249 if (Utf16.isSurrogate2(c))
250 throw new EscapeSyntaxException("illegal_surrogate_pair", line, column + 1);
251 if (++bufpos == available)
252 AdjustBuffSize();
253 buffer[bufpos] = c;
254 // UpdateLineColumn(c);
255 try {
256 c = ReadChar();
257 }
258 catch (EOFException e) {
259 throw new EscapeSyntaxException("illegal_surrogate_pair", line, column + 1);
260 }
261 if (!Utf16.isSurrogate2(c))
262 throw new EscapeSyntaxException("illegal_surrogate_pair", line, column + 2);
263 }
264 break;
265 }
266 // fall through
267 case '\uFFFE':
268 case '\uFFFF':
269 throw new EscapeSyntaxException("illegal_char_code", line, column + 1);
270 }
271 }
272 catch (EOFException e) {
273 if (bufpos == -1) {
274 if (++bufpos == available)
275 AdjustBuffSize();
276 bufline[bufpos] = line;
277 bufcolumn[bufpos] = column;
278 }
279 throw e;
280 }
281 if (++bufpos == available)
282 AdjustBuffSize();
283 buffer[bufpos] = c;
284 UpdateLineColumn(c);
285 try {
286 if (c != '\\' || PeekChar() != 'x')
287 return c;
288 }
289 catch (EOFException e) {
290 return c;
291 }
292
293 int xCnt = 1;
294 for (;;) {
295 ReadChar();
296 if (++bufpos == available)
297 AdjustBuffSize();
298 buffer[bufpos] = 'x';
299 UpdateLineColumn('x');
300 try {
301 c = PeekChar();
302 }
303 catch (EOFException e) {
304 backup(xCnt);
305 return '\\';
306 }
307 if (c == '{') {
308 ReadChar();
309 column++;
310 // backup past the 'x's
311 bufpos -= xCnt;
312 if (bufpos < 0)
313 bufpos += bufsize;
314 break;
315 }
316 if (c != 'x') {
317 backup(xCnt);
318 return '\\';
319 }
320 xCnt++;
321 }
322 try {
323 int scalarValue = hexval(ReadChar());
324 column++;
325 if (scalarValue < 0)
326 throw new EscapeSyntaxException("illegal_hex_digit", line, column);
327 while ((c = ReadChar()) != '}') {
328 column++;
329 int n = hexval(c);
330 if (n < 0)
331 throw new EscapeSyntaxException("illegal_hex_digit", line, column);
332 scalarValue <<= 4;
333 scalarValue |= n;
334 if (scalarValue >= 0x110000)
335 throw new EscapeSyntaxException("char_code_too_big", line, column);
336 }
337 column++; // for the '}'
338 if (scalarValue <= 0xFFFF) {
339 c = (char)scalarValue;
340 switch (c) {
341 case '\n':
342 case '\r':
343 case '\t':
344 break;
345 default:
346 if (c >= 0x20 && !Utf16.isSurrogate(c))
347 break;
348 // fall through
349 case '\uFFFE':
350 case '\uFFFF':
351 throw new EscapeSyntaxException("illegal_char_code_ref", line, column);
352 }
353 buffer[bufpos] = c;
354 return c;
355 }
356 c = Utf16.surrogate1(scalarValue);
357 buffer[bufpos] = c;
358 int bufpos1 = bufpos;
359 if (++bufpos == bufsize)
360 bufpos = 0;
361 buffer[bufpos] = Utf16.surrogate2(scalarValue);
362 bufline[bufpos] = bufline[bufpos1];
363 bufcolumn[bufpos] = bufcolumn[bufpos1];
364 backup(1);
365 return c;
366 }
367 catch (EOFException e) {
368 throw new EscapeSyntaxException("incomplete_escape", line, column);
369 }
370 }
371
372 /**
373 * @deprecated
374 * @see #getEndColumn
375 */
376
377 public final int getColumn() {
378 return bufcolumn[bufpos];
379 }
380
381 /**
382 * @deprecated
383 * @see #getEndLine
384 */
385
386 public final int getLine() {
387 return bufline[bufpos];
388 }
389
390 public final int getEndColumn() {
391 return bufcolumn[bufpos];
392 }
393
394 public final int getEndLine() {
395 return bufline[bufpos];
396 }
397
398 public final int getBeginColumn() {
399 return bufcolumn[tokenBegin];
400 }
401
402 public final int getBeginLine() {
403 return bufline[tokenBegin];
404 }
405
406 public final void backup(int amount) {
407
408 inBuf += amount;
409 if ((bufpos -= amount) < 0)
410 bufpos += bufsize;
411 }
412
413 public JavaCharStream(java.io.Reader dstream,
414 int startline, int startcolumn, int buffersize) {
415 inputStream = dstream;
416 line = startline;
417 column = startcolumn - 1;
418
419 available = bufsize = buffersize;
420 buffer = new char[buffersize];
421 bufline = new int[buffersize];
422 bufcolumn = new int[buffersize];
423 nextCharBuf = new char[4096];
424 skipBOM();
425 }
426
427 public JavaCharStream(java.io.Reader dstream,
428 int startline, int startcolumn) {
429 this(dstream, startline, startcolumn, 4096);
430 }
431
432 public JavaCharStream(java.io.Reader dstream) {
433 this(dstream, 1, 1, 4096);
434 }
435
436 public void ReInit(java.io.Reader dstream,
437 int startline, int startcolumn, int buffersize) {
438 inputStream = dstream;
439 closed = false;
440 line = startline;
441 column = startcolumn - 1;
442
443 if (buffer == null || buffersize != buffer.length) {
444 available = bufsize = buffersize;
445 buffer = new char[buffersize];
446 bufline = new int[buffersize];
447 bufcolumn = new int[buffersize];
448 nextCharBuf = new char[4096];
449 }
450 prevCharIsLF = false;
451 tokenBegin = inBuf = maxNextCharInd = 0;
452 nextCharInd = bufpos = -1;
453 skipBOM();
454 }
455
456 public void ReInit(java.io.Reader dstream,
457 int startline, int startcolumn) {
458 ReInit(dstream, startline, startcolumn, 4096);
459 }
460
461 public void ReInit(java.io.Reader dstream) {
462 ReInit(dstream, 1, 1, 4096);
463 }
464
465 public JavaCharStream(java.io.InputStream dstream, String encoding,
466 int startline, int startcolumn, int buffersize)
467 throws java.io.UnsupportedEncodingException {
468 this(encoding == null
469 ? new java.io.InputStreamReader(dstream)
470 : new java.io.InputStreamReader(dstream, encoding),
471 startline, startcolumn, buffersize);
472 }
473
474 public JavaCharStream(java.io.InputStream dstream, int startline,
475 int startcolumn, int buffersize) {
476 this(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize);
477 }
478
479 public JavaCharStream(java.io.InputStream dstream, String encoding, int startline,
480 int startcolumn)
481 throws java.io.UnsupportedEncodingException {
482 this(dstream, encoding, startline, startcolumn, 4096);
483 }
484
485 public JavaCharStream(java.io.InputStream dstream, int startline,
486 int startcolumn) {
487 this(dstream, startline, startcolumn, 4096);
488 }
489
490 public JavaCharStream(java.io.InputStream dstream, String encoding)
491 throws java.io.UnsupportedEncodingException {
492 this(dstream, encoding, 1, 1, 4096);
493 }
494
495 public JavaCharStream(java.io.InputStream dstream) {
496 this(dstream, 1, 1, 4096);
497 }
498
499 public void ReInit(java.io.InputStream dstream, String encoding,
500 int startline, int startcolumn, int buffersize)
501 throws java.io.UnsupportedEncodingException {
502 ReInit(encoding == null
503 ? new java.io.InputStreamReader(dstream)
504 : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);
505 }
506
507 public void ReInit(java.io.InputStream dstream, int startline,
508 int startcolumn, int buffersize) {
509 ReInit(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize);
510 }
511
512 public void ReInit(java.io.InputStream dstream, String encoding, int startline,
513 int startcolumn)
514 throws java.io.UnsupportedEncodingException {
515 ReInit(dstream, encoding, startline, startcolumn, 4096);
516 }
517
518 public void ReInit(java.io.InputStream dstream, int startline,
519 int startcolumn) {
520 ReInit(dstream, startline, startcolumn, 4096);
521 }
522
523 public void ReInit(java.io.InputStream dstream, String encoding)
524 throws java.io.UnsupportedEncodingException {
525 ReInit(dstream, encoding, 1, 1, 4096);
526 }
527
528 public void ReInit(java.io.InputStream dstream) {
529 ReInit(dstream, 1, 1, 4096);
530 }
531
532 static private final char BOM = '\ufeff';
533
534 private void skipBOM() {
535 try {
536 if (PeekChar() == BOM)
537 ReadChar();
538 }
539 catch (EOFException e) {
540 }
541 }
542
543 public final String GetImage() {
544 if (bufpos >= tokenBegin)
545 return new String(buffer, tokenBegin, bufpos - tokenBegin + 1);
546 else
547 return new String(buffer, tokenBegin, bufsize - tokenBegin) +
548 new String(buffer, 0, bufpos + 1);
549 }
550
551 public final char[] GetSuffix(int len) {
552 char[] ret = new char[len];
553
554 if ((bufpos + 1) >= len)
555 System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
556 else {
557 System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0,
558 len - bufpos - 1);
559 System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
560 }
561
562 return ret;
563 }
564
565 public void Done() {
566 nextCharBuf = null;
567 buffer = null;
568 bufline = null;
569 bufcolumn = null;
570 }
571
572 /**
573 * Method to adjust line and column numbers for the start of a token.<BR>
574 */
575 public void adjustBeginLineColumn(int newLine, int newCol) {
576 int start = tokenBegin;
577 int len;
578
579 if (bufpos >= tokenBegin) {
580 len = bufpos - tokenBegin + inBuf + 1;
581 }
582 else {
583 len = bufsize - tokenBegin + bufpos + 1 + inBuf;
584 }
585
586 int i = 0, j = 0, k = 0;
587 int nextColDiff = 0, columnDiff = 0;
588
589 while (i < len &&
590 bufline[j = start % bufsize] == bufline[k = ++start % bufsize]) {
591 bufline[j] = newLine;
592 nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
593 bufcolumn[j] = newCol + columnDiff;
594 columnDiff = nextColDiff;
595 i++;
596 }
597
598 if (i < len) {
599 bufline[j] = newLine++;
600 bufcolumn[j] = newCol + columnDiff;
601
602 while (i++ < len) {
603 if (bufline[j = start % bufsize] != bufline[++start % bufsize])
604 bufline[j] = newLine++;
605 else
606 bufline[j] = newLine;
607 }
608 }
609
610 line = bufline[j];
611 column = bufcolumn[j];
612 }
613
614 }