Crates.io | tree-sitter |
lib.rs | tree-sitter |
version | |
source | src |
created_at | 2018-05-18 18:58:01.338758 |
updated_at | 2024-12-11 06:49:30.318657 |
description | Rust bindings to the Tree-sitter parsing library |
homepage | https://tree-sitter.github.io/tree-sitter |
repository | https://github.com/tree-sitter/tree-sitter |
max_upload_size | |
id | 66032 |
Cargo.toml error: | TOML parse error at line 29, column 1 | 29 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
Rust bindings to the Tree-sitter parsing library.
First, create a parser:
use tree_sitter::{InputEdit, Language, Parser, Point};
let mut parser = Parser::new();
Add the cc
crate to your Cargo.toml
under [build-dependencies]
:
[build-dependencies]
cc="*"
Then, add a language as a dependency:
[dependencies]
tree-sitter = "0.22"
tree-sitter-rust = "0.21"
To then use a language, you assign them to the parser.
parser.set_language(&tree_sitter_rust::language()).expect("Error loading Rust grammar");
Now you can parse source code:
let source_code = "fn test() {}";
let mut tree = parser.parse(source_code, None).unwrap();
let root_node = tree.root_node();
assert_eq!(root_node.kind(), "source_file");
assert_eq!(root_node.start_position().column, 0);
assert_eq!(root_node.end_position().column, 12);
Once you have a syntax tree, you can update it when your source code changes.
Passing in the previous edited tree makes parse
run much more quickly:
let new_source_code = "fn test(a: u32) {}";
tree.edit(&InputEdit {
start_byte: 8,
old_end_byte: 8,
new_end_byte: 14,
start_position: Point::new(0, 8),
old_end_position: Point::new(0, 8),
new_end_position: Point::new(0, 14),
});
let new_tree = parser.parse(new_source_code, Some(&tree));
The source code to parse can be provided either as a string, a slice, a vector, or as a function that returns a slice. The text can be encoded as either UTF8 or UTF16:
// Store some source code in an array of lines.
let lines = &[
"pub fn foo() {",
" 1",
"}",
];
// Parse the source code using a custom callback. The callback is called
// with both a byte offset and a row/column offset.
let tree = parser.parse_with(&mut |_byte: usize, position: Point| -> &[u8] {
let row = position.row as usize;
let column = position.column as usize;
if row < lines.len() {
if column < lines[row].as_bytes().len() {
&lines[row].as_bytes()[column..]
} else {
b"\n"
}
} else {
&[]
}
}, None).unwrap();
assert_eq!(
tree.root_node().to_sexp(),
"(source_file (function_item (visibility_modifier) (identifier) (parameters) (block (number_literal))))"
);
tree-sitter
to use the standard library.
std::error:Error
trait.regex
performance optimizations are enabled.tree-sitter
to be built for Wasm targets using the wasmtime-c-api
crate.