summaryrefslogtreecommitdiff
path: root/helix-syntax
diff options
context:
space:
mode:
authorBlaž Hrastnik2020-09-09 05:41:12 +0000
committerBlaž Hrastnik2020-09-09 05:41:12 +0000
commit7eac12a4bb02aaa96f63de612077a12df1d5fedc (patch)
treec54b40453427e9f029577d710ebb92325a4d4349 /helix-syntax
parent563e094916b6e0d3ef0fafe8141372116e35ebde (diff)
Add helix-syntax as a wrapper around tree-sitter parsers.
Diffstat (limited to 'helix-syntax')
-rw-r--r--helix-syntax/Cargo.toml14
-rw-r--r--helix-syntax/build.rs126
m---------helix-syntax/languages/tree-sitter-agda0
m---------helix-syntax/languages/tree-sitter-bash0
m---------helix-syntax/languages/tree-sitter-c0
m---------helix-syntax/languages/tree-sitter-c-sharp0
m---------helix-syntax/languages/tree-sitter-cpp0
m---------helix-syntax/languages/tree-sitter-css0
m---------helix-syntax/languages/tree-sitter-go0
m---------helix-syntax/languages/tree-sitter-haskell0
m---------helix-syntax/languages/tree-sitter-html0
m---------helix-syntax/languages/tree-sitter-java0
m---------helix-syntax/languages/tree-sitter-javascript0
m---------helix-syntax/languages/tree-sitter-json0
m---------helix-syntax/languages/tree-sitter-julia0
m---------helix-syntax/languages/tree-sitter-php0
m---------helix-syntax/languages/tree-sitter-python0
m---------helix-syntax/languages/tree-sitter-ruby0
m---------helix-syntax/languages/tree-sitter-rust0
m---------helix-syntax/languages/tree-sitter-scala0
m---------helix-syntax/languages/tree-sitter-swift0
m---------helix-syntax/languages/tree-sitter-typescript0
-rw-r--r--helix-syntax/src/lib.rs87
23 files changed, 227 insertions, 0 deletions
diff --git a/helix-syntax/Cargo.toml b/helix-syntax/Cargo.toml
new file mode 100644
index 00000000..ef8ed863
--- /dev/null
+++ b/helix-syntax/Cargo.toml
@@ -0,0 +1,14 @@
+[package]
+name = "helix-syntax"
+version = "0.1.0"
+authors = ["Blaž Hrastnik <blaz@mxxn.io>"]
+edition = "2018"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
+tree-sitter = "0.16"
+enum-iterator = "0.6"
+
+[build-dependencies]
+cc = { version = "1", features = ["parallel"] }
diff --git a/helix-syntax/build.rs b/helix-syntax/build.rs
new file mode 100644
index 00000000..217ba9fa
--- /dev/null
+++ b/helix-syntax/build.rs
@@ -0,0 +1,126 @@
+use cc::Build;
+
+use std::io::{BufWriter, Read, Write};
+use std::path::{Path, PathBuf};
+use std::{env, fs};
+
+fn get_opt_level() -> u32 {
+ env::var("OPT_LEVEL").unwrap().parse::<u32>().unwrap()
+}
+
+fn get_debug() -> bool {
+ env::var("DEBUG").unwrap() == "true"
+}
+
+fn collect_tree_sitter_dirs(ignore: Vec<String>) -> Vec<String> {
+ let mut dirs = Vec::new();
+ for entry in fs::read_dir("languages").unwrap() {
+ if let Ok(entry) = entry {
+ let path = entry.path();
+ let dir = path.file_name().unwrap().to_str().unwrap().to_string();
+ if !ignore.contains(&dir) {
+ dirs.push(dir);
+ }
+ }
+ }
+ dirs
+}
+
+fn collect_src_files(dir: &str) -> (Vec<String>, Vec<String>) {
+ eprintln!("Collect files for {}", dir);
+
+ let mut c_files = Vec::new();
+ let mut cpp_files = Vec::new();
+ let path = PathBuf::from("languages").join(&dir).join("src");
+ for entry in fs::read_dir(path).unwrap() {
+ if let Ok(entry) = entry {
+ let path = entry.path();
+ if path
+ .file_stem()
+ .unwrap()
+ .to_str()
+ .unwrap()
+ .starts_with("binding")
+ {
+ continue;
+ }
+ if let Some(ext) = path.extension() {
+ if ext == "c" {
+ c_files.push(path.to_str().unwrap().to_string());
+ } else if ext == "cc" || ext == "cpp" || ext == "cxx" {
+ cpp_files.push(path.to_str().unwrap().to_string());
+ }
+ }
+ }
+ }
+ (c_files, cpp_files)
+}
+
+fn build_c(files: Vec<String>, language: &str) {
+ let mut build = cc::Build::new();
+ for file in files {
+ build
+ .file(&file)
+ .include(PathBuf::from(file).parent().unwrap())
+ .pic(true)
+ .opt_level(get_opt_level())
+ .debug(get_debug())
+ .warnings(false)
+ .flag_if_supported("-std=c99");
+ }
+ build.compile(&format!("tree-sitter-{}-c", language));
+}
+
+fn build_cpp(files: Vec<String>, language: &str) {
+ let mut build = cc::Build::new();
+ for file in files {
+ build
+ .file(&file)
+ .include(PathBuf::from(file).parent().unwrap())
+ .pic(true)
+ .opt_level(get_opt_level())
+ .debug(get_debug())
+ .warnings(false)
+ .cpp(true);
+ }
+ build.compile(&format!("tree-sitter-{}-cpp", language));
+}
+
+fn build_dir(dir: &str, language: &str) {
+ println!("Build language {}", language);
+ if PathBuf::from("languages")
+ .join(dir)
+ .read_dir()
+ .unwrap()
+ .next()
+ .is_none()
+ {
+ eprintln!(
+ "The directory {} is empty, did you use 'git clone --recursive'?",
+ dir
+ );
+ eprintln!("You can fix in using 'git submodule init && git submodule update --recursive'.");
+ std::process::exit(1);
+ }
+ let (c, cpp) = collect_src_files(&dir);
+ if !c.is_empty() {
+ build_c(c, &language);
+ }
+ if !cpp.is_empty() {
+ build_cpp(cpp, &language);
+ }
+}
+
+fn main() {
+ let ignore = vec![
+ "tree-sitter-typescript".to_string(),
+ "tree-sitter-cpp".to_string(),
+ ];
+ let dirs = collect_tree_sitter_dirs(ignore);
+ for dir in dirs {
+ let language = &dir[12..]; // skip tree-sitter- prefix
+ build_dir(&dir, &language);
+ }
+ build_dir("tree-sitter-typescript/tsx", "tsx");
+ build_dir("tree-sitter-typescript/typescript", "typescript");
+}
diff --git a/helix-syntax/languages/tree-sitter-agda b/helix-syntax/languages/tree-sitter-agda
new file mode 160000
+Subproject 7fcba5a1b9f83f52a3812e8cdaf16cb60b069af
diff --git a/helix-syntax/languages/tree-sitter-bash b/helix-syntax/languages/tree-sitter-bash
new file mode 160000
+Subproject 8ece09ca4c0b5e59b124cd19fa92c76b1a9e9dd
diff --git a/helix-syntax/languages/tree-sitter-c b/helix-syntax/languages/tree-sitter-c
new file mode 160000
+Subproject 99151b1e9293c9e025498fee7e6691e1a52e1d0
diff --git a/helix-syntax/languages/tree-sitter-c-sharp b/helix-syntax/languages/tree-sitter-c-sharp
new file mode 160000
+Subproject 075a1b2ff5fae3142a9318d9479d568843d2fe5
diff --git a/helix-syntax/languages/tree-sitter-cpp b/helix-syntax/languages/tree-sitter-cpp
new file mode 160000
+Subproject 5e7476bd014445abdae879661e9caf299215478
diff --git a/helix-syntax/languages/tree-sitter-css b/helix-syntax/languages/tree-sitter-css
new file mode 160000
+Subproject 23f2cb97d47860c517f67f03e1f4b621d5bd208
diff --git a/helix-syntax/languages/tree-sitter-go b/helix-syntax/languages/tree-sitter-go
new file mode 160000
+Subproject 34181774b3e86b7801c939c79c7b80a82df91a2
diff --git a/helix-syntax/languages/tree-sitter-haskell b/helix-syntax/languages/tree-sitter-haskell
new file mode 160000
+Subproject 2a0aa1cb5f1b787a4056a29fa0791e87846e33f
diff --git a/helix-syntax/languages/tree-sitter-html b/helix-syntax/languages/tree-sitter-html
new file mode 160000
+Subproject 7f442e1c6163d450c69c75c7a621badc3a0ea98
diff --git a/helix-syntax/languages/tree-sitter-java b/helix-syntax/languages/tree-sitter-java
new file mode 160000
+Subproject ee0a2a076785145e350fbd413775d1e003f7931
diff --git a/helix-syntax/languages/tree-sitter-javascript b/helix-syntax/languages/tree-sitter-javascript
new file mode 160000
+Subproject feca6ec5e577fa30766f0c0a1e03d32c073027f
diff --git a/helix-syntax/languages/tree-sitter-json b/helix-syntax/languages/tree-sitter-json
new file mode 160000
+Subproject d3976b27df8622ed17bef6dd5e358b398e73c67
diff --git a/helix-syntax/languages/tree-sitter-julia b/helix-syntax/languages/tree-sitter-julia
new file mode 160000
+Subproject 6a0863f1ce3fcf6f99dc0addb7886dcbd27c5a4
diff --git a/helix-syntax/languages/tree-sitter-php b/helix-syntax/languages/tree-sitter-php
new file mode 160000
+Subproject b0c0367d4b7058921fdc4ba11e257441a64ab80
diff --git a/helix-syntax/languages/tree-sitter-python b/helix-syntax/languages/tree-sitter-python
new file mode 160000
+Subproject 58f57240834d6b88624e32ad0ab9531d55fb7a5
diff --git a/helix-syntax/languages/tree-sitter-ruby b/helix-syntax/languages/tree-sitter-ruby
new file mode 160000
+Subproject 14a5e56a6fff1f2d40c151ae38b5581fc5c4457
diff --git a/helix-syntax/languages/tree-sitter-rust b/helix-syntax/languages/tree-sitter-rust
new file mode 160000
+Subproject 40620bf4097cbc9cea79504d7e877865df43a19
diff --git a/helix-syntax/languages/tree-sitter-scala b/helix-syntax/languages/tree-sitter-scala
new file mode 160000
+Subproject 211bb726bb5857f872247b600c7c1808e641a8d
diff --git a/helix-syntax/languages/tree-sitter-swift b/helix-syntax/languages/tree-sitter-swift
new file mode 160000
+Subproject a22fa5e19bae50098e2252ea96cba3aba43f4c5
diff --git a/helix-syntax/languages/tree-sitter-typescript b/helix-syntax/languages/tree-sitter-typescript
new file mode 160000
+Subproject 220ae17fad029f86513498648c90198e8bed872
diff --git a/helix-syntax/src/lib.rs b/helix-syntax/src/lib.rs
new file mode 100644
index 00000000..1ca36ca6
--- /dev/null
+++ b/helix-syntax/src/lib.rs
@@ -0,0 +1,87 @@
+use enum_iterator::IntoEnumIterator;
+use tree_sitter::Language;
+
+#[macro_export]
+macro_rules! mk_extern {
+ ( $( $name:ident ),* ) => {
+ $(
+ extern "C" { pub fn $name() -> Language; }
+ )*
+ };
+}
+
+#[macro_export]
+macro_rules! mk_enum {
+ ( $( $camel:ident ),* ) => {
+ #[derive(Clone, Debug, IntoEnumIterator, PartialEq)]
+ pub enum LANG {
+ $(
+ $camel,
+ )*
+ }
+ };
+}
+
+#[macro_export]
+macro_rules! mk_get_language {
+ ( $( ($camel:ident, $name:ident) ),* ) => {
+ pub fn get_language(lang: &LANG) -> Language {
+ unsafe {
+ match lang {
+ $(
+ LANG::$camel => $name(),
+ )*
+ }
+ }
+ }
+ };
+}
+
+#[macro_export]
+macro_rules! mk_get_language_name {
+ ( $( $camel:ident ),* ) => {
+ pub fn get_language_name(lang: &LANG) -> &'static str {
+ match lang {
+ $(
+ LANG::$camel => stringify!($camel),
+ )*
+ }
+ }
+ };
+}
+
+#[macro_export]
+macro_rules! mk_langs {
+ ( $( ($camel:ident, $name:ident) ),* ) => {
+ mk_extern!($( $name ),*);
+ mk_enum!($( $camel ),*);
+ mk_get_language!($( ($camel, $name) ),*);
+ mk_get_language_name!($( $camel ),*);
+ };
+}
+
+mk_langs!(
+ // 1) Name for enum
+ // 2) tree-sitter function to call to get a Language
+ (Agda, tree_sitter_agda),
+ (Bash, tree_sitter_bash),
+ (C, tree_sitter_c),
+ (CSharp, tree_sitter_c_sharp),
+ // (Cpp, tree_sitter_cpp),
+ (Css, tree_sitter_css),
+ (Go, tree_sitter_go),
+ (Haskell, tree_sitter_haskell),
+ (Html, tree_sitter_html),
+ (Java, tree_sitter_java),
+ (Javascript, tree_sitter_javascript),
+ (Json, tree_sitter_json),
+ (Julia, tree_sitter_julia),
+ (Php, tree_sitter_php),
+ (Python, tree_sitter_python),
+ (Ruby, tree_sitter_ruby),
+ (Rust, tree_sitter_rust),
+ (Scala, tree_sitter_scala),
+ (Swift, tree_sitter_swift),
+ (Tsx, tree_sitter_tsx),
+ (Typescript, tree_sitter_typescript)
+);