tree_sitter_strings/lib.rs
1// -*- coding: utf-8 -*-
2// ------------------------------------------------------------------------------------------------
3// Copyright © 2020, tree-sitter-strings authors.
4// See the LICENSE file in this repo for license details.
5// ------------------------------------------------------------------------------------------------
6
7//! This crate provides a strings 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//! class Test {
17//! int double(int x) {
18//! return x * 2;
19//! }
20//! }
21//! "#;
22//! let mut parser = Parser::new();
23//! parser.set_language(tree_sitter_strings::language()).expect("Error loading strings grammar");
24//! let parsed = parser.parse(code, None);
25//! # let parsed = parsed.unwrap();
26//! # let root = parsed.root_node();
27//! # assert!(!root.has_error());
28//! ```
29//!
30//! [Language]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Language.html
31//! [language func]: fn.language.html
32//! [Parser]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Parser.html
33//! [tree-sitter]: https://tree-sitter.github.io/
34
35use tree_sitter::Language;
36
37extern "C" {
38 fn tree_sitter_strings() -> Language;
39}
40
41/// Returns the tree-sitter [Language][] for this grammar.
42///
43/// [Language]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Language.html
44pub fn language() -> Language {
45 unsafe { tree_sitter_strings() }
46}
47
48/// The source of the strings tree-sitter grammar description.
49pub const GRAMMAR: &'static str = include_str!("../../grammar.js");
50
51/// The syntax highlighting query for this language.
52pub const HIGHLIGHT_QUERY: &'static str = include_str!("../../queries/highlights.scm");
53
54/// The content of the [`node-types.json`][] file for this grammar.
55///
56/// [`node-types.json`]: https://tree-sitter.github.io/tree-sitter/using-parsers#static-node-types
57pub const NODE_TYPES: &'static str = include_str!("../../src/node-types.json");
58
59/// The symbol tagging query for this language.
60pub const TAGGING_QUERY: &'static str = include_str!("../../queries/tags.scm");
61
62#[cfg(test)]
63mod tests {
64 #[test]
65 fn can_load_grammar() {
66 let mut parser = tree_sitter::Parser::new();
67 parser
68 .set_language(super::language())
69 .expect("Error loading Strings grammar");
70 }
71}