tree_sitter_python/lib.rs
1//! This crate provides Python language support for the [tree-sitter] parsing library.
2//!
3//! Typically, you will use the [`LANGUAGE`] constant to add this language to a
4//! tree-sitter [`Parser`], and then use the parser to parse some code:
5//!
6//! ```
7//! let code = r#"
8//! def double(x):
9//! return x * 2
10//! "#;
11//! let mut parser = tree_sitter::Parser::new();
12//! let language = tree_sitter_python::LANGUAGE;
13//! parser
14//! .set_language(&language.into())
15//! .expect("Error loading Python parser");
16//! let tree = parser.parse(code, None).unwrap();
17//! assert!(!tree.root_node().has_error());
18//! ```
19//!
20//! [`Parser`]: https://docs.rs/tree-sitter/0.25.8/tree_sitter/struct.Parser.html
21//! [tree-sitter]: https://tree-sitter.github.io/
22
23use tree_sitter_language::LanguageFn;
24
25extern "C" {
26 fn tree_sitter_python() -> *const ();
27}
28
29/// The tree-sitter [`LanguageFn`] for this grammar.
30pub const LANGUAGE: LanguageFn = unsafe { LanguageFn::from_raw(tree_sitter_python) };
31
32/// The content of the [`node-types.json`] file for this grammar.
33///
34/// [`node-types.json`]: https://tree-sitter.github.io/tree-sitter/using-parsers/6-static-node-types
35pub const NODE_TYPES: &str = include_str!("../../src/node-types.json");
36
37/// The syntax highlighting query for this language.
38pub const HIGHLIGHTS_QUERY: &str = include_str!("../../queries/highlights.scm");
39
40/// The symbol tagging query for this language.
41pub const TAGS_QUERY: &str = include_str!("../../queries/tags.scm");
42
43#[cfg(test)]
44mod tests {
45 #[test]
46 fn test_can_load_grammar() {
47 let mut parser = tree_sitter::Parser::new();
48 parser
49 .set_language(&super::LANGUAGE.into())
50 .expect("Error loading Python parser");
51 }
52}