forked from omnixerio/ubo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUsoParser.java
More file actions
627 lines (541 loc) · 19.4 KB
/
UsoParser.java
File metadata and controls
627 lines (541 loc) · 19.4 KB
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
package dev.ultreon.ubo;
import dev.ultreon.ubo.types.*;
import java.io.EOFException;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.BitSet;
import java.util.UUID;
public class UsoParser {
private final char[] chars;
private int pos;
public UsoParser(String input) {
this.chars = input.toCharArray();
}
private DataType<?> readUso() throws IOException {
int read = read();
switch (read) {
case '[':
return readList();
case '{':
return readMap();
case '(':
return readArray();
case '<':
return readUUID();
case '"':
return readString();
case '\'':
return new CharType(readChar());
case 'x':
return readBitSet();
case 't':
case 'f':
this.unread();
return readBoolean();
default:
if (Character.isDigit(read)) return readNumber(read);
throw new IOException("Invalid USO: " + (char) read);
}
}
private DataType<?> readBoolean() throws IOException {
StringBuilder builder = new StringBuilder();
while (true) {
int r = read();
if (r == -1) break;
if (!Character.isAlphabetic(r)) break;
builder.append((char) r);
}
unread();
String string = builder.toString();
if (string.equalsIgnoreCase("true")) return new BooleanType(true);
if (string.equalsIgnoreCase("false")) return new BooleanType(false);
throw new IOException("Invalid boolean: " + string);
}
private DataType<?> readArray() throws IOException {
int read = read();
switch ((char) read) {
case 'b':
if (read() != ';') throw new IOException("Invalid array: expected ';'");
return readByteArray();
case 's':
if (read() != ';') throw new IOException("Invalid array: expected ';'");
return readShortArray();
case 'i':
if (read() != ';') throw new IOException("Invalid array: expected ';'");
return readIntArray();
case 'l':
if (read() != ';') throw new IOException("Invalid array: expected ';'");
return readLongArray();
case 'f':
if (read() != ';') throw new IOException("Invalid array: expected ';'");
return readFloatArray();
case 'd':
if (read() != ';') throw new IOException("Invalid array: expected ';'");
return readDoubleArray();
case 'B':
if (read() != ';') throw new IOException("Invalid array: expected ';'");
return readBitSet();
case 'c':
if (read() != ';') throw new IOException("Invalid array: expected ';'");
return readCharArray();
default:
throw new IOException("Invalid array");
}
}
private CharArrayType readCharArray() throws IOException {
char[] chars = new char[0];
while (true) {
int r;
if ((r = read()) == -1) {
throw new EOFException("Invalid character: EOF");
}
if (r != '\'') {
throw new IOException("Invalid character: expected ' but got " + (char) r);
}
char c = readChar();
r = read();
chars = add(chars, c);
if (r == ',') continue;
if (r == ')') break;
throw new IOException("Invalid array: expected , or ) but got " + (char) r);
}
return new CharArrayType(chars);
}
private ByteArrayType readByteArray() throws IOException {
byte[] bytes = new byte[0];
while (true) {
StringBuilder builder = new StringBuilder();
int r;
boolean first = true;
while (true) {
r = read();
if (first && r == '-') {
builder.append((char) r);
first = false;
continue;
}
if (!Character.isDigit(r)) break;
builder.append((char) r);
first = false;
}
if (r == -1) {
throw new IOException("Invalid number");
}
byte number = Byte.parseByte(builder.toString());
bytes = add(bytes, number);
if (r == ',') continue;
if (r == ')') break;
throw new IOException("Invalid array: expected ',' or ')' but got " + (char) r);
}
return new ByteArrayType(bytes);
}
private ShortArrayType readShortArray() throws IOException {
short[] shorts = new short[0];
while (true) {
StringBuilder builder = new StringBuilder();
int r;
boolean first = true;
while (true) {
r = read();
if (first && r == '-') {
builder.append((char) r);
first = false;
continue;
}
if (!Character.isDigit(r)) break;
builder.append((char) r);
}
if (r == -1) {
throw new IOException("Invalid number");
}
short number = Short.parseShort(builder.toString());
shorts = add(shorts, number);
if (r == ',') continue;
if (r == ')') break;
throw new IOException("Invalid array: expected ',' or ')' but got " + (char) r);
}
return new ShortArrayType(shorts);
}
private IntArrayType readIntArray() throws IOException {
int[] ints = new int[0];
while (true) {
StringBuilder builder = new StringBuilder();
int r;
boolean first = true;
while (true) {
r = read();
if (first && r == '-') {
builder.append((char) r);
first = false;
continue;
}
if (!Character.isDigit(r)) break;
builder.append((char) r);
}
if (r == -1) {
throw new IOException("Invalid number");
}
int number = Integer.parseInt(builder.toString());
ints = add(ints, number);
if (r == ',') continue;
if (r == ')') break;
throw new IOException("Invalid array: expected ',' or ')' but got " + (char) r);
}
return new IntArrayType(ints);
}
private LongArrayType readLongArray() throws IOException {
long[] longs = new long[0];
while (true) {
StringBuilder builder = new StringBuilder();
int r;
boolean first = true;
while (true) {
r = read();
if (first && r == '-') {
builder.append((char) r);
first = false;
continue;
}
if (!Character.isDigit(r)) break;
builder.append((char) r);
}
if (r == -1) {
throw new IOException("Invalid number");
}
long number = Long.parseLong(builder.toString());
longs = add(longs, number);
if (r == ',') continue;
if (r == ')') break;
throw new IOException("Invalid array: expected ',' or ')' but got " + (char) r);
}
return new LongArrayType(longs);
}
private FloatArrayType readFloatArray() throws IOException {
float[] floats = new float[0];
while (true) {
StringBuilder builder = new StringBuilder();
int r;
boolean first = true;
while (true) {
r = read();
if (first && r == '-') {
builder.append((char) r);
first = false;
continue;
}
if (!Character.isDigit(r) && r != '.') break;
builder.append((char) r);
}
if (r == -1) {
throw new IOException("Invalid number");
}
float number = Float.parseFloat(builder.toString());
floats = add(floats, number);
if (r == ',') continue;
if (r == ')') break;
throw new IOException("Invalid array: expected ',' or ')' but got " + (char) r);
}
return new FloatArrayType(floats);
}
private DoubleArrayType readDoubleArray() throws IOException {
double[] doubles = new double[0];
while (true) {
StringBuilder builder = new StringBuilder();
int r;
boolean first = true;
while (true) {
r = read();
if (first && r == '-') {
builder.append((char) r);
first = false;
continue;
}
if (!Character.isDigit(r) && r != '.') break;
builder.append((char) r);
}
if (r == -1) {
throw new IOException("Invalid number");
}
double number = Double.parseDouble(builder.toString());
doubles = add(doubles, number);
if (r == ',') continue;
if (r == ')') break;
throw new IOException("Invalid array: expected ',' or ')' but got " + (char) r);
}
return new DoubleArrayType(doubles);
}
private char readChar() throws IOException {
int read = read();
if (read == '\\') {
read = read();
if (read == 't') {
read = '\t';
} else if (read == 'n') {
read = '\n';
} else if (read == 'r') {
read = '\r';
} else if (read == 'b') {
read = '\b';
} else if (read == 'f') {
read = '\f';
} else if (read == '0') {
read = '\0';
} else if (read == 'u') {
read = read();
read = (read << 4) + read();
read = (read << 4) + read();
read = (read << 4) + read();
}
} else if (read == -1) {
throw new EOFException("Invalid char: reached end of stream");
}
if (read() != '\'') throw new IOException("Invalid char: expected ' but got " + (char) read);
return (char) read;
}
private byte[] add(byte[] bytes, byte number) {
byte[] newBytes = new byte[bytes.length + 1];
System.arraycopy(bytes, 0, newBytes, 0, bytes.length);
newBytes[bytes.length] = number;
return newBytes;
}
private short[] add(short[] bytes, short number) {
short[] newBytes = new short[bytes.length + 1];
System.arraycopy(bytes, 0, newBytes, 0, bytes.length);
newBytes[bytes.length] = number;
return newBytes;
}
private int[] add(int[] bytes, int number) {
int[] newBytes = new int[bytes.length + 1];
System.arraycopy(bytes, 0, newBytes, 0, bytes.length);
newBytes[bytes.length] = number;
return newBytes;
}
private long[] add(long[] bytes, long number) {
long[] newBytes = new long[bytes.length + 1];
System.arraycopy(bytes, 0, newBytes, 0, bytes.length);
newBytes[bytes.length] = number;
return newBytes;
}
private float[] add(float[] bytes, float number) {
float[] newBytes = new float[bytes.length + 1];
System.arraycopy(bytes, 0, newBytes, 0, bytes.length);
newBytes[bytes.length] = number;
return newBytes;
}
private double[] add(double[] bytes, double number) {
double[] newBytes = new double[bytes.length + 1];
System.arraycopy(bytes, 0, newBytes, 0, bytes.length);
newBytes[bytes.length] = number;
return newBytes;
}
private char[] add(char[] bytes, char number) {
char[] newBytes = new char[bytes.length + 1];
System.arraycopy(bytes, 0, newBytes, 0, bytes.length);
newBytes[bytes.length] = number;
return newBytes;
}
private BitSetType readBitSet() throws IOException {
BitSet set = new BitSet();
int i = 0;
loop:
while (true) {
int read = read();
switch (read) {
case '0':
set.clear(i);
break;
case '1':
set.set(i);
break;
case ';':
break loop;
case -1:
throw new EOFException("Invalid bitset: EOF");
default:
throw new IOException("Invalid bitset: expected '0', '1' or ';', got " + (char) read);
}
i++;
}
return new BitSetType(set);
}
private DataType<?> readNumber(int read) throws IOException {
StringBuilder builder = new StringBuilder();
builder.append((char) read);
while (true) {
read = read();
if (!Character.isDigit(read) && read != '.') break;
builder.append((char) read);
}
if (read == -1) {
throw new IOException("Invalid number");
}
switch ((char) read) {
case 'b':
return new ByteType(Byte.parseByte(builder.toString()));
case 's':
return new ShortType(Short.parseShort(builder.toString()));
case 'i':
return new IntType(Integer.parseInt(builder.toString()));
case 'l':
return new LongType(Long.parseLong(builder.toString()));
case 'f':
return new FloatType(Float.parseFloat(builder.toString()));
case 'd':
return new DoubleType(Double.parseDouble(builder.toString()));
case 'I':
return new BigIntType(new BigInteger(builder.toString()));
case 'D':
return new BigDecType(new BigDecimal(builder.toString()));
default:
throw new IOException("Invalid number");
}
}
private DataType<?> readList() throws IOException {
DataType<?> dataType = readUso();
if (dataType == null) {
throw new IOException("Invalid list: expected at least one element");
}
int id = dataType.id();
int read = read();
if (read == ']') {
ListType<DataType<?>> list = new ListType<>(id);
list.add(dataType);
return list;
} else if (read != ',') {
throw new IOException("Invalid list: expected ',' or ']'");
}
readWhitespace();
ListType<DataType<?>> list = new ListType<>(id);
list.add(dataType);
while (true) {
DataType<?> cur = readUso();
if (cur == null) {
throw new IOException("Invalid list: invalid element at index " + list.size());
}
if (cur.id() != id) {
throw new IOException("Invalid list, ID mismatch: should be " + id + " but was " + cur.id());
}
list.add(cur);
readWhitespace();
read = read();
if (read == ',') {
readWhitespace();
read = read();
if (read == ']') {
return list;
} else {
unread();
}
} else if (read == ']') {
return list;
} else {
throw new IOException("Invalid list: expected ',' or ']' but got " + (char) read);
}
}
}
private DataType<?> readMap() throws IOException {
MapType map = new MapType();
int read = read();
while (read != '}') {
if (read != '"') throw new IOException("Invalid map: expected '\"' but got " + (char) read);
StringType key = readString();
readWhitespace();
if (read() != ':') throw new IOException("Invalid map: expected ':' but got " + (char) read);
read();
readWhitespace();
DataType<?> value = readUso();
map.put(key.getValue(), value);
readWhitespace();
read = read();
if (read == ',') {
readWhitespace();
read = read();
if (read == '}') {
read();
break;
}
} else if (read == '}') {
break;
} else {
throw new IOException("Invalid map: expected ',' or '}' but got " + (char) read);
}
}
return map;
}
private StringType readString() {
StringBuilder builder = new StringBuilder();
int read = read();
while (read != '"') {
builder.append((char) read);
if (read == '\\') {
read = read();
if (read == 'n') {
builder.append('\n');
} else if (read == 'r') {
builder.append('\r');
} else if (read == 't') {
builder.append('\t');
} else if (read == 'b') {
builder.append('\b');
} else if (read == 'f') {
builder.append('\f');
} else if (read == '0') {
builder.append('\0');
} else if (read == 'u') {
builder.append((char) (read() << 12
| read() << 8
| read() << 4
| read()));
} else {
builder.append((char) read);
}
}
read = read();
}
return new StringType(builder.toString());
}
private void readWhitespace() {
while (true) {
int read = read();
if (!Character.isWhitespace(read)) {
unread();
return;
}
}
}
private UUIDType readUUID() throws IOException {
StringBuilder builder = new StringBuilder();
while (true) {
int read = read();
if (read == '>') break;
if (read == -1) throw new EOFException("Invalid UUID: EOF");
if (Character.isWhitespace(read)) continue;
builder.append((char) read);
}
try {
return new UUIDType(UUID.fromString(builder.toString()));
} catch (IllegalArgumentException e) {
throw new IOException("Invalid UUID: " + builder, e);
}
}
private int unread() {
if (this.pos <= 0) {
return -1;
}
return this.chars[--this.pos];
}
private int read() {
if (this.pos >= this.chars.length) {
return -1;
}
return this.chars[this.pos++];
}
public DataType<?> parse() throws IOException {
try {
return readUso();
} catch (Exception e) {
throw new IOException("Unable to parse USO at pos " + pos + ": " + e.getMessage(), e);
}
}
}