1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
//! Dynamic grammar loading for tree-sitter.
//!
//! Loads tree-sitter grammars from shared libraries (.so/.dylib/.dll).
//! Also loads highlight queries (.scm files) for syntax highlighting.
//! Grammars are compiled from arborium sources via `cargo xtask build-grammars`.
//!
//! # ABI Compatibility
//!
//! Tree-sitter grammars have an ABI version embedded at compile time. The tree-sitter
//! library only loads grammars within its supported version range:
//! - tree-sitter 0.24: ABI 13-14
//! - tree-sitter 0.25+: ABI 13-15
//!
//! Arborium grammar crates embed the ABI version in their parser.c source. When arborium
//! updates to use newer tree-sitter, grammars must be recompiled. Stale grammars in
//! `~/.config/moss/grammars/` may cause `LanguageError { version: N }` if incompatible.
//!
//! # Lifetime Requirements
//!
//! **IMPORTANT**: The `GrammarLoader` must outlive any `Language` or `Tree` obtained from it.
//! The loader holds the shared library (`Library`) that contains the grammar's code. If the
//! loader is dropped, the library is unloaded, and any `Language`/`Tree` references become
//! dangling pointers (use-after-free, likely segfault).
//!
//! Safe patterns:
//! - Use a global singleton loader (see `normalize::parsers::grammar_loader()`)
//! - Keep the loader in scope for the duration of tree usage
//! - Return `(Tree, GrammarLoader)` tuples from helper functions
//!
//! Unsafe pattern (causes segfault):
//! ```ignore
//! fn parse(code: &str) -> Tree {
//! let loader = GrammarLoader::new(); // Created here
//! let lang = loader.get("python").unwrap();
//! let mut parser = Parser::new();
//! parser.set_language(&lang).unwrap();
//! parser.parse(code, None).unwrap() // Tree returned
//! } // loader dropped here - library unloaded!
//! // Tree now has dangling pointers -> segfault on use
//! ```
use libloading::{Library, Symbol};
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::{Arc, RwLock};
use tree_sitter::Language;
use tree_sitter_language::LanguageFn;
/// Loaded grammar with its backing library.
///
/// The `_library` field keeps the shared library loaded in memory. The `language`
/// field contains pointers into this library's memory. Dropping the library while
/// the language is in use causes undefined behavior (typically segfault).
struct LoadedGrammar {
/// Backing shared library - must outlive any use of `language`.
_library: Library,
/// Tree-sitter Language (contains pointers into `_library`).
language: Language,
}
/// Dynamic grammar loader with caching.
pub struct GrammarLoader {
/// Search paths for grammar libraries.
search_paths: Vec<PathBuf>,
/// Cached loaded grammars.
cache: RwLock<HashMap<String, Arc<LoadedGrammar>>>,
/// Cached highlight queries.
highlight_cache: RwLock<HashMap<String, Arc<String>>>,
/// Cached injection queries.
injection_cache: RwLock<HashMap<String, Arc<String>>>,
}
impl GrammarLoader {
/// Create a new grammar loader with default search paths.
///
/// Search order:
/// 1. `MOSS_GRAMMAR_PATH` environment variable (colon-separated)
/// 2. `~/.config/moss/grammars/`
pub fn new() -> Self {
let mut paths = Vec::new();
// Environment variable takes priority
if let Ok(env_path) = std::env::var("MOSS_GRAMMAR_PATH") {
for p in env_path.split(':') {
if !p.is_empty() {
paths.push(PathBuf::from(p));
}
}
}
// User config directory
if let Some(config) = dirs::config_dir() {
paths.push(config.join("moss/grammars"));
}
Self {
search_paths: paths,
cache: RwLock::new(HashMap::new()),
highlight_cache: RwLock::new(HashMap::new()),
injection_cache: RwLock::new(HashMap::new()),
}
}
/// Create a loader with custom search paths.
pub fn with_paths(paths: Vec<PathBuf>) -> Self {
Self {
search_paths: paths,
cache: RwLock::new(HashMap::new()),
highlight_cache: RwLock::new(HashMap::new()),
injection_cache: RwLock::new(HashMap::new()),
}
}
/// Add a search path.
pub fn add_path(&mut self, path: PathBuf) {
self.search_paths.push(path);
}
/// Get a grammar by name.
///
/// Returns None if grammar not found in search paths.
pub fn get(&self, name: &str) -> Option<Language> {
// Check cache first
if let Some(loaded) = self.cache.read().ok()?.get(name) {
return Some(loaded.language.clone());
}
self.load_external(name)
}
/// Get the highlight query for a grammar.
///
/// Returns None if no highlight query found for the grammar.
/// Query files are {name}.highlights.scm in the grammar search paths.
pub fn get_highlights(&self, name: &str) -> Option<Arc<String>> {
// Check cache first
if let Some(query) = self.highlight_cache.read().ok()?.get(name) {
return Some(Arc::clone(query));
}
self.load_query(name, "highlights", &self.highlight_cache)
}
/// Get the injection query for a grammar.
///
/// Returns None if no injection query found for the grammar.
/// Query files are {name}.injections.scm in the grammar search paths.
pub fn get_injections(&self, name: &str) -> Option<Arc<String>> {
// Check cache first
if let Some(query) = self.injection_cache.read().ok()?.get(name) {
return Some(Arc::clone(query));
}
self.load_query(name, "injections", &self.injection_cache)
}
/// Load a query file (.scm) from external file.
fn load_query(
&self,
name: &str,
query_type: &str,
cache: &RwLock<HashMap<String, Arc<String>>>,
) -> Option<Arc<String>> {
let scm_name = format!("{name}.{query_type}.scm");
for search_path in &self.search_paths {
let scm_path = search_path.join(&scm_name);
if scm_path.exists() {
if let Ok(content) = std::fs::read_to_string(&scm_path) {
let query = Arc::new(content);
// Cache it
if let Ok(mut c) = cache.write() {
c.insert(name.to_string(), Arc::clone(&query));
}
return Some(query);
}
}
}
None
}
/// Load a grammar from external .so file.
fn load_external(&self, name: &str) -> Option<Language> {
let lib_name = grammar_lib_name(name);
for search_path in &self.search_paths {
let lib_path = search_path.join(&lib_name);
if lib_path.exists() {
if let Some(lang) = self.load_from_path(name, &lib_path) {
return Some(lang);
}
}
}
None
}
/// Load grammar from a specific path.
fn load_from_path(&self, name: &str, path: &Path) -> Option<Language> {
// SAFETY: Loading shared libraries is inherently unsafe. We accept this risk because:
// 1. Grammars come from arborium (bundled) or user-configured search paths
// 2. The alternative (no dynamic loading) would require compiling all grammars statically
// 3. Tree-sitter grammars are widely used and well-tested
let library = unsafe { Library::new(path).ok()? };
let symbol_name = grammar_symbol_name(name);
// SAFETY: We call the tree-sitter grammar function which returns a Language pointer.
// The function signature is defined by tree-sitter's C ABI. We trust that:
// 1. The symbol exists (checked by library.get)
// 2. The function conforms to tree-sitter's expected signature
// 3. The returned Language is valid for the lifetime of the library
let language = unsafe {
let func: Symbol<unsafe extern "C" fn() -> *const ()> =
library.get(symbol_name.as_bytes()).ok()?;
let lang_fn = LanguageFn::from_raw(*func);
Language::new(lang_fn)
};
// Cache the loaded grammar
let loaded = Arc::new(LoadedGrammar {
_library: library,
language: language.clone(),
});
if let Ok(mut cache) = self.cache.write() {
cache.insert(name.to_string(), loaded);
}
Some(language)
}
/// List available grammars in search paths.
pub fn available_external(&self) -> Vec<String> {
let mut grammars = Vec::new();
let ext = grammar_extension();
for path in &self.search_paths {
if let Ok(entries) = std::fs::read_dir(path) {
for entry in entries.flatten() {
let name = entry.file_name();
let name_str = name.to_string_lossy();
if name_str.ends_with(ext) {
let grammar_name = name_str.trim_end_matches(ext);
if !grammars.contains(&grammar_name.to_string()) {
grammars.push(grammar_name.to_string());
}
}
}
}
}
grammars.sort();
grammars
}
}
impl Default for GrammarLoader {
fn default() -> Self {
Self::new()
}
}
/// Get the library file name for a grammar.
fn grammar_lib_name(name: &str) -> String {
let ext = grammar_extension();
format!("{name}{ext}")
}
/// Get the expected symbol name for a grammar.
fn grammar_symbol_name(name: &str) -> String {
// Special cases for arborium grammars with non-standard symbol names
match name {
"rust" => return "tree_sitter_rust_orchard".to_string(),
"vb" => return "tree_sitter_vb_dotnet".to_string(),
_ => {}
}
// Most grammars use tree_sitter_{name} with hyphens replaced by underscores
let normalized = name.replace('-', "_");
format!("tree_sitter_{normalized}")
}
/// Get the shared library extension for the current platform.
fn grammar_extension() -> &'static str {
if cfg!(target_os = "macos") {
".dylib"
} else if cfg!(target_os = "windows") {
".dll"
} else {
".so"
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_grammar_lib_name() {
let name = grammar_lib_name("python");
assert!(name.starts_with("python."));
}
#[test]
fn test_grammar_symbol_name() {
assert_eq!(grammar_symbol_name("python"), "tree_sitter_python");
assert_eq!(grammar_symbol_name("rust"), "tree_sitter_rust_orchard");
assert_eq!(grammar_symbol_name("ssh-config"), "tree_sitter_ssh_config");
assert_eq!(grammar_symbol_name("vb"), "tree_sitter_vb_dotnet");
}
#[test]
fn test_load_from_env() {
// Set up env var pointing to target/grammars
let grammar_path = std::env::current_dir().unwrap().join("target/grammars");
if !grammar_path.exists() {
eprintln!("Skipping: run `cargo xtask build-grammars` first");
return;
}
// SAFETY: This is a test that runs single-threaded
unsafe {
std::env::set_var("MOSS_GRAMMAR_PATH", grammar_path.to_str().unwrap());
}
let loader = GrammarLoader::new();
// Should load python from .so
let ext = grammar_extension();
if grammar_path.join(format!("python{ext}")).exists() {
let lang = loader.get("python");
assert!(lang.is_some(), "Failed to load python grammar");
}
// Clean up
// SAFETY: This is a test that runs single-threaded
unsafe {
std::env::remove_var("MOSS_GRAMMAR_PATH");
}
}
}