{"record":{"id":"ba82e8152aad55e6","repo":"apple/pkl","slug":"unexpectedtokenforexpression","errorCode":"unexpectedTokenForExpression","errorMessage":"Unexpected token `{0}`.","messagePattern":"Unexpected token `(.+?)`\\.","errorType":"exception","errorClass":"GenericParserError","httpStatus":null,"severity":"error","filePath":"pkl-parser/src/main/java/org/pkl/parser/GenericParserImpl.java","lineNumber":908,"sourceCode":"            ff(children);\n            children.add(parseExpr());\n            yield new Node(NodeType.LET_EXPR, children);\n          }\n          case TRUE, FALSE -> new Node(NodeType.BOOL_LITERAL_EXPR, next().span);\n          case INT, HEX, BIN, OCT -> new Node(NodeType.INT_LITERAL_EXPR, next().span);\n          case FLOAT -> new Node(NodeType.FLOAT_LITERAL_EXPR, next().span);\n          case STRING_START -> parseSingleLineStringLiteralExpr();\n          case STRING_MULTI_START -> parseMultiLineStringLiteralExpr();\n          case IDENTIFIER -> parseUnqualifiedAccessExpr();\n          case EOF ->\n              throw parserError(\n                  ErrorMessages.create(\"unexpectedEndOfFile\"), prev().span.stopSpan());\n          default -> {\n            var text = _lookahead.text(lexer);\n            if (expectation != null) {\n              throw parserError(\"unexpectedToken\", text, expectation);\n            }\n            throw parserError(\"unexpectedTokenForExpression\", text);\n          }\n        };\n    return parseExprRest(expr);\n  }\n\n  @SuppressWarnings(\"DuplicatedCode\")\n  private Node parseExprRest(Node expr) {\n    // amends\n    if (lookahead() == Token.LBRACE) {\n      var children = new ArrayList<Node>();\n      children.add(expr);\n      ff(children);\n      if (expr.type == NodeType.PARENTHESIZED_EXPR\n          || expr.type == NodeType.AMENDS_EXPR\n          || expr.type == NodeType.NEW_EXPR) {\n        children.add(parseObjectBody());\n        return parseExprRest(new Node(NodeType.AMENDS_EXPR, children));\n      }","sourceCodeStart":890,"sourceCodeEnd":926,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-parser/src/main/java/org/pkl/parser/GenericParserImpl.java#L890-L926","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nx = , y = 2\n// after\nx = 1\ny = 2","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { parser.parse(src) } catch (e) {\n  if (/^Unexpected token/.test(String(e.message)) && !/Expected/.test(String(e.message))) {\n    // generic expression-position syntax error: show span context\n  }\n  throw e;\n}","preventionTips":["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"],"tags":["parser","pkl","syntax-error","unexpected-token"],"backgroundTag":"unexpected-token","analyzedSha":"f3efcbfc9b60d30053b0536d664948d7aa1b8673","analyzedAt":"2026-09-08T13:10:45.570Z","contentChangedAt":"2026-09-08T13:10:45.570Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}