apple/pkl · error · GenericParserError
unexpectedTokenForExpression
unexpectedTokenForExpression
Error message
Unexpected token `{0}`. What it means
The parser found a token that cannot appear in expression position and no specific expectation was set, so it throws the generic 'Unexpected token' message naming the token. This is the fallback expression syntax error in parseExpr.
Source
Thrown at pkl-parser/src/main/java/org/pkl/parser/GenericParserImpl.java:908
ff(children);
children.add(parseExpr());
yield new Node(NodeType.LET_EXPR, children);
}
case TRUE, FALSE -> new Node(NodeType.BOOL_LITERAL_EXPR, next().span);
case INT, HEX, BIN, OCT -> new Node(NodeType.INT_LITERAL_EXPR, next().span);
case FLOAT -> new Node(NodeType.FLOAT_LITERAL_EXPR, next().span);
case STRING_START -> parseSingleLineStringLiteralExpr();
case STRING_MULTI_START -> parseMultiLineStringLiteralExpr();
case IDENTIFIER -> parseUnqualifiedAccessExpr();
case EOF ->
throw parserError(
ErrorMessages.create("unexpectedEndOfFile"), prev().span.stopSpan());
default -> {
var text = _lookahead.text(lexer);
if (expectation != null) {
throw parserError("unexpectedToken", text, expectation);
}
throw parserError("unexpectedTokenForExpression", text);
}
};
return parseExprRest(expr);
}
@SuppressWarnings("DuplicatedCode")
private Node parseExprRest(Node expr) {
// amends
if (lookahead() == Token.LBRACE) {
var children = new ArrayList<Node>();
children.add(expr);
ff(children);
if (expr.type == NodeType.PARENTHESIZED_EXPR
|| expr.type == NodeType.AMENDS_EXPR
|| expr.type == NodeType.NEW_EXPR) {
children.add(parseObjectBody());
return parseExprRest(new Node(NodeType.AMENDS_EXPR, children));
}View on GitHub (pinned to f3efcbfc9b)
Solutions
- Delete or correct the unexpected token at the reported span
- Look slightly earlier in the file for a missing operand or bracket that displaced the token
- Reformat with a Pkl formatter to expose structural mistakes
Example fix
// before x = , y = 2 // after x = 1 y = 2
Defensive patterns
Strategy: try-catch
Try / catch
try { parser.parse(src) } catch (e) {
if (/^Unexpected token/.test(String(e.message)) && !/Expected/.test(String(e.message))) {
// generic expression-position syntax error: show span context
}
throw e;
} Prevention
- Avoid dangling commas/colons in hand-edited files
- Check the token before the reported one for a missing operand
- Use a Pkl-aware formatter to catch structural slips
When it happens
Trigger: parseExpr's default branch when `expectation == null` and `_lookahead` is not a valid expression-start token — e.g. a stray `}` `,` `:` or operator at top level.
Common situations: Dangling comma or colon from an edit; an operator without a left operand; keywords like `then`/`else` appearing outside their construct; mismatched brackets shifting everything into expression position.
Understand the failure class
- 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)
- unexpectedToken
- missingDelimiter
- unexpectedEndOfFile
- stringContentMustBeginOnNewLine
AI-assisted analysis of apple/pkl@f3efcbfc9b (2026-09-08).
Data as JSON: /api/errors/ba82e8152aad55e6.
Report an issue: GitHub.