From 70d0524023e7f230503cf8702a0f8cad6142e06d Mon Sep 17 00:00:00 2001 From: Luni-4 Date: Tue, 23 Feb 2021 16:27:12 +0100 Subject: [PATCH 1/4] tree-sitter-preproc: Add Rust bindings --- tree-sitter-preproc/.gitignore | 2 + tree-sitter-preproc/Cargo.toml | 27 +++++++++ tree-sitter-preproc/bindings/rust/README.md | 37 ++++++++++++ tree-sitter-preproc/bindings/rust/build.rs | 29 ++++++++++ tree-sitter-preproc/bindings/rust/lib.rs | 64 +++++++++++++++++++++ 5 files changed, 159 insertions(+) create mode 100644 tree-sitter-preproc/Cargo.toml create mode 100644 tree-sitter-preproc/bindings/rust/README.md create mode 100644 tree-sitter-preproc/bindings/rust/build.rs create mode 100644 tree-sitter-preproc/bindings/rust/lib.rs diff --git a/tree-sitter-preproc/.gitignore b/tree-sitter-preproc/.gitignore index b736b4a18..18ca5b037 100644 --- a/tree-sitter-preproc/.gitignore +++ b/tree-sitter-preproc/.gitignore @@ -1,3 +1,5 @@ +Cargo.lock node_modules build package-lock.json +/target/ diff --git a/tree-sitter-preproc/Cargo.toml b/tree-sitter-preproc/Cargo.toml new file mode 100644 index 000000000..cdc835919 --- /dev/null +++ b/tree-sitter-preproc/Cargo.toml @@ -0,0 +1,27 @@ +[package] +name = "tree-sitter-preproc" +description = "Preproc grammar for the tree-sitter parsing library" +version = "0.16.0" +authors = ["Calixte Denizet "] +license = "MIT" +readme = "bindings/rust/README.md" +keywords = ["incremental", "parsing", "preproc"] +categories = ["parsing", "text-editors"] +repository = "https://github.com/tree-sitter/tree-sitter-preproc" +edition = "2018" + +build = "bindings/rust/build.rs" +include = [ + "bindings/rust/*", + "grammar.js", + "src/*", +] + +[lib] +path = "bindings/rust/lib.rs" + +[dependencies] +tree-sitter = "^0.17" + +[build-dependencies] +cc = "^1.0" diff --git a/tree-sitter-preproc/bindings/rust/README.md b/tree-sitter-preproc/bindings/rust/README.md new file mode 100644 index 000000000..153d5ecc5 --- /dev/null +++ b/tree-sitter-preproc/bindings/rust/README.md @@ -0,0 +1,37 @@ +# tree-sitter-preproc + +This crate provides a Preproc grammar for the [tree-sitter][] parsing library. To +use this crate, add it to the `[dependencies]` section of your `Cargo.toml` +file. (Note that you will probably also need to depend on the +[`tree-sitter`][tree-sitter crate] crate to use the parsed result in any useful +way.) + +``` toml +[dependencies] +tree-sitter = "0.17" +tree-sitter-preproc = "0.16" +``` + +Typically, you will use the [language][language func] function to add this +grammar to a tree-sitter [Parser][], and then use the parser to parse some code: + +``` rust +let code = r#" + int double(int x) { + return x * 2; + } +"#; +let mut parser = Parser::new(); +parser.set_language(tree_sitter_preproc::language()).expect("Error loading Preproc grammar"); +let parsed = parser.parse(code, None); +``` + +If you have any questions, please reach out to us in the [tree-sitter +discussions] page. + +[Language]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Language.html +[language func]: https://docs.rs/tree-sitter-preproc/*/tree_sitter_preproc/fn.language.html +[Parser]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Parser.html +[tree-sitter]: https://tree-sitter.github.io/ +[tree-sitter crate]: https://crates.io/crates/tree-sitter +[tree-sitter discussions]: https://github.com/tree-sitter/tree-sitter/discussions diff --git a/tree-sitter-preproc/bindings/rust/build.rs b/tree-sitter-preproc/bindings/rust/build.rs new file mode 100644 index 000000000..19e7949a0 --- /dev/null +++ b/tree-sitter-preproc/bindings/rust/build.rs @@ -0,0 +1,29 @@ +// Adapted from tree-sitter-python bindings +use std::path::Path; +extern crate cc; + +fn main() { + let src_dir = 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); + println!("cargo:rerun-if-changed={}", parser_path.to_str().unwrap()); + c_config.compile("parser"); + + 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); + println!("cargo:rerun-if-changed={}", scanner_path.to_str().unwrap()); + cpp_config.compile("scanner"); +} diff --git a/tree-sitter-preproc/bindings/rust/lib.rs b/tree-sitter-preproc/bindings/rust/lib.rs new file mode 100644 index 000000000..be6502cdf --- /dev/null +++ b/tree-sitter-preproc/bindings/rust/lib.rs @@ -0,0 +1,64 @@ +// Adapted from tree-sitter-python bindings +// -*- coding: utf-8 -*- +// ------------------------------------------------------------------------------------------------ +// Copyright © 2021, tree-sitter-preproc authors. +// See the LICENSE file in this repo for license details. +// ------------------------------------------------------------------------------------------------ + +//! This crate provides a Preproc grammar for the [tree-sitter][] parsing library. +//! +//! Typically, you will use the [language][language func] function to add this grammar to a +//! tree-sitter [Parser][], and then use the parser to parse some code: +//! +//! ``` +//! use tree_sitter::Parser; +//! +//! let code = r#" +//! int double(int x) { +//! return x * 2; +//! } +//! "#; +//! let mut parser = Parser::new(); +//! parser.set_language(tree_sitter_preproc::language()).expect("Error loading Preproc grammar"); +//! let parsed = parser.parse(code, None); +//! # let parsed = parsed.unwrap(); +//! # let root = parsed.root_node(); +//! # assert!(!root.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 +//! [tree-sitter]: https://tree-sitter.github.io/ + +use tree_sitter::Language; + +extern "C" { + fn tree_sitter_preproc() -> Language; +} + +/// Returns 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_preproc() } +} + +/// The source of the Preproc tree-sitter grammar description. +pub const GRAMMAR: &str = include_str!("../../grammar.js"); + +/// 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: &str = include_str!("../../src/node-types.json"); + +#[cfg(test)] +mod tests { + #[test] + fn can_load_grammar() { + let mut parser = tree_sitter::Parser::new(); + parser + .set_language(super::language()) + .expect("Error loading Preproc grammar"); + } +} From 84cd12937c662450cfa3c39b88683f5776447284 Mon Sep 17 00:00:00 2001 From: Luni-4 Date: Tue, 23 Feb 2021 16:39:11 +0100 Subject: [PATCH 2/4] Add tree-sitter-preproc crate --- Cargo.lock | 9 +++++++++ Cargo.toml | 1 + build.rs | 1 + src/macros.rs | 5 +++++ 4 files changed, 16 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 09027e101..f9ab35e57 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1641,6 +1641,7 @@ dependencies = [ "termcolor", "tree-sitter", "tree-sitter-java", + "tree-sitter-preproc", ] [[package]] @@ -2123,6 +2124,14 @@ dependencies = [ "tree-sitter", ] +[[package]] +name = "tree-sitter-preproc" +version = "0.16.0" +dependencies = [ + "cc", + "tree-sitter", +] + [[package]] name = "trust-dns-proto" version = "0.19.5" diff --git a/Cargo.toml b/Cargo.toml index cb7558bc9..4888ab5f6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,6 +32,7 @@ termcolor = "^1.1" tree-sitter = "^0.17" tree-sitter-java = "^0.16" +tree-sitter-preproc = { path = "./tree-sitter-preproc" } [dev-dependencies] pretty_assertions = "^0.6" diff --git a/build.rs b/build.rs index 3ffe74a12..b8c36caf9 100644 --- a/build.rs +++ b/build.rs @@ -156,6 +156,7 @@ fn main() { mk_predef("c_specials", "SPECIALS"); } let ignore = vec![ + "tree-sitter-preproc".to_string(), "tree-sitter-typescript".to_string(), "tree-sitter-cpp".to_string(), ]; diff --git a/src/macros.rs b/src/macros.rs index 610d6bdfc..2462fe847 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -37,6 +37,11 @@ macro_rules! get_language { tree_sitter_java::language() } }; + (tree_sitter_preproc) => { + fn get_language() -> Language { + tree_sitter_preproc::language() + } + }; ($name:ident) => { fn get_language() -> Language { extern "C" { From 01fa676e9e903a8eb30ae322c078f9dd5b92d30d Mon Sep 17 00:00:00 2001 From: Luni-4 Date: Tue, 23 Feb 2021 16:50:53 +0100 Subject: [PATCH 3/4] enums: Add tree-sitter-preproc crate --- enums/Cargo.toml | 1 + enums/src/macros.rs | 8 ++++---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/enums/Cargo.toml b/enums/Cargo.toml index dc339f26a..72e9c3903 100644 --- a/enums/Cargo.toml +++ b/enums/Cargo.toml @@ -18,3 +18,4 @@ libc = "^0.2" tree-sitter = "^0.17" tree-sitter-java = "^0.16" +tree-sitter-preproc = { path = "../tree-sitter-preproc" } diff --git a/enums/src/macros.rs b/enums/src/macros.rs index b2939f098..9861617db 100644 --- a/enums/src/macros.rs +++ b/enums/src/macros.rs @@ -14,10 +14,10 @@ macro_rules! mk_enum { macro_rules! mk_get_language { ( $( ($camel:ident, $name:ident) ),* ) => { pub fn get_language(lang: &LANG) -> Language { - if let LANG::Java = lang { - tree_sitter_java::language() - } else { - match lang { + match lang { + LANG::Java => tree_sitter_java::language(), + LANG::Preproc => tree_sitter_preproc::language(), + _ => match lang { $( LANG::$camel => { extern "C" { fn $name() -> Language; } From 42a49b7b962613c3e2c7da38473224c68fb5bb49 Mon Sep 17 00:00:00 2001 From: Luni-4 Date: Tue, 23 Feb 2021 16:51:18 +0100 Subject: [PATCH 4/4] enums: Update Cargo.lock --- enums/Cargo.lock | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/enums/Cargo.lock b/enums/Cargo.lock index 9a42a1c55..f365661e3 100644 --- a/enums/Cargo.lock +++ b/enums/Cargo.lock @@ -164,6 +164,7 @@ dependencies = [ "phf_codegen", "tree-sitter", "tree-sitter-java", + "tree-sitter-preproc", ] [[package]] @@ -492,6 +493,14 @@ dependencies = [ "tree-sitter", ] +[[package]] +name = "tree-sitter-preproc" +version = "0.16.0" +dependencies = [ + "cc", + "tree-sitter", +] + [[package]] name = "unicode-width" version = "0.1.8"