fix(parser): fix empty array and nested bracket parsing

Improve the `ValueParser` to correctly disambiguate between empty
arrays `[]` and the start of nested structures. This ensures that
the parser can distinguish between an array containing a single
element that starts with a bracket and an actual empty array.

Update tests to reflect that parsed tuples are returned as arrays
rather than objects.
This commit is contained in:
2026-04-22 00:28:10 +08:00
parent 585f33b856
commit b5be558b57
2 changed files with 27 additions and 20 deletions
+18 -3
View File
@@ -304,9 +304,23 @@ class ValueParser {
if (!elementIsTupleOrArray) {
this.consume();
hasOpenBracket = true;
} else if (this.input[this.pos + 1] === "[") {
} else {
// Element is tuple or array - need to disambiguate
// Save position to check if this is an empty array
const savedPos = this.pos;
this.consume();
hasOpenBracket = true;
this.skipWhitespace();
if (this.peek() === "]") {
// Empty array []
this.consume();
return [];
} else if (this.peek() === "[") {
// Nested brackets [[ - this is the array opener
hasOpenBracket = true;
} else {
// [ belongs to the first element, restore position
this.pos = savedPos;
}
}
}
@@ -324,7 +338,8 @@ class ValueParser {
const result: unknown[] = [];
while (true) {
this.skipWhitespace();
result.push(this.parseValue(schema.element, false));
result.push(this.parseValue(schema.element, elementIsTupleOrArray));
this.skipWhitespace();
if (!this.consumeStr(";")) {