2020-05-12 16:16:48 -07:00
|
|
|
# Rust Tree-sitter
|
2018-05-17 14:35:31 -07:00
|
|
|
|
docs: update badges; fix markdown lint complains
Linter config `.vscode/settings.json`:
```json
{
"[markdown]": {
"files.trimTrailingWhitespace": false,
},
"markdownlint.config": {
"default": true,
// "ul-style": {
// "style": "asterisk"
// },
"MD001": false,
"MD024": false,
"MD025": false,
"MD033": false,
"MD041": false,
"MD053": false,
},
}
```
2023-04-16 21:14:19 +03:00
|
|
|
[![crates.io badge]][crates.io]
|
|
|
|
|
|
|
|
|
|
[crates.io]: https://crates.io/crates/tree-sitter
|
|
|
|
|
[crates.io badge]: https://img.shields.io/crates/v/tree-sitter.svg?color=%23B48723
|
2018-05-17 14:35:31 -07:00
|
|
|
|
|
|
|
|
Rust bindings to the [Tree-sitter][] parsing library.
|
|
|
|
|
|
2023-07-24 02:07:01 -04:00
|
|
|
## Basic Usage
|
2018-05-18 11:42:13 -07:00
|
|
|
|
|
|
|
|
First, create a parser:
|
|
|
|
|
|
|
|
|
|
```rust
|
2024-04-19 02:25:42 +09:00
|
|
|
use tree_sitter::{InputEdit, Language, Parser, Point};
|
2018-06-19 16:19:37 -07:00
|
|
|
|
2018-06-28 10:25:01 +02:00
|
|
|
let mut parser = Parser::new();
|
2018-05-18 11:42:13 -07:00
|
|
|
```
|
|
|
|
|
|
2020-03-30 18:43:41 +02:00
|
|
|
Add the `cc` crate to your `Cargo.toml` under `[build-dependencies]`:
|
2020-05-12 16:16:48 -07:00
|
|
|
|
2020-03-30 18:43:41 +02:00
|
|
|
```toml
|
|
|
|
|
[build-dependencies]
|
|
|
|
|
cc="*"
|
|
|
|
|
```
|
|
|
|
|
|
2023-07-24 02:07:01 -04:00
|
|
|
Then, add a language as a dependency:
|
2018-05-18 11:42:13 -07:00
|
|
|
|
2023-07-24 02:07:01 -04:00
|
|
|
```toml
|
|
|
|
|
[dependencies]
|
2024-04-19 02:25:42 +09:00
|
|
|
tree-sitter = "0.22"
|
|
|
|
|
tree-sitter-rust = "0.21"
|
2023-07-24 02:07:01 -04:00
|
|
|
```
|
2018-05-18 11:42:13 -07:00
|
|
|
|
2023-07-24 02:07:01 -04:00
|
|
|
To then use a language, you assign them to the parser.
|
|
|
|
|
|
|
|
|
|
```rust
|
2024-04-19 02:25:42 +09:00
|
|
|
parser.set_language(&tree_sitter_rust::language()).expect("Error loading Rust grammar");
|
2018-05-18 11:42:13 -07:00
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Now you can parse source code:
|
|
|
|
|
|
|
|
|
|
```rust
|
|
|
|
|
let source_code = "fn test() {}";
|
2024-04-19 02:25:42 +09:00
|
|
|
let mut tree = parser.parse(source_code, None).unwrap();
|
2018-05-18 11:42:13 -07:00
|
|
|
let root_node = tree.root_node();
|
2018-06-19 16:19:37 -07:00
|
|
|
|
2018-05-18 11:42:13 -07:00
|
|
|
assert_eq!(root_node.kind(), "source_file");
|
|
|
|
|
assert_eq!(root_node.start_position().column, 0);
|
|
|
|
|
assert_eq!(root_node.end_position().column, 12);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Editing
|
|
|
|
|
|
2023-07-24 02:07:01 -04:00
|
|
|
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:
|
2018-05-18 11:42:13 -07:00
|
|
|
|
|
|
|
|
```rust
|
2024-04-19 02:25:42 +09:00
|
|
|
let new_source_code = "fn test(a: u32) {}";
|
2018-05-18 11:42:13 -07:00
|
|
|
|
2024-02-08 03:24:02 +07:00
|
|
|
tree.edit(&InputEdit {
|
2018-05-18 11:42:13 -07:00
|
|
|
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),
|
|
|
|
|
});
|
2018-06-19 16:19:37 -07:00
|
|
|
|
2019-02-05 10:59:31 -08:00
|
|
|
let new_tree = parser.parse(new_source_code, Some(&tree));
|
2018-05-18 11:42:13 -07:00
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Text Input
|
|
|
|
|
|
2023-07-24 02:07:01 -04:00
|
|
|
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:
|
2018-05-18 11:42:13 -07:00
|
|
|
|
|
|
|
|
```rust
|
2018-06-19 16:19:37 -07:00
|
|
|
// 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.
|
2024-04-19 02:25:42 +09:00
|
|
|
let tree = parser.parse_with(&mut |_byte: usize, position: Point| -> &[u8] {
|
2018-06-19 16:19:37 -07:00
|
|
|
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..]
|
2018-05-18 11:42:13 -07:00
|
|
|
} else {
|
2024-02-29 00:49:09 -05:00
|
|
|
b"\n"
|
2018-05-18 11:42:13 -07:00
|
|
|
}
|
2018-06-19 16:19:37 -07:00
|
|
|
} else {
|
|
|
|
|
&[]
|
2018-05-18 11:42:13 -07:00
|
|
|
}
|
2018-06-19 16:19:37 -07:00
|
|
|
}, None).unwrap();
|
2018-05-18 11:42:13 -07:00
|
|
|
|
2018-06-19 16:19:37 -07:00
|
|
|
assert_eq!(
|
|
|
|
|
tree.root_node().to_sexp(),
|
|
|
|
|
"(source_file (function_item (visibility_modifier) (identifier) (parameters) (block (number_literal))))"
|
|
|
|
|
);
|
2018-05-18 11:42:13 -07:00
|
|
|
```
|
|
|
|
|
|
2018-05-17 14:35:31 -07:00
|
|
|
[tree-sitter]: https://github.com/tree-sitter/tree-sitter
|
2024-07-28 10:58:29 +03:00
|
|
|
|
|
|
|
|
## Features
|
|
|
|
|
|
|
|
|
|
- **std** - This feature is enabled by default and allows `tree-sitter` to use the standard library.
|
|
|
|
|
- Error types implement the `std::error:Error` trait.
|
|
|
|
|
- `regex` performance optimizations are enabled.
|
|
|
|
|
- The DOT graph methods are enabled.
|
|
|
|
|
- **wasm** - This feature is enabled for Wasm targets. `tree-sitter` to be built for Wasm targets using the `wasmtime-c-api` crate.
|