treesitter-types-python 0.1.1

Pre-generated strongly-typed AST types for Python (tree-sitter-python)
Documentation

Strongly-typed AST types for Python, auto-generated from tree-sitter-python's node-types.json.

This crate is generated by treesitter-types and is automatically kept up to date when a new version of the grammar crate is released.

These types have been tested by parsing the CPython source code.

See the Tree-sitter project for more information about the underlying parser framework.

Example

use treesitter_types_python::*;

// A minimal Python hello-world program.
let src = b"\
def main():
    print(\"Hello, World!\")

main()
";

// Parse the source with tree-sitter and convert into typed AST.
let mut parser = tree_sitter::Parser::new();
parser.set_language(&tree_sitter_python::LANGUAGE.into()).unwrap();
let tree = parser.parse(src, None).unwrap();
let module = Module::from_node(tree.root_node(), src).unwrap();

// The module has two top-level children.
assert_eq!(module.children.len(), 2);

// 1) The function definition — `def main(): ...`.
let ModuleChildren::CompoundStatement(stmt) = &module.children[0] else {
    panic!("expected a compound statement");
};
let CompoundStatement::FunctionDefinition(func) = stmt.as_ref() else {
    panic!("expected a function definition");
};
assert_eq!(func.name.text(), "main");
assert!(func.parameters.children.is_empty()); // no parameters
assert!(func.return_type.is_none());           // no return type annotation

// 2) The call expression — `main()`.
let ModuleChildren::SimpleStatement(call_stmt) = &module.children[1] else {
    panic!("expected a simple statement");
};
let SimpleStatement::ExpressionStatement(expr) = call_stmt.as_ref() else {
    panic!("expected an expression statement");
};
assert_eq!(expr.children.len(), 1);