{"record":{"id":"96014fcaf7eb0775","repo":"apple/pkl","slug":"errormessages-create-errorkey-args","errorCode":null,"errorMessage":"ErrorMessages.create(errorKey, args)","messagePattern":"ErrorMessages\\.create\\(errorKey, args\\)","errorType":"exception","errorClass":"ParserError","httpStatus":null,"severity":"error","filePath":"pkl-parser/src/main/java/org/pkl/parser/ParserImpl.java","lineNumber":1731,"sourceCode":"      assert node != null;\n      return node.span();\n    }\n  }\n\n  private FullToken expect(Token type, String errorKey, Object... messageArgs) {\n    if (lookahead != type) {\n      var span = spanLookahead;\n      if (lookahead == Token.EOF || _lookahead.newLinesBetween > 0) {\n        // don't point at the EOF or the next line, but at the end of the last token\n        span = prev.span.stopSpan().move(1);\n      }\n      var args = messageArgs;\n      if (errorKey.startsWith(\"unexpectedToken\")) {\n        args = new Object[messageArgs.length + 1];\n        args[0] = lookahead == Token.EOF ? \"EOF\" : _lookahead.text(lexer);\n        System.arraycopy(messageArgs, 0, args, 1, messageArgs.length);\n      }\n      throw new ParserError(ErrorMessages.create(errorKey, args), span);\n    }\n    return next();\n  }\n\n  private <T> List<T> parseListOf(Token separator, Supplier<T> parser) {\n    var res = new ArrayList<T>();\n    res.add(parser.get());\n    while (lookahead == separator) {\n      next();\n      res.add(parser.get());\n    }\n    return res;\n  }\n\n  private <T> List<T> parseListOf(Token separator, Token terminator, Supplier<T> parser) {\n    var res = new ArrayList<T>();\n    res.add(parser.get());\n    while (lookahead == separator) {","sourceCodeStart":1713,"sourceCodeEnd":1749,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-parser/src/main/java/org/pkl/parser/ParserImpl.java#L1713-L1749","documentation":"This is the parser's generic expectation failure: parseError assembles an error key (e.g. unexpectedToken, unexpectedTokenForType) plus message arguments from the current lookahead and throws ParserError via ErrorMessages.create(errorKey, args). For keys starting with unexpectedToken it automatically prepends the offending token text (or \"EOF\") so the rendered message reads like `Unexpected token `)`. Expected ...`. The message shown is the internal factory call, meaning the concrete key/args were built here at ParserImpl.java:1731.","triggerScenarios":"Any parse where the lookahead token doesn't satisfy the parser's current expectation: a wrong delimiter, missing operator, wrong keyword, or wrong token type at the reported span — e.g. `expect(...)` failing while parsing expressions, type annotations, or object bodies.","commonSituations":"Syntax typos in .pkl files (missing comma/brace, stray token); a language-server or build plugin surfacing raw parse errors for malformed config; generated Pkl emitting tokens in the wrong order.","solutions":["Read the rendered message's offending token and expected list; fix the source at the reported span accordingly","Run `pkl format` or an editor with Pkl syntax highlighting to locate the malformed construct","If the key is unexpectedToken, compare the shown token against the grammar at that position and insert/replace the expected token","Isolate the failing snippet in a minimal .pkl file to pinpoint which construct the parser rejects"],"exampleFix":"// before\nx: 1 2\n// after (insert expected operator/separator)\nx: 1 + 2","handlingStrategy":"try-catch","validationCode":"null","typeGuard":"null","tryCatchPattern":"try {\n  const result = parser.parse(source);\n} catch (e) {\n  if (e instanceof ParserError) {\n    // e.errorId (e.g. 'unexpectedToken'), e.span, and rendered message tell you\n    // the offending token and what was expected; surface span to the user\n    reportDiagnostic(e.span, e.getMessage());\n  } else { throw e; }\n}","preventionTips":["Develop .pkl files with the IDE plugin for live syntax feedback","Run a formatter/linter over generated Pkl before parsing","Keep ParserError handling centralized so all parse failures render span + message consistently"],"tags":["parser","pkl","unexpected-token","syntax-error"],"backgroundTag":"unexpected-token-parse-error","analyzedSha":"f3efcbfc9b60d30053b0536d664948d7aa1b8673","analyzedAt":"2026-09-08T13:10:45.570Z","contentChangedAt":"2026-09-08T13:10:45.570Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}