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};
|
2019-02-19 11:24:50 -08:00
|
|
|
use once_cell::unsync::OnceCell;
|
2019-01-07 17:57:27 -08:00
|
|
|
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};
|
2019-02-19 17:07:12 -08:00
|
|
|
use tree_sitter_highlight::{load_property_sheet, Properties};
|
2019-01-07 17:57:27 -08:00
|
|
|
|
|
|
|
|
#[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-05-30 12:05:53 -07:00
|
|
|
#[derive(Default)]
|
2019-01-07 17:57:27 -08:00
|
|
|
pub struct LanguageConfiguration {
|
2019-08-07 17:41:45 -07:00
|
|
|
pub scope: Option<String>,
|
|
|
|
|
pub content_regex: Option<Regex>,
|
|
|
|
|
pub _first_line_regex: Option<Regex>,
|
|
|
|
|
pub injection_regex: Option<Regex>,
|
|
|
|
|
pub file_types: Vec<String>,
|
|
|
|
|
pub highlight_property_sheet_path: Option<PathBuf>,
|
|
|
|
|
language_id: usize,
|
2019-02-19 11:24:50 -08:00
|
|
|
highlight_property_sheet: OnceCell<Option<PropertySheet<Properties>>>,
|
2019-01-07 17:57:27 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct Loader {
|
|
|
|
|
parser_lib_path: PathBuf,
|
2019-08-07 17:41:45 -07:00
|
|
|
languages_by_id: Vec<(PathBuf, OnceCell<Language>)>,
|
|
|
|
|
language_configurations: Vec<LanguageConfiguration>,
|
|
|
|
|
language_configuration_ids_by_file_type: HashMap<String, Vec<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,
|
2019-08-07 17:41:45 -07:00
|
|
|
languages_by_id: Vec::new(),
|
|
|
|
|
language_configurations: 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-") {
|
2019-08-07 17:41:45 -07:00
|
|
|
self.find_language_configurations_at_path(
|
|
|
|
|
&parser_container_dir.join(parser_dir_name),
|
|
|
|
|
)
|
|
|
|
|
.ok();
|
2019-02-06 12:59:19 -08:00
|
|
|
}
|
2019-01-07 17:57:27 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-07 17:41:45 -07:00
|
|
|
pub fn languages_at_path(&mut self, path: &Path) -> Result<Vec<Language>> {
|
|
|
|
|
if let Ok(configurations) = self.find_language_configurations_at_path(path) {
|
|
|
|
|
let mut language_ids = configurations
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|c| c.language_id)
|
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
language_ids.sort();
|
|
|
|
|
language_ids.dedup();
|
|
|
|
|
language_ids
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|id| self.language_for_id(id))
|
|
|
|
|
.collect::<Result<Vec<_>>>()
|
2019-01-11 13:30:45 -08:00
|
|
|
} else {
|
2019-08-07 17:41:45 -07:00
|
|
|
Ok(Vec::new())
|
2019-01-11 13:30:45 -08:00
|
|
|
}
|
2019-01-07 17:57:27 -08:00
|
|
|
}
|
|
|
|
|
|
2019-08-07 17:41:45 -07:00
|
|
|
pub fn get_all_language_configurations(&self) -> Vec<(&LanguageConfiguration, &Path)> {
|
|
|
|
|
self.language_configurations
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|c| (c, self.languages_by_id[c.language_id].0.as_ref()))
|
|
|
|
|
.collect()
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-20 14:38:19 -08:00
|
|
|
pub fn language_configuration_for_scope(
|
|
|
|
|
&self,
|
|
|
|
|
scope: &str,
|
|
|
|
|
) -> Result<Option<(Language, &LanguageConfiguration)>> {
|
2019-08-07 17:41:45 -07:00
|
|
|
for configuration in &self.language_configurations {
|
|
|
|
|
if configuration.scope.as_ref().map_or(false, |s| s == scope) {
|
|
|
|
|
let language = self.language_for_id(configuration.language_id)?;
|
|
|
|
|
return Ok(Some((language, configuration)));
|
2019-02-20 14:38:19 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Ok(None)
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-11 13:30:45 -08:00
|
|
|
pub fn language_configuration_for_file_name(
|
2019-02-19 11:24:50 -08:00
|
|
|
&self,
|
2019-01-07 17:57:27 -08:00
|
|
|
path: &Path,
|
2019-02-06 16:03:50 -08:00
|
|
|
) -> Result<Option<(Language, &LanguageConfiguration)>> {
|
2019-08-07 17:41:45 -07:00
|
|
|
// Find all the language configurations that match this file name
|
|
|
|
|
// or a suffix of the file name.
|
|
|
|
|
let configuration_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-08-07 17:41:45 -07:00
|
|
|
|
|
|
|
|
if let Some(configuration_ids) = configuration_ids {
|
|
|
|
|
if !configuration_ids.is_empty() {
|
|
|
|
|
let configuration;
|
|
|
|
|
|
|
|
|
|
// If there is only one language configuration, then use it.
|
|
|
|
|
if configuration_ids.len() == 1 {
|
|
|
|
|
configuration = &self.language_configurations[configuration_ids[0]];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If multiple language configurations match, then determine which
|
|
|
|
|
// one to use by applying the configurations' content regexes.
|
|
|
|
|
else {
|
|
|
|
|
let file_contents = fs::read_to_string(path)?;
|
|
|
|
|
let mut best_score = -2isize;
|
|
|
|
|
let mut best_configuration_id = None;
|
|
|
|
|
for configuration_id in configuration_ids {
|
|
|
|
|
let config = &self.language_configurations[*configuration_id];
|
|
|
|
|
|
|
|
|
|
// If the language configuration has a content regex, assign
|
|
|
|
|
// a score based on the length of the first match.
|
|
|
|
|
let score;
|
|
|
|
|
if let Some(content_regex) = &config.content_regex {
|
|
|
|
|
if let Some(mat) = content_regex.find(&file_contents) {
|
|
|
|
|
score = (mat.end() - mat.start()) as isize;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If the content regex does not match, then *penalize* this
|
|
|
|
|
// language configuration, so that language configurations
|
|
|
|
|
// without content regexes are preferred over those with
|
|
|
|
|
// non-matching content regexes.
|
|
|
|
|
else {
|
|
|
|
|
score = -1;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
score = 0;
|
|
|
|
|
}
|
|
|
|
|
if score > best_score {
|
|
|
|
|
best_configuration_id = Some(*configuration_id);
|
|
|
|
|
best_score = score;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
configuration = &self.language_configurations[best_configuration_id.unwrap()];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let language = self.language_for_id(configuration.language_id)?;
|
|
|
|
|
return Ok(Some((language, configuration)));
|
2019-01-07 17:57:27 -08:00
|
|
|
}
|
|
|
|
|
}
|
2019-08-07 17:41:45 -07:00
|
|
|
|
2019-01-07 17:57:27 -08:00
|
|
|
Ok(None)
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-19 11:24:50 -08:00
|
|
|
pub fn language_configuration_for_injection_string(
|
|
|
|
|
&self,
|
|
|
|
|
string: &str,
|
|
|
|
|
) -> Result<Option<(Language, &LanguageConfiguration)>> {
|
|
|
|
|
let mut best_match_length = 0;
|
|
|
|
|
let mut best_match_position = None;
|
2019-08-07 17:41:45 -07:00
|
|
|
for (i, configuration) in self.language_configurations.iter().enumerate() {
|
|
|
|
|
if let Some(injection_regex) = &configuration.injection_regex {
|
|
|
|
|
if let Some(mat) = injection_regex.find(string) {
|
|
|
|
|
let length = mat.end() - mat.start();
|
|
|
|
|
if length > best_match_length {
|
|
|
|
|
best_match_position = Some(i);
|
|
|
|
|
best_match_length = length;
|
2019-02-19 11:24:50 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-08-07 17:41:45 -07:00
|
|
|
|
|
|
|
|
if let Some(i) = best_match_position {
|
|
|
|
|
let configuration = &self.language_configurations[i];
|
|
|
|
|
let language = self.language_for_id(configuration.language_id)?;
|
|
|
|
|
Ok(Some((language, configuration)))
|
2019-02-19 11:24:50 -08:00
|
|
|
} else {
|
|
|
|
|
Ok(None)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-07 17:41:45 -07:00
|
|
|
fn language_for_id(&self, id: usize) -> Result<Language> {
|
|
|
|
|
let (path, language) = &self.languages_by_id[id];
|
|
|
|
|
language
|
|
|
|
|
.get_or_try_init(|| {
|
|
|
|
|
let src_path = path.join("src");
|
|
|
|
|
self.load_language_at_path(&src_path, &src_path)
|
|
|
|
|
})
|
|
|
|
|
.map(|l| *l)
|
2019-01-11 13:30:45 -08:00
|
|
|
}
|
|
|
|
|
|
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,
|
|
|
|
|
}
|
2019-05-30 16:52:30 -07:00
|
|
|
let mut grammar_file =
|
|
|
|
|
fs::File::open(grammar_path).map_err(Error::wrap(|| "Failed to read grammar.json"))?;
|
|
|
|
|
let grammar_json: GrammarJSON = serde_json::from_reader(BufReader::new(&mut grammar_file))
|
|
|
|
|
.map_err(Error::wrap(|| "Failed to parse grammar.json"))?;
|
2019-02-13 19:30:59 -08:00
|
|
|
|
|
|
|
|
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-05-30 16:52:30 -07:00
|
|
|
let recompile = needs_recompile(&library_path, &parser_path, &scanner_path).map_err(
|
|
|
|
|
Error::wrap(|| "Failed to compare source and binary timestamps"),
|
|
|
|
|
)?;
|
|
|
|
|
|
|
|
|
|
if recompile {
|
2019-01-11 13:30:45 -08:00
|
|
|
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-04-05 13:04:37 -07:00
|
|
|
.arg("-g")
|
2019-01-11 13:30:45 -08:00
|
|
|
.arg("-I")
|
|
|
|
|
.arg(header_path)
|
|
|
|
|
.arg("-o")
|
2019-03-03 18:23:01 -08:00
|
|
|
.arg(&library_path)
|
|
|
|
|
.arg("-O2");
|
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-05-30 16:52:30 -07:00
|
|
|
let output = command
|
|
|
|
|
.output()
|
|
|
|
|
.map_err(Error::wrap(|| "Failed to execute C compiler"))?;
|
2019-01-11 14:44:32 -08:00
|
|
|
if !output.status.success() {
|
2019-05-30 16:52:30 -07:00
|
|
|
return Err(Error::new(format!(
|
2019-02-06 16:03:50 -08:00
|
|
|
"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-05-30 16:52:30 -07:00
|
|
|
let library = Library::new(&library_path).map_err(Error::wrap(|| {
|
|
|
|
|
format!("Error opening dynamic library {:?}", &library_path)
|
|
|
|
|
}))?;
|
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 {
|
2019-05-30 16:52:30 -07:00
|
|
|
let language_fn: Symbol<unsafe extern "C" fn() -> Language> = library
|
|
|
|
|
.get(language_fn_name.as_bytes())
|
|
|
|
|
.map_err(Error::wrap(|| {
|
|
|
|
|
format!("Failed to load symbol {}", language_fn_name)
|
|
|
|
|
}))?;
|
2019-01-07 17:57:27 -08:00
|
|
|
language_fn()
|
|
|
|
|
};
|
|
|
|
|
mem::forget(library);
|
|
|
|
|
Ok(language)
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-07 17:41:45 -07:00
|
|
|
fn find_language_configurations_at_path<'a>(
|
|
|
|
|
&'a mut self,
|
|
|
|
|
parser_path: &Path,
|
|
|
|
|
) -> Result<&[LanguageConfiguration]> {
|
2019-01-07 17:57:27 -08:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
|
struct LanguageConfigurationJSON {
|
2019-08-07 17:41:45 -07:00
|
|
|
#[serde(default)]
|
|
|
|
|
path: PathBuf,
|
2019-02-20 14:38:19 -08:00
|
|
|
scope: Option<String>,
|
2019-01-07 17:57:27 -08:00
|
|
|
#[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>,
|
2019-02-19 11:24:50 -08:00
|
|
|
#[serde(rename = "injection-regex")]
|
|
|
|
|
injection_regex: Option<String>,
|
2019-01-07 17:57:27 -08:00
|
|
|
highlights: Option<String>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
|
struct PackageJSON {
|
2019-08-07 17:41:45 -07:00
|
|
|
#[serde(default)]
|
2019-01-07 17:57:27 -08:00
|
|
|
#[serde(rename = "tree-sitter")]
|
2019-08-07 17:41:45 -07:00
|
|
|
tree_sitter: Vec<LanguageConfigurationJSON>,
|
2019-01-07 17:57:27 -08:00
|
|
|
}
|
|
|
|
|
|
2019-08-07 17:41:45 -07:00
|
|
|
let initial_language_configuration_count = self.language_configurations.len();
|
|
|
|
|
|
2019-05-30 12:05:53 -07:00
|
|
|
if let Ok(package_json_contents) = fs::read_to_string(&parser_path.join("package.json")) {
|
|
|
|
|
let package_json = serde_json::from_str::<PackageJSON>(&package_json_contents);
|
|
|
|
|
if let Ok(package_json) = package_json {
|
2019-08-07 17:41:45 -07:00
|
|
|
if package_json.tree_sitter.is_empty() {
|
|
|
|
|
return Ok(&[]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let language_count = self.languages_by_id.len();
|
|
|
|
|
for config_json in package_json.tree_sitter {
|
|
|
|
|
// Determine the path to the parser directory. This can be specified in
|
|
|
|
|
// the package.json, but defaults to the directory containing the package.json.
|
|
|
|
|
let language_path = parser_path.join(config_json.path);
|
|
|
|
|
|
|
|
|
|
// Determine if a previous language configuration in this package.json file
|
|
|
|
|
// already uses the same language.
|
|
|
|
|
let mut language_id = None;
|
|
|
|
|
for (id, (path, _)) in
|
|
|
|
|
self.languages_by_id.iter().enumerate().skip(language_count)
|
|
|
|
|
{
|
|
|
|
|
if language_path == *path {
|
|
|
|
|
language_id = Some(id);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If not, add a new language path to the list.
|
|
|
|
|
let language_id = language_id.unwrap_or_else(|| {
|
|
|
|
|
self.languages_by_id.push((language_path, OnceCell::new()));
|
|
|
|
|
self.languages_by_id.len() - 1
|
2019-05-30 12:05:53 -07:00
|
|
|
});
|
2019-01-07 17:57:27 -08:00
|
|
|
|
2019-08-07 17:41:45 -07:00
|
|
|
let configuration = LanguageConfiguration {
|
|
|
|
|
scope: config_json.scope,
|
|
|
|
|
language_id,
|
|
|
|
|
file_types: config_json.file_types.unwrap_or(Vec::new()),
|
|
|
|
|
content_regex: config_json
|
|
|
|
|
.content_regex
|
|
|
|
|
.and_then(|r| RegexBuilder::new(&r).multi_line(true).build().ok()),
|
|
|
|
|
_first_line_regex: config_json
|
|
|
|
|
.first_line_regex
|
|
|
|
|
.and_then(|r| RegexBuilder::new(&r).multi_line(true).build().ok()),
|
|
|
|
|
injection_regex: config_json
|
|
|
|
|
.injection_regex
|
|
|
|
|
.and_then(|r| RegexBuilder::new(&r).multi_line(true).build().ok()),
|
|
|
|
|
highlight_property_sheet_path: config_json
|
|
|
|
|
.highlights
|
|
|
|
|
.map(|h| parser_path.join(h)),
|
|
|
|
|
highlight_property_sheet: OnceCell::new(),
|
|
|
|
|
};
|
|
|
|
|
|
2019-05-30 12:05:53 -07:00
|
|
|
for file_type in &configuration.file_types {
|
|
|
|
|
self.language_configuration_ids_by_file_type
|
|
|
|
|
.entry(file_type.to_string())
|
|
|
|
|
.or_insert(Vec::new())
|
2019-08-07 17:41:45 -07:00
|
|
|
.push(self.language_configurations.len());
|
2019-05-30 12:05:53 -07:00
|
|
|
}
|
2019-08-07 17:41:45 -07:00
|
|
|
|
|
|
|
|
self.language_configurations.push(configuration);
|
2019-05-30 12:05:53 -07:00
|
|
|
}
|
2019-01-07 17:57:27 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-07 17:41:45 -07:00
|
|
|
Ok(&self.language_configurations[initial_language_configuration_count..])
|
2019-01-07 17:57:27 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-19 11:24:50 -08:00
|
|
|
impl LanguageConfiguration {
|
|
|
|
|
pub fn highlight_property_sheet(
|
|
|
|
|
&self,
|
|
|
|
|
language: Language,
|
|
|
|
|
) -> Result<Option<&PropertySheet<Properties>>> {
|
|
|
|
|
self.highlight_property_sheet
|
|
|
|
|
.get_or_try_init(|| {
|
|
|
|
|
if let Some(path) = &self.highlight_property_sheet_path {
|
2019-05-30 16:52:30 -07:00
|
|
|
let sheet_json = fs::read_to_string(path).map_err(Error::wrap(|| {
|
|
|
|
|
format!(
|
|
|
|
|
"Failed to read property sheet {:?}",
|
|
|
|
|
path.file_name().unwrap()
|
|
|
|
|
)
|
|
|
|
|
}))?;
|
|
|
|
|
let sheet =
|
|
|
|
|
load_property_sheet(language, &sheet_json).map_err(Error::wrap(|| {
|
|
|
|
|
format!(
|
|
|
|
|
"Failed to parse property sheet {:?}",
|
|
|
|
|
path.file_name().unwrap()
|
|
|
|
|
)
|
|
|
|
|
}))?;
|
2019-02-19 11:24:50 -08:00
|
|
|
Ok(Some(sheet))
|
|
|
|
|
} else {
|
|
|
|
|
Ok(None)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.map(Option::as_ref)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
}
|