{"record":{"id":"f7909d299554ca5f","repo":"apple/pkl","slug":"keywordnotallowedhere","errorCode":"keywordNotAllowedHere","errorMessage":"Keyword `{0}` is not allowed here.","messagePattern":"Keyword `(.+?)` is not allowed here\\.","errorType":"exception","errorClass":"GenericParserError","httpStatus":null,"severity":"error","filePath":"pkl-parser/src/main/java/org/pkl/parser/GenericParserImpl.java","lineNumber":204,"sourceCode":"    while (lookahead.isModifier()) {\n      modifiers.add(make(NodeType.MODIFIER, next().span));\n      hasModifier = true;\n      ff(children);\n    }\n    if (hasModifier) children.add(new Node(NodeType.MODIFIER_LIST, modifiers));\n    return new HeaderResult(hasDocComment, hasAnnotation, hasModifier);\n  }\n\n  private Node parseModuleMember(List<Node> preChildren) {\n    return switch (lookahead) {\n      case IDENTIFIER -> parseClassProperty(preChildren);\n      case TYPE_ALIAS -> parseTypeAlias(preChildren);\n      case CLASS -> parseClass(preChildren);\n      case FUNCTION -> parseClassMethod(preChildren);\n      case EOF -> throw parserError(\"unexpectedEndOfFile\");\n      default -> {\n        if (lookahead.isKeyword()) {\n          throw parserError(\"keywordNotAllowedHere\", lookahead.text());\n        }\n        if (lookahead == Token.DOC_COMMENT) {\n          throw parserError(\"danglingDocComment\");\n        }\n        throw parserError(\"invalidTopLevelToken\");\n      }\n    };\n  }\n\n  private Node parseTypeAlias(List<Node> preChildren) {\n    var headerParts = getHeaderParts(preChildren);\n    var children = new ArrayList<>(headerParts.preffixes);\n    var headers = new ArrayList<Node>();\n    if (headerParts.modifierList != null) {\n      headers.add(headerParts.modifierList);\n    }\n    // typealias keyword\n    headers.add(makeTerminal(next()));","sourceCodeStart":186,"sourceCodeEnd":222,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-parser/src/main/java/org/pkl/parser/GenericParserImpl.java#L186-L222","documentation":"The parser throws keywordNotAllowedHere (message 'Keyword `{0}` is not allowed here.') when a Pkl keyword token appears where a module member should start. Only identifiers and the specific keywords class/typealias/function may begin a module member; any other keyword (e.g. `if`, `let`, `when`, `is`) in top-level member position is rejected, with the offending keyword text as the parameter.","triggerScenarios":"Writing a bare keyword at top level of a .pkl module, e.g. `if` or `let` as the first token of a line in the module body, or accidentally deleting a member name so only its keyword remains (e.g. a stray `function` without a name is handled, but `is SomeType` at top level triggers this).","commonSituations":"Copy-pasting expression-level syntax into module scope; a botched find/replace removing an identifier and leaving its keyword; typos where a reserved word was meant to be an identifier (e.g. using `when` as a property name unquoted).","solutions":["Remove the stray keyword or replace it with a valid module member declaration (property, class, typealias, or method).","If the keyword was meant as an identifier, rename it to a non-reserved name (e.g. `when` -> `whenValue`).","Check the surrounding lines for a deleted/merged member definition that left the keyword orphaned."],"exampleFix":"// before\nmodule my.mod\nif (cond) { x = 1 }\n// after\nmodule my.mod\nx = if (cond) 1 else 2","handlingStrategy":"validation","validationCode":"// lint: first token of each top-level line must not be a non-member keyword\nconst memberKeywords = new Set([\"class\", \"typealias\", \"function\"]);\nconst reserved = new Set([\"if\", \"let\", \"when\", \"is\", \"amends\", \"extends\", \"import\", \"module\", \"nothing\", \"unknown\", \"read\", \"output\", \"super\", \"this\", \"outer\"]);\nfor (const line of pklSource.split(\"\\n\")) {\n  const w = line.trim().split(/\\s+/)[0];\n  if (reserved.has(w) && !memberKeywords.has(w)) throw new Error(`Keyword ${w} not allowed at member position`);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use only property/class/typealias/function declarations at module top level.","Avoid naming identifiers with Pkl reserved words.","Review refactors that delete member names so no bare keyword is left behind."],"tags":["pkl","parser","syntax","keyword"],"backgroundTag":"invalid-syntax","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"}