{"record":{"id":"759f4294e34967fd","repo":"BoundaryML/baml","slug":"an-element-at-at-was-a-token-when-it-should-have-been-a-node","errorCode":null,"errorMessage":"An element at {at:?} was a token when it should have been a node.","messagePattern":"An element at (.+?) was a token when it should have been a node\\.","errorType":"exception","errorClass":"StrongAstError","httpStatus":null,"severity":"error","filePath":"baml_language/crates/baml_fmt/src/ast/mod.rs","lineNumber":76,"sourceCode":"    #[error(\"Expected token/node of kind {expected:?}, but was unable to find it in {parent:?}\")]\n    MissingExpectedElement {\n        expected: SyntaxKind,\n        parent: TextRange,\n    },\n    /// When an element is expected (not of a single specific [`SyntaxKind`]) but there were no more children left.\n    #[error(\"Expected token/node {desc}, but was unable to find it in {parent:?}\")]\n    MissingExpectedElementDesc {\n        desc: Cow<'static, str>,\n        parent: TextRange,\n    },\n    /// When the node isn't expected to have any more children (e.g. a statement found a `;`) but there are still children left.\n    #[error(\"Unexpected additional element at {at:?} in {parent:?}\")]\n    UnexpectedAdditionalElement { parent: TextRange, at: TextRange },\n    /// When an element is expected to be a node but it's actually a token.\n    #[error(\"An element at {at:?} was a node when it should have been a token.\")]\n    ShouldBeNode { at: TextRange },\n    /// When an element is expected to be a token but it's actually a node.\n    #[error(\"An element at {at:?} was a token when it should have been a node.\")]\n    ShouldBeToken { at: TextRange },\n}\nimpl StrongAstError {\n    /// Checks that the given node is of the specified [`SyntaxKind`].\n    ///\n    /// # Errors\n    /// Returns [`StrongAstError::UnexpectedKind`] if the element not the expected kind.\n    pub fn assert_kind_node(node: &SyntaxNode, expected: SyntaxKind) -> Result<(), Self> {\n        if node.kind() == expected {\n            Ok(())\n        } else {\n            Err(Self::UnexpectedKind {\n                expected,\n                found: node.kind(),\n                at: node.text_range(),\n            })\n        }\n    }","sourceCodeStart":58,"sourceCodeEnd":94,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/baml_language/crates/baml_fmt/src/ast/mod.rs#L58-L94","documentation":"StrongAstError::ShouldBeToken is the inverse of ShouldBeNode: the FromCST conversion expected a leaf token (e.g. a keyword, identifier, or punctuation) at position `at` but found a composite node instead. Conversion stops because the AST builder cannot consume a node where a token is required.","triggerScenarios":"A conversion helper that reads the next token (e.g. expects a specific keyword or punctuation) encounters a nested node at that position in the parent — the CST nests an element the grammar says should be flat.","commonSituations":"Grammar changes nesting previously-flat tokens; parser recovery wrapping tokens in error nodes; mismatched parser/baml_fmt versions; programmatically built trees with wrong child granularity.","solutions":["Inspect the source at the `at` range and correct the syntax (avoid wrapping the expected token in an unexpected construct)","Resolve all parser errors before formatting to prevent recovery-induced wrapping","Sync parser and baml_fmt versions so token/node shapes agree","If constructing CSTs programmatically, insert tokens (not nodes) where tokens are required"],"exampleFix":null,"handlingStrategy":"type-guard","validationCode":"// verify the element at the position is a token, not a node\nlet el = parent.children_with_tokens().next()?;\nif matches!(el, SyntaxElement::Node(_)) {\n    return Err(\"expected token, found node\");\n}","typeGuard":"fn is_token(e: &SyntaxElement) -> bool { matches!(e, SyntaxElement::Token(_)) }","tryCatchPattern":"match StrongAst::from_cst(node) {\n    Ok(ast) => use(ast),\n    Err(StrongAstError::ShouldBeToken { at }) => {\n        report(at, \"expected a token but found a node\");\n    }\n    Err(e) => report_all(e),\n}","preventionTips":["Avoid constructs that nest the expected token unexpectedly","Fix parser errors so recovery doesn't wrap tokens in error nodes","Sync parser grammar with the strong-AST builder expectations","Insert tokens (not nodes) where the grammar requires leaves"],"tags":["parser","ast","syntax","formatter"],"backgroundTag":"schema-validation-failed","analyzedSha":"bd85ce9dee1463ff04d27efd20531013a4ff46c1","analyzedAt":"2026-09-12T03:38:25.718Z","contentChangedAt":"2026-09-12T03:38:25.718Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}