bash-ast 0.8.20

Typed Rust AST over tree-sitter-bash. Parses bash source into a strongly-typed tree suitable for structural analysis (permission gating, linting, refactoring) rather than execution.
Documentation
use tree_sitter::{Parser, Tree};

use crate::ast::Program;
use crate::convert;
use crate::error::ParseError;

/// Parse bash source into a tree-sitter CST. Useful when callers want to walk the
/// raw CST themselves; most consumers want [`parse_to_ast`] instead.
pub fn parse(source: &str) -> Result<Tree, ParseError> {
    let mut parser = Parser::new();
    parser
        .set_language(&tree_sitter_bash::LANGUAGE.into())
        .map_err(|_| ParseError::LanguageInit)?;
    parser.parse(source, None).ok_or(ParseError::NoTree)
}

/// Parse bash source and convert to the typed AST. Returns an error if any node kind
/// is unrecognized (we treat unknown kinds as a bug rather than silently dropping
/// them, so the typed surface stays in lockstep with the grammar).
pub fn parse_to_ast(source: &str) -> Result<Program, ParseError> {
    let tree = parse(source)?;
    convert::tree_to_program(&tree, source.as_bytes())
}