{"record":{"id":"cd18dc3e2a9d061a","repo":"BoundaryML/baml","slug":"expected-token-node-of-kind-expected-but-found-found-at-at","errorCode":null,"errorMessage":"Expected token/node of kind {expected:?}, but found {found:?} at {at:?}","messagePattern":"Expected token/node of kind (.+?), but found (.+?) at (.+?)","errorType":"exception","errorClass":"StrongAstError","httpStatus":null,"severity":"error","filePath":"baml_language/crates/baml_fmt/src/ast/mod.rs","lineNumber":44,"sourceCode":"};\n\npub trait FromCST: Sized {\n    fn from_cst(elem: SyntaxElement) -> Result<Self, StrongAstError>;\n}\n\n/// This AST node only ever has exactly one [`SyntaxKind`].\n///\n/// Helps with conveniently printing error messages.\npub trait KnownKind {\n    /// Should be constant, but we can't use `const` because it's a trait.\n    fn kind() -> SyntaxKind;\n}\n\n/// Errors that can occur when parsing from a [`SyntaxNode`] with [`FromCST`].\n#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]\npub enum StrongAstError {\n    /// When an element is expected (of a specific [`SyntaxKind`]) but was found to be of a different kind.\n    #[error(\"Expected token/node of kind {expected:?}, but found {found:?} at {at:?}\")]\n    UnexpectedKind {\n        expected: SyntaxKind,\n        found: SyntaxKind,\n        at: TextRange,\n    },\n    /// When an element is expected but was found to be of a different kind.\n    #[error(\"Expected token/node {expected_desc}, but found {found:?} at {at:?}\")]\n    UnexpectedKindDesc {\n        expected_desc: Cow<'static, str>,\n        found: SyntaxKind,\n        at: TextRange,\n    },\n    /// When an element is expected (of a specific [`SyntaxKind`]) but there were no more children left.\n    #[error(\"Expected token/node of kind {expected:?}, but was unable to find it in {parent:?}\")]\n    MissingExpectedElement {\n        expected: SyntaxKind,\n        parent: TextRange,\n    },","sourceCodeStart":26,"sourceCodeEnd":62,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/baml_language/crates/baml_fmt/src/ast/mod.rs#L26-L62","documentation":"StrongAstError::UnexpectedKind is raised by the baml_fmt CST-to-strong-AST conversion (FromCST) when a parser step expects a token or node of a specific SyntaxKind but encounters a different kind at a given TextRange. The error carries the expected kind, the found kind, and the source range, enabling precise diagnostics for malformed or unsupported syntax trees fed to the formatter's AST builder.","triggerScenarios":"Calling FromCST conversion (via the formatter pipeline: parse -> strong AST) on a SyntaxNode whose child at the position the converter expects has a different SyntaxKind — i.e. the CST produced by the parser does not match the grammar the AST builder assumes.","commonSituations":"Feeding a node of the wrong kind into a strong-AST conversion helper; parser bug or grammar change leaving an unexpected token where the builder expects another; hand-built or mutated syntax trees passed to baml_fmt; formatter run on a file from a newer BAML syntax version.","solutions":["Look at `at` (TextRange) in the message and inspect the source text there; fix the BAML syntax at that location","Verify the input file parses cleanly with the parser before running the formatter (check for parse errors)","Ensure the baml_fmt and parser crate versions match (grammar drift between versions causes kind mismatches)","If the tree is built/mutated programmatically, validate child kinds before conversion"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// before converting, verify the child kind at the expected position\nlet child = parent.first_child_or_token().ok_or(\"no children\")?;\nif child.kind() != expected_kind {\n    return Err(format!(\"kind mismatch at {:?}: {:?}\", child.text_range(), child.kind()));\n}","typeGuard":"fn is_kind(e: &SyntaxElement, k: SyntaxKind) -> bool { e.kind() == k }","tryCatchPattern":"match StrongAst::from_cst(node) {\n    Ok(ast) => use(ast),\n    Err(StrongAstError::UnexpectedKind { expected, found, at }) => {\n        report(at, format!(\"expected {expected:?}, found {found:?}\"));\n    }\n    Err(e) => report_all(e),\n}","preventionTips":["Fix parser errors before CST->AST conversion","Keep parser and baml_fmt crates on the same version","Use the `at` TextRange to locate and correct offending syntax","Validate programmatically built trees against expected kinds"],"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-14T11:17:12.474Z"}