BoundaryML/baml · error · StrongAstError

Expected token/node of kind {expected:?}, but was unable to

Error message

Expected token/node of kind {expected:?}, but was unable to find it in {parent:?}

What it means

StrongAstError::MissingExpectedElement is raised during FromCST conversion when the builder requires a child of a specific SyntaxKind but the parent node has no more children left to consume. It reports the expected kind and the parent node's TextRange so the missing element can be located.

Source

Thrown at baml_language/crates/baml_fmt/src/ast/mod.rs:58

/// Errors that can occur when parsing from a [`SyntaxNode`] with [`FromCST`].
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum StrongAstError {
    /// When an element is expected (of a specific [`SyntaxKind`]) but was found to be of a different kind.
    #[error("Expected token/node of kind {expected:?}, but found {found:?} at {at:?}")]
    UnexpectedKind {
        expected: SyntaxKind,
        found: SyntaxKind,
        at: TextRange,
    },
    /// When an element is expected but was found to be of a different kind.
    #[error("Expected token/node {expected_desc}, but found {found:?} at {at:?}")]
    UnexpectedKindDesc {
        expected_desc: Cow<'static, str>,
        found: SyntaxKind,
        at: TextRange,
    },
    /// When an element is expected (of a specific [`SyntaxKind`]) but there were no more children left.
    #[error("Expected token/node of kind {expected:?}, but was unable to find it in {parent:?}")]
    MissingExpectedElement {
        expected: SyntaxKind,
        parent: TextRange,
    },
    /// When an element is expected (not of a single specific [`SyntaxKind`]) but there were no more children left.
    #[error("Expected token/node {desc}, but was unable to find it in {parent:?}")]
    MissingExpectedElementDesc {
        desc: Cow<'static, str>,
        parent: TextRange,
    },
    /// When the node isn't expected to have any more children (e.g. a statement found a `;`) but there are still children left.
    #[error("Unexpected additional element at {at:?} in {parent:?}")]
    UnexpectedAdditionalElement { parent: TextRange, at: TextRange },
    /// When an element is expected to be a node but it's actually a token.
    #[error("An element at {at:?} was a node when it should have been a token.")]
    ShouldBeNode { at: TextRange },
    /// When an element is expected to be a token but it's actually a node.
    #[error("An element at {at:?} was a token when it should have been a node.")]

View on GitHub (pinned to bd85ce9dee)

Solutions

  1. Examine the parent TextRange and add the missing syntax element in the BAML source (e.g. the missing token/keyword)
  2. Re-parse the file and check the parser's error list — fix reported syntax errors first
  3. Match parser and formatter crate versions to avoid grammar-shape drift
  4. If building trees programmatically, always append required children before conversion

Example fix

// before
function Foo(param: int) {
// after
function Foo(param: int) {
  ...
} // include all required tokens/children
Defensive patterns

Strategy: validation

Validate before calling

// ensure the parent still has the required child before consuming
if parent.children().count() < required_children {
    return Err("truncated tree: missing required element");
}

Try / catch

match StrongAst::from_cst(node) {
    Ok(ast) => use(ast),
    Err(StrongAstError::MissingExpectedElement { expected, parent }) => {
        report(parent, format!("missing {expected:?}"));
    }
    Err(e) => report_all(e),
}

Prevention

When it happens

Trigger: A strongly-typed AST conversion helper iterates the parent's children expecting a required token/node (e.g. a closing brace, identifier, or keyword) but the child iterator is exhausted — the tree is truncated or missing mandatory children.

Common situations: Incomplete BAML source (e.g. unclosed block) that still yields a tree; parser recovery dropping required tokens; older/newer grammar producing trees without elements the current AST builder requires.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


AI-assisted analysis of BoundaryML/baml@bd85ce9dee (2026-09-12). Data as JSON: /api/errors/3614171616ba65e0. Report an issue: GitHub.