Skip to content

Commit 62e1d99

Browse files
authored
Merge pull request tree-sitter#2424 from amaanq/rust-docs
docs: update Rust bindings' README
2 parents 8204d63 + a4f9395 commit 62e1d99

File tree

1 file changed

+15
-26
lines changed

1 file changed

+15
-26
lines changed

lib/binding_rust/README.md

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
Rust bindings to the [Tree-sitter][] parsing library.
99

10-
### Basic Usage
10+
## Basic Usage
1111

1212
First, create a parser:
1313

@@ -17,38 +17,25 @@ use tree_sitter::{Parser, Language};
1717
let mut parser = Parser::new();
1818
```
1919

20-
Tree-sitter languages consist of generated C code. To make sure they're properly compiled and linked, you can create a [build script](https://doc.rust-lang.org/cargo/reference/build-scripts.html) like the following (assuming `tree-sitter-javascript` is in your root directory):
21-
22-
```rust
23-
use std::path::PathBuf;
24-
25-
fn main() {
26-
let dir: PathBuf = ["tree-sitter-javascript", "src"].iter().collect();
27-
28-
cc::Build::new()
29-
.include(&dir)
30-
.file(dir.join("parser.c"))
31-
.file(dir.join("scanner.c"))
32-
.compile("tree-sitter-javascript");
33-
}
34-
```
35-
3620
Add the `cc` crate to your `Cargo.toml` under `[build-dependencies]`:
3721

3822
```toml
3923
[build-dependencies]
4024
cc="*"
4125
```
4226

43-
To then use languages from rust, you must declare them as `extern "C"` functions and invoke them with `unsafe`. Then you can assign them to the parser.
27+
Then, add a language as a dependency:
4428

45-
```rust
46-
extern "C" { fn tree_sitter_c() -> Language; }
47-
extern "C" { fn tree_sitter_rust() -> Language; }
48-
extern "C" { fn tree_sitter_javascript() -> Language; }
29+
```toml
30+
[dependencies]
31+
tree-sitter = "0.20.10"
32+
tree-sitter-rust = "0.20.3"
33+
```
4934

50-
let language = unsafe { tree_sitter_rust() };
51-
parser.set_language(language).unwrap();
35+
To then use a language, you assign them to the parser.
36+
37+
```rust
38+
parser.set_language(tree_sitter_rust::language()).expect("Error loading Rust grammar");
5239
```
5340

5441
Now you can parse source code:
@@ -65,7 +52,8 @@ assert_eq!(root_node.end_position().column, 12);
6552

6653
### Editing
6754

68-
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:
55+
Once you have a syntax tree, you can update it when your source code changes.
56+
Passing in the previous edited tree makes `parse` run much more quickly:
6957

7058
```rust
7159
let new_source_code = "fn test(a: u32) {}"
@@ -84,7 +72,8 @@ let new_tree = parser.parse(new_source_code, Some(&tree));
8472

8573
### Text Input
8674

87-
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:
75+
The source code to parse can be provided either as a string, a slice, a vector,
76+
or as a function that returns a slice. The text can be encoded as either UTF8 or UTF16:
8877

8978
```rust
9079
// Store some source code in an array of lines.

0 commit comments

Comments
 (0)