ae-tree-sitter-bundle 0.1.0

A bundle of tree-sitter parsers and queries, ready for editor integration for ae editor
Documentation
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
//! Build script for `tree-sitter-bundle`.
//!
//! For every grammar whose Cargo feature is enabled AND whose source has been
//! vendored into `grammars/<name>/`, this script:
//!   1. compiles the grammar's `parser.c` (and `scanner.c` / `scanner.cc` if present),
//!   2. resolves its highlight/injection/locals queries (with local overrides),
//!   3. emits `$OUT_DIR/generated.rs` registering the language for the runtime API.
//!
//! Grammars whose feature is on but whose source is missing are skipped with a
//! warning, so you can vendor lazily. Run `scripts/fetch-grammars.sh` to vendor.

use std::collections::BTreeSet;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use std::{env, fs};

#[derive(Default)]
struct Grammar {
    name: String,
    repo: String,
    rev: String,
    /// Symbol override for the single-language case.
    symbol: Option<String>,
    extensions: Vec<String>,
    /// Explicit sub-languages for multi-grammar repos (typescript, ocaml, ...).
    languages: Vec<Lang>,
}

#[derive(Default)]
struct Lang {
    id: String,
    /// Source dir relative to the grammar root. Defaults to "src".
    src: Option<String>,
    symbol: Option<String>,
    extensions: Vec<String>,
}

/// Minimal parser for our own `grammars.toml`. We control the format (flat
/// `key = "string"` and `key = ["a", "b"]` lines under `[[grammar]]` /
/// `[[grammar.language]]` tables), so a tiny reader avoids pulling a full TOML
/// crate into build-dependencies and keeps the crate buildable on old toolchains.
fn parse_manifest(text: &str) -> Vec<Grammar> {
    fn unquote(s: &str) -> String {
        s.trim().trim_matches('"').to_string()
    }
    fn parse_array(s: &str) -> Vec<String> {
        s.trim()
            .trim_start_matches('[')
            .trim_end_matches(']')
            .split(',')
            .map(|p| p.trim().trim_matches('"').to_string())
            .filter(|p| !p.is_empty())
            .collect()
    }

    let mut grammars: Vec<Grammar> = Vec::new();
    let mut in_language = false;

    for raw in text.lines() {
        let line = raw.split('#').next().unwrap_or("").trim();
        if line.is_empty() {
            continue;
        }
        if line == "[[grammar]]" {
            grammars.push(Grammar::default());
            in_language = false;
            continue;
        }
        if line == "[[grammar.language]]" {
            if let Some(g) = grammars.last_mut() {
                g.languages.push(Lang::default());
            }
            in_language = true;
            continue;
        }
        let Some((key, value)) = line.split_once('=') else {
            continue;
        };
        let key = key.trim();
        let value = value.trim();
        let Some(g) = grammars.last_mut() else { continue };

        if in_language {
            let Some(l) = g.languages.last_mut() else { continue };
            match key {
                "id" => l.id = unquote(value),
                "src" => l.src = Some(unquote(value)),
                "symbol" => l.symbol = Some(unquote(value)),
                "extensions" => l.extensions = parse_array(value),
                _ => {}
            }
        } else {
            match key {
                "name" => g.name = unquote(value),
                "repo" => g.repo = unquote(value),
                "rev" => g.rev = unquote(value),
                "symbol" => g.symbol = Some(unquote(value)),
                "extensions" => g.extensions = parse_array(value),
                _ => {}
            }
        }
    }
    grammars
}

/// One compiled, ready-to-register language.
struct Built {
    id: String,
    symbol: String,
    extensions: Vec<String>,
}

fn default_symbol(id: &str) -> String {
    let mut s = String::from("tree_sitter_");
    for ch in id.chars() {
        s.push(if ch.is_ascii_alphanumeric() { ch } else { '_' });
    }
    s
}

fn feature_enabled(name: &str) -> bool {
    let key: String = name
        .chars()
        .map(|c| if c.is_ascii_alphanumeric() { c.to_ascii_uppercase() } else { '_' })
        .collect();
    env::var(format!("CARGO_FEATURE_{key}")).is_ok()
}

/// Whether build-time fetching is permitted. On by default; turned off for
/// offline/reproducible builds via `TS_BUNDLE_NO_FETCH=1` or when Cargo is in
/// offline mode (`cargo build --offline` / `--frozen`).
fn fetch_allowed() -> bool {
    if env::var_os("TS_BUNDLE_NO_FETCH").is_some() {
        return false;
    }
    if env::var("CARGO_NET_OFFLINE").map(|v| v == "true").unwrap_or(false) {
        return false;
    }
    true
}

/// Resolve a grammar's source directory, fetching it if necessary.
///
/// Order of preference:
///   1. a committed/vendored copy at `grammars/<name>/` (offline, reproducible);
///   2. a previously-fetched copy in the cache, keyed by revision;
///   3. a fresh `git` fetch into the cache (unless fetching is disabled).
///
/// The cache defaults to `$OUT_DIR/grammars` (persists across incremental builds,
/// re-fetched after `cargo clean`); set `TS_BUNDLE_GRAMMAR_CACHE` to a stable
/// directory to keep it across cleans.
fn ensure_grammar_source(
    name: &str,
    repo: &str,
    rev: &str,
    manifest_dir: &Path,
    out_dir: &Path,
) -> Option<PathBuf> {
    // 1. committed/vendored copy wins.
    let local = manifest_dir.join("grammars").join(name);
    if dir_has_content(&local) {
        return Some(local);
    }

    // 2. cached fetch, keyed by revision so re-pinning re-fetches.
    let cache_root = env::var_os("TS_BUNDLE_GRAMMAR_CACHE")
        .map(PathBuf::from)
        .unwrap_or_else(|| out_dir.join("grammars"));
    let short = &rev[..rev.len().min(12)];
    let dest = cache_root.join(format!("{name}-{short}"));
    if dest.join(".fetched").exists() {
        return Some(dest);
    }

    // 3. fetch.
    if !fetch_allowed() {
        return None;
    }
    if repo.is_empty() || rev.is_empty() {
        println!("cargo:warning=grammar '{name}' has no repo/rev to fetch");
        return None;
    }
    println!("cargo:warning=fetching grammar '{name}' @ {short}");
    match git_fetch(repo, rev, &dest) {
        Ok(()) => {
            let _ = fs::write(dest.join(".fetched"), rev);
            Some(dest)
        }
        Err(e) => {
            println!("cargo:warning=fetch failed for '{name}': {e}");
            let _ = fs::remove_dir_all(&dest);
            None
        }
    }
}

fn dir_has_content(p: &Path) -> bool {
    p.is_dir()
        && fs::read_dir(p)
            .map(|mut it| it.next().is_some())
            .unwrap_or(false)
}

/// Shallow-clone `repo` at `rev` into `dest`, then drop the `.git` directory.
fn git_fetch(repo: &str, rev: &str, dest: &Path) -> Result<(), String> {
    let _ = fs::remove_dir_all(dest);
    fs::create_dir_all(dest).map_err(|e| e.to_string())?;
    git(&["init", "-q"], dest)?;
    git(&["remote", "add", "origin", repo], dest)?;
    // Try fetching the exact revision shallowly; fall back to a full fetch for
    // servers that don't allow fetch-by-sha.
    if git(&["fetch", "--depth", "1", "origin", rev], dest).is_err() {
        git(&["fetch", "origin"], dest)?;
    }
    git(&["checkout", "-q", rev], dest)?;
    let _ = fs::remove_dir_all(dest.join(".git"));
    Ok(())
}

fn git(args: &[&str], dir: &Path) -> Result<(), String> {
    let status = Command::new("git")
        .args(args)
        .current_dir(dir)
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .status()
        .map_err(|e| format!("`git` not available: {e}"))?;
    if status.success() {
        Ok(())
    } else {
        Err(format!("git {:?} exited with {status}", args))
    }
}

fn main() {
    let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
    let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
    let queries_override = manifest_dir.join("queries");
    let manifest_path = manifest_dir.join("grammars.toml");

    println!("cargo:rerun-if-changed=grammars.toml");
    println!("cargo:rerun-if-changed=grammars");
    println!("cargo:rerun-if-changed=queries");
    println!("cargo:rerun-if-env-changed=TS_BUNDLE_GRAMMAR_CACHE");
    println!("cargo:rerun-if-env-changed=TS_BUNDLE_NO_FETCH");

    let manifest = parse_manifest(
        &fs::read_to_string(&manifest_path).expect("grammars.toml not found"),
    );

    let mut built: Vec<Built> = Vec::new();
    let mut seen_symbols: BTreeSet<String> = BTreeSet::new();

    for grammar in &manifest {
        if !feature_enabled(&grammar.name) {
            continue;
        }
        let gdir = match ensure_grammar_source(
            &grammar.name,
            &grammar.repo,
            &grammar.rev,
            &manifest_dir,
            &out_dir,
        ) {
            Some(d) => d,
            None => {
                println!(
                    "cargo:warning=grammar '{}' enabled but unavailable \
                     (not vendored and fetch disabled/failed); skipping",
                    grammar.name
                );
                continue;
            }
        };

        // Normalize into a list of language entries.
        let langs: Vec<Lang> = if grammar.languages.is_empty() {
            vec![Lang {
                id: grammar.name.clone(),
                src: Some("src".into()),
                symbol: grammar.symbol.clone(),
                extensions: grammar.extensions.clone(),
            }]
        } else {
            grammar.languages.iter().map(|l| Lang {
                id: l.id.clone(),
                src: Some(l.src.clone().unwrap_or_else(|| "src".into())),
                symbol: l.symbol.clone(),
                extensions: if l.extensions.is_empty() {
                    grammar.extensions.clone()
                } else {
                    l.extensions.clone()
                },
            }).collect()
        };

        for lang in &langs {
            let src_rel = lang.src.clone().unwrap_or_else(|| "src".into());
            let src_dir = gdir.join(&src_rel);
            let parser_c = src_dir.join("parser.c");
            if !parser_c.exists() {
                println!(
                    "cargo:warning=language '{}': no parser.c at {}; skipping",
                    lang.id,
                    parser_c.display()
                );
                continue;
            }
            let symbol = lang.symbol.clone().unwrap_or_else(|| default_symbol(&lang.id));
            if !seen_symbols.insert(symbol.clone()) {
                println!("cargo:warning=duplicate symbol '{symbol}'; skipping '{}'", lang.id);
                continue;
            }

            compile_language(&lang.id, &src_dir);

            built.push(Built {
                id: lang.id.clone(),
                symbol,
                extensions: lang.extensions.clone(),
            });

            resolve_queries(&lang.id, &gdir, &src_dir, &queries_override, &out_dir);
        }
    }

    if built.is_empty() {
        println!(
            "cargo:warning=tree-sitter-bundle: no grammars compiled. Enable language \
             features (e.g. --features rust,python or --features full) and vendor sources."
        );
    }

    write_generated(&out_dir, &built);
}

/// Compile parser.c (+ scanner) for one language into its own static archive.
fn compile_language(id: &str, src_dir: &Path) {
    let safe: String = id.chars().map(|c| if c.is_ascii_alphanumeric() { c } else { '_' }).collect();

    // C sources: parser.c + optional scanner.c
    let mut c = cc::Build::new();
    c.include(src_dir)
        .warnings(false)
        .flag_if_supported("-w")
        .flag_if_supported("-std=c11")
        .file(src_dir.join("parser.c"));
    let scanner_c = src_dir.join("scanner.c");
    if scanner_c.exists() {
        c.file(scanner_c);
    }
    c.compile(&format!("ts_{safe}_c"));

    // C++ scanner, if any, compiled separately and linked against libstdc++.
    let scanner_cc = src_dir.join("scanner.cc");
    let scanner_cpp = src_dir.join("scanner.cpp");
    let cc_file = if scanner_cc.exists() {
        Some(scanner_cc)
    } else if scanner_cpp.exists() {
        Some(scanner_cpp)
    } else {
        None
    };
    if let Some(file) = cc_file {
        let mut cpp = cc::Build::new();
        cpp.cpp(true)
            .include(src_dir)
            .warnings(false)
            .flag_if_supported("-w")
            .flag_if_supported("-std=c++14")
            .file(file)
            .compile(&format!("ts_{safe}_cc"));
        // Ensure the C++ runtime is linked.
        println!("cargo:rustc-link-lib=stdc++");
    }
}

/// Copy the best-matching query files into OUT_DIR (or write empty placeholders).
fn resolve_queries(id: &str, gdir: &Path, src_dir: &Path, overrides: &Path, out_dir: &Path) {
    let dst_dir = out_dir.join("queries").join(id);
    fs::create_dir_all(&dst_dir).unwrap();

    for kind in ["highlights", "injections", "locals"] {
        let file = format!("{kind}.scm");
        let src_parent = src_dir.parent().unwrap_or(src_dir);
        let candidates = [
            overrides.join(id).join(&file),          // local override wins
            src_dir.join("queries").join(&file),     // queries inside src (rare)
            src_parent.join("queries").join(&file),  // queries beside src (typescript/markdown subdirs)
            gdir.join("queries").join(&file),        // repo-root queries
            gdir.join("queries").join(id).join(&file),
        ];
        let dst = dst_dir.join(&file);
        let found = candidates.iter().find(|p| p.exists());
        match found {
            Some(p) => {
                fs::copy(p, &dst).unwrap();
            }
            None => {
                // empty placeholder so include_str! always succeeds
                fs::write(&dst, "").unwrap();
            }
        }
    }
}

fn write_generated(out_dir: &Path, built: &[Built]) {
    let mut s = String::new();
    s.push_str("// @generated by build.rs - do not edit\n");
    s.push_str("use tree_sitter_language::LanguageFn;\n\n");
    s.push_str("pub(crate) struct RawLanguage {\n");
    s.push_str("    pub name: &'static str,\n");
    s.push_str("    pub language: LanguageFn,\n");
    s.push_str("    pub highlights: &'static str,\n");
    s.push_str("    pub injections: &'static str,\n");
    s.push_str("    pub locals: &'static str,\n");
    s.push_str("    pub extensions: &'static [&'static str],\n");
    s.push_str("}\n\n");

    s.push_str("extern \"C\" {\n");
    for b in built {
        s.push_str(&format!("    fn {}() -> *const ();\n", b.symbol));
    }
    s.push_str("}\n\n");

    s.push_str("pub(crate) static RAW_LANGUAGES: &[RawLanguage] = &[\n");
    for b in built {
        let exts = b
            .extensions
            .iter()
            .map(|e| format!("\"{e}\""))
            .collect::<Vec<_>>()
            .join(", ");
        s.push_str("    RawLanguage {\n");
        s.push_str(&format!("        name: \"{}\",\n", b.id));
        s.push_str(&format!(
            "        language: unsafe {{ LanguageFn::from_raw({}) }},\n",
            b.symbol
        ));
        s.push_str(&format!(
            "        highlights: include_str!(concat!(env!(\"OUT_DIR\"), \"/queries/{}/highlights.scm\")),\n",
            b.id
        ));
        s.push_str(&format!(
            "        injections: include_str!(concat!(env!(\"OUT_DIR\"), \"/queries/{}/injections.scm\")),\n",
            b.id
        ));
        s.push_str(&format!(
            "        locals: include_str!(concat!(env!(\"OUT_DIR\"), \"/queries/{}/locals.scm\")),\n",
            b.id
        ));
        s.push_str(&format!("        extensions: &[{exts}],\n"));
        s.push_str("    },\n");
    }
    s.push_str("];\n");

    fs::write(out_dir.join("generated.rs"), s).unwrap();
}