treesitter_types_python/lib.rs
1//! Strongly-typed AST types for Python, auto-generated from
2//! [`tree-sitter-python`](https://docs.rs/tree-sitter-python)'s `node-types.json`.
3//!
4//! This crate is generated by [`treesitter-types`](https://docs.rs/treesitter-types) and is
5//! automatically kept up to date when a new version of the grammar crate is released.
6//!
7//! These types have been tested by parsing the
8//! [CPython](https://github.com/python/cpython) source code.
9//!
10//! See the [Tree-sitter](https://tree-sitter.github.io/tree-sitter/) project for more
11//! information about the underlying parser framework.
12//!
13//! # Example
14//!
15//! ```
16//! use treesitter_types_python::*;
17//!
18//! // A minimal Python hello-world program.
19//! let src = b"\
20//! def main():
21//! print(\"Hello, World!\")
22//!
23//! main()
24//! ";
25//!
26//! // Parse the source with tree-sitter and convert into typed AST.
27//! let mut parser = tree_sitter::Parser::new();
28//! parser.set_language(&tree_sitter_python::LANGUAGE.into()).unwrap();
29//! let tree = parser.parse(src, None).unwrap();
30//! let module = Module::from_node(tree.root_node(), src).unwrap();
31//!
32//! // The module has two top-level children.
33//! assert_eq!(module.children.len(), 2);
34//!
35//! // 1) The function definition — `def main(): ...`.
36//! let ModuleChildren::CompoundStatement(stmt) = &module.children[0] else {
37//! panic!("expected a compound statement");
38//! };
39//! let CompoundStatement::FunctionDefinition(func) = stmt.as_ref() else {
40//! panic!("expected a function definition");
41//! };
42//! assert_eq!(func.name.text(), "main");
43//! assert!(func.parameters.children.is_empty()); // no parameters
44//! assert!(func.return_type.is_none()); // no return type annotation
45//!
46//! // 2) The call expression — `main()`.
47//! let ModuleChildren::SimpleStatement(call_stmt) = &module.children[1] else {
48//! panic!("expected a simple statement");
49//! };
50//! let SimpleStatement::ExpressionStatement(expr) = call_stmt.as_ref() else {
51//! panic!("expected an expression statement");
52//! };
53//! assert_eq!(expr.children.len(), 1);
54//! ```
55
56pub use tree_sitter_python;
57pub use treesitter_types::tree_sitter;
58pub use treesitter_types::{FromNode, LeafNode, ParseError, Span, Spanned};
59
60mod generated;
61pub use generated::*;