forked from openjdk/amber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnicodeReader.java
750 lines (655 loc) · 21.2 KB
/
UnicodeReader.java
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
/*
* Copyright (c) 2011, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.tools.javac.parser;
import java.util.Arrays;
import com.sun.tools.javac.resources.CompilerProperties.Errors;
import com.sun.tools.javac.util.Log;
import static com.sun.tools.javac.util.LayoutCharacters.EOI;
import static com.sun.tools.javac.util.LayoutCharacters.tabulate;
/**
* The unicode character reader used by the javac/javadoc lexer/tokenizer, returns characters
* one by one as contained in the input stream, handling unicode escape sequences accordingly.
*
* <p><b>This is NOT part of any supported API.
* If you write code that depends on this, you do so at your own risk.
* This code and its internal interfaces are subject to change or
* deletion without notice.</b></p>
*/
public class UnicodeReader {
/**
* Buffer containing characters from source file. May contain extraneous characters
* beyond this.length.
*/
private final char[] buffer;
/**
* Length of meaningful content in buffer.
*/
private final int length;
/**
* Virtual position offset in the original buffer.
*/
private final int offset;
/**
* Character buffer index of character currently being observed.
*/
private int position;
/**
* Number of characters combined to provide character currently being observed. Typically
* one, but may be more when combinations of surrogate pairs and unicode escape sequences
* are read.
*/
private int width;
/**
* Character currently being observed. If a surrogate pair is read then will be the high
* member of the pair.
*/
private char character;
/**
* Codepoint of character currently being observed. Typically equivalent to the character
* but will have a value greater that 0xFFFF when a surrogate pair.
*/
private int codepoint;
/**
* true if the last character was a backslash. This is used to handle the special case
* when a backslash precedes an unicode escape. In that case, the second backslash
* is treated as a backslash and not part of an unicode escape.
*/
private boolean wasBackslash;
/**
* true if the last character was derived from an unicode escape sequence.
*/
private boolean wasUnicodeEscape;
/**
* Log for error reporting.
*/
private final Log log;
/**
* Constructor.
*
* @param sf scan factory.
* @param array array containing contents of source.
* @param length length of meaningful content in buffer.
*/
@SuppressWarnings("this-escape")
protected UnicodeReader(ScannerFactory sf, char[] array, int length) {
this(sf.log, array, length);
}
/**
* Constructor.
*
* @param log Log for error reporting.
* @param array array containing contents of source.
* @param length length of meaningful content in buffer.
*/
protected UnicodeReader(Log log, char[] array, int length) {
this(log, array, 0, 0, length);
}
/**
* Constructor.
*
* @param log Log for error reporting.
* @param array array containing contents of source.
* @param pos start of meaningful content in buffer.
* @param endPos end of meaningful content in buffer.
*/
@SuppressWarnings("this-escape")
protected UnicodeReader(Log log, char[] array, int offset, int pos, int endPos) {
this.buffer = array;
this.length = endPos;
this.offset = offset;
this.position = pos;
this.width = 0;
this.character = '\0';
this.codepoint = 0;
this.wasBackslash = false;
this.wasUnicodeEscape = false;
this.log = log;
nextCodePoint();
}
/**
* Returns the character buffer.
*
* @return character buffer.
*/
protected char[] buffer() {
return buffer;
}
/**
* Returns the length of the buffer. This is length of meaningful content in buffer and
* not the length of the buffer array.
*
* @return length of the buffer.
*/
protected int length() {
return length;
}
/**
* Return true if current position is within the meaningful part of the buffer.
*
* @return true if current position is within the meaningful part of the buffer.
*/
protected boolean isAvailable() {
return position < length;
}
/**
* Fetches the next 16-bit character from the buffer and places it in this.character.
*/
private void nextCodeUnit() {
// Index of next character in buffer.
int index = position + width;
// If past end of buffer.
if (length <= index) {
// End of file is marked with EOI.
character = EOI;
} else {
// Next character in buffer.
character = buffer[index];
// Increment length of codepoint.
width++;
}
}
/**
* Fetches the next 16-bit character from the buffer. If an unicode escape
* is detected then converts the unicode escape to a character.
*/
private void nextUnicodeInputCharacter() {
// Position to next codepoint.
position += width;
// Codepoint has no characters yet.
width = 0;
// Fetch next character.
nextCodeUnit();
if (character == '\\' && (!wasBackslash || wasUnicodeEscape)) {
// Is a backslash and may be an unicode escape.
switch (unicodeEscape()) {
case BACKSLASH -> {
wasUnicodeEscape = false;
wasBackslash = !wasBackslash;
}
case VALID_ESCAPE -> {
wasUnicodeEscape = true;
wasBackslash = character == '\\' && !wasBackslash;
}
case BROKEN_ESCAPE -> nextUnicodeInputCharacter(); //skip broken unicode escapes
}
} else {
wasBackslash = false;
wasUnicodeEscape = false;
}
// Codepoint and character match if not surrogate.
codepoint = (int)character;
}
/**
* Fetches the nextcode point from the buffer. If an unicode escape is recognized
* then converts unicode escape to a character. If two characters are a surrogate pair
* then converts to a codepoint.
*/
private void nextCodePoint() {
// Next unicode character.
nextUnicodeInputCharacter();
// Return early if ASCII or not a surrogate pair.
if (isASCII() || !Character.isHighSurrogate(character)) {
return;
}
// Capture high surrogate and position.
char hi = character;
int savePosition = position;
int saveWidth = width;
// Get potential low surrogate.
nextUnicodeInputCharacter();
char lo = character;
if (Character.isLowSurrogate(lo)) {
// Start codepoint at start of high surrogate.
position = savePosition;
width += saveWidth;
// Compute codepoint.
codepoint = Character.toCodePoint(hi, lo);
} else {
// Restore to treat high surrogate as just a character.
position = savePosition;
width = saveWidth;
character = hi;
codepoint = (int)hi;
// Could potential report an error here (old code did not.)
}
}
/**
* Converts an unicode escape into a character.
*
* @return true if was an unicode escape.
*/
private UnicodeEscapeResult unicodeEscape() {
// Start of unicode escape (past backslash.)
int start = position + width;
// Default to backslash result, unless proven otherwise.
character = '\\';
width = 1;
// Skip multiple 'u'.
int index;
for (index = start; index < length; index++) {
if (buffer[index] != 'u') {
break;
}
}
// Needs to have been at least one u.
if (index == start) {
return UnicodeEscapeResult.BACKSLASH;
}
int code = 0;
for (int i = 0; i < 4; i++) {
// Translate and merge digit.
int digit = index < length ? Character.digit(buffer[index], 16) : -1;
code = code << 4 | digit;
// If invalid digit.
if (code < 0) {
break;
}
// On to next character.
index++;
}
// Skip digits even if error.
width = index - position;
// If all digits are good.
if (code >= 0) {
character = (char)code;
return UnicodeEscapeResult.VALID_ESCAPE;
} else {
log.error(index, Errors.IllegalUnicodeEsc);
return UnicodeEscapeResult.BROKEN_ESCAPE;
}
}
private enum UnicodeEscapeResult {
BACKSLASH,
VALID_ESCAPE,
BROKEN_ESCAPE;
}
/**
* Return the virtual position in the character buffer.
*
* @return virtual position in the character buffer.
*/
protected int position() {
return offset + position;
}
/**
* Reset the reader to the specified virtual position.
* Warning: Do not use when previous character was an ASCII or unicode backslash.
* @param pos
*/
protected void reset(int pos) {
position = pos - offset;
width = 0;
wasBackslash = false;
wasUnicodeEscape = false;
nextCodePoint();
}
/**
* Return the current character in at the current position.
*
* @return current character in at the current position.
*/
protected char get() {
return character;
}
/**
* Return the current codepoint in at the current position.
*
* @return current codepoint in at the current position.
*/
protected int getCodepoint() {
return codepoint;
}
/**
* Returns true if the current codepoint is a surrogate.
*
* @return true if the current codepoint is a surrogate.
*/
protected boolean isSurrogate() {
return 0xFFFF < codepoint;
}
/**
* Returns true if the current character is ASCII.
*
* @return true if the current character is ASCII.
*/
protected boolean isASCII() {
return character <= 0x7F;
}
/**
* Advances the current character to the next character.
*
* @return next character.
*/
protected char next() {
nextCodePoint();
return character;
}
/**
* Compare character. Returns true if a match.
*
* @param ch character to match.
*
* @return true if a match.
*/
protected boolean is(char ch) {
return character == ch;
}
/**
* Match one of the arguments. Returns true if a match.
*/
protected boolean isOneOf(char ch1, char ch2) {
return is(ch1) || is(ch2);
}
protected boolean isOneOf(char ch1, char ch2, char ch3) {
return is(ch1) || is(ch2) || is(ch3);
}
protected boolean isOneOf(char ch1, char ch2, char ch3, char ch4) {
return is(ch1) || is(ch2) || is(ch3) || is(ch4);
}
protected boolean isOneOf(char ch1, char ch2, char ch3, char ch4, char ch5, char ch6) {
return is(ch1) || is(ch2) || is(ch3) || is(ch4) || is(ch5) || is(ch6);
}
/**
* Tests to see if current character is in the range of lo to hi characters (inclusive).
*
* @param lo lowest character in range.
* @param hi highest character in range.
*
* @return true if the current character is in range.
*/
protected boolean inRange(char lo, char hi) {
return lo <= character && character <= hi;
}
/**
* Compare character and advance if a match. Returns true if a match.
*
* @param ch character to match.
*
* @return true if a match.
*/
protected boolean accept(char ch) {
if (is(ch)) {
next();
return true;
}
return false;
}
/**
* Match one of the arguments and advance if a match. Returns true if a match.
*/
protected boolean acceptOneOf(char ch1, char ch2) {
if (isOneOf(ch1, ch2)) {
next();
return true;
}
return false;
}
/**
* Match one of the arguments and advance if a match. Returns true if a match.
*/
protected boolean acceptOneOf(char ch1, char ch2, char ch3) {
if (isOneOf(ch1, ch2, ch3)) {
next();
return true;
}
return false;
}
/**
* Return a reader which is bracketed by the currect position
* and the next line terminator.
*
* @return a new reader
*/
protected UnicodeReader lineReader() {
int pos = position;
skipToEOLN();
int endPos = position;
accept('\r');
accept('\n');
return new UnicodeReader(log, buffer, offset, pos, endPos);
}
/**
* Return a reader which is bracketed by the {@code pos}
* and {@code endPos}.
*
* @param pos initial position
* @param endPos end position
*
* @return a new reader
*/
protected UnicodeReader lineReader(int pos, int endPos) {
return new UnicodeReader(log, buffer, offset, pos - offset, endPos - offset);
}
/**
* Skip over all occurrences of character.
*
* @param ch character to accept.
*
* @return number of characters skipped
*/
protected int skip(char ch) {
int count = 0;
while (accept(ch)) {
count++;
}
return count;
}
/**
* Is ASCII white space character.
*
* @return true if is ASCII white space character
*/
protected boolean isWhitespace() {
return isOneOf(' ', '\t', '\f');
}
/**
* Skip over ASCII white space characters.
*/
protected void skipWhitespace() {
while (acceptOneOf(' ', '\t', '\f')) {
// next
}
}
/**
* Is ASCII line terminator.
*
* @return true if is ASCII line terminator.
*/
protected boolean isEOLN() {
return isOneOf('\r', '\n');
}
/**
* Skip to end of line.
*/
protected void skipToEOLN() {
while (isAvailable()) {
if (isEOLN()) {
break;
}
next();
}
}
/**
* Compare string and advance if a match. Returns true if a match.
* Warning: Do not use when previous character was a backslash
* (confuses state of wasBackslash.)
*
* @param string string to match character for character.
*
* @return true if a match.
*/
protected boolean accept(String string) {
// Quick test.
if (string.length() == 0 || !is(string.charAt(0))) {
return false;
}
// Be prepared to retreat if not a match.
int savedPosition = position();
nextCodePoint();
// Check each character.
for (int i = 1; i < string.length(); i++) {
if (!is(string.charAt(i))) {
// Restart if not a match.
reset(savedPosition);
return false;
}
nextCodePoint();
}
return true;
}
/**
* Convert an ASCII digit from its base (8, 10, or 16) to its value. Does not
* advance character.
*
* @param pos starting position.
* @param digitRadix base of number being converted.
*
* @return value of digit.
*/
protected int digit(int pos, int digitRadix) {
int result;
// Just an ASCII digit.
if (inRange('0', '9')) {
// Fast common case.
result = character - '0';
return result < digitRadix ? result : -1;
}
// Handle other digits.
result = isSurrogate() ? Character.digit(codepoint, digitRadix) :
Character.digit(character, digitRadix);
if (result >= 0 && !isASCII()) {
log.error(position(), Errors.IllegalNonasciiDigit);
character = "0123456789abcdef".charAt(result);
}
return result;
}
/**
* Returns the input buffer. Unicode escape sequences are not translated.
*
* @return the input buffer.
*/
public char[] getRawCharacters() {
return length == buffer.length ? buffer : Arrays.copyOf(buffer, length);
}
/**
* Returns a copy of a character array subset of the input buffer.
* The returned array begins at the {@code beginIndex} and
* extends to the character at index {@code endIndex - 1}.
* Thus the length of the substring is {@code endIndex-beginIndex}.
* This behavior is like
* {@code String.substring(beginIndex, endIndex)}.
* Unicode escape sequences are not translated.
*
* @param beginIndex the beginning index, inclusive.
* @param endIndex the ending index, exclusive.
*
* @throws ArrayIndexOutOfBoundsException if either offset is outside of the
* array bounds
*/
public char[] getRawCharacters(int beginIndex, int endIndex) {
return Arrays.copyOfRange(buffer, beginIndex, endIndex);
}
/**
* Returns a string subset of the input buffer.
* The returned string begins at the {@code beginIndex} and
* extends to the character at index {@code endIndex - 1}.
* Thus the length of the substring is {@code endIndex-beginIndex}.
* This behavior is like
* {@code String.substring(beginIndex, endIndex)}.
* Unicode escape sequences are not translated.
*
* @param beginIndex the beginning index, inclusive.
* @param endIndex the ending index, exclusive.
*
* @throws ArrayIndexOutOfBoundsException if either offset is outside of the
* array bounds
*/
public String getRawString(int beginIndex, int endIndex) {
return new String(buffer, beginIndex, endIndex - beginIndex);
}
/**
* Returns a string subset of the input buffer.
* The returned string begins at the {@code position} and
* extends to the character at index {@code length - 1}.
* Thus the length of the substring is {@code length-position}.
* This behavior is like
* {@code String.substring(position, length)}.
* Unicode escape sequences are not translated.
*
* @throws ArrayIndexOutOfBoundsException if either offset is outside of the
* array bounds
*/
public String getRawString() {
return getRawString(position, length);
}
/**
* This is a specialized version of UnicodeReader that keeps track of the
* column position within a given character stream. Used for Javadoc
* processing to build a table for mapping positions in the comment string
* to positions in the source file.
*/
static class PositionTrackingReader extends UnicodeReader {
/**
* Current column in the comment.
*/
private int column;
/**
* Constructor.
*
* @param reader existing reader
* @param pos start of meaningful content in buffer.
* @param endPos end of meaningful content in buffer.
*/
protected PositionTrackingReader(UnicodeReader reader, int pos, int endPos) {
super(reader.log, reader.getRawCharacters(pos, endPos), reader.offset + pos, 0, endPos - pos);
this.column = 0;
}
/**
* Advances the current character to the next character. Tracks column.
*
* @return next character.
*/
@Override
protected char next() {
super.next();
if (isOneOf('\n', '\r', '\f')) {
column = 0;
} else if (is('\t')) {
column = tabulate(column);
} else {
column++;
}
return get();
}
/**
* Returns the current column.
*
* @return the current column.
*/
protected int column() {
return column;
}
}
}