2019-02-06 16:03:50 -08:00
|
|
|
use super::error::{Error, Result};
|
2019-01-07 17:57:27 -08:00
|
|
|
use libloading::{Library, Symbol};
|
|
|
|
|
use regex::{Regex, RegexBuilder};
|
2019-02-01 14:39:37 -08:00
|
|
|
use serde_derive::Deserialize;
|
2019-01-07 17:57:27 -08:00
|
|
|
use std::collections::HashMap;
|
2019-02-13 19:30:59 -08:00
|
|
|
use std::io::BufReader;
|
2019-01-07 17:57:27 -08:00
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
|
use std::process::Command;
|
2019-01-11 13:30:45 -08:00
|
|
|
use std::time::SystemTime;
|
2019-02-06 16:03:50 -08:00
|
|
|
use std::{fs, mem};
|
2019-01-07 17:57:27 -08:00
|
|
|
use tree_sitter::{Language, PropertySheet};
|
|
|
|
|
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
|
const DYLIB_EXTENSION: &'static str = "so";
|
|
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
|
const DYLIB_EXTENSION: &'static str = "dll";
|
|
|
|
|
|
2019-01-16 14:09:19 -08:00
|
|
|
const BUILD_TARGET: &'static str = env!("BUILD_TARGET");
|
|
|
|
|
|
2019-01-07 17:57:27 -08:00
|
|
|
struct LanguageRepo {
|
|
|
|
|
path: PathBuf,
|
|
|
|
|
language: Option<Language>,
|
|
|
|
|
configurations: Vec<LanguageConfiguration>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct LanguageConfiguration {
|
2019-01-15 10:27:39 -08:00
|
|
|
_name: String,
|
|
|
|
|
_content_regex: Option<Regex>,
|
|
|
|
|
_first_line_regex: Option<Regex>,
|
2019-01-07 17:57:27 -08:00
|
|
|
file_types: Vec<String>,
|
2019-02-06 16:03:50 -08:00
|
|
|
_highlight_property_sheet: Option<std::result::Result<PropertySheet, PathBuf>>,
|
2019-01-07 17:57:27 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct Loader {
|
|
|
|
|
parser_lib_path: PathBuf,
|
|
|
|
|
language_repos: Vec<LanguageRepo>,
|
2019-01-11 13:30:45 -08:00
|
|
|
language_configuration_ids_by_file_type: HashMap<String, Vec<(usize, usize)>>,
|
2019-01-07 17:57:27 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsafe impl Send for Loader {}
|
|
|
|
|
unsafe impl Sync for Loader {}
|
|
|
|
|
|
|
|
|
|
impl Loader {
|
|
|
|
|
pub fn new(parser_lib_path: PathBuf) -> Self {
|
|
|
|
|
Loader {
|
|
|
|
|
parser_lib_path,
|
|
|
|
|
language_repos: Vec::new(),
|
2019-01-11 13:30:45 -08:00
|
|
|
language_configuration_ids_by_file_type: HashMap::new(),
|
2019-01-07 17:57:27 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-06 16:03:50 -08:00
|
|
|
pub fn find_all_languages(&mut self, parser_src_paths: &Vec<PathBuf>) -> Result<()> {
|
2019-01-07 17:57:27 -08:00
|
|
|
for parser_container_dir in parser_src_paths.iter() {
|
2019-02-06 12:59:19 -08:00
|
|
|
if let Ok(entries) = fs::read_dir(parser_container_dir) {
|
|
|
|
|
for entry in entries {
|
|
|
|
|
let entry = entry?;
|
|
|
|
|
if let Some(parser_dir_name) = entry.file_name().to_str() {
|
|
|
|
|
if parser_dir_name.starts_with("tree-sitter-") {
|
|
|
|
|
self.find_language_at_path(&parser_container_dir.join(parser_dir_name))
|
|
|
|
|
.ok();
|
|
|
|
|
}
|
2019-01-07 17:57:27 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-06 16:03:50 -08:00
|
|
|
pub fn language_at_path(&mut self, path: &Path) -> Result<Option<Language>> {
|
2019-01-11 13:30:45 -08:00
|
|
|
if let Ok(id) = self.find_language_at_path(path) {
|
|
|
|
|
Ok(Some(self.language_configuration_for_id(id)?.0))
|
|
|
|
|
} else {
|
|
|
|
|
Ok(None)
|
|
|
|
|
}
|
2019-01-07 17:57:27 -08:00
|
|
|
}
|
|
|
|
|
|
2019-01-11 13:30:45 -08:00
|
|
|
pub fn language_configuration_for_file_name(
|
2019-01-07 17:57:27 -08:00
|
|
|
&mut self,
|
|
|
|
|
path: &Path,
|
2019-02-06 16:03:50 -08:00
|
|
|
) -> Result<Option<(Language, &LanguageConfiguration)>> {
|
2019-01-11 13:30:45 -08:00
|
|
|
let ids = path
|
2019-01-07 17:57:27 -08:00
|
|
|
.file_name()
|
|
|
|
|
.and_then(|n| n.to_str())
|
2019-01-11 13:30:45 -08:00
|
|
|
.and_then(|file_name| self.language_configuration_ids_by_file_type.get(file_name))
|
2019-01-07 17:57:27 -08:00
|
|
|
.or_else(|| {
|
|
|
|
|
path.extension()
|
|
|
|
|
.and_then(|extension| extension.to_str())
|
|
|
|
|
.and_then(|extension| {
|
2019-01-11 13:30:45 -08:00
|
|
|
self.language_configuration_ids_by_file_type.get(extension)
|
2019-01-07 17:57:27 -08:00
|
|
|
})
|
|
|
|
|
});
|
2019-01-11 13:30:45 -08:00
|
|
|
if let Some(ids) = ids {
|
2019-01-07 17:57:27 -08:00
|
|
|
// TODO use `content-regex` to pick one
|
2019-01-11 13:30:45 -08:00
|
|
|
for (repo_id, configuration_id) in ids.iter().cloned() {
|
|
|
|
|
let (language, configurations) = self.language_configuration_for_id(repo_id)?;
|
|
|
|
|
return Ok(Some((language, &configurations[configuration_id])));
|
2019-01-07 17:57:27 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Ok(None)
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-11 13:30:45 -08:00
|
|
|
fn language_configuration_for_id(
|
2019-01-07 17:57:27 -08:00
|
|
|
&mut self,
|
2019-01-11 13:30:45 -08:00
|
|
|
id: usize,
|
2019-02-06 16:03:50 -08:00
|
|
|
) -> Result<(Language, &Vec<LanguageConfiguration>)> {
|
2019-01-11 13:30:45 -08:00
|
|
|
let repo = &self.language_repos[id];
|
2019-01-07 17:57:27 -08:00
|
|
|
let language = if let Some(language) = repo.language {
|
|
|
|
|
language
|
|
|
|
|
} else {
|
2019-01-15 10:27:39 -08:00
|
|
|
let src_path = repo.path.join("src");
|
2019-02-13 19:30:59 -08:00
|
|
|
let language = self.load_language_at_path(&src_path, &src_path)?;
|
2019-01-11 13:30:45 -08:00
|
|
|
self.language_repos[id].language = Some(language);
|
2019-01-07 17:57:27 -08:00
|
|
|
language
|
|
|
|
|
};
|
2019-01-11 13:30:45 -08:00
|
|
|
Ok((language, &self.language_repos[id].configurations))
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-13 19:30:59 -08:00
|
|
|
pub fn load_language_at_path(&self, src_path: &Path, header_path: &Path) -> Result<Language> {
|
|
|
|
|
let grammar_path = src_path.join("grammar.json");
|
2019-01-15 10:27:39 -08:00
|
|
|
let parser_path = src_path.join("parser.c");
|
2019-02-13 19:30:59 -08:00
|
|
|
let mut scanner_path = src_path.join("scanner.c");
|
2019-01-11 13:30:45 -08:00
|
|
|
|
2019-02-13 19:30:59 -08:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
|
struct GrammarJSON {
|
|
|
|
|
name: String,
|
|
|
|
|
}
|
|
|
|
|
let mut grammar_file = fs::File::open(grammar_path)?;
|
|
|
|
|
let grammar_json: GrammarJSON = serde_json::from_reader(BufReader::new(&mut grammar_file))?;
|
|
|
|
|
|
|
|
|
|
let scanner_path = if scanner_path.exists() {
|
|
|
|
|
Some(scanner_path)
|
2019-01-07 17:57:27 -08:00
|
|
|
} else {
|
2019-02-13 19:30:59 -08:00
|
|
|
scanner_path.set_extension("cc");
|
|
|
|
|
if scanner_path.exists() {
|
|
|
|
|
Some(scanner_path)
|
2019-01-11 13:30:45 -08:00
|
|
|
} else {
|
2019-02-13 19:30:59 -08:00
|
|
|
None
|
2019-01-11 13:30:45 -08:00
|
|
|
}
|
2019-02-13 19:30:59 -08:00
|
|
|
};
|
2019-01-11 13:30:45 -08:00
|
|
|
|
2019-02-13 19:30:59 -08:00
|
|
|
self.load_language_from_sources(
|
|
|
|
|
&grammar_json.name,
|
|
|
|
|
&header_path,
|
|
|
|
|
&parser_path,
|
|
|
|
|
&scanner_path,
|
|
|
|
|
)
|
2019-01-07 17:57:27 -08:00
|
|
|
}
|
|
|
|
|
|
2019-01-11 13:30:45 -08:00
|
|
|
pub fn load_language_from_sources(
|
|
|
|
|
&self,
|
|
|
|
|
name: &str,
|
|
|
|
|
header_path: &Path,
|
|
|
|
|
parser_path: &Path,
|
|
|
|
|
scanner_path: &Option<PathBuf>,
|
2019-02-06 16:03:50 -08:00
|
|
|
) -> Result<Language> {
|
2019-01-07 17:57:27 -08:00
|
|
|
let mut library_path = self.parser_lib_path.join(name);
|
|
|
|
|
library_path.set_extension(DYLIB_EXTENSION);
|
|
|
|
|
|
2019-01-11 13:30:45 -08:00
|
|
|
if needs_recompile(&library_path, &parser_path, &scanner_path)? {
|
|
|
|
|
let mut config = cc::Build::new();
|
|
|
|
|
config
|
2019-01-15 10:27:39 -08:00
|
|
|
.cpp(true)
|
2019-01-11 13:30:45 -08:00
|
|
|
.opt_level(2)
|
|
|
|
|
.cargo_metadata(false)
|
2019-01-16 14:09:19 -08:00
|
|
|
.target(BUILD_TARGET)
|
|
|
|
|
.host(BUILD_TARGET);
|
2019-01-11 13:30:45 -08:00
|
|
|
let compiler = config.get_compiler();
|
2019-01-11 14:44:32 -08:00
|
|
|
let mut command = Command::new(compiler.path());
|
|
|
|
|
for (key, value) in compiler.env() {
|
|
|
|
|
command.env(key, value);
|
|
|
|
|
}
|
2019-01-11 13:30:45 -08:00
|
|
|
|
|
|
|
|
if cfg!(windows) {
|
|
|
|
|
command
|
|
|
|
|
.args(&["/nologo", "/LD", "/I"])
|
|
|
|
|
.arg(header_path)
|
|
|
|
|
.arg("/Od")
|
|
|
|
|
.arg(parser_path);
|
|
|
|
|
if let Some(scanner_path) = scanner_path.as_ref() {
|
|
|
|
|
command.arg(scanner_path);
|
|
|
|
|
}
|
|
|
|
|
command
|
|
|
|
|
.arg("/link")
|
|
|
|
|
.arg(format!("/out:{}", library_path.to_str().unwrap()));
|
|
|
|
|
} else {
|
|
|
|
|
command
|
|
|
|
|
.arg("-shared")
|
|
|
|
|
.arg("-fPIC")
|
2019-02-06 16:19:08 -08:00
|
|
|
.arg("-fno-exceptions")
|
2019-01-11 13:30:45 -08:00
|
|
|
.arg("-I")
|
|
|
|
|
.arg(header_path)
|
|
|
|
|
.arg("-o")
|
2019-02-06 13:07:03 -08:00
|
|
|
.arg(&library_path);
|
2019-01-11 13:30:45 -08:00
|
|
|
if let Some(scanner_path) = scanner_path.as_ref() {
|
|
|
|
|
if scanner_path.extension() == Some("c".as_ref()) {
|
2019-01-15 16:12:30 -08:00
|
|
|
command.arg("-xc").arg("-std=c99").arg(scanner_path);
|
2019-01-11 13:30:45 -08:00
|
|
|
} else {
|
2019-02-06 13:07:03 -08:00
|
|
|
command.arg(scanner_path);
|
2019-01-11 13:30:45 -08:00
|
|
|
}
|
|
|
|
|
}
|
2019-02-06 13:07:03 -08:00
|
|
|
command.arg("-xc").arg(parser_path);
|
2019-01-07 17:57:27 -08:00
|
|
|
}
|
2019-01-11 13:30:45 -08:00
|
|
|
|
2019-01-11 14:44:32 -08:00
|
|
|
let output = command.output()?;
|
|
|
|
|
if !output.status.success() {
|
2019-02-06 16:03:50 -08:00
|
|
|
return Err(Error(format!(
|
|
|
|
|
"Parser compilation failed.\nStdout: {}\nStderr: {}",
|
|
|
|
|
String::from_utf8_lossy(&output.stdout),
|
|
|
|
|
String::from_utf8_lossy(&output.stderr)
|
|
|
|
|
)));
|
2019-01-11 14:44:32 -08:00
|
|
|
}
|
2019-01-07 17:57:27 -08:00
|
|
|
}
|
|
|
|
|
|
2019-02-06 16:03:50 -08:00
|
|
|
let library = Library::new(&library_path).map_err(|e| {
|
|
|
|
|
Error(format!(
|
|
|
|
|
"Error opening dynamic library {:?}: {}",
|
|
|
|
|
&library_path, e
|
|
|
|
|
))
|
|
|
|
|
})?;
|
2019-01-15 10:27:39 -08:00
|
|
|
let language_fn_name = format!("tree_sitter_{}", replace_dashes_with_underscores(name));
|
2019-01-07 17:57:27 -08:00
|
|
|
let language = unsafe {
|
|
|
|
|
let language_fn: Symbol<unsafe extern "C" fn() -> Language> =
|
|
|
|
|
library.get(language_fn_name.as_bytes())?;
|
|
|
|
|
language_fn()
|
|
|
|
|
};
|
|
|
|
|
mem::forget(library);
|
|
|
|
|
Ok(language)
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-06 16:03:50 -08:00
|
|
|
fn find_language_at_path<'a>(&'a mut self, parser_path: &Path) -> Result<usize> {
|
2019-01-07 17:57:27 -08:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
|
struct LanguageConfigurationJSON {
|
|
|
|
|
name: String,
|
|
|
|
|
#[serde(rename = "file-types")]
|
|
|
|
|
file_types: Option<Vec<String>>,
|
|
|
|
|
#[serde(rename = "content-regex")]
|
|
|
|
|
content_regex: Option<String>,
|
|
|
|
|
#[serde(rename = "first-line-regex")]
|
|
|
|
|
first_line_regex: Option<String>,
|
|
|
|
|
highlights: Option<String>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
|
struct PackageJSON {
|
|
|
|
|
#[serde(rename = "tree-sitter")]
|
|
|
|
|
tree_sitter: Option<Vec<LanguageConfigurationJSON>>,
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-11 14:44:32 -08:00
|
|
|
let package_json_contents = fs::read_to_string(&parser_path.join("package.json"))?;
|
2019-01-07 17:57:27 -08:00
|
|
|
let package_json: PackageJSON = serde_json::from_str(&package_json_contents)?;
|
|
|
|
|
let configurations = package_json
|
|
|
|
|
.tree_sitter
|
|
|
|
|
.map_or(Vec::new(), |configurations| {
|
|
|
|
|
configurations
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|conf| LanguageConfiguration {
|
2019-01-15 10:27:39 -08:00
|
|
|
_name: conf.name,
|
2019-01-07 17:57:27 -08:00
|
|
|
file_types: conf.file_types.unwrap_or(Vec::new()),
|
2019-01-15 10:27:39 -08:00
|
|
|
_content_regex: conf
|
2019-01-07 17:57:27 -08:00
|
|
|
.content_regex
|
|
|
|
|
.and_then(|r| RegexBuilder::new(&r).multi_line(true).build().ok()),
|
2019-01-15 10:27:39 -08:00
|
|
|
_first_line_regex: conf
|
2019-01-07 17:57:27 -08:00
|
|
|
.first_line_regex
|
|
|
|
|
.and_then(|r| RegexBuilder::new(&r).multi_line(true).build().ok()),
|
2019-01-15 10:27:39 -08:00
|
|
|
_highlight_property_sheet: conf.highlights.map(|d| Err(d.into())),
|
2019-01-07 17:57:27 -08:00
|
|
|
})
|
|
|
|
|
.collect()
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
for (i, configuration) in configurations.iter().enumerate() {
|
|
|
|
|
for file_type in &configuration.file_types {
|
2019-01-11 13:30:45 -08:00
|
|
|
self.language_configuration_ids_by_file_type
|
2019-01-07 17:57:27 -08:00
|
|
|
.entry(file_type.to_string())
|
|
|
|
|
.or_insert(Vec::new())
|
|
|
|
|
.push((self.language_repos.len(), i));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.language_repos.push(LanguageRepo {
|
|
|
|
|
path: parser_path.to_owned(),
|
|
|
|
|
language: None,
|
|
|
|
|
configurations,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Ok(self.language_repos.len() - 1)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-11 13:30:45 -08:00
|
|
|
fn needs_recompile(
|
|
|
|
|
lib_path: &Path,
|
|
|
|
|
parser_c_path: &Path,
|
|
|
|
|
scanner_path: &Option<PathBuf>,
|
2019-02-06 16:03:50 -08:00
|
|
|
) -> Result<bool> {
|
2019-01-11 13:30:45 -08:00
|
|
|
if !lib_path.exists() {
|
|
|
|
|
return Ok(true);
|
|
|
|
|
}
|
|
|
|
|
let lib_mtime = mtime(lib_path)?;
|
|
|
|
|
if mtime(parser_c_path)? > lib_mtime {
|
|
|
|
|
return Ok(true);
|
|
|
|
|
}
|
|
|
|
|
if let Some(scanner_path) = scanner_path {
|
|
|
|
|
if mtime(scanner_path)? > lib_mtime {
|
|
|
|
|
return Ok(true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Ok(false)
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-06 16:03:50 -08:00
|
|
|
fn mtime(path: &Path) -> Result<SystemTime> {
|
2019-01-11 13:30:45 -08:00
|
|
|
Ok(fs::metadata(path)?.modified()?)
|
2019-01-07 17:57:27 -08:00
|
|
|
}
|
2019-01-15 10:27:39 -08:00
|
|
|
|
|
|
|
|
fn replace_dashes_with_underscores(name: &str) -> String {
|
|
|
|
|
let mut result = String::with_capacity(name.len());
|
|
|
|
|
for c in name.chars() {
|
|
|
|
|
if c == '-' {
|
|
|
|
|
result.push('_');
|
|
|
|
|
} else {
|
|
|
|
|
result.push(c);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
result
|
|
|
|
|
}
|