{"record":{"id":"b6cbe5dc2cfab02e","repo":"apple/pkl","slug":"invalidtopleveltoken","errorCode":"invalidTopLevelToken","errorMessage":"Invalid token at position. Expected a class, typealias, method, or property.","messagePattern":"Invalid token at position\\. Expected a class, typealias, method, or property\\.","errorType":"exception","errorClass":"GenericParserError","httpStatus":null,"severity":"error","filePath":"pkl-parser/src/main/java/org/pkl/parser/GenericParserImpl.java","lineNumber":209,"sourceCode":"    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()));\n    ff(headers);\n    headers.add(parseIdentifier());\n    ff(headers);\n    if (lookahead == Token.LT) {\n      headers.add(parseTypeParameterList());","sourceCodeStart":191,"sourceCodeEnd":227,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-parser/src/main/java/org/pkl/parser/GenericParserImpl.java#L191-L227","documentation":"The parser throws invalidTopLevelToken (\"Invalid token at position. Expected a class, typealias, method, or property.\") when a token that is neither a keyword nor a doc comment starts a top-level module member position and is not one of the allowed member starts. parseModuleMember accepts only IDENTIFIER (property), TYPE_ALIAS, CLASS, or FUNCTION; anything else falls through to this catch-all error.","triggerScenarios":"Placing an expression, literal, operator, punctuation, or string at module top level, e.g. a stray `}`, `= 5`, a quoted string line, a `@Annotation` with no following declaration, or a number literal in the module body.","commonSituations":"Accidentally pasting object/expression content at module scope; unbalanced braces earlier in the file causing the parser to see body tokens at top level; an annotation left with its target declaration deleted; typos like starting a line with `=` or `.`.","solutions":["Replace the invalid token with a valid module member: a property (`name = value`), `class X {}`, `typealias Y = ...`, or `function f() = ...`.","Check for unbalanced braces above the offending line — an earlier mistake can push object-level tokens into top-level position.","If an annotation is dangling, add the declaration it annotates or remove the annotation.","If the content belongs inside an object or class body, move it under the appropriate declaration."],"exampleFix":"// before\nmodule my.mod\n\"some stray string\"\n// after\nmodule my.mod\ngreeting = \"some stray string\"","handlingStrategy":"validation","validationCode":"// lint: top-level member lines must start with an identifier or a member keyword/annotation\nconst ok = /^\\s*([A-Za-z_][\\w.]*|class|typealias|function|@|\\/\\/\\/|\\/\\/|}|$)/;\nfor (const line of pklSource.split(\"\\n\")) {\n  if (!ok.test(line)) throw new Error(`Invalid top-level token: ${line.trim().slice(0, 20)}`);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Start every top-level line with a property name, class/typealias/function keyword, or annotation.","Fix brace balance before assuming the reported line is the real problem.","Don't paste object-body expressions directly into module scope."],"tags":["pkl","parser","syntax"],"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"}