{"record":{"id":"ee83d4cf4c78eb98","repo":"apple/pkl","slug":"unexpectedtokenforexpression-ee83d4","errorCode":"unexpectedTokenForExpression","errorMessage":"Unexpected token `{0}`.","messagePattern":"Unexpected token `(.+?)`\\.","errorType":"exception","errorClass":"ParserError","httpStatus":null,"severity":"error","filePath":"pkl-parser/src/main/java/org/pkl/parser/ParserImpl.java","lineNumber":1047,"sourceCode":"            if (lookahead == Token.LPAREN\n                && !precededBySemicolon\n                && _lookahead.newLinesBetween == 0) {\n              var args = parseArgumentList();\n              yield new UnqualifiedAccessExpr(\n                  identifier, args, identifier.span().endWith(args.span()));\n            } else {\n              yield new UnqualifiedAccessExpr(identifier, null, identifier.span());\n            }\n          }\n          case EOF ->\n              throw new ParserError(\n                  ErrorMessages.create(\"unexpectedEndOfFile\"), prev.span.stopSpan().move(1));\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 Expr parseExprRest(Expr expr) {\n    // non-null\n    if (lookahead == Token.NON_NULL) {\n      var end = next().span;\n      var res = new NonNullExpr(expr, expr.span().endWith(end));\n      return parseExprRest(res);\n    }\n    // amends\n    if (lookahead == Token.LBRACE) {\n      if (expr instanceof ParenthesizedExpr\n          || expr instanceof AmendsExpr\n          || expr instanceof NewExpr) {","sourceCodeStart":1029,"sourceCodeEnd":1065,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-parser/src/main/java/org/pkl/parser/ParserImpl.java#L1029-L1065","documentation":"Expression-level parse failure without a specific expectation: a token appeared where an expression was required (or where no particular token was mandated). The parser throws \"Unexpected token `{0}`\" when it cannot continue parsing an expression and has no expectation to report.","triggerScenarios":"Starting a line with a token that cannot begin an expression (e.g. `)`, `,`, `=>`, an operator like `*` or `&&`); two expressions juxtaposed without an operator; malformed constructs such as `x = ;` or an empty right-hand side of `=`.","commonSituations":"Stray commas or semicolons carried over from JSON/Java habits; accidentally deleting the right-hand side of an assignment; line-continuation mistakes where an operator from the previous line was dropped.","solutions":["Remove the offending token if it's stray punctuation (`;`, `,`, `)` that has no opener).","Supply the missing expression (e.g. after `=` or an operator that ended the previous line).","Insert the intended operator between two juxtaposed expressions (`+`, `.`, whitespace-amend only where valid).","Check the previous line for an accidentally deleted operator or operand that shifted tokens out of place."],"exampleFix":"// before\nx =\ny = 2\n// after\nx = 1\ny = 2","handlingStrategy":"try-catch","validationCode":"// Flag lines that begin with tokens that cannot start an expression\nconst BAD_EXPR_START = /^\\s*[),;]+|^[*/%]|^&&|^\\|\\|/;\nfunction linesNotStartingAnExpression(lines) {\n  return lines.map((l, i) => [l, i]).filter(([l]) => BAD_EXPR_START.test(l)).map(([, i]) => i + 1);\n}","typeGuard":null,"tryCatchPattern":"try {\n  return parsePkl(src);\n} catch (e) {\n  if (/^Unexpected token `/.test(e.message) && !/Expected `/.test(e.message)) {\n    throw new Error('Expression expected near ' + e.span + ': remove stray punctuation or supply the missing operand: ' + e.message);\n  }\n  throw e;\n}","preventionTips":["Don't carry over `;` and stray `,` from JSON/Java.","Always give assignments a right-hand side.","When wrapping lines, keep the operator at the end of the continued line."],"tags":["parser","unexpected-token","expression","syntax"],"backgroundTag":"json-parse-error","analyzedSha":"f3efcbfc9b60d30053b0536d664948d7aa1b8673","analyzedAt":"2026-09-08T13:10:45.570Z","contentChangedAt":"2026-09-08T13:10:45.570Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}