Skip to content

Commit 4afaeb1

Browse files
committed
Add SNBT parser implementation with tokenizer
1 parent 3ae3368 commit 4afaeb1

2 files changed

Lines changed: 326 additions & 0 deletions

File tree

Lines changed: 306 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,306 @@
1+
/*
2+
* Hello Minecraft! Launcher
3+
* Copyright (C) 2026 huangyuhui <huanghongxun2008@126.com> and contributors
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
*/
18+
package org.glavo.nbt.internal.snbt;
19+
20+
import org.glavo.nbt.tag.CompoundTag;
21+
import org.glavo.nbt.tag.Tag;
22+
import org.jetbrains.annotations.Nullable;
23+
import org.jetbrains.annotations.Unmodifiable;
24+
25+
import java.util.Objects;
26+
27+
public final class SNBTParser {
28+
public static final @Unmodifiable CompoundTag EMPTY_COMPOUND_TAG = new CompoundTag();
29+
30+
private static boolean isAsciiDigit(int ch) {
31+
return ch >= '0' && ch <= '9';
32+
}
33+
34+
private final CharSequence input;
35+
private final int beginIndex;
36+
private final int endIndex;
37+
38+
private int cursor;
39+
40+
private @Nullable StringBuilder buffer;
41+
42+
public SNBTParser(CharSequence input, int beginIndex, int endIndex) {
43+
Objects.checkFromToIndex(beginIndex, endIndex, input.length());
44+
this.input = input;
45+
this.beginIndex = beginIndex;
46+
this.endIndex = endIndex;
47+
}
48+
49+
private int getCodePoint() {
50+
if (input instanceof String string) {
51+
return string.codePointAt(cursor);
52+
} else {
53+
char c0 = input.charAt(cursor);
54+
if (Character.isHighSurrogate(c0) && cursor < endIndex - 1) {
55+
char c1 = input.charAt(cursor + 1);
56+
if (Character.isLowSurrogate(c1)) {
57+
return Character.toCodePoint(c0, c1);
58+
}
59+
}
60+
return c0;
61+
}
62+
}
63+
64+
private int indexOf(char ch) {
65+
for (int i = cursor; i < endIndex; i++) {
66+
if (input.charAt(i) == ch) {
67+
return i;
68+
}
69+
}
70+
return -1;
71+
}
72+
73+
private StringBuilder getBuilder() {
74+
if (buffer == null) {
75+
buffer = new StringBuilder();
76+
} else {
77+
buffer.setLength(0);
78+
}
79+
return buffer;
80+
}
81+
82+
private void skipWhiteSpace() {
83+
while (cursor < endIndex) {
84+
int ch = getCodePoint();
85+
if (Character.isWhitespace(ch)) {
86+
cursor += Character.charCount(ch);
87+
} else {
88+
break;
89+
}
90+
}
91+
}
92+
93+
private void ensureNotEOF() {
94+
if (cursor >= endIndex) {
95+
throw new IllegalArgumentException("Unexpected end of input");
96+
}
97+
}
98+
99+
private void ensureNotEOF(int expected) {
100+
if (cursor >= endIndex - expected) {
101+
throw new IllegalArgumentException("Unexpected end of input");
102+
}
103+
}
104+
105+
private Token nextToken() {
106+
skipWhiteSpace();
107+
108+
if (cursor >= endIndex) {
109+
return Token.SimpleToken.EOF;
110+
}
111+
112+
int firstChar = getCodePoint();
113+
int firstCharCursor = cursor;
114+
115+
cursor += 1;
116+
117+
Token token = switch (firstChar) {
118+
case '{' -> Token.SimpleToken.LEFT_BRACE;
119+
case '}' -> Token.SimpleToken.RIGHT_BRACE;
120+
case '[' -> Token.SimpleToken.LEFT_BRACKET;
121+
case ']' -> Token.SimpleToken.RIGHT_BRACKET;
122+
case ',' -> Token.SimpleToken.COMMA;
123+
case ':' -> Token.SimpleToken.COLON;
124+
default -> null;
125+
};
126+
127+
if (token != null) {
128+
return token;
129+
}
130+
131+
if (firstChar == '\'' || firstChar == '"') {
132+
// Scan string prefix without escape
133+
while (cursor < endIndex) {
134+
int ch = getCodePoint();
135+
int chCount = Character.charCount(ch);
136+
if (ch == firstChar) {
137+
cursor += chCount;
138+
139+
String value = input.subSequence(firstCharCursor, cursor - chCount).toString();
140+
return value.isEmpty() ? Token.StringToken.EMPTY : new Token.StringToken(value);
141+
} else if (ch == '\\') {
142+
break;
143+
} else {
144+
cursor += Character.charCount(ch);
145+
}
146+
}
147+
148+
StringBuilder builder = getBuilder();
149+
builder.append(input, firstCharCursor + 1, cursor);
150+
151+
while (cursor < endIndex) {
152+
int ch = getCodePoint();
153+
if (ch == firstChar) {
154+
cursor += Character.charCount(ch);
155+
return new Token.StringToken(builder.toString());
156+
} else if (ch == '\\') {
157+
cursor += 1;
158+
ensureNotEOF();
159+
160+
int ch0 = getCodePoint();
161+
cursor += Character.charCount(ch0);
162+
switch (ch0) {
163+
case 'b' -> builder.append('\b');
164+
case 'f' -> builder.append('\f');
165+
case 'n' -> builder.append('\n');
166+
case 'r' -> builder.append('\r');
167+
case 's' -> builder.append(' ');
168+
case 't' -> builder.append('\t');
169+
case 'x' -> {
170+
ensureNotEOF(2);
171+
172+
try {
173+
builder.append((char) Integer.parseUnsignedInt(input, cursor, cursor + 2, 16));
174+
} catch (NumberFormatException e) {
175+
throw new IllegalArgumentException("Invalid hex escape sequence: \\x" + input.subSequence(cursor, cursor + 2), e);
176+
}
177+
178+
cursor += 2;
179+
}
180+
case 'u' -> {
181+
ensureNotEOF(4);
182+
try {
183+
builder.append((char) Integer.parseUnsignedInt(input, cursor, cursor + 4, 16));
184+
} catch (NumberFormatException e) {
185+
throw new IllegalArgumentException("Invalid Unicode escape sequence: \\u" + input.subSequence(cursor, cursor + 4), e);
186+
}
187+
cursor += 4;
188+
}
189+
case 'U' -> {
190+
ensureNotEOF(8);
191+
try {
192+
builder.appendCodePoint(Integer.parseUnsignedInt(input, cursor, cursor + 8, 16));
193+
} catch (NumberFormatException e) {
194+
throw new IllegalArgumentException("Invalid Unicode escape sequence: \\U" + input.subSequence(cursor, cursor + 8), e);
195+
}
196+
cursor += 8;
197+
}
198+
case 'N' -> {
199+
ensureNotEOF(2);
200+
if (input.charAt(cursor) != '{') {
201+
throw new IllegalArgumentException("Invalid named Unicode escape sequence: \\N" + input.charAt(cursor));
202+
}
203+
204+
cursor += 1;
205+
206+
int end = indexOf('}');
207+
if (end < 0) {
208+
throw new IllegalArgumentException("Unexpected end of named Unicode escape sequence: \\N");
209+
}
210+
if (end == cursor + 1) {
211+
throw new IllegalArgumentException("Empty named Unicode escape sequence at " + (cursor - 2));
212+
}
213+
214+
String name = input.subSequence(cursor, end).toString();
215+
int cp;
216+
try {
217+
cp = Character.codePointOf(name);
218+
} catch (IllegalArgumentException e) {
219+
throw new IllegalArgumentException("Invalid named Unicode escape sequence: \\N{" + name + "}", e);
220+
}
221+
builder.appendCodePoint(cp);
222+
cursor = end + 1;
223+
}
224+
default -> builder.appendCodePoint(ch);
225+
226+
}
227+
} else {
228+
cursor += Character.charCount(ch);
229+
builder.appendCodePoint(ch);
230+
}
231+
}
232+
} else if (isAsciiDigit(firstChar)
233+
|| firstChar == '-' || firstChar == '+'
234+
|| (firstChar == '.' && cursor < endIndex && isAsciiDigit(input.charAt(cursor)))) {
235+
// Scan number
236+
while (cursor < endIndex) {
237+
char ch = input.charAt(cursor);
238+
if (isAsciiDigit(ch)
239+
|| ch >= 'a' && ch <= 'z'
240+
|| ch >= 'A' && ch <= 'Z'
241+
|| ch == '.' || ch == '+' || ch == '-' || ch == '_') {
242+
cursor += 1;
243+
} else {
244+
break;
245+
}
246+
}
247+
248+
String value = input.subSequence(firstCharCursor, cursor).toString()
249+
.replace("_", ""); // Remove underscores
250+
251+
// TODO
252+
}
253+
254+
throw new UnsupportedOperationException("Not implemented"); // TODO
255+
}
256+
257+
// public @Nullable Tag nextTag(boolean immutable) {
258+
// skipWhiteSpace();
259+
//
260+
// if (cursor >= endIndex) {
261+
// return null;
262+
// }
263+
//
264+
// int ch = getCodePoint();
265+
// if (ch == '{') {
266+
// cursor += 1;
267+
// skipWhiteSpace();
268+
//
269+
// if (immutable && getCodePoint() == '}') {
270+
// cursor += 1;
271+
// return EMPTY_COMPOUND_TAG;
272+
// }
273+
//
274+
//
275+
// }
276+
// }
277+
278+
279+
private sealed interface Token {
280+
enum SimpleToken implements Token {
281+
LEFT_BRACE, // {
282+
RIGHT_BRACE, // }
283+
LEFT_BRACKET, // [
284+
RIGHT_BRACKET, // ]
285+
COMMA, // ,
286+
COLON, // :
287+
DOT, // .
288+
EOF
289+
}
290+
291+
record StringToken(String value) implements Token {
292+
private static final StringToken EMPTY = new StringToken("");
293+
}
294+
295+
enum BooleanToken implements Token {
296+
TRUE(true),
297+
FALSE(false);
298+
299+
private final boolean value;
300+
301+
BooleanToken(boolean value) {
302+
this.value = value;
303+
}
304+
}
305+
}
306+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
17+
@NotNullByDefault
18+
package org.glavo.nbt.internal.snbt;
19+
20+
import org.jetbrains.annotations.NotNullByDefault;

0 commit comments

Comments
 (0)