1#![doc = include_str!("../README.md")]
2#![cfg_attr(docsrs, feature(doc_cfg))]
3
4#[cfg(any(feature = "tree-sitter-highlight", feature = "tree-sitter-tags"))]
5use std::ops::Range;
6#[cfg(feature = "tree-sitter-highlight")]
7use std::sync::Mutex;
8use std::{
9 collections::HashMap,
10 env,
11 ffi::{OsStr, OsString},
12 fs,
13 io::{BufRead, BufReader},
14 marker::PhantomData,
15 mem,
16 path::{Path, PathBuf},
17 process::Command,
18 sync::LazyLock,
19 time::SystemTime,
20};
21
22use anyhow::{anyhow, Context, Error, Result};
23use etcetera::BaseStrategy as _;
24use fs4::fs_std::FileExt;
25use indoc::indoc;
26use libloading::{Library, Symbol};
27use once_cell::unsync::OnceCell;
28use path_slash::PathBufExt as _;
29use regex::{Regex, RegexBuilder};
30use semver::Version;
31use serde::{Deserialize, Deserializer, Serialize};
32use tree_sitter::Language;
33#[cfg(any(feature = "tree-sitter-highlight", feature = "tree-sitter-tags"))]
34use tree_sitter::QueryError;
35#[cfg(feature = "tree-sitter-highlight")]
36use tree_sitter::QueryErrorKind;
37#[cfg(feature = "tree-sitter-highlight")]
38use tree_sitter_highlight::HighlightConfiguration;
39#[cfg(feature = "tree-sitter-tags")]
40use tree_sitter_tags::{Error as TagsError, TagsConfiguration};
41use url::Url;
42
43static GRAMMAR_NAME_REGEX: LazyLock<Regex> =
44 LazyLock::new(|| Regex::new(r#""name":\s*"(.*?)""#).unwrap());
45
46pub const EMSCRIPTEN_TAG: &str = concat!("docker.io/emscripten/emsdk:", env!("EMSCRIPTEN_VERSION"));
47
48#[derive(Default, Deserialize, Serialize)]
49pub struct Config {
50 #[serde(default)]
51 #[serde(
52 rename = "parser-directories",
53 deserialize_with = "deserialize_parser_directories"
54 )]
55 pub parser_directories: Vec<PathBuf>,
56}
57
58#[derive(Serialize, Deserialize, Clone, Default)]
59#[serde(untagged)]
60pub enum PathsJSON {
61 #[default]
62 Empty,
63 Single(PathBuf),
64 Multiple(Vec<PathBuf>),
65}
66
67impl PathsJSON {
68 fn into_vec(self) -> Option<Vec<PathBuf>> {
69 match self {
70 Self::Empty => None,
71 Self::Single(s) => Some(vec![s]),
72 Self::Multiple(s) => Some(s),
73 }
74 }
75
76 const fn is_empty(&self) -> bool {
77 matches!(self, Self::Empty)
78 }
79}
80
81#[derive(Serialize, Deserialize, Clone)]
82#[serde(untagged)]
83pub enum PackageJSONAuthor {
84 String(String),
85 Object {
86 name: String,
87 email: Option<String>,
88 url: Option<String>,
89 },
90}
91
92#[derive(Serialize, Deserialize, Clone)]
93#[serde(untagged)]
94pub enum PackageJSONRepository {
95 String(String),
96 Object { url: String },
97}
98
99#[derive(Serialize, Deserialize)]
100pub struct PackageJSON {
101 pub name: String,
102 pub version: Version,
103 pub description: Option<String>,
104 pub author: Option<PackageJSONAuthor>,
105 pub maintainers: Option<Vec<PackageJSONAuthor>>,
106 pub license: Option<String>,
107 pub repository: Option<PackageJSONRepository>,
108 #[serde(default)]
109 #[serde(rename = "tree-sitter", skip_serializing_if = "Option::is_none")]
110 pub tree_sitter: Option<Vec<LanguageConfigurationJSON>>,
111}
112
113fn default_path() -> PathBuf {
114 PathBuf::from(".")
115}
116
117#[derive(Serialize, Deserialize, Clone)]
118#[serde(rename_all = "kebab-case")]
119pub struct LanguageConfigurationJSON {
120 #[serde(default = "default_path")]
121 pub path: PathBuf,
122 pub scope: Option<String>,
123 pub file_types: Option<Vec<String>>,
124 pub content_regex: Option<String>,
125 pub first_line_regex: Option<String>,
126 pub injection_regex: Option<String>,
127 #[serde(default, skip_serializing_if = "PathsJSON::is_empty")]
128 pub highlights: PathsJSON,
129 #[serde(default, skip_serializing_if = "PathsJSON::is_empty")]
130 pub injections: PathsJSON,
131 #[serde(default, skip_serializing_if = "PathsJSON::is_empty")]
132 pub locals: PathsJSON,
133 #[serde(default, skip_serializing_if = "PathsJSON::is_empty")]
134 pub tags: PathsJSON,
135 #[serde(default, skip_serializing_if = "PathsJSON::is_empty")]
136 pub external_files: PathsJSON,
137}
138
139#[derive(Serialize, Deserialize)]
140#[serde(rename_all = "kebab-case")]
141pub struct TreeSitterJSON {
142 #[serde(rename = "$schema")]
143 pub schema: Option<String>,
144 pub grammars: Vec<Grammar>,
145 pub metadata: Metadata,
146 #[serde(default)]
147 pub bindings: Bindings,
148}
149
150impl TreeSitterJSON {
151 pub fn from_file(path: &Path) -> Result<Self> {
152 Ok(serde_json::from_str(&fs::read_to_string(
153 path.join("tree-sitter.json"),
154 )?)?)
155 }
156
157 #[must_use]
158 pub fn has_multiple_language_configs(&self) -> bool {
159 self.grammars.len() > 1
160 }
161}
162
163#[derive(Serialize, Deserialize)]
164#[serde(rename_all = "kebab-case")]
165pub struct Grammar {
166 pub name: String,
167 #[serde(skip_serializing_if = "Option::is_none")]
168 pub camelcase: Option<String>,
169 #[serde(skip_serializing_if = "Option::is_none")]
170 pub title: Option<String>,
171 pub scope: String,
172 #[serde(skip_serializing_if = "Option::is_none")]
173 pub path: Option<PathBuf>,
174 #[serde(default, skip_serializing_if = "PathsJSON::is_empty")]
175 pub external_files: PathsJSON,
176 pub file_types: Option<Vec<String>>,
177 #[serde(default, skip_serializing_if = "PathsJSON::is_empty")]
178 pub highlights: PathsJSON,
179 #[serde(default, skip_serializing_if = "PathsJSON::is_empty")]
180 pub injections: PathsJSON,
181 #[serde(default, skip_serializing_if = "PathsJSON::is_empty")]
182 pub locals: PathsJSON,
183 #[serde(default, skip_serializing_if = "PathsJSON::is_empty")]
184 pub tags: PathsJSON,
185 #[serde(skip_serializing_if = "Option::is_none")]
186 pub injection_regex: Option<String>,
187 #[serde(skip_serializing_if = "Option::is_none")]
188 pub first_line_regex: Option<String>,
189 #[serde(skip_serializing_if = "Option::is_none")]
190 pub content_regex: Option<String>,
191 #[serde(skip_serializing_if = "Option::is_none")]
192 pub class_name: Option<String>,
193}
194
195#[derive(Serialize, Deserialize)]
196pub struct Metadata {
197 pub version: Version,
198 #[serde(skip_serializing_if = "Option::is_none")]
199 pub license: Option<String>,
200 #[serde(skip_serializing_if = "Option::is_none")]
201 pub description: Option<String>,
202 #[serde(skip_serializing_if = "Option::is_none")]
203 pub authors: Option<Vec<Author>>,
204 #[serde(skip_serializing_if = "Option::is_none")]
205 pub links: Option<Links>,
206 #[serde(skip)]
207 pub namespace: Option<String>,
208}
209
210#[derive(Serialize, Deserialize)]
211pub struct Author {
212 pub name: String,
213 #[serde(skip_serializing_if = "Option::is_none")]
214 pub email: Option<String>,
215 #[serde(skip_serializing_if = "Option::is_none")]
216 pub url: Option<String>,
217}
218
219#[derive(Serialize, Deserialize)]
220pub struct Links {
221 pub repository: Url,
222 #[serde(skip_serializing_if = "Option::is_none")]
223 pub funding: Option<Url>,
224 #[serde(skip_serializing_if = "Option::is_none")]
225 pub homepage: Option<String>,
226}
227
228#[derive(Serialize, Deserialize)]
229#[serde(default)]
230pub struct Bindings {
231 pub c: bool,
232 pub go: bool,
233 #[serde(skip)]
234 pub java: bool,
235 #[serde(skip)]
236 pub kotlin: bool,
237 pub node: bool,
238 pub python: bool,
239 pub rust: bool,
240 pub swift: bool,
241 pub zig: bool,
242}
243
244impl Default for Bindings {
245 fn default() -> Self {
246 Self {
247 c: true,
248 go: true,
249 java: false,
250 kotlin: false,
251 node: true,
252 python: true,
253 rust: true,
254 swift: true,
255 zig: false,
256 }
257 }
258}
259
260fn deserialize_parser_directories<'de, D>(deserializer: D) -> Result<Vec<PathBuf>, D::Error>
264where
265 D: Deserializer<'de>,
266{
267 let paths = Vec::<PathBuf>::deserialize(deserializer)?;
268 let Ok(home) = etcetera::home_dir() else {
269 return Ok(paths);
270 };
271 let standardized = paths
272 .into_iter()
273 .map(|path| standardize_path(path, &home))
274 .collect();
275 Ok(standardized)
276}
277
278fn standardize_path(path: PathBuf, home: &Path) -> PathBuf {
279 if let Ok(p) = path.strip_prefix("~") {
280 return home.join(p);
281 }
282 if let Ok(p) = path.strip_prefix("$HOME") {
283 return home.join(p);
284 }
285 path
286}
287
288impl Config {
289 #[must_use]
290 pub fn initial() -> Self {
291 let home_dir = etcetera::home_dir().expect("Cannot determine home directory");
292 Self {
293 parser_directories: vec![
294 home_dir.join("github"),
295 home_dir.join("src"),
296 home_dir.join("source"),
297 home_dir.join("projects"),
298 home_dir.join("dev"),
299 home_dir.join("git"),
300 ],
301 }
302 }
303}
304
305const BUILD_TARGET: &str = env!("BUILD_TARGET");
306const BUILD_HOST: &str = env!("BUILD_HOST");
307
308pub struct LanguageConfiguration<'a> {
309 pub scope: Option<String>,
310 pub content_regex: Option<Regex>,
311 pub first_line_regex: Option<Regex>,
312 pub injection_regex: Option<Regex>,
313 pub file_types: Vec<String>,
314 pub root_path: PathBuf,
315 pub highlights_filenames: Option<Vec<PathBuf>>,
316 pub injections_filenames: Option<Vec<PathBuf>>,
317 pub locals_filenames: Option<Vec<PathBuf>>,
318 pub tags_filenames: Option<Vec<PathBuf>>,
319 pub language_name: String,
320 language_id: usize,
321 #[cfg(feature = "tree-sitter-highlight")]
322 highlight_config: OnceCell<Option<HighlightConfiguration>>,
323 #[cfg(feature = "tree-sitter-tags")]
324 tags_config: OnceCell<Option<TagsConfiguration>>,
325 #[cfg(feature = "tree-sitter-highlight")]
326 highlight_names: &'a Mutex<Vec<String>>,
327 #[cfg(feature = "tree-sitter-highlight")]
328 use_all_highlight_names: bool,
329 _phantom: PhantomData<&'a ()>,
330}
331
332pub struct Loader {
333 pub parser_lib_path: PathBuf,
334 languages_by_id: Vec<(PathBuf, OnceCell<Language>, Option<Vec<PathBuf>>)>,
335 language_configurations: Vec<LanguageConfiguration<'static>>,
336 language_configuration_ids_by_file_type: HashMap<String, Vec<usize>>,
337 language_configuration_in_current_path: Option<usize>,
338 language_configuration_ids_by_first_line_regex: HashMap<String, Vec<usize>>,
339 #[cfg(feature = "tree-sitter-highlight")]
340 highlight_names: Box<Mutex<Vec<String>>>,
341 #[cfg(feature = "tree-sitter-highlight")]
342 use_all_highlight_names: bool,
343 debug_build: bool,
344 sanitize_build: bool,
345 force_rebuild: bool,
346
347 #[cfg(feature = "wasm")]
348 wasm_store: Mutex<Option<tree_sitter::WasmStore>>,
349}
350
351pub struct CompileConfig<'a> {
352 pub src_path: &'a Path,
353 pub header_paths: Vec<&'a Path>,
354 pub parser_path: PathBuf,
355 pub scanner_path: Option<PathBuf>,
356 pub external_files: Option<&'a [PathBuf]>,
357 pub output_path: Option<PathBuf>,
358 pub flags: &'a [&'a str],
359 pub sanitize: bool,
360 pub name: String,
361}
362
363impl<'a> CompileConfig<'a> {
364 #[must_use]
365 pub fn new(
366 src_path: &'a Path,
367 externals: Option<&'a [PathBuf]>,
368 output_path: Option<PathBuf>,
369 ) -> Self {
370 Self {
371 src_path,
372 header_paths: vec![src_path],
373 parser_path: src_path.join("parser.c"),
374 scanner_path: None,
375 external_files: externals,
376 output_path,
377 flags: &[],
378 sanitize: false,
379 name: String::new(),
380 }
381 }
382}
383
384unsafe impl Sync for Loader {}
385
386impl Loader {
387 pub fn new() -> Result<Self> {
388 let parser_lib_path = if let Ok(path) = env::var("TREE_SITTER_LIBDIR") {
389 PathBuf::from(path)
390 } else {
391 if cfg!(target_os = "macos") {
392 let legacy_apple_path = etcetera::base_strategy::Apple::new()?
393 .cache_dir() .join("tree-sitter");
395 if legacy_apple_path.exists() && legacy_apple_path.is_dir() {
396 std::fs::remove_dir_all(legacy_apple_path)?;
397 }
398 }
399
400 etcetera::choose_base_strategy()?
401 .cache_dir()
402 .join("tree-sitter")
403 .join("lib")
404 };
405 Ok(Self::with_parser_lib_path(parser_lib_path))
406 }
407
408 #[must_use]
409 pub fn with_parser_lib_path(parser_lib_path: PathBuf) -> Self {
410 Self {
411 parser_lib_path,
412 languages_by_id: Vec::new(),
413 language_configurations: Vec::new(),
414 language_configuration_ids_by_file_type: HashMap::new(),
415 language_configuration_in_current_path: None,
416 language_configuration_ids_by_first_line_regex: HashMap::new(),
417 #[cfg(feature = "tree-sitter-highlight")]
418 highlight_names: Box::new(Mutex::new(Vec::new())),
419 #[cfg(feature = "tree-sitter-highlight")]
420 use_all_highlight_names: true,
421 debug_build: false,
422 sanitize_build: false,
423 force_rebuild: false,
424
425 #[cfg(feature = "wasm")]
426 wasm_store: Mutex::default(),
427 }
428 }
429
430 #[cfg(feature = "tree-sitter-highlight")]
431 #[cfg_attr(docsrs, doc(cfg(feature = "tree-sitter-highlight")))]
432 pub fn configure_highlights(&mut self, names: &[String]) {
433 self.use_all_highlight_names = false;
434 let mut highlights = self.highlight_names.lock().unwrap();
435 highlights.clear();
436 highlights.extend(names.iter().cloned());
437 }
438
439 #[must_use]
440 #[cfg(feature = "tree-sitter-highlight")]
441 #[cfg_attr(docsrs, doc(cfg(feature = "tree-sitter-highlight")))]
442 pub fn highlight_names(&self) -> Vec<String> {
443 self.highlight_names.lock().unwrap().clone()
444 }
445
446 pub fn find_all_languages(&mut self, config: &Config) -> Result<()> {
447 if config.parser_directories.is_empty() {
448 eprintln!("Warning: You have not configured any parser directories!");
449 eprintln!("Please run `tree-sitter init-config` and edit the resulting");
450 eprintln!("configuration file to indicate where we should look for");
451 eprintln!("language grammars.\n");
452 }
453 for parser_container_dir in &config.parser_directories {
454 if let Ok(entries) = fs::read_dir(parser_container_dir) {
455 for entry in entries {
456 let entry = entry?;
457 if let Some(parser_dir_name) = entry.file_name().to_str() {
458 if parser_dir_name.starts_with("tree-sitter-") {
459 self.find_language_configurations_at_path(
460 &parser_container_dir.join(parser_dir_name),
461 false,
462 )
463 .ok();
464 }
465 }
466 }
467 }
468 }
469 Ok(())
470 }
471
472 pub fn languages_at_path(&mut self, path: &Path) -> Result<Vec<(Language, String)>> {
473 if let Ok(configurations) = self.find_language_configurations_at_path(path, true) {
474 let mut language_ids = configurations
475 .iter()
476 .map(|c| (c.language_id, c.language_name.clone()))
477 .collect::<Vec<_>>();
478 language_ids.sort_unstable();
479 language_ids.dedup();
480 language_ids
481 .into_iter()
482 .map(|(id, name)| Ok((self.language_for_id(id)?, name)))
483 .collect::<Result<Vec<_>>>()
484 } else {
485 Ok(Vec::new())
486 }
487 }
488
489 #[must_use]
490 pub fn get_all_language_configurations(&self) -> Vec<(&LanguageConfiguration, &Path)> {
491 self.language_configurations
492 .iter()
493 .map(|c| (c, self.languages_by_id[c.language_id].0.as_ref()))
494 .collect()
495 }
496
497 pub fn language_configuration_for_scope(
498 &self,
499 scope: &str,
500 ) -> Result<Option<(Language, &LanguageConfiguration)>> {
501 for configuration in &self.language_configurations {
502 if configuration.scope.as_ref().is_some_and(|s| s == scope) {
503 let language = self.language_for_id(configuration.language_id)?;
504 return Ok(Some((language, configuration)));
505 }
506 }
507 Ok(None)
508 }
509
510 pub fn language_configuration_for_first_line_regex(
511 &self,
512 path: &Path,
513 ) -> Result<Option<(Language, &LanguageConfiguration)>> {
514 self.language_configuration_ids_by_first_line_regex
515 .iter()
516 .try_fold(None, |_, (regex, ids)| {
517 if let Some(regex) = Self::regex(Some(regex)) {
518 let file = fs::File::open(path)?;
519 let reader = BufReader::new(file);
520 let first_line = reader.lines().next().transpose()?;
521 if let Some(first_line) = first_line {
522 if regex.is_match(&first_line) && !ids.is_empty() {
523 let configuration = &self.language_configurations[ids[0]];
524 let language = self.language_for_id(configuration.language_id)?;
525 return Ok(Some((language, configuration)));
526 }
527 }
528 }
529
530 Ok(None)
531 })
532 }
533
534 pub fn language_configuration_for_file_name(
535 &self,
536 path: &Path,
537 ) -> Result<Option<(Language, &LanguageConfiguration)>> {
538 let configuration_ids = path
541 .file_name()
542 .and_then(|n| n.to_str())
543 .and_then(|file_name| self.language_configuration_ids_by_file_type.get(file_name))
544 .or_else(|| {
545 let mut path = path.to_owned();
546 let mut extensions = Vec::with_capacity(2);
547 while let Some(extension) = path.extension() {
548 extensions.push(extension.to_str()?.to_string());
549 path = PathBuf::from(path.file_stem()?.to_os_string());
550 }
551 extensions.reverse();
552 self.language_configuration_ids_by_file_type
553 .get(&extensions.join("."))
554 });
555
556 if let Some(configuration_ids) = configuration_ids {
557 if !configuration_ids.is_empty() {
558 let configuration = if configuration_ids.len() == 1 {
559 &self.language_configurations[configuration_ids[0]]
560 }
561 else {
564 let file_contents = fs::read(path)
565 .with_context(|| format!("Failed to read path {}", path.display()))?;
566 let file_contents = String::from_utf8_lossy(&file_contents);
567 let mut best_score = -2isize;
568 let mut best_configuration_id = None;
569 for configuration_id in configuration_ids {
570 let config = &self.language_configurations[*configuration_id];
571
572 let score;
575 if let Some(content_regex) = &config.content_regex {
576 if let Some(mat) = content_regex.find(&file_contents) {
577 score = (mat.end() - mat.start()) as isize;
578 }
579 else {
584 score = -1;
585 }
586 } else {
587 score = 0;
588 }
589 if score > best_score {
590 best_configuration_id = Some(*configuration_id);
591 best_score = score;
592 }
593 }
594
595 &self.language_configurations[best_configuration_id.unwrap()]
596 };
597
598 let language = self.language_for_id(configuration.language_id)?;
599 return Ok(Some((language, configuration)));
600 }
601 }
602
603 Ok(None)
604 }
605
606 pub fn language_configuration_for_injection_string(
607 &self,
608 string: &str,
609 ) -> Result<Option<(Language, &LanguageConfiguration)>> {
610 let mut best_match_length = 0;
611 let mut best_match_position = None;
612 for (i, configuration) in self.language_configurations.iter().enumerate() {
613 if let Some(injection_regex) = &configuration.injection_regex {
614 if let Some(mat) = injection_regex.find(string) {
615 let length = mat.end() - mat.start();
616 if length > best_match_length {
617 best_match_position = Some(i);
618 best_match_length = length;
619 }
620 }
621 }
622 }
623
624 if let Some(i) = best_match_position {
625 let configuration = &self.language_configurations[i];
626 let language = self.language_for_id(configuration.language_id)?;
627 Ok(Some((language, configuration)))
628 } else {
629 Ok(None)
630 }
631 }
632
633 pub fn language_for_configuration(
634 &self,
635 configuration: &LanguageConfiguration,
636 ) -> Result<Language> {
637 self.language_for_id(configuration.language_id)
638 }
639
640 fn language_for_id(&self, id: usize) -> Result<Language> {
641 let (path, language, externals) = &self.languages_by_id[id];
642 language
643 .get_or_try_init(|| {
644 let src_path = path.join("src");
645 self.load_language_at_path(CompileConfig::new(
646 &src_path,
647 externals.as_deref(),
648 None,
649 ))
650 })
651 .cloned()
652 }
653
654 pub fn compile_parser_at_path(
655 &self,
656 grammar_path: &Path,
657 output_path: PathBuf,
658 flags: &[&str],
659 ) -> Result<()> {
660 let src_path = grammar_path.join("src");
661 let mut config = CompileConfig::new(&src_path, None, Some(output_path));
662 config.flags = flags;
663 self.load_language_at_path(config).map(|_| ())
664 }
665
666 pub fn load_language_at_path(&self, mut config: CompileConfig) -> Result<Language> {
667 let grammar_path = config.src_path.join("grammar.json");
668 config.name = Self::grammar_json_name(&grammar_path)?;
669 self.load_language_at_path_with_name(config)
670 }
671
672 pub fn load_language_at_path_with_name(&self, mut config: CompileConfig) -> Result<Language> {
673 let mut lib_name = config.name.to_string();
674 let language_fn_name = format!(
675 "tree_sitter_{}",
676 replace_dashes_with_underscores(&config.name)
677 );
678 if self.debug_build {
679 lib_name.push_str(".debug._");
680 }
681
682 if self.sanitize_build {
683 lib_name.push_str(".sanitize._");
684 config.sanitize = true;
685 }
686
687 if config.output_path.is_none() {
688 fs::create_dir_all(&self.parser_lib_path)?;
689 }
690
691 let mut recompile = self.force_rebuild || config.output_path.is_some(); let output_path = config.output_path.unwrap_or_else(|| {
694 let mut path = self.parser_lib_path.join(lib_name);
695 path.set_extension(env::consts::DLL_EXTENSION);
696 #[cfg(feature = "wasm")]
697 if self.wasm_store.lock().unwrap().is_some() {
698 path.set_extension("wasm");
699 }
700 path
701 });
702 config.output_path = Some(output_path.clone());
703
704 let parser_path = config.src_path.join("parser.c");
705 config.scanner_path = self.get_scanner_path(config.src_path);
706
707 let mut paths_to_check = vec![parser_path];
708
709 if let Some(scanner_path) = config.scanner_path.as_ref() {
710 paths_to_check.push(scanner_path.clone());
711 }
712
713 paths_to_check.extend(
714 config
715 .external_files
716 .unwrap_or_default()
717 .iter()
718 .map(|p| config.src_path.join(p)),
719 );
720
721 if !recompile {
722 recompile = needs_recompile(&output_path, &paths_to_check)
723 .with_context(|| "Failed to compare source and binary timestamps")?;
724 }
725
726 #[cfg(feature = "wasm")]
727 if let Some(wasm_store) = self.wasm_store.lock().unwrap().as_mut() {
728 if recompile {
729 self.compile_parser_to_wasm(
730 &config.name,
731 None,
732 config.src_path,
733 config
734 .scanner_path
735 .as_ref()
736 .and_then(|p| p.strip_prefix(config.src_path).ok()),
737 &output_path,
738 false,
739 )?;
740 }
741
742 let wasm_bytes = fs::read(&output_path)?;
743 return Ok(wasm_store.load_language(&config.name, &wasm_bytes)?);
744 }
745
746 let lock_path = if env::var("CROSS_RUNNER").is_ok() {
747 tempfile::tempdir()
748 .unwrap()
749 .path()
750 .join("tree-sitter")
751 .join("lock")
752 .join(format!("{}.lock", config.name))
753 } else {
754 etcetera::choose_base_strategy()?
755 .cache_dir()
756 .join("tree-sitter")
757 .join("lock")
758 .join(format!("{}.lock", config.name))
759 };
760
761 if let Ok(lock_file) = fs::OpenOptions::new().write(true).open(&lock_path) {
762 recompile = false;
763 if lock_file.try_lock_exclusive().is_err() {
764 lock_file.lock_exclusive()?;
767 recompile = false;
768 } else {
769 let time = lock_file.metadata()?.modified()?.elapsed()?.as_secs();
773 if time > 30 {
774 fs::remove_file(&lock_path)?;
775 recompile = true;
776 }
777 }
778 }
779
780 if recompile {
781 fs::create_dir_all(lock_path.parent().unwrap()).with_context(|| {
782 format!(
783 "Failed to create directory {}",
784 lock_path.parent().unwrap().display()
785 )
786 })?;
787 let lock_file = fs::OpenOptions::new()
788 .create(true)
789 .truncate(true)
790 .write(true)
791 .open(&lock_path)?;
792 lock_file.lock_exclusive()?;
793
794 self.compile_parser_to_dylib(&config, &lock_file, &lock_path)?;
795
796 if config.scanner_path.is_some() {
797 self.check_external_scanner(&config.name, &output_path)?;
798 }
799 }
800
801 let library = unsafe { Library::new(&output_path) }
802 .with_context(|| format!("Error opening dynamic library {}", output_path.display()))?;
803 let language = unsafe {
804 let language_fn = library
805 .get::<Symbol<unsafe extern "C" fn() -> Language>>(language_fn_name.as_bytes())
806 .with_context(|| format!("Failed to load symbol {language_fn_name}"))?;
807 language_fn()
808 };
809 mem::forget(library);
810 Ok(language)
811 }
812
813 fn compile_parser_to_dylib(
814 &self,
815 config: &CompileConfig,
816 lock_file: &fs::File,
817 lock_path: &Path,
818 ) -> Result<(), Error> {
819 let mut cc_config = cc::Build::new();
820 cc_config
821 .cargo_metadata(false)
822 .cargo_warnings(false)
823 .target(BUILD_TARGET)
824 .host(BUILD_HOST)
825 .debug(self.debug_build)
826 .file(&config.parser_path)
827 .includes(&config.header_paths)
828 .std("c11");
829
830 if let Some(scanner_path) = config.scanner_path.as_ref() {
831 cc_config.file(scanner_path);
832 }
833
834 if self.debug_build {
835 cc_config.opt_level(0).extra_warnings(true);
836 } else {
837 cc_config.opt_level(2).extra_warnings(false);
838 }
839
840 for flag in config.flags {
841 cc_config.define(flag, None);
842 }
843
844 let compiler = cc_config.get_compiler();
845 let mut command = Command::new(compiler.path());
846 command.args(compiler.args());
847 for (key, value) in compiler.env() {
848 command.env(key, value);
849 }
850
851 let output_path = config.output_path.as_ref().unwrap();
852
853 if compiler.is_like_msvc() {
854 let out = format!("-out:{}", output_path.to_str().unwrap());
855 command.arg(if self.debug_build { "-LDd" } else { "-LD" });
856 command.arg("-utf-8");
857 command.args(cc_config.get_files());
858 command.arg("-link").arg(out);
859 } else {
860 command.arg("-Werror=implicit-function-declaration");
861 if cfg!(any(target_os = "macos", target_os = "ios")) {
862 command.arg("-dynamiclib");
863 command.arg("-UTREE_SITTER_REUSE_ALLOCATOR");
865 } else {
866 command.arg("-shared");
867 }
868 command.args(cc_config.get_files());
869 command.arg("-o").arg(output_path);
870 }
871
872 let output = command.output().with_context(|| {
873 format!("Failed to execute the C compiler with the following command:\n{command:?}")
874 })?;
875
876 FileExt::unlock(lock_file)?;
877 fs::remove_file(lock_path)?;
878
879 if output.status.success() {
880 Ok(())
881 } else {
882 Err(anyhow!(
883 "Parser compilation failed.\nStdout: {}\nStderr: {}",
884 String::from_utf8_lossy(&output.stdout),
885 String::from_utf8_lossy(&output.stderr)
886 ))
887 }
888 }
889
890 #[cfg(unix)]
891 fn check_external_scanner(&self, name: &str, library_path: &Path) -> Result<()> {
892 let prefix = if cfg!(any(target_os = "macos", target_os = "ios")) {
893 "_"
894 } else {
895 ""
896 };
897 let mut must_have = vec![
898 format!("{prefix}tree_sitter_{name}_external_scanner_create"),
899 format!("{prefix}tree_sitter_{name}_external_scanner_destroy"),
900 format!("{prefix}tree_sitter_{name}_external_scanner_serialize"),
901 format!("{prefix}tree_sitter_{name}_external_scanner_deserialize"),
902 format!("{prefix}tree_sitter_{name}_external_scanner_scan"),
903 ];
904
905 let command = Command::new("nm")
906 .arg("-W")
907 .arg("-U")
908 .arg(library_path)
909 .output();
910 if let Ok(output) = command {
911 if output.status.success() {
912 let mut found_non_static = false;
913 for line in String::from_utf8_lossy(&output.stdout).lines() {
914 if line.contains(" T ") {
915 if let Some(function_name) =
916 line.split_whitespace().collect::<Vec<_>>().get(2)
917 {
918 if !line.contains("tree_sitter_") {
919 if !found_non_static {
920 found_non_static = true;
921 eprintln!("Warning: Found non-static non-tree-sitter functions in the external scannner");
922 }
923 eprintln!(" `{function_name}`");
924 } else {
925 must_have.retain(|f| f != function_name);
926 }
927 }
928 }
929 }
930 if found_non_static {
931 eprintln!("Consider making these functions static, they can cause conflicts when another tree-sitter project uses the same function name");
932 }
933
934 if !must_have.is_empty() {
935 let missing = must_have
936 .iter()
937 .map(|f| format!(" `{f}`"))
938 .collect::<Vec<_>>()
939 .join("\n");
940
941 return Err(anyhow!(format!(
942 indoc! {"
943 Missing required functions in the external scanner, parsing won't work without these!
944
945 {}
946
947 You can read more about this at https://tree-sitter.github.io/tree-sitter/creating-parsers/4-external-scanners
948 "},
949 missing,
950 )));
951 }
952 }
953 }
954
955 Ok(())
956 }
957
958 #[cfg(windows)]
959 fn check_external_scanner(&self, _name: &str, _library_path: &Path) -> Result<()> {
960 Ok(())
971 }
972
973 pub fn compile_parser_to_wasm(
974 &self,
975 language_name: &str,
976 root_path: Option<&Path>,
977 src_path: &Path,
978 scanner_filename: Option<&Path>,
979 output_path: &Path,
980 force_docker: bool,
981 ) -> Result<(), Error> {
982 #[derive(PartialEq, Eq)]
983 enum EmccSource {
984 Native,
985 Docker,
986 Podman,
987 }
988
989 let root_path = root_path.unwrap_or(src_path);
990 let emcc_name = if cfg!(windows) { "emcc.bat" } else { "emcc" };
991
992 let source = if !force_docker && Command::new(emcc_name).output().is_ok() {
994 EmccSource::Native
995 } else if Command::new("docker")
996 .output()
997 .is_ok_and(|out| out.status.success())
998 {
999 EmccSource::Docker
1000 } else if Command::new("podman")
1001 .arg("--version")
1002 .output()
1003 .is_ok_and(|out| out.status.success())
1004 {
1005 EmccSource::Podman
1006 } else {
1007 return Err(anyhow!(
1008 "You must have either emcc, docker, or podman on your PATH to run this command"
1009 ));
1010 };
1011
1012 let mut command = match source {
1013 EmccSource::Native => {
1014 let mut command = Command::new(emcc_name);
1015 command.current_dir(src_path);
1016 command
1017 }
1018
1019 EmccSource::Docker | EmccSource::Podman => {
1020 let mut command = match source {
1021 EmccSource::Docker => Command::new("docker"),
1022 EmccSource::Podman => Command::new("podman"),
1023 EmccSource::Native => unreachable!(),
1024 };
1025 command.args(["run", "--rm"]);
1026
1027 let workdir = if root_path == src_path {
1029 PathBuf::from("/src")
1030 } else {
1031 let mut path = PathBuf::from("/src");
1032 path.push(src_path.strip_prefix(root_path).unwrap());
1033 path
1034 };
1035 command.args(["--workdir", &workdir.to_slash_lossy()]);
1036
1037 let mut volume_string = OsString::from(&root_path);
1039 volume_string.push(":/src:Z");
1040 command.args([OsStr::new("--volume"), &volume_string]);
1041
1042 command.env("PODMAN_USERNS", "keep-id");
1049
1050 #[cfg(unix)]
1053 {
1054 #[link(name = "c")]
1055 extern "C" {
1056 fn getuid() -> u32;
1057 }
1058 if source == EmccSource::Docker {
1060 let user_id = unsafe { getuid() };
1061 command.args(["--user", &user_id.to_string()]);
1062 }
1063 };
1064
1065 command.args([EMSCRIPTEN_TAG, "emcc"]);
1067 command
1068 }
1069 };
1070
1071 let output_name = "output.wasm";
1072
1073 command.args([
1074 "-o",
1075 output_name,
1076 "-Os",
1077 "-s",
1078 "WASM=1",
1079 "-s",
1080 "SIDE_MODULE=2",
1081 "-s",
1082 "TOTAL_MEMORY=33554432",
1083 "-s",
1084 "NODEJS_CATCH_EXIT=0",
1085 "-s",
1086 &format!("EXPORTED_FUNCTIONS=[\"_tree_sitter_{language_name}\"]"),
1087 "-fno-exceptions",
1088 "-fvisibility=hidden",
1089 "-I",
1090 ".",
1091 ]);
1092
1093 if let Some(scanner_filename) = scanner_filename {
1094 command.arg(scanner_filename);
1095 }
1096
1097 command.arg("parser.c");
1098 let status = command
1099 .spawn()
1100 .with_context(|| "Failed to run emcc command")?
1101 .wait()?;
1102 if !status.success() {
1103 return Err(anyhow!("emcc command failed"));
1104 }
1105
1106 fs::rename(src_path.join(output_name), output_path)
1107 .context("failed to rename wasm output file")?;
1108
1109 Ok(())
1110 }
1111
1112 #[must_use]
1113 #[cfg(feature = "tree-sitter-highlight")]
1114 pub fn highlight_config_for_injection_string<'a>(
1115 &'a self,
1116 string: &str,
1117 ) -> Option<&'a HighlightConfiguration> {
1118 match self.language_configuration_for_injection_string(string) {
1119 Err(e) => {
1120 eprintln!("Failed to load language for injection string '{string}': {e}",);
1121 None
1122 }
1123 Ok(None) => None,
1124 Ok(Some((language, configuration))) => {
1125 match configuration.highlight_config(language, None) {
1126 Err(e) => {
1127 eprintln!(
1128 "Failed to load property sheet for injection string '{string}': {e}",
1129 );
1130 None
1131 }
1132 Ok(None) => None,
1133 Ok(Some(config)) => Some(config),
1134 }
1135 }
1136 }
1137 }
1138
1139 #[must_use]
1140 pub fn get_language_configuration_in_current_path(&self) -> Option<&LanguageConfiguration> {
1141 self.language_configuration_in_current_path
1142 .map(|i| &self.language_configurations[i])
1143 }
1144
1145 pub fn find_language_configurations_at_path(
1146 &mut self,
1147 parser_path: &Path,
1148 set_current_path_config: bool,
1149 ) -> Result<&[LanguageConfiguration]> {
1150 let initial_language_configuration_count = self.language_configurations.len();
1151
1152 let ts_json = TreeSitterJSON::from_file(parser_path);
1153 if let Ok(config) = ts_json {
1154 let language_count = self.languages_by_id.len();
1155 for grammar in config.grammars {
1156 let language_path = parser_path.join(grammar.path.unwrap_or(PathBuf::from(".")));
1160
1161 let mut language_id = None;
1164 for (id, (path, _, _)) in
1165 self.languages_by_id.iter().enumerate().skip(language_count)
1166 {
1167 if language_path == *path {
1168 language_id = Some(id);
1169 }
1170 }
1171
1172 let language_id = if let Some(language_id) = language_id {
1174 language_id
1175 } else {
1176 self.languages_by_id.push((
1177 language_path,
1178 OnceCell::new(),
1179 grammar.external_files.clone().into_vec().map(|files| {
1180 files.into_iter()
1181 .map(|path| {
1182 let path = parser_path.join(path);
1183 if path.starts_with(parser_path) {
1185 Ok(path)
1186 } else {
1187 Err(anyhow!("External file path {path:?} is outside of parser directory {parser_path:?}"))
1188 }
1189 })
1190 .collect::<Result<Vec<_>>>()
1191 }).transpose()?,
1192 ));
1193 self.languages_by_id.len() - 1
1194 };
1195
1196 let configuration = LanguageConfiguration {
1197 root_path: parser_path.to_path_buf(),
1198 language_name: grammar.name,
1199 scope: Some(grammar.scope),
1200 language_id,
1201 file_types: grammar.file_types.unwrap_or_default(),
1202 content_regex: Self::regex(grammar.content_regex.as_deref()),
1203 first_line_regex: Self::regex(grammar.first_line_regex.as_deref()),
1204 injection_regex: Self::regex(grammar.injection_regex.as_deref()),
1205 injections_filenames: grammar.injections.into_vec(),
1206 locals_filenames: grammar.locals.into_vec(),
1207 tags_filenames: grammar.tags.into_vec(),
1208 highlights_filenames: grammar.highlights.into_vec(),
1209 #[cfg(feature = "tree-sitter-highlight")]
1210 highlight_config: OnceCell::new(),
1211 #[cfg(feature = "tree-sitter-tags")]
1212 tags_config: OnceCell::new(),
1213 #[cfg(feature = "tree-sitter-highlight")]
1214 highlight_names: &self.highlight_names,
1215 #[cfg(feature = "tree-sitter-highlight")]
1216 use_all_highlight_names: self.use_all_highlight_names,
1217 _phantom: PhantomData,
1218 };
1219
1220 for file_type in &configuration.file_types {
1221 self.language_configuration_ids_by_file_type
1222 .entry(file_type.to_string())
1223 .or_default()
1224 .push(self.language_configurations.len());
1225 }
1226 if let Some(first_line_regex) = &configuration.first_line_regex {
1227 self.language_configuration_ids_by_first_line_regex
1228 .entry(first_line_regex.to_string())
1229 .or_default()
1230 .push(self.language_configurations.len());
1231 }
1232
1233 self.language_configurations.push(unsafe {
1234 mem::transmute::<LanguageConfiguration<'_>, LanguageConfiguration<'static>>(
1235 configuration,
1236 )
1237 });
1238
1239 if set_current_path_config && self.language_configuration_in_current_path.is_none()
1240 {
1241 self.language_configuration_in_current_path =
1242 Some(self.language_configurations.len() - 1);
1243 }
1244 }
1245 } else if let Err(e) = ts_json {
1246 match e.downcast_ref::<std::io::Error>() {
1247 Some(e) if e.kind() == std::io::ErrorKind::NotFound => {}
1249 _ => {
1250 eprintln!(
1251 "Warning: Failed to parse {} -- {e}",
1252 parser_path.join("tree-sitter.json").display()
1253 );
1254 }
1255 }
1256 }
1257
1258 if self.language_configurations.len() == initial_language_configuration_count
1262 && parser_path.join("src").join("grammar.json").exists()
1263 {
1264 let grammar_path = parser_path.join("src").join("grammar.json");
1265 let language_name = Self::grammar_json_name(&grammar_path)?;
1266 let configuration = LanguageConfiguration {
1267 root_path: parser_path.to_owned(),
1268 language_name,
1269 language_id: self.languages_by_id.len(),
1270 file_types: Vec::new(),
1271 scope: None,
1272 content_regex: None,
1273 first_line_regex: None,
1274 injection_regex: None,
1275 injections_filenames: None,
1276 locals_filenames: None,
1277 highlights_filenames: None,
1278 tags_filenames: None,
1279 #[cfg(feature = "tree-sitter-highlight")]
1280 highlight_config: OnceCell::new(),
1281 #[cfg(feature = "tree-sitter-tags")]
1282 tags_config: OnceCell::new(),
1283 #[cfg(feature = "tree-sitter-highlight")]
1284 highlight_names: &self.highlight_names,
1285 #[cfg(feature = "tree-sitter-highlight")]
1286 use_all_highlight_names: self.use_all_highlight_names,
1287 _phantom: PhantomData,
1288 };
1289 self.language_configurations.push(unsafe {
1290 mem::transmute::<LanguageConfiguration<'_>, LanguageConfiguration<'static>>(
1291 configuration,
1292 )
1293 });
1294 self.languages_by_id
1295 .push((parser_path.to_owned(), OnceCell::new(), None));
1296 }
1297
1298 Ok(&self.language_configurations[initial_language_configuration_count..])
1299 }
1300
1301 fn regex(pattern: Option<&str>) -> Option<Regex> {
1302 pattern.and_then(|r| RegexBuilder::new(r).multi_line(true).build().ok())
1303 }
1304
1305 fn grammar_json_name(grammar_path: &Path) -> Result<String> {
1306 let file = fs::File::open(grammar_path).with_context(|| {
1307 format!("Failed to open grammar.json at {}", grammar_path.display())
1308 })?;
1309
1310 let first_three_lines = BufReader::new(file)
1311 .lines()
1312 .take(3)
1313 .collect::<Result<Vec<_>, _>>()
1314 .with_context(|| {
1315 format!(
1316 "Failed to read the first three lines of grammar.json at {}",
1317 grammar_path.display()
1318 )
1319 })?
1320 .join("\n");
1321
1322 let name = GRAMMAR_NAME_REGEX
1323 .captures(&first_three_lines)
1324 .and_then(|c| c.get(1))
1325 .ok_or_else(|| {
1326 anyhow!(
1327 "Failed to parse the language name from grammar.json at {}",
1328 grammar_path.display()
1329 )
1330 })?;
1331
1332 Ok(name.as_str().to_string())
1333 }
1334
1335 pub fn select_language(
1336 &mut self,
1337 path: &Path,
1338 current_dir: &Path,
1339 scope: Option<&str>,
1340 ) -> Result<Language> {
1341 if let Some(scope) = scope {
1342 if let Some(config) = self
1343 .language_configuration_for_scope(scope)
1344 .with_context(|| format!("Failed to load language for scope '{scope}'"))?
1345 {
1346 Ok(config.0)
1347 } else {
1348 Err(anyhow!("Unknown scope '{scope}'"))
1349 }
1350 } else if let Some((lang, _)) = self
1351 .language_configuration_for_file_name(path)
1352 .with_context(|| {
1353 format!(
1354 "Failed to load language for file name {}",
1355 path.file_name().unwrap().to_string_lossy()
1356 )
1357 })?
1358 {
1359 Ok(lang)
1360 } else if let Some(id) = self.language_configuration_in_current_path {
1361 Ok(self.language_for_id(self.language_configurations[id].language_id)?)
1362 } else if let Some(lang) = self
1363 .languages_at_path(current_dir)
1364 .with_context(|| "Failed to load language in current directory")?
1365 .first()
1366 .cloned()
1367 {
1368 Ok(lang.0)
1369 } else if let Some(lang) = self.language_configuration_for_first_line_regex(path)? {
1370 Ok(lang.0)
1371 } else {
1372 Err(anyhow!("No language found"))
1373 }
1374 }
1375
1376 pub fn debug_build(&mut self, flag: bool) {
1377 self.debug_build = flag;
1378 }
1379
1380 pub fn sanitize_build(&mut self, flag: bool) {
1381 self.sanitize_build = flag;
1382 }
1383
1384 pub fn force_rebuild(&mut self, rebuild: bool) {
1385 self.force_rebuild = rebuild;
1386 }
1387
1388 #[cfg(feature = "wasm")]
1389 #[cfg_attr(docsrs, doc(cfg(feature = "wasm")))]
1390 pub fn use_wasm(&mut self, engine: &tree_sitter::wasmtime::Engine) {
1391 *self.wasm_store.lock().unwrap() = Some(tree_sitter::WasmStore::new(engine).unwrap());
1392 }
1393
1394 #[must_use]
1395 pub fn get_scanner_path(&self, src_path: &Path) -> Option<PathBuf> {
1396 let path = src_path.join("scanner.c");
1397 path.exists().then_some(path)
1398 }
1399}
1400
1401impl LanguageConfiguration<'_> {
1402 #[cfg(feature = "tree-sitter-highlight")]
1403 pub fn highlight_config(
1404 &self,
1405 language: Language,
1406 paths: Option<&[PathBuf]>,
1407 ) -> Result<Option<&HighlightConfiguration>> {
1408 let (highlights_filenames, injections_filenames, locals_filenames) = match paths {
1409 Some(paths) => (
1410 Some(
1411 paths
1412 .iter()
1413 .filter(|p| p.ends_with("highlights.scm"))
1414 .cloned()
1415 .collect::<Vec<_>>(),
1416 ),
1417 Some(
1418 paths
1419 .iter()
1420 .filter(|p| p.ends_with("tags.scm"))
1421 .cloned()
1422 .collect::<Vec<_>>(),
1423 ),
1424 Some(
1425 paths
1426 .iter()
1427 .filter(|p| p.ends_with("locals.scm"))
1428 .cloned()
1429 .collect::<Vec<_>>(),
1430 ),
1431 ),
1432 None => (None, None, None),
1433 };
1434 self.highlight_config
1435 .get_or_try_init(|| {
1436 let (highlights_query, highlight_ranges) = self.read_queries(
1437 if highlights_filenames.is_some() {
1438 highlights_filenames.as_deref()
1439 } else {
1440 self.highlights_filenames.as_deref()
1441 },
1442 "highlights.scm",
1443 )?;
1444 let (injections_query, injection_ranges) = self.read_queries(
1445 if injections_filenames.is_some() {
1446 injections_filenames.as_deref()
1447 } else {
1448 self.injections_filenames.as_deref()
1449 },
1450 "injections.scm",
1451 )?;
1452 let (locals_query, locals_ranges) = self.read_queries(
1453 if locals_filenames.is_some() {
1454 locals_filenames.as_deref()
1455 } else {
1456 self.locals_filenames.as_deref()
1457 },
1458 "locals.scm",
1459 )?;
1460
1461 if highlights_query.is_empty() {
1462 Ok(None)
1463 } else {
1464 let mut result = HighlightConfiguration::new(
1465 language,
1466 &self.language_name,
1467 &highlights_query,
1468 &injections_query,
1469 &locals_query,
1470 )
1471 .map_err(|error| match error.kind {
1472 QueryErrorKind::Language => Error::from(error),
1473 _ => {
1474 if error.offset < injections_query.len() {
1475 Self::include_path_in_query_error(
1476 error,
1477 &injection_ranges,
1478 &injections_query,
1479 0,
1480 )
1481 } else if error.offset < injections_query.len() + locals_query.len() {
1482 Self::include_path_in_query_error(
1483 error,
1484 &locals_ranges,
1485 &locals_query,
1486 injections_query.len(),
1487 )
1488 } else {
1489 Self::include_path_in_query_error(
1490 error,
1491 &highlight_ranges,
1492 &highlights_query,
1493 injections_query.len() + locals_query.len(),
1494 )
1495 }
1496 }
1497 })?;
1498 let mut all_highlight_names = self.highlight_names.lock().unwrap();
1499 if self.use_all_highlight_names {
1500 for capture_name in result.query.capture_names() {
1501 if !all_highlight_names.iter().any(|x| x == capture_name) {
1502 all_highlight_names.push((*capture_name).to_string());
1503 }
1504 }
1505 }
1506 result.configure(all_highlight_names.as_slice());
1507 drop(all_highlight_names);
1508 Ok(Some(result))
1509 }
1510 })
1511 .map(Option::as_ref)
1512 }
1513
1514 #[cfg(feature = "tree-sitter-tags")]
1515 pub fn tags_config(&self, language: Language) -> Result<Option<&TagsConfiguration>> {
1516 self.tags_config
1517 .get_or_try_init(|| {
1518 let (tags_query, tags_ranges) =
1519 self.read_queries(self.tags_filenames.as_deref(), "tags.scm")?;
1520 let (locals_query, locals_ranges) =
1521 self.read_queries(self.locals_filenames.as_deref(), "locals.scm")?;
1522 if tags_query.is_empty() {
1523 Ok(None)
1524 } else {
1525 TagsConfiguration::new(language, &tags_query, &locals_query)
1526 .map(Some)
1527 .map_err(|error| {
1528 if let TagsError::Query(error) = error {
1529 if error.offset < locals_query.len() {
1530 Self::include_path_in_query_error(
1531 error,
1532 &locals_ranges,
1533 &locals_query,
1534 0,
1535 )
1536 } else {
1537 Self::include_path_in_query_error(
1538 error,
1539 &tags_ranges,
1540 &tags_query,
1541 locals_query.len(),
1542 )
1543 }
1544 } else {
1545 error.into()
1546 }
1547 })
1548 }
1549 })
1550 .map(Option::as_ref)
1551 }
1552
1553 #[cfg(any(feature = "tree-sitter-highlight", feature = "tree-sitter-tags"))]
1554 fn include_path_in_query_error(
1555 mut error: QueryError,
1556 ranges: &[(PathBuf, Range<usize>)],
1557 source: &str,
1558 start_offset: usize,
1559 ) -> Error {
1560 let offset_within_section = error.offset - start_offset;
1561 let (path, range) = ranges
1562 .iter()
1563 .find(|(_, range)| range.contains(&offset_within_section))
1564 .unwrap_or_else(|| ranges.last().unwrap());
1565 error.offset = offset_within_section - range.start;
1566 error.row = source[range.start..offset_within_section]
1567 .matches('\n')
1568 .count();
1569 Error::from(error).context(format!("Error in query file {}", path.display()))
1570 }
1571
1572 #[allow(clippy::type_complexity)]
1573 #[cfg(any(feature = "tree-sitter-highlight", feature = "tree-sitter-tags"))]
1574 fn read_queries(
1575 &self,
1576 paths: Option<&[PathBuf]>,
1577 default_path: &str,
1578 ) -> Result<(String, Vec<(PathBuf, Range<usize>)>)> {
1579 let mut query = String::new();
1580 let mut path_ranges = Vec::new();
1581 if let Some(paths) = paths {
1582 for path in paths {
1583 let abs_path = self.root_path.join(path);
1584 let prev_query_len = query.len();
1585 query += &fs::read_to_string(&abs_path)
1586 .with_context(|| format!("Failed to read query file {}", path.display()))?;
1587 path_ranges.push((path.clone(), prev_query_len..query.len()));
1588 }
1589 } else {
1590 if default_path == "highlights.scm" || default_path == "tags.scm" {
1592 eprintln!(
1593 indoc! {"
1594 Warning: you should add a `{}` entry pointing to the highlights path in the `tree-sitter` object in the grammar's tree-sitter.json file.
1595 See more here: https://tree-sitter.github.io/tree-sitter/3-syntax-highlighting#query-paths
1596 "},
1597 default_path.replace(".scm", "")
1598 );
1599 }
1600 let queries_path = self.root_path.join("queries");
1601 let path = queries_path.join(default_path);
1602 if path.exists() {
1603 query = fs::read_to_string(&path)
1604 .with_context(|| format!("Failed to read query file {}", path.display()))?;
1605 path_ranges.push((PathBuf::from(default_path), 0..query.len()));
1606 }
1607 }
1608
1609 Ok((query, path_ranges))
1610 }
1611}
1612
1613fn needs_recompile(lib_path: &Path, paths_to_check: &[PathBuf]) -> Result<bool> {
1614 if !lib_path.exists() {
1615 return Ok(true);
1616 }
1617 let lib_mtime = mtime(lib_path)
1618 .with_context(|| format!("Failed to read mtime of {}", lib_path.display()))?;
1619 for path in paths_to_check {
1620 if mtime(path)? > lib_mtime {
1621 return Ok(true);
1622 }
1623 }
1624 Ok(false)
1625}
1626
1627fn mtime(path: &Path) -> Result<SystemTime> {
1628 Ok(fs::metadata(path)?.modified()?)
1629}
1630
1631fn replace_dashes_with_underscores(name: &str) -> String {
1632 let mut result = String::with_capacity(name.len());
1633 for c in name.chars() {
1634 if c == '-' {
1635 result.push('_');
1636 } else {
1637 result.push(c);
1638 }
1639 }
1640 result
1641}