yolowsyntax 0.2.0

Pure-Rust, hardware-accelerated tree-sitter grammar runtime and code outline extractor for 41 languages.
Documentation
yolowsyntax-0.2.0 has been yanked.

yolowsyntax

Pure-Rust, Hardware-Accelerated Tree-Sitter Runtime & 41-Language Code Outline Extractor

English · 简体中文 · Benchmarks · Changelog

Crates.io Docs.rs License: MIT Rust: 1.89+

A pure-Rust, hardware-adaptive parser runtime compatible with converted tree-sitter parse tables and ported external scanners, extracted from PageSift and released under MIT (see THIRD-PARTY.md for MIT / Apache-2.0 upstream grammar and scanner notices). Zero C compiler required at build time, #![deny(unsafe_op_in_unsafe_fn)] with audited SIMD intrinsics and portable scalar fallbacks, a single pure-Rust runtime dependency (yolowzstd 0.3.1), embedded Zstd-compressed parse tables for 41 languages with lazy per-language decompression, and direct-arena code outline extraction.


At a Glance

  • Zero C Toolchain, Hardware-Adaptive Pure Rust: No cc, no bindgen, no C FFI. Uses #![deny(unsafe_op_in_unsafe_fn)] with runtime AVX2/SSSE3/SSE2 (x86-64) and NEON (AArch64) dispatch, bit-identical scalar fallbacks, and Linux PROT_NONE MMU guard-page zero-overread tests (MSRV 1.89+, verified on Linux x86-64, Windows x86-64, and macOS AArch64).
  • 41 Embedded Languages, Lazy Zstd Decompression: Ships yolowzstd 0.3.1 level-22 .pslg.z parse tables (4.30 MiB across 41 grammars; 3.95 MiB .crate package) and pure-Rust ports of upstream external scanners for one-shot (&[u8]) parsing. Only the languages you actually call are decompressed (0.074–12.193 ms cold load) and cached via OnceLock using zero-copy streaming table loading.
  • Scope & Compatibility: Embeds 41 compiled grammars and ported scanners, rather than aiming for drop-in C tree-sitter API compatibility—there is no dynamic C grammar loading, no Tree-sitter Query DSL, and no incremental re-parsing (tree.edit).
  • 5,875 / 5,875 (100.00%) Active Official Corpus Cases: Passes all 5,875 active test cases across 41 pinned upstream test/corpus suites (0 failed; 5,844 S-exp matches + 31 expected :error cases; 22 skipped due to :language(...) dialect mismatch across shared suites; :skip/:cst/excluded :platform directives are also skipped by the runner, with 0 present here).
  • Direct-Arena Code Outline Extraction (outline): For 32 symbol-bearing languages, walks the parse arena directly to extract symbols, imports, and contiguous sections (losslessly concatenating back to the original source). The remaining 9 markup/config grammars still parse into syntax trees via api::parse(), while outline() short-circuits in O(1) to None (also returning None when a file yields no definitions or exceeds 2 MiB / 20% error bytes).
  • Cooperative Resource Budgets (Limits): Bounds GLR stack depth, version count (max_versions = 6), AST node count (max_nodes = 4,000,000), parser step budget, and an optional cooperative wall-clock deadline (max_millis = 0 by default = disabled), returning a tiny truncated degraded tree on overflow. Note: this is in-process cooperative bounding, not OS/cgroup memory isolation, and does not guarantee protection against OOM for arbitrary host allocations outside parser step/budget checks.

Story

When PageSift added source-code extraction for LLM and RAG pipelines, we needed to split source files along real structural boundaries—top-level functions, classes, structs, traits, and imports—rather than chopping code at arbitrary line counts.

Using the standard C tree-sitter ecosystem meant pulling in a C compiler (cc), linking dozens of native src/parser.c and src/scanner.c compilation units across 41 languages, and passing untrusted files from the open web through C FFI. At the same time, we did not need incremental editing for a live IDE buffer—we needed a one-shot, pure-Rust parser with explicit runtime budgets.

So we built yolowsyntax:

  1. A compact .pslg.z binary table converter (ts-convert) that reads upstream parser.c files offline and packs their parse tables into Zstd level-22 blobs (yolowzstd decompresses a language lazily on first use and streams entries straight into dense/sparse tables).
  2. A hardware-coupled Elkhound hybrid LR(1)/GLR runtime (#![deny(unsafe_op_in_unsafe_fn)]) combining Moore-factored + Mealy sink-fused lexing, 2-nibble vpshufb/vqtbl1q_u8 SIMD self-loops, DFS-compiled O(1) keyword hash tables, half-cache-line 32 B Node hot/cold splitting, 8 B stack Links, whole-loop AVX2 specialization, pure-Rust external scanners, and cooperative Limits.
  3. A direct-arena lossless code outline layer (outline) that extracts top-level/container symbols, imports, and contiguous byte-range sections (sections joined == original file when Some) without building intermediate trees or descending into function bodies.

Install

[dependencies]
yolowsyntax = "0.2"

To enable the offline grammar converter and corpus/parse CLI tools (ts-convert, ts-corpus, ts-parse):

[dependencies]
yolowsyntax = { version = "0.2", features = ["tools"] }

Usage

1. Extract Code Outline (outline)

use yolowsyntax::outline;

let src = r#"
use std::collections::HashMap;

pub struct Config {
    pub limit: usize,
}

pub fn build_index(items: &[&str]) -> HashMap<String, usize> {
    items.iter().enumerate().map(|(i, s)| ((*s).to_string(), i)).collect()
}
"#;

let out = outline("rust", src).expect("supported language with symbols");
assert_eq!(out.imports, ["use std::collections::HashMap;"]);
assert_eq!(out.symbols.len(), 2);

// Every section maps cleanly back to the original source bytes
let reconstructed: String = out.sections.iter().map(|s| &src[s.start..s.end]).collect();
assert_eq!(reconstructed, src);

2. Direct Syntax Tree Parsing with Cooperative Budgets (api::parse)

use yolowsyntax::{Limits, api::parse, langs::Lang};

let lang = Lang::from_tag("python").unwrap().table().unwrap();
let limits = Limits {
    max_stack_depth: 2048,
    max_millis: 500, // default is 0 (disabled)
    ..Limits::default()
};

let tree = parse(lang, b"def greet(name):\n    return f'hi {name}'\n", limits);
assert!(!tree.has_error());
assert!(!tree.truncated);
println!("{}", tree.to_sexp(lang));

outline() Semantics & When It Returns None

  • symbols: Top-level declarations plus methods/constructors/fields nested directly inside types (struct, class, trait, impl, interface, enum) or single-file module containers; capped at 2,000 symbols.
  • imports: Extracted import/use/include statements; capped at 500 entries.
  • sections: Contiguous non-overlapping byte ranges covering [0..source.len()) anchored on top-level symbol boundaries (leading preamble has title: None).
  • Returns None when: the tag is unknown, input is empty or > 2 MiB, parsing hits Limits (including the internal 3,000 ms cooperative budget), syntax error spans exceed 20% of input bytes, the language has no outline rules (html, css, vue, svelte, nix, dockerfile, make, scheme, hcl), or symbols is empty (e.g., expression-only files or code wrapped inside a single IIFE/block).

Supported Languages (41)

  • Systems & Compiled: c, cpp, rust, go, objc, d, pascal, verilog
  • JVM & Managed: java, csharp, kotlin, groovy, dart
  • Web & Scripting: javascript (jsx), typescript, tsx, python, php, ruby, lua, bash (sh, shell, zsh), powershell
  • Functional & ML: ocaml, elixir, erlang, elm, gleam, scheme, commonlisp, julia, r
  • Web Templates, Config & Build: html, css, vue, svelte, nix, dockerfile, cmake, make, hcl (terraform), solidity

Benchmarks

Measured on Intel Core i7-10700 (taskset -c 4, AVX2), Linux 7.0.0-43-generic x86-64, rustc 1.99.0 (--release, lto = "fat", codegen-units = 1) against tree-sitter 0.26.13 + upstream C grammar crates (median of 25 alternating rounds). Both corpus parity and error parity are verified by the harness (y_err == ts_err); full scripts, RSS (4,104–21,624 KiB), real OSS file runs, and 41-language results are in benchmarks/README.md.

~64 KiB Corpus-Concatenated Suite (Historical Baseline, Hardware/Dataset-Specific)

These 2.80×–5.46× speedups apply specifically to ~64 KiB concatenated upstream test/corpus snippets under this harness configuration, not as a universal multiplier for all inputs:

Language Active Corpus (yolowsyntax) yolowsyntax parse (ms ↓) C tree-sitter parse (ms ↓) yolowsyntax outline (ms ↓) Speed (ts / yolow ↑) Parity / outline
C 85 / 85 (100%) 3.856 21.048 4.293 5.46× exact / some(361)
Go 67 / 67 (100%) 3.808 11.136 4.343 2.92× exact(both_err) / some(724)
Java 108 / 108 (100%) 2.765 9.141 3.191 3.31× exact / some(615)
Python 117 / 117 (100%) 4.881 15.624 5.271 3.20× both_err_diff / some(255)
JavaScript 116 / 116 (100%) 4.626 13.738 5.062 2.97× exact(both_err) / some(253)
TypeScript 111 / 111 (100%) 4.252 11.893 4.973 2.80× exact(both_err) / some(902)
Rust 151 / 151 (100%) 4.334 13.891 4.854 3.21× exact(both_err) / some(722)
Bash 100 / 100 (100%) 8.699 26.799 8.783 3.08× exact(both_err) / some(2)

Standalone Real-World OSS Files (31 KiB – 176 KiB, real-bench)

Language (File) Bytes yolowsyntax parse (ms ↓) C tree-sitter parse (ms ↓) yolowsyntax outline (ms ↓) Speed (ts / yolow ↑) Parity / outline
C (parser.c) 78,797 1.865 14.066 1.971 7.54× exact(both_err) / some(59)
Go (decode.go) 36,112 1.700 4.609 1.752 2.71× exact / some(38)
Java (ImmutableList.java) 31,280 0.609 2.107 0.674 3.46× exact / some(85)
Python (argparse.py) 102,236 2.590 9.678 2.675 3.74× clean(py_expr_stmt) / some(161)
JavaScript (moment.js) 176,435 5.016 19.178 5.016 3.82× exact / none
TypeScript (types.ts) 153,859 7.077 20.494 7.440 2.90× exact / some(372)
Rust (searcher/mod.rs) 39,771 0.710 2.600 0.795 3.66× exact / some(72)
Bash (nvm.sh) 148,844 4.968 35.476 5.066 7.14× exact / none

License

MIT License — see LICENSE, THIRD-PARTY.md (for MIT and Apache-2.0 upstream grammar/scanner attributions), and UPSTREAM.md.