tree_sitter_preproc_code_split/lib.rs
1// Adapted from tree-sitter-python bindings
2// -*- coding: utf-8 -*-
3// ------------------------------------------------------------------------------------------------
4// Copyright © 2021, tree-sitter-preproc authors.
5// See the LICENSE file in this repo for license details.
6// ------------------------------------------------------------------------------------------------
7
8//! This crate provides Preproc grammar support for the [tree-sitter][] parsing library.
9//!
10//! Typically, you will use the [LANGUAGE][] constant to add this grammar to a
11//! tree-sitter [Parser][], and then use the parser to parse some code:
12//!
13//! ```
14//! use tree_sitter::Parser;
15//!
16//! let code = r#"
17//! int double(int x) {
18//! return x * 2;
19//! }
20//! "#;
21//! let mut parser = Parser::new();
22//! let language = tree_sitter_preproc::LANGUAGE;
23//! parser
24//! .set_language(&language.into())
25//! .expect("Error loading Preproc parser");
26//! let tree = parser.parse(code, None).unwrap();
27//! assert!(!tree.root_node().has_error());
28//! ```
29//!
30//! [Parser]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Parser.html
31//! [tree-sitter]: https://tree-sitter.github.io/
32
33use tree_sitter_language::LanguageFn;
34
35extern "C" {
36 fn tree_sitter_preproc() -> *const ();
37}
38
39/// The tree-sitter [`LanguageFn`][LanguageFn] for this grammar.
40///
41/// [LanguageFn]: https://docs.rs/tree-sitter-language/*/tree_sitter_language/struct.LanguageFn.html
42pub const LANGUAGE: LanguageFn = unsafe { LanguageFn::from_raw(tree_sitter_preproc) };
43
44/// The source of the Preproc tree-sitter grammar description.
45pub const GRAMMAR: &str = include_str!("../../grammar.js");
46
47/// The content of the [`node-types.json`][] file for this grammar.
48///
49/// [`node-types.json`]: https://tree-sitter.github.io/tree-sitter/using-parsers#static-node-types
50pub const NODE_TYPES: &str = include_str!("../../src/node-types.json");
51
52#[cfg(test)]
53mod tests {
54 #[test]
55 fn test_can_load_grammar() {
56 let mut parser = tree_sitter::Parser::new();
57 parser
58 .set_language(&super::LANGUAGE.into())
59 .expect("Error loading Preproc parser");
60 }
61}