tree_sitter_toml/
lib.rs

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