Skip to content

Commit 1be73c2

Browse files
authored
Add Rust package (#103)
1 parent ab7f796 commit 1be73c2

File tree

5 files changed

+153
-0
lines changed

5 files changed

+153
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
Cargo.lock
12
node_modules
23
build
34
*.log
45
package-lock.json
56
examples
67
!examples/ast.rs
8+
/target/

Cargo.toml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
[package]
2+
name = "tree-sitter-rust"
3+
description = "Rust grammar for the tree-sitter parsing library"
4+
version = "0.16.0"
5+
authors = ["Max Brunsfeld <[email protected]>"]
6+
license = "MIT"
7+
readme = "bindings/rust/README.md"
8+
keywords = ["incremental", "parsing", "rust"]
9+
categories = ["parsing", "text-editors"]
10+
repository = "https://github.com/tree-sitter/tree-sitter-rust"
11+
edition = "2018"
12+
13+
build = "bindings/rust/build.rs"
14+
include = [
15+
"bindings/rust/*",
16+
"grammar.js",
17+
"queries/*",
18+
"src/*",
19+
]
20+
autoexamples = false
21+
22+
[lib]
23+
path = "bindings/rust/lib.rs"
24+
25+
[dependencies]
26+
tree-sitter = "0.17"
27+
28+
[build-dependencies]
29+
cc = "1.0"

bindings/rust/README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# tree-sitter-rust
2+
3+
This crate provides a Rust grammar for the [tree-sitter][] parsing library. To
4+
use this crate, add it to the `[dependencies]` section of your `Cargo.toml`
5+
file. (Note that you will probably also need to depend on the
6+
[`tree-sitter`][tree-sitter crate] crate to use the parsed result in any useful
7+
way.)
8+
9+
``` toml
10+
[dependencies]
11+
tree-sitter = "0.17"
12+
tree-sitter-rust = "0.16"
13+
```
14+
15+
Typically, you will use the [language][language func] function to add this
16+
grammar to a tree-sitter [Parser][], and then use the parser to parse some code:
17+
18+
``` rust
19+
let code = r#"
20+
fn double(x: i32) -> i32 {
21+
x * 2
22+
}
23+
"#;
24+
let mut parser = Parser::new();
25+
parser.set_language(tree_sitter_rust::language()).expect("Error loading Rust grammar");
26+
let parsed = parser.parse(code, None);
27+
```
28+
29+
If you have any questions, please reach out to us in the [tree-sitter
30+
discussions] page.
31+
32+
[Language]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Language.html
33+
[language func]: https://docs.rs/tree-sitter-rust/*/tree_sitter_rust/fn.language.html
34+
[Parser]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Parser.html
35+
[tree-sitter]: https://tree-sitter.github.io/
36+
[tree-sitter crate]: https://crates.io/crates/tree-sitter
37+
[tree-sitter discussions]: https://github.com/tree-sitter/tree-sitter/discussions

bindings/rust/build.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use std::path::Path;
2+
extern crate cc;
3+
4+
fn main() {
5+
let src_dir = Path::new("src");
6+
7+
let mut c_config = cc::Build::new();
8+
c_config.include(&src_dir);
9+
c_config
10+
.flag_if_supported("-Wno-unused-parameter")
11+
.flag_if_supported("-Wno-unused-but-set-variable")
12+
.flag_if_supported("-Wno-trigraphs");
13+
let parser_path = src_dir.join("parser.c");
14+
c_config.file(&parser_path);
15+
let scanner_path = src_dir.join("scanner.c");
16+
c_config.file(&scanner_path);
17+
println!("cargo:rerun-if-changed={}", parser_path.to_str().unwrap());
18+
c_config.compile("parser-scanner");
19+
}

bindings/rust/lib.rs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// -*- coding: utf-8 -*-
2+
// ------------------------------------------------------------------------------------------------
3+
// Copyright © 2021, tree-sitter-rust authors.
4+
// See the LICENSE file in this repo for license details.
5+
// ------------------------------------------------------------------------------------------------
6+
7+
//! This crate provides a Rust grammar for the [tree-sitter][] parsing library.
8+
//!
9+
//! Typically, you will use the [language][language func] function to add this grammar to a
10+
//! tree-sitter [Parser][], and then use the parser to parse some code:
11+
//!
12+
//! ```
13+
//! use tree_sitter::Parser;
14+
//!
15+
//! let code = r#"
16+
//! fn double(x: i32) -> i32 {
17+
//! x * 2
18+
//! }
19+
//! "#;
20+
//! let mut parser = Parser::new();
21+
//! parser.set_language(tree_sitter_rust::language()).expect("Error loading Rust grammar");
22+
//! let parsed = parser.parse(code, None);
23+
//! # let parsed = parsed.unwrap();
24+
//! # let root = parsed.root_node();
25+
//! # assert!(!root.has_error());
26+
//! ```
27+
//!
28+
//! [Language]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Language.html
29+
//! [language func]: fn.language.html
30+
//! [Parser]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Parser.html
31+
//! [tree-sitter]: https://tree-sitter.github.io/
32+
33+
use tree_sitter::Language;
34+
35+
extern "C" {
36+
fn tree_sitter_rust() -> Language;
37+
}
38+
39+
/// Returns the tree-sitter [Language][] for this grammar.
40+
///
41+
/// [Language]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Language.html
42+
pub fn language() -> Language {
43+
unsafe { tree_sitter_rust() }
44+
}
45+
46+
/// The source of the Rust tree-sitter grammar description.
47+
pub const GRAMMAR: &str = include_str!("../../grammar.js");
48+
49+
/// The syntax highlighting query for this language.
50+
pub const HIGHLIGHT_QUERY: &str = include_str!("../../queries/highlights.scm");
51+
52+
/// The content of the [`node-types.json`][] file for this grammar.
53+
///
54+
/// [`node-types.json`]: https://tree-sitter.github.io/tree-sitter/using-parsers#static-node-types
55+
pub const NODE_TYPES: &str = include_str!("../../src/node-types.json");
56+
57+
#[cfg(test)]
58+
mod tests {
59+
#[test]
60+
fn can_load_grammar() {
61+
let mut parser = tree_sitter::Parser::new();
62+
parser
63+
.set_language(super::language())
64+
.expect("Error loading Rust grammar");
65+
}
66+
}

0 commit comments

Comments
 (0)