aboutsummaryrefslogtreecommitdiffstats
path: root/tree-sitter-yts/bindings/rust
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--tree-sitter-yts/bindings/rust/build.rs84
-rw-r--r--tree-sitter-yts/bindings/rust/lib.rs70
2 files changed, 79 insertions, 75 deletions
diff --git a/tree-sitter-yts/bindings/rust/build.rs b/tree-sitter-yts/bindings/rust/build.rs
index 075dc92..95250d7 100644
--- a/tree-sitter-yts/bindings/rust/build.rs
+++ b/tree-sitter-yts/bindings/rust/build.rs
@@ -1,50 +1,56 @@
-// yt - A fully featured command line YouTube client
-//
-// Copyright (C) 2024 Benedikt Peetz <benedikt.peetz@b-peetz.de>
-// SPDX-License-Identifier: GPL-3.0-or-later
-//
-// This file is part of Yt.
-//
-// You should have received a copy of the License along with this program.
-// If not, see <https://www.gnu.org/licenses/gpl-3.0.txt>.
-
fn main() {
let src_dir = std::path::Path::new("src");
let mut c_config = cc::Build::new();
- c_config.include(&src_dir);
- c_config
- .flag_if_supported("-Wno-unused-parameter")
- .flag_if_supported("-Wno-unused-but-set-variable")
- .flag_if_supported("-Wno-trigraphs");
- let parser_path = src_dir.join("parser.c");
- c_config.file(&parser_path);
+ c_config.std("c11").include(src_dir);
- // If your language uses an external scanner written in C,
- // then include this block of code:
+ #[cfg(target_env = "msvc")]
+ c_config.flag("-utf-8");
- /*
- let scanner_path = src_dir.join("scanner.c");
- c_config.file(&scanner_path);
- println!("cargo:rerun-if-changed={}", scanner_path.to_str().unwrap());
- */
+ if std::env::var("TARGET").unwrap() == "wasm32-unknown-unknown" {
+ let Ok(wasm_headers) = std::env::var("DEP_TREE_SITTER_LANGUAGE_WASM_HEADERS") else {
+ panic!("Environment variable DEP_TREE_SITTER_LANGUAGE_WASM_HEADERS must be set by the language crate");
+ };
+ let Ok(wasm_src) =
+ std::env::var("DEP_TREE_SITTER_LANGUAGE_WASM_SRC").map(std::path::PathBuf::from)
+ else {
+ panic!("Environment variable DEP_TREE_SITTER_LANGUAGE_WASM_SRC must be set by the language crate");
+ };
+
+ c_config.include(&wasm_headers);
+ c_config.files([
+ wasm_src.join("stdio.c"),
+ wasm_src.join("stdlib.c"),
+ wasm_src.join("string.c"),
+ ]);
+ }
- c_config.compile("parser");
+ let parser_path = src_dir.join("parser.c");
+ c_config.file(&parser_path);
println!("cargo:rerun-if-changed={}", parser_path.to_str().unwrap());
- // If your language uses an external scanner written in C++,
- // then include this block of code:
+ let scanner_path = src_dir.join("scanner.c");
+ if scanner_path.exists() {
+ c_config.file(&scanner_path);
+ println!("cargo:rerun-if-changed={}", scanner_path.to_str().unwrap());
+ }
+
+ c_config.compile("tree-sitter-yts");
- /*
- let mut cpp_config = cc::Build::new();
- cpp_config.cpp(true);
- cpp_config.include(&src_dir);
- cpp_config
- .flag_if_supported("-Wno-unused-parameter")
- .flag_if_supported("-Wno-unused-but-set-variable");
- let scanner_path = src_dir.join("scanner.cc");
- cpp_config.file(&scanner_path);
- cpp_config.compile("scanner");
- println!("cargo:rerun-if-changed={}", scanner_path.to_str().unwrap());
- */
+ println!("cargo:rustc-check-cfg=cfg(with_highlights_query)");
+ if !"queries/highlights.scm".is_empty() && std::path::Path::new("queries/highlights.scm").exists() {
+ println!("cargo:rustc-cfg=with_highlights_query");
+ }
+ println!("cargo:rustc-check-cfg=cfg(with_injections_query)");
+ if !"queries/injections.scm".is_empty() && std::path::Path::new("queries/injections.scm").exists() {
+ println!("cargo:rustc-cfg=with_injections_query");
+ }
+ println!("cargo:rustc-check-cfg=cfg(with_locals_query)");
+ if !"queries/locals.scm".is_empty() && std::path::Path::new("queries/locals.scm").exists() {
+ println!("cargo:rustc-cfg=with_locals_query");
+ }
+ println!("cargo:rustc-check-cfg=cfg(with_tags_query)");
+ if !"queries/tags.scm".is_empty() && std::path::Path::new("queries/tags.scm").exists() {
+ println!("cargo:rustc-cfg=with_tags_query");
+ }
}
diff --git a/tree-sitter-yts/bindings/rust/lib.rs b/tree-sitter-yts/bindings/rust/lib.rs
index 0880238..35f14ce 100644
--- a/tree-sitter-yts/bindings/rust/lib.rs
+++ b/tree-sitter-yts/bindings/rust/lib.rs
@@ -1,54 +1,52 @@
-// yt - A fully featured command line YouTube client
-//
-// Copyright (C) 2024 Benedikt Peetz <benedikt.peetz@b-peetz.de>
-// SPDX-License-Identifier: GPL-3.0-or-later
-//
-// This file is part of Yt.
-//
-// You should have received a copy of the License along with this program.
-// If not, see <https://www.gnu.org/licenses/gpl-3.0.txt>.
-
-//! This crate provides yts language support for the [tree-sitter][] parsing library.
+//! This crate provides YTS language support for the [tree-sitter] parsing library.
//!
-//! Typically, you will use the [language][language func] function to add this language to a
-//! tree-sitter [Parser][], and then use the parser to parse some code:
+//! Typically, you will use the [`LANGUAGE`] constant to add this language to a
+//! tree-sitter [`Parser`], and then use the parser to parse some code:
//!
//! ```
-//! let code = "";
+//! let code = r#"
+//! "#;
//! let mut parser = tree_sitter::Parser::new();
-//! parser.set_language(tree_sitter_yts::language()).expect("Error loading yts grammar");
+//! let language = tree_sitter_YTS::LANGUAGE;
+//! parser
+//! .set_language(&language.into())
+//! .expect("Error loading YTS parser");
//! let tree = parser.parse(code, None).unwrap();
+//! assert!(!tree.root_node().has_error());
//! ```
//!
-//! [Language]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Language.html
-//! [language func]: fn.language.html
-//! [Parser]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Parser.html
+//! [`Parser`]: https://docs.rs/tree-sitter/0.26.8/tree_sitter/struct.Parser.html
//! [tree-sitter]: https://tree-sitter.github.io/
-use tree_sitter::Language;
+use tree_sitter_language::LanguageFn;
extern "C" {
- fn tree_sitter_yts() -> Language;
+ fn tree_sitter_YTS() -> *const ();
}
-/// Get the tree-sitter [Language][] for this grammar.
-///
-/// [Language]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Language.html
-pub fn language() -> Language {
- unsafe { tree_sitter_yts() }
-}
+/// The tree-sitter [`LanguageFn`] for this grammar.
+pub const LANGUAGE: LanguageFn = unsafe { LanguageFn::from_raw(tree_sitter_YTS) };
-/// The content of the [`node-types.json`][] file for this grammar.
+/// The content of the [`node-types.json`] file for this grammar.
///
-/// [`node-types.json`]: https://tree-sitter.github.io/tree-sitter/using-parsers#static-node-types
-pub const NODE_TYPES: &'static str = include_str!("../../src/node-types.json");
+/// [`node-types.json`]: https://tree-sitter.github.io/tree-sitter/using-parsers/6-static-node-types
+pub const NODE_TYPES: &str = include_str!("../../src/node-types.json");
+
+#[cfg(with_highlights_query)]
+/// The syntax highlighting query for this grammar.
+pub const HIGHLIGHTS_QUERY: &str = include_str!("../../queries/highlights.scm");
+
+#[cfg(with_injections_query)]
+/// The language injection query for this grammar.
+pub const INJECTIONS_QUERY: &str = include_str!("../../queries/injections.scm");
-// Uncomment these to include any queries that this grammar contains
+#[cfg(with_locals_query)]
+/// The local variable query for this grammar.
+pub const LOCALS_QUERY: &str = include_str!("../../queries/locals.scm");
-// pub const HIGHLIGHTS_QUERY: &'static str = include_str!("../../queries/highlights.scm");
-// pub const INJECTIONS_QUERY: &'static str = include_str!("../../queries/injections.scm");
-// pub const LOCALS_QUERY: &'static str = include_str!("../../queries/locals.scm");
-// pub const TAGS_QUERY: &'static str = include_str!("../../queries/tags.scm");
+#[cfg(with_tags_query)]
+/// The symbol tagging query for this grammar.
+pub const TAGS_QUERY: &str = include_str!("../../queries/tags.scm");
#[cfg(test)]
mod tests {
@@ -56,7 +54,7 @@ mod tests {
fn test_can_load_grammar() {
let mut parser = tree_sitter::Parser::new();
parser
- .set_language(super::language())
- .expect("Error loading yts language");
+ .set_language(&super::LANGUAGE.into())
+ .expect("Error loading YTS parser");
}
}