apple/pkl · error · GenericParserError
unexpectedToken
unexpectedToken
Error message
Unexpected token `{0}`. Expected `{1}`. What it means
A member predicate's closing brackets were malformed: after the first `]`, the expected `]]` didn't match, or there was whitespace between the two `]` characters. Pkl requires the predicate syntax `is[...]` / `[[...]]`-style double bracket to be contiguous.
Source
Thrown at pkl-parser/src/main/java/org/pkl/parser/GenericParserImpl.java:552
children.add(new Node(NodeType.CLASS_METHOD_BODY, body));
return new Node(NodeType.OBJECT_METHOD, children);
}
private Node parseMemberPredicate() {
var children = new ArrayList<Node>();
children.add(makeTerminal(next()));
ff(children);
children.add(parseExpr());
ff(children);
var firstBrack = expect(Token.RBRACK, "unexpectedToken", "]]");
children.add(makeTerminal(firstBrack));
var secondbrack = expect(Token.RBRACK, "unexpectedToken", "]]");
children.add(makeTerminal(secondbrack));
if (firstBrack.span.charIndex() != secondbrack.span.charIndex() - 1) {
// There shouldn't be any whitespace between the first and second ']'.
var span = firstBrack.span.endWith(secondbrack.span);
var text = lexer.textFor(span.charIndex(), span.length());
throw parserError(ErrorMessages.create("unexpectedToken", text, "]]"), firstBrack.span);
}
ff(children);
if (lookahead == Token.ASSIGN) {
children.add(makeTerminal(next()));
ff(children);
children.add(parseExpr("}"));
return new Node(NodeType.MEMBER_PREDICATE, children);
}
children.addAll(parseBodyList());
return new Node(NodeType.MEMBER_PREDICATE, children);
}
private Node parseObjectEntry() {
var children = new ArrayList<Node>();
var header = new ArrayList<Node>();
expect(Token.LBRACK, header, "unexpectedToken", "[");
ff(header);
header.add(parseExpr());View on GitHub (pinned to f3efcbfc9b)
Solutions
- Write the double closing bracket with no whitespace: `]]`
- Ensure the predicate opening used `[[` and closing `]]` in matching pairs
- Let a Pkl-aware formatter normalize the predicate syntax
Example fix
// before x is[ Int ] ] // after x is[Int]
Defensive patterns
Strategy: validation
Validate before calling
/\]\s*\]/.test(src) === false // reject whitespace between predicate closing brackets
Prevention
- Never type a space between the two closing brackets of a member predicate
- Copy predicate syntax verbatim from docs
- Enable a formatter that normalizes `]]`
When it happens
Trigger: parseMemberPredicate calls expect(RBRACK, "unexpectedToken", "]]") or detects `firstBrack.charIndex != secondbrack.charIndex - 1` — e.g. writing `is[ x ] [ ]` with a space between `]` and `]`, or omitting the second bracket.
Common situations: Typing a space between the closing brackets of `when (cond) { ... }`-style member predicates; hand-writing predicate syntax copied loosely from docs; editor auto-spacing between brackets.
Understand the failure class
Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.
- Parsing and encoding errors: unexpected token, malformed input — why parsers reject input and how to find the real culprit.
Related errors
- ErrorMessages.create(errorKey, args)
- unexpectedTokenForExpression
- missingDelimiter
- unexpectedEndOfFile
- stringContentMustBeginOnNewLine
AI-assisted analysis of apple/pkl@f3efcbfc9b (2026-09-08).
Data as JSON: /api/errors/5cdc22622550cf37.
Report an issue: GitHub.