Add helix-syntax as a wrapper around tree-sitter parsers.

This commit is contained in:
Blaž Hrastnik 2020-09-09 14:41:12 +09:00
parent 563e094916
commit 7eac12a4bb
27 changed files with 417 additions and 0 deletions

14
helix-syntax/Cargo.toml Normal file
View file

@ -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"] }

126
helix-syntax/build.rs Normal file
View file

@ -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");
}

@ -0,0 +1 @@
Subproject commit 7fcba5a1b9f83f52a3812e8cdaf16cb60b069aff

@ -0,0 +1 @@
Subproject commit 8ece09ca4c0b5e59b124cd19fa92c76b1a9e9dd4

@ -0,0 +1 @@
Subproject commit 99151b1e9293c9e025498fee7e6691e1a52e1d03

@ -0,0 +1 @@
Subproject commit 075a1b2ff5fae3142a9318d9479d568843d2fe5d

@ -0,0 +1 @@
Subproject commit 5e7476bd014445abdae879661e9caf299215478a

@ -0,0 +1 @@
Subproject commit 23f2cb97d47860c517f67f03e1f4b621d5bd2085

@ -0,0 +1 @@
Subproject commit 34181774b3e86b7801c939c79c7b80a82df91a2b

@ -0,0 +1 @@
Subproject commit 2a0aa1cb5f1b787a4056a29fa0791e87846e33fb

@ -0,0 +1 @@
Subproject commit 7f442e1c6163d450c69c75c7a621badc3a0ea98f

@ -0,0 +1 @@
Subproject commit ee0a2a076785145e350fbd413775d1e003f79315

@ -0,0 +1 @@
Subproject commit feca6ec5e577fa30766f0c0a1e03d32c073027f9

@ -0,0 +1 @@
Subproject commit d3976b27df8622ed17bef6dd5e358b398e73c676

@ -0,0 +1 @@
Subproject commit 6a0863f1ce3fcf6f99dc0addb7886dcbd27c5a48

@ -0,0 +1 @@
Subproject commit b0c0367d4b7058921fdc4ba11e257441a64ab809

@ -0,0 +1 @@
Subproject commit 58f57240834d6b88624e32ad0ab9531d55fb7a5d

@ -0,0 +1 @@
Subproject commit 14a5e56a6fff1f2d40c151ae38b5581fc5c44574

@ -0,0 +1 @@
Subproject commit 40620bf4097cbc9cea79504d7e877865df43a19e

@ -0,0 +1 @@
Subproject commit 211bb726bb5857f872247b600c7c1808e641a8d4

@ -0,0 +1 @@
Subproject commit a22fa5e19bae50098e2252ea96cba3aba43f4c58

@ -0,0 +1 @@
Subproject commit 220ae17fad029f86513498648c90198e8bed872e

87
helix-syntax/src/lib.rs Normal file
View file

@ -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)
);