/
ScannerImpl.java
executable file
·572 lines (529 loc) · 18.1 KB
/
ScannerImpl.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
// (c) 2012 B Smith-Mannschott -- Distributed under the Eclipse Public License
package us.bpsm.edn.parser;
import static us.bpsm.edn.Symbol.newSymbol;
import static us.bpsm.edn.Tag.newTag;
import static us.bpsm.edn.parser.Parser.Config.BIG_DECIMAL_TAG;
import static us.bpsm.edn.parser.Parser.Config.BIG_INTEGER_TAG;
import static us.bpsm.edn.parser.Parser.Config.DOUBLE_TAG;
import static us.bpsm.edn.parser.Parser.Config.LONG_TAG;
import static us.bpsm.edn.util.CharClassify.*;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import us.bpsm.edn.EdnSyntaxException;
import us.bpsm.edn.EdnIOException;
import us.bpsm.edn.Keyword;
import us.bpsm.edn.Symbol;
import us.bpsm.edn.util.CharClassify;
class ScannerImpl implements Scanner {
static final Symbol NIL_SYMBOL = newSymbol("nil");
static final Symbol TRUE_SYMBOL = newSymbol("true");
static final Symbol FALSE_SYMBOL = newSymbol("false");
static final Symbol SLASH_SYMBOL = newSymbol("/");
static final int END = -1;
private final TagHandler longHandler;
private final TagHandler bigDecimalHandler;
private final TagHandler bigIntegerHandler;
private final TagHandler doubleHandler;
private final boolean unicodeEscapesInStringLiteralsAreAccepted;
/**
* Scanner may throw an IOException during construction, in which case
* an attempt will be made to close Reader cleanly.
* @param cfg this scanner's configuration, never null.
*/
ScannerImpl(Parser.Config cfg) {
if (cfg == null) {
throw new IllegalArgumentException("cfg must not be null");
}
this.longHandler = cfg.getTagHandler(LONG_TAG);
this.bigIntegerHandler = cfg.getTagHandler(BIG_INTEGER_TAG);
this.doubleHandler = cfg.getTagHandler(DOUBLE_TAG);
this.bigDecimalHandler = cfg.getTagHandler(BIG_DECIMAL_TAG);
this.unicodeEscapesInStringLiteralsAreAccepted =
cfg.unicodeEscapesInStringLiteralsAreAccepted();
}
/* (non-Javadoc)
* @see us.bpsm.edn.parser.ScannerIf#nextToken(us.bpsm.edn.parser.Parseable)
*/
public Object nextToken(Parseable pbr) {
try {
return scanNextToken(pbr);
} catch (IOException e) {
throw new EdnIOException(e);
}
}
private Object scanNextToken(Parseable pbr) throws IOException {
skipWhitespaceAndComments(pbr);
int curr = pbr.read();
switch(curr) {
case END:
return Token.END_OF_INPUT;
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
return readSymbol(curr, pbr);
case 'f':
return readSymbolOrFalse(curr, pbr);
case 'g':
case 'h':
case 'i':
case 'j':
case 'k':
case 'l':
case 'm':
return readSymbol(curr, pbr);
case 'n':
return readSymbolOrNil(curr, pbr);
case 'o':
case 'p':
case 'q':
case 'r':
case 's':
return readSymbol(curr, pbr);
case 't':
return readSymbolOrTrue(curr, pbr);
case 'u':
case 'v':
case 'w':
case 'x':
case 'y':
case 'z':
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
case 'G':
case 'H':
case 'I':
case 'J':
case 'K':
case 'L':
case 'M':
case 'N':
case 'O':
case 'P':
case 'Q':
case 'R':
case 'S':
case 'T':
case 'U':
case 'V':
case 'W':
case 'X':
case 'Y':
case 'Z':
case '*':
case '!':
case '_':
case '?':
case '/':
case '$':
case '%':
case '&':
case '>':
case '<':
case '=':
return readSymbol(curr, pbr);
case '.':
return readSymbol(curr, pbr);
case '+':
case '-':
return readSymbolOrNumber(curr, pbr);
case ':':
return readKeyword(pbr);
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
return readNumber(curr, pbr);
case '{':
return Token.BEGIN_MAP;
case '}':
return Token.END_MAP_OR_SET;
case '[':
return Token.BEGIN_VECTOR;
case ']':
return Token.END_VECTOR;
case '(':
return Token.BEGIN_LIST;
case ')':
return Token.END_LIST;
case '#':
return readHashDispatched(pbr);
case '"':
return readStringLiteral(pbr);
case '\\':
return readCharacterLiteral(pbr);
default:
throw new EdnSyntaxException(
String.format("Unexpected character '%c', \\"+"u%04x",
(char)curr, curr));
}
}
private Object readHashDispatched(Parseable pbr) throws IOException {
int peek = pbr.read();
switch(peek) {
case END:
throw new EdnSyntaxException("Unexpected end of input following '#'");
case '{':
return Token.BEGIN_SET;
case '_':
return Token.DISCARD;
case ':':
return Token.DEFAULT_NAMESPACE_FOLLOWS;
default:
return newTag(readSymbol(peek, pbr));
}
}
private Object readSymbolOrNumber(int curr, Parseable pbr)
throws IOException {
int peek = pbr.read();
if (peek == END) {
return readSymbol(curr, pbr);
} else {
unread(pbr, peek);
if (isDigit((char)peek)) {
return readNumber(curr, pbr);
} else {
return readSymbol(curr, pbr);
}
}
}
private static Parseable unread(Parseable pbr, int ch) throws IOException {
pbr.unread(ch);
return pbr;
}
private Object readSymbolOrTrue(int curr, Parseable pbr)
throws IOException {
Symbol sym = readSymbol(curr, pbr);
return TRUE_SYMBOL.equals(sym) ? true : sym;
}
private Object readSymbolOrNil(int curr, Parseable pbr)
throws IOException {
Symbol sym = readSymbol(curr, pbr);
return NIL_SYMBOL.equals(sym) ? Token.NIL : sym;
}
private Object readSymbolOrFalse(int curr, Parseable pbr)
throws IOException {
Symbol sym = readSymbol(curr, pbr);
return FALSE_SYMBOL.equals(sym) ? false : sym;
}
private void skipWhitespaceAndComments(Parseable pbr) throws IOException {
for (;;) {
skipWhitespace(pbr);
int curr = pbr.read();
if (curr != ';') {
unread(pbr, curr);
break;
}
skipComment(pbr);
}
}
private void skipWhitespace(Parseable pbr) throws IOException {
int curr;
do {
curr = pbr.read();
} while (curr != END && isWhitespace((char)curr));
unread(pbr, curr);
}
private void skipComment(Parseable pbr) throws IOException {
int curr;
do {
curr = pbr.read();
} while (curr != END && curr != '\n' && curr != '\r');
unread(pbr, curr);
}
private char readCharacterLiteral(Parseable pbr) throws IOException {
int curr = pbr.read();
if (curr == END) {
throw new EdnSyntaxException(
"Unexpected end of input following '\'");
} else if (isWhitespace((char)curr) && curr != ',') {
throw new EdnSyntaxException(
"A backslash introducing character literal must not be "+
"immediately followed by whitespace.");
}
StringBuilder b = new StringBuilder();
do {
b.append((char)curr);
curr = pbr.read();
} while (curr != END && !separatesTokens((char)curr));
unread(pbr, curr);
if (b.length() == 1) {
return b.charAt(0);
} else {
return charForName(b.toString());
}
}
private static char charForName(String name) {
switch (name.charAt(0)) {
case 'u':
if (name.length() == 5) {
try {
int n = Integer.parseInt(name.substring(1), 16);
if (Character.MIN_VALUE <= n && n <= Character.MAX_VALUE) {
return (char) n;
}
// Fall through to default, which throws EdnSyntaxException
} catch (NumberFormatException e) {
// Fall through to default, which throws EdnSyntaxException
}
}
case 'n':
if ("newline".equals(name)) {
return '\n';
}
// fall through
case 's':
if ("space".equals(name)) {
return ' ';
}
// fall through
case 't':
if ("tab".equals(name)) {
return '\t';
}
// fall through
case 'b':
if ("backspace".equals(name)) {
return '\b';
}
// fall through
case 'f':
if ("formfeed".equals(name)) {
return '\f';
}
// fall through
case 'r':
if ("return".equals(name)) {
return '\r';
}
// fall through
default:
throw new EdnSyntaxException(
"The character \\"+ name +" was not recognized.");
}
}
private String readStringLiteral(Parseable pbr) throws IOException {
StringBuffer b = new StringBuffer();
for (;;) {
int curr = pbr.read();
switch (curr) {
case END:
throw new EdnSyntaxException(
"Unexpected end of input in string literal");
case '"':
return b.toString();
case '\\':
curr = pbr.read();
switch (curr) {
case END:
throw new EdnSyntaxException(
"Unexpected end of input in string literal");
case 'b':
b.append('\b');
break;
case 't':
b.append('\t');
break;
case 'n':
b.append('\n');
break;
case 'f':
b.append('\f');
break;
case 'r':
b.append('\r');
break;
case '"':
b.append('"');
break;
case '\'':
b.append('\'');
break;
case '\\':
b.append('\\');
break;
case 'u':
if (!unicodeEscapesInStringLiteralsAreAccepted) {
throw new EdnSyntaxException(
"Unsupported '" + ((char) curr)
+ "' escape in string. "
+ "(Unicode escapes disabled by Parser.Config)"
);
}
/*
2020-05-01 Support for reading unicode escapes within
string literals is an extension to EDN. It is not part of
the spec described here: https://github.com/edn-format/edn
*/
int v = 0;
for (int i = 0; i < 4; i++) {
curr = pbr.read();
int d = Character.digit(curr, 16);
if (d == -1) {
throw new EdnSyntaxException(
"Invalid \\u Unicode escape in string.");
}
v = v * 16 + d;
}
b.append((char)v);
break;
default:
throw new EdnSyntaxException("Unsupported '"+ ((char)curr)
+"' escape in string");
}
break;
default:
b.append((char)curr);
}
}
}
private Object readNumber(int curr, Parseable pbr) throws IOException {
assert curr != END && CharClassify.startsNumber((char)curr);
StringBuffer digits = new StringBuffer();
if (curr != '+') {
digits.append((char)curr);
}
curr = pbr.read();
while (curr != END && isDigit((char)curr)) {
digits.append((char)curr);
curr = pbr.read();
}
if (curr == '.' || curr == 'e' || curr == 'E' || curr == 'M') {
if (curr == '.') {
do {
digits.append((char)curr);
curr = pbr.read();
} while (curr != END && isDigit((char) curr));
}
if (curr == 'e' || curr == 'E') {
digits.append((char)curr);
curr = pbr.read();
if (curr == END) {
throw new EdnSyntaxException(
"Unexpected end of input in numeric literal");
}
if (!(curr == '-' || curr == '+' || isDigit((char)curr))) {
throw new EdnSyntaxException(
"Not a number: '"+ digits + ((char)curr) +"'.");
}
do {
digits.append((char)curr);
curr = pbr.read();
} while (curr != END && isDigit((char)curr));
}
final boolean decimal = (curr == 'M');
if (decimal) {
curr = pbr.read();
}
if (curr != END && !separatesTokens((char)curr)) {
throw new EdnSyntaxException(
"Not a number: '"+ digits + ((char)curr) +"'.");
}
unread(pbr, curr);
if (decimal) {
BigDecimal d = new BigDecimal(digits.toString());
return bigDecimalHandler.transform(BIG_DECIMAL_TAG, d);
} else {
double d = Double.parseDouble(digits.toString());
return doubleHandler.transform(DOUBLE_TAG, d);
}
} else {
final boolean bigint = (curr == 'N');
if (bigint) {
curr = pbr.read();
}
if (curr != END && !separatesTokens((char)curr)) {
throw new EdnSyntaxException(
"Not a number: '"+ digits + ((char)curr) +"'.");
}
unread(pbr, curr);
final BigInteger n = new BigInteger(digits.toString());
if (bigint || MIN_LONG.compareTo(n) > 0 || n.compareTo(MAX_LONG) > 0) {
return bigIntegerHandler.transform(BIG_INTEGER_TAG, n);
} else {
return longHandler.transform(LONG_TAG, n.longValue());
}
}
}
private Keyword readKeyword(Parseable pbr) throws IOException {
Symbol sym = readSymbol(pbr);
if (SLASH_SYMBOL.equals(sym)) {
throw new EdnSyntaxException("':/' is not a valid keyword.");
}
return Keyword.newKeyword(sym);
}
private Symbol readSymbol(Parseable pbr) throws IOException {
return readSymbol(pbr.read(), pbr);
}
private Symbol readSymbol(int curr, Parseable pbr) throws IOException {
if (curr == END) {
throw new EdnSyntaxException(
"Unexpected end of input while reading an identifier");
}
StringBuilder b = new StringBuilder();
int n = 0;
int p = Integer.MIN_VALUE;
do {
if (curr == '/') {
n += 1;
p = b.length();
}
b.append((char)curr);
curr = pbr.read();
} while (curr != END && symbolConstituent((char)curr));
unread(pbr, curr);
validateUseOfSlash(b, n, p);
return makeSymbol(b, n, p);
}
private Symbol makeSymbol(StringBuilder b, int slashCount, int slashPos) {
if (slashCount == 0) {
return newSymbol(b.toString());
} else if (slashCount == 1) {
if (slashPos == 0) {
assert b.length() == 1 && b.charAt(0) == '/';
return newSymbol(b.toString());
} else {
return newSymbol(b.substring(0, slashPos), b.substring(slashPos+1));
}
} else {
assert slashCount == 2 && slashPos == b.length() - 1 && b.charAt(b.length() - 2) == '/';
return newSymbol(b.substring(0, slashPos - 1), "/");
}
}
private void validateUseOfSlash(CharSequence s, int slashCount, int lastSlashPos) {
assert s.length() > 0;
if (slashCount == 1) {
if (s.length() != 1) {
if (lastSlashPos == s.length() - 1) {
throw new EdnSyntaxException(
"The name '"+ s +"' must not end with '/'.");
} else if (lastSlashPos == 0) {
throw new EdnSyntaxException(
"The name '"+ s +"' must not start with '/'.");
}
}
} else if (slashCount == 2) {
if (s.length() == 2) {
throw new EdnSyntaxException("The name '//' is not valid.");
} else if (lastSlashPos != s.length() - 1 || s.charAt(lastSlashPos - 1) != '/') {
throw new EdnSyntaxException("Incorrect use of '/' in name.");
}
} else if (slashCount > 3) {
throw new EdnSyntaxException("Too many '/' in name.");
}
}
private static final BigInteger MIN_LONG = BigInteger.valueOf(Long.MIN_VALUE);
private static final BigInteger MAX_LONG = BigInteger.valueOf(Long.MAX_VALUE);
}