Skip to content

Commit 4dd9874

Browse files
committed
Refactor SNBT unquoted string parsing logic
1 parent 7b08fbb commit 4dd9874

1 file changed

Lines changed: 22 additions & 10 deletions

File tree

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

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,16 @@ private void ensureNotEOF(int expected) {
8989
}
9090
}
9191

92-
private Token nextToken() {
92+
private static boolean isUnquotedStringPart(int ch) {
93+
return TextUtils.isAsciiDigit(ch)
94+
|| ch >= 'a' && ch <= 'z'
95+
|| ch >= 'A' && ch <= 'Z'
96+
|| ch == '_'
97+
|| ch == '-' || ch == '+'
98+
|| ch == '.';
99+
}
100+
101+
private Token readNextToken() {
93102
skipWhiteSpace();
94103

95104
if (cursor >= endIndex) {
@@ -233,25 +242,28 @@ private Token nextToken() {
233242
builder.appendCodePoint(ch);
234243
}
235244
}
236-
} else if (TextUtils.isAsciiDigit(firstChar)
237-
|| firstChar == '-' || firstChar == '+' || firstChar == '.') {
245+
246+
throw new IllegalArgumentException("Unexpected end of string literal: " + input.subSequence(firstCharCursor, cursor));
247+
} else if (isUnquotedStringPart(firstChar)) {
238248
// Scan number
239249
while (cursor < endIndex) {
240250
char ch = input.charAt(cursor);
241-
if (TextUtils.isAsciiDigit(ch)
242-
|| ch >= 'a' && ch <= 'z'
243-
|| ch >= 'A' && ch <= 'Z'
244-
|| ch == '.' || ch == '+' || ch == '-' || ch == '_') {
251+
if (isUnquotedStringPart(ch)) {
245252
cursor += 1;
246253
} else {
247254
break;
248255
}
249256
}
250257

251-
return Token.NumberToken.parse(input, firstCharCursor, cursor);
258+
if (TextUtils.isAsciiDigit(firstChar)
259+
|| firstChar == '-' || firstChar == '+' || firstChar == '.') {
260+
return Token.NumberToken.parse(input, firstCharCursor, cursor);
261+
} else {
262+
return new Token.StringToken(input.subSequence(firstCharCursor, cursor).toString(), false);
263+
}
264+
} else {
265+
throw new IllegalArgumentException("Unexpected character: " + (char) firstChar);
252266
}
253-
254-
throw new UnsupportedOperationException("Not implemented"); // TODO
255267
}
256268

257269
}

0 commit comments

Comments
 (0)