Skip to content

Commit 20e39d5

Browse files
committed
Refactor SNBT token types into separate classes
1 parent 75293e9 commit 20e39d5

5 files changed

Lines changed: 223 additions & 166 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright 2026 Glavo
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.glavo.nbt.internal.snbt;
17+
18+
/// Floating types for SNBT.
19+
enum FloatingType {
20+
FLOAT, DOUBLE
21+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2026 Glavo
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.glavo.nbt.internal.snbt;
17+
18+
/// Integral types for SNBT.
19+
enum IntegralType {
20+
BYTE(Byte.MAX_VALUE, Byte.MIN_VALUE),
21+
SHORT(Short.MAX_VALUE, Short.MIN_VALUE),
22+
INT(Integer.MAX_VALUE, Integer.MIN_VALUE),
23+
LONG(Long.MAX_VALUE, Long.MIN_VALUE);
24+
25+
final long maxSigned;
26+
final long maxUnsigned;
27+
final long min;
28+
29+
IntegralType(long maxSigned, long min) {
30+
this.maxSigned = maxSigned;
31+
this.maxUnsigned = maxSigned == Long.MAX_VALUE ? Long.MAX_VALUE : maxSigned - min;
32+
this.min = min;
33+
}
34+
35+
void check(long value, boolean unsigned) {
36+
long max = unsigned ? maxUnsigned : maxSigned;
37+
if (value < min || value > max) {
38+
throw new IllegalArgumentException("Value out of range: " + value + " (min: " + min + ", max: " + max + ")");
39+
}
40+
}
41+
}

‎src/main/java/org/glavo/nbt/internal/snbt/SNBTParser.java‎

Lines changed: 1 addition & 151 deletions
Original file line numberDiff line numberDiff line change
@@ -30,86 +30,6 @@ static boolean isAsciiDigit(int ch) {
3030
return ch >= '0' && ch <= '9';
3131
}
3232

33-
static Token parseNumberToken(String value) {
34-
if (value.endsWith("_")) {
35-
throw new IllegalArgumentException("Invalid number literal: " + value);
36-
}
37-
38-
String clean = value.replaceAll("_", ""); // Remove underscores
39-
if (clean.isEmpty()) {
40-
throw new IllegalArgumentException("Invalid number literal: " + value);
41-
}
42-
43-
if (clean.indexOf('.') >= 0 || clean.indexOf('e') >= 0 || clean.indexOf('E') >= 0) {
44-
int endIndex = clean.length();
45-
46-
FloatingType floatingType;
47-
if (clean.endsWith("f") || clean.endsWith("F")) {
48-
floatingType = FloatingType.FLOAT;
49-
endIndex -= 1;
50-
} else if (clean.endsWith("d") || clean.endsWith("D")) {
51-
floatingType = FloatingType.DOUBLE;
52-
endIndex -= 1;
53-
} else {
54-
floatingType = FloatingType.DOUBLE;
55-
}
56-
57-
return new Token.FloatingToken(Double.parseDouble(clean.substring(0, endIndex)), floatingType);
58-
} else {
59-
int beginIndex;
60-
int radix;
61-
if (clean.startsWith("0x") || clean.startsWith("0X")) {
62-
beginIndex = 2;
63-
radix = 16;
64-
} else if (clean.startsWith("0b") || clean.startsWith("0B")) {
65-
beginIndex = 2;
66-
radix = 2;
67-
} else {
68-
beginIndex = 0;
69-
radix = 10;
70-
}
71-
72-
int endIndex = clean.length();
73-
74-
@Nullable
75-
Boolean unsignedSuffixChar = endIndex - beginIndex > 2 ? switch (clean.charAt(endIndex - 2)) {
76-
case 's', 'S' -> false;
77-
case 'u', 'U' -> true;
78-
default -> null;
79-
} : null;
80-
81-
@Nullable
82-
IntegralType typeSuffixChar = endIndex - beginIndex > 1 ? switch (clean.charAt(endIndex - 1)) {
83-
case 'b', 'B' -> radix != 16 || unsignedSuffixChar != null ? IntegralType.BYTE : null;
84-
case 's', 'S' -> IntegralType.SHORT;
85-
case 'i', 'I' -> IntegralType.INT;
86-
case 'l', 'L' -> IntegralType.LONG;
87-
default -> null;
88-
} : null;
89-
90-
if (typeSuffixChar != null) {
91-
endIndex -= 1;
92-
}
93-
if (unsignedSuffixChar != null) {
94-
endIndex -= 1;
95-
}
96-
97-
boolean unsigned = unsignedSuffixChar != null ? unsignedSuffixChar : radix != 10;
98-
IntegralType type = typeSuffixChar != null ? typeSuffixChar : IntegralType.INT;
99-
100-
try {
101-
long parsed = unsigned
102-
? Long.parseUnsignedLong(clean, beginIndex, endIndex, radix)
103-
: Long.parseLong(clean, beginIndex, endIndex, radix);
104-
type.check(parsed, unsigned);
105-
return new Token.IntegralToken(parsed, type, unsigned);
106-
} catch (NumberFormatException e) {
107-
throw new IllegalArgumentException("Invalid number literal: " + value, e);
108-
}
109-
}
110-
111-
}
112-
11333
private final CharSequence input;
11434
private final int beginIndex;
11535
private final int endIndex;
@@ -338,7 +258,7 @@ private Token nextToken() {
338258
}
339259
}
340260

341-
return parseNumberToken(input.subSequence(firstCharCursor, cursor).toString());
261+
return Token.NumberToken.parse(input.subSequence(firstCharCursor, cursor).toString());
342262
}
343263

344264
throw new UnsupportedOperationException("Not implemented"); // TODO
@@ -365,74 +285,4 @@ private Token nextToken() {
365285
// }
366286
// }
367287

368-
enum IntegralType {
369-
BYTE(Byte.MAX_VALUE, Byte.MIN_VALUE),
370-
SHORT(Short.MAX_VALUE, Short.MIN_VALUE),
371-
INT(Integer.MAX_VALUE, Integer.MIN_VALUE),
372-
LONG(Long.MAX_VALUE, Long.MIN_VALUE);
373-
374-
final long maxSigned;
375-
final long maxUnsigned;
376-
final long min;
377-
378-
IntegralType(long maxSigned, long min) {
379-
this.maxSigned = maxSigned;
380-
this.maxUnsigned = maxSigned == Long.MAX_VALUE ? Long.MAX_VALUE : maxSigned - min;
381-
this.min = min;
382-
}
383-
384-
void check(long value, boolean unsigned) {
385-
long max = unsigned ? maxUnsigned : maxSigned;
386-
if (value < min || value > max) {
387-
throw new IllegalArgumentException("Value out of range: " + value + " (min: " + min + ", max: " + max + ")");
388-
}
389-
}
390-
}
391-
392-
enum FloatingType {
393-
FLOAT, DOUBLE
394-
}
395-
396-
sealed interface Token {
397-
enum SimpleToken implements Token {
398-
LEFT_BRACE, // {
399-
RIGHT_BRACE, // }
400-
LEFT_BRACKET, // [
401-
RIGHT_BRACKET, // ]
402-
COMMA, // ,
403-
COLON, // :
404-
DOT, // .
405-
EOF
406-
}
407-
408-
enum ArrayBeginToken implements Token {
409-
BYTE_ARRAY,
410-
INT_ARRAY,
411-
LONG_ARRAY,
412-
}
413-
414-
enum BooleanToken implements Token {
415-
TRUE(true),
416-
FALSE(false);
417-
418-
private final boolean value;
419-
420-
BooleanToken(boolean value) {
421-
this.value = value;
422-
}
423-
}
424-
425-
record StringToken(String value) implements Token {
426-
}
427-
428-
record IntegralToken(long value,
429-
IntegralType type,
430-
boolean unsigned) implements Token {
431-
432-
}
433-
434-
record FloatingToken(double value, FloatingType type) implements Token {
435-
436-
}
437-
}
438288
}
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
/*
2+
* Copyright 2026 Glavo
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.glavo.nbt.internal.snbt;
17+
18+
import org.jetbrains.annotations.Nullable;
19+
20+
sealed interface Token {
21+
enum SimpleToken implements Token {
22+
LEFT_BRACE, // {
23+
RIGHT_BRACE, // }
24+
LEFT_BRACKET, // [
25+
RIGHT_BRACKET, // ]
26+
COMMA, // ,
27+
COLON, // :
28+
DOT, // .
29+
EOF
30+
}
31+
32+
enum ArrayBeginToken implements Token {
33+
BYTE_ARRAY,
34+
INT_ARRAY,
35+
LONG_ARRAY,
36+
}
37+
38+
enum BooleanToken implements Token {
39+
TRUE(true),
40+
FALSE(false);
41+
42+
private final boolean value;
43+
44+
BooleanToken(boolean value) {
45+
this.value = value;
46+
}
47+
}
48+
49+
record StringToken(String value) implements Token {
50+
}
51+
52+
53+
sealed interface NumberToken extends Token {
54+
static NumberToken parse(String value) {
55+
if (value.endsWith("_")) {
56+
throw new IllegalArgumentException("Invalid number literal: " + value);
57+
}
58+
59+
String clean = value.replaceAll("_", ""); // Remove underscores
60+
if (clean.isEmpty()) {
61+
throw new IllegalArgumentException("Invalid number literal: " + value);
62+
}
63+
64+
if (clean.indexOf('.') >= 0 || clean.indexOf('e') >= 0 || clean.indexOf('E') >= 0) {
65+
int endIndex = clean.length();
66+
67+
FloatingType floatingType;
68+
if (clean.endsWith("f") || clean.endsWith("F")) {
69+
floatingType = FloatingType.FLOAT;
70+
endIndex -= 1;
71+
} else if (clean.endsWith("d") || clean.endsWith("D")) {
72+
floatingType = FloatingType.DOUBLE;
73+
endIndex -= 1;
74+
} else {
75+
floatingType = FloatingType.DOUBLE;
76+
}
77+
78+
return new Token.FloatingToken(Double.parseDouble(clean.substring(0, endIndex)), floatingType);
79+
} else {
80+
int beginIndex;
81+
int radix;
82+
if (clean.startsWith("0x") || clean.startsWith("0X")) {
83+
beginIndex = 2;
84+
radix = 16;
85+
} else if (clean.startsWith("0b") || clean.startsWith("0B")) {
86+
beginIndex = 2;
87+
radix = 2;
88+
} else {
89+
beginIndex = 0;
90+
radix = 10;
91+
}
92+
93+
int endIndex = clean.length();
94+
95+
@Nullable
96+
Boolean unsignedSuffixChar = endIndex - beginIndex > 2 ? switch (clean.charAt(endIndex - 2)) {
97+
case 's', 'S' -> false;
98+
case 'u', 'U' -> true;
99+
default -> null;
100+
} : null;
101+
102+
@Nullable
103+
IntegralType typeSuffixChar = endIndex - beginIndex > 1 ? switch (clean.charAt(endIndex - 1)) {
104+
case 'b', 'B' -> radix != 16 || unsignedSuffixChar != null ? IntegralType.BYTE : null;
105+
case 's', 'S' -> IntegralType.SHORT;
106+
case 'i', 'I' -> IntegralType.INT;
107+
case 'l', 'L' -> IntegralType.LONG;
108+
default -> null;
109+
} : null;
110+
111+
if (typeSuffixChar != null) {
112+
endIndex -= 1;
113+
}
114+
if (unsignedSuffixChar != null) {
115+
endIndex -= 1;
116+
}
117+
118+
boolean unsigned = unsignedSuffixChar != null ? unsignedSuffixChar : radix != 10;
119+
IntegralType type = typeSuffixChar != null ? typeSuffixChar : IntegralType.INT;
120+
121+
try {
122+
long parsed = unsigned
123+
? Long.parseUnsignedLong(clean, beginIndex, endIndex, radix)
124+
: Long.parseLong(clean, beginIndex, endIndex, radix);
125+
type.check(parsed, unsigned);
126+
return new Token.IntegralToken(parsed, type, unsigned);
127+
} catch (NumberFormatException e) {
128+
throw new IllegalArgumentException("Invalid number literal: " + value, e);
129+
}
130+
}
131+
132+
}
133+
134+
}
135+
136+
record IntegralToken(long value,
137+
IntegralType type,
138+
boolean unsigned) implements NumberToken {
139+
140+
}
141+
142+
record FloatingToken(double value, FloatingType type) implements NumberToken {
143+
144+
}
145+
}

0 commit comments

Comments
 (0)