Oaks is a modular parser framework for Rust that provides a unified approach to building language parsers. Built on the oak-core foundation, Oaks offers a comprehensive set of tools for lexical analysis, parsing, and syntax tree manipulation.
Oaks is designed for developers who need more than what parser generators can offer. Our philosophy focuses on three core pillars:
- Hand-written Optimization: Unlike generic parser generators, Oaks encourages hand-written parsing logic. This gives you absolute control over the parsing process, allowing for micro-optimizations that generators often miss.
- Language-Specific Acceleration: Every language has its own quirks. Oaks provides the infrastructure to leverage language-specific features for maximum performance, ensuring that your parser is as fast as possible for its specific domain.
- Human-Centric Design: We believe parsers should be easy to debug and use. Oaks produces highly readable syntax trees and provides clear, actionable error messages that help developers identify and fix issues instantly.
- Modular Architecture: Decouples the core parsing engine from language-specific logic. Implement new languages by defining
TokenTypeandElementTypewithout touching the core infrastructure. - Lossless Green/Red Tree: Implements a Rowan-style architecture. Green Trees are immutable and interned for memory efficiency, while Red Trees provide a parent-aware, position-aware view for effortless traversal.
- Structural Sharing: Modifications to the tree use
Arc-based sharing. Only the modified nodes and their direct ancestors are recreated, making transformations and refactorings extremely memory-efficient. - Error Recovery: The parser can recover from syntax errors to produce a partial but valid tree, ensuring that features like highlighting and autocompletion remain functional during active editing.
- Incremental Parsing: By utilizing an
IncrementalCache, the framework only re-parses the changed portions of the source code, enabling lightning-fast updates for large files in IDE environments. - Source Mapping: Absolute position information is dynamically computed from Red Nodes, providing a stable and accurate foundation for Source Maps and diagnostic reporting.
Implemented by manipulating Trivia (whitespace, line breaks, comments) within the GreenNode structure. It ensures 100% lossless code formatting by reconstructing the source text while preserving or adjusting non-functional tokens.
Utilizes the Visitor pattern to traverse the RedTree. By leveraging absolute span information computed from red nodes, it performs efficient static analysis and provides precise diagnostic locations for coding standard violations.
Supports dual-mode highlighting: fast Lexer-based highlighting using the raw token stream, and precise Parser-based highlighting that utilizes the full syntax tree to distinguish between semantic categories like function calls, types, and variables.
Powered by the Transformer trait, it enables high-performance code refactoring. It leverages the Structural Sharing property of the Red-Green tree architecture; only modified nodes and their parent paths are recreated as new GreenNodes, while unchanged subtrees are efficiently reused via Arc.
| Component | Description | Status |
|---|---|---|
oak-core |
Core parsing infrastructure and traits | β Active |
oak-lsp |
Language Server Protocol support | β Active |
oak-vfs |
Virtual File System for analysis | β Active |
oak-highlight |
Multi-language syntax highlighter | β Active |
oak-pretty-print |
Code formatting and pretty printing | β Active |
oak-mcp |
Model Context Protocol integration | β Active |
oak-hover |
Semantic hover information provider | β Active |
oak-repl |
Interactive parser testing tool | β Active |
oak-visualize |
AST visualization and graph rendering | π Development |
oaks |
Main unified library | β Active |
Oaks supports a wide range of languages through its modular architecture. Below are some of the supported parsers:
oak-c,oak-cpp,oak-rust,oak-go,oak-zig,oak-swift,oak-ada,oak-d,oak-nim,oak-vlang
oak-javascript,oak-html,oak-css,oak-sass,oak-scss,oak-stylus,oak-vue,oak-php,oak-python,oak-ruby,oak-perl,oak-lua,oak-bash,oak-powershell,oak-bat,oak-cmd,oak-tcl
oak-json(with JSON5),oak-yaml,oak-toml,oak-ini,oak-csv,oak-xml,oak-nix,oak-dhall,oak-dot
oak-java,oak-kotlin,oak-scala,oak-clojure,oak-elixir,oak-erlang,oak-fsharp,oak-ocaml,oak-julia,oak-matlab,oak-roak-markdown,oak-ascii-doc,oak-tex,oak-typstoak-sql,oak-regex,oak-wgsl,oak-hlsl,oak-watoak-coq,oak-lean,oak-prolog
oak-cobol,oak-pascal,oak-delphi,oak-vhdl
oak-voc,oak-voml,oak-von,oak-gsgl,oak-jasm,oak-msil,oak-mojo
...and many more being added regularly.
Basic usage example with oak-c:
use oak_core::{SourceText, Parser};
use oak_c::{CLanguage, CParser};
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create source text from input
let source = SourceText::new("int main() { return 0; }");
// Parse the source code
let parser = CParser::new(CLanguage::default());
let result = parser.parse(&source);
// Handle the result
match result {
Ok(tree) => println!("Parsed successfully: {:?}", tree),
Err(errors) => println!("Parse errors: {:?}", errors),
}
Ok(())
}Oaks is designed from the ground up to be a High-Performance LSP Foundation. Its architecture solves the most challenging problems in building modern IDE support:
- Native LSP & MCP Support: Built-in support for Language Server Protocol and Model Context Protocol, enabling seamless integration with both modern IDEs and AI agents.
- Virtual File System (VFS): Integrated
oak-vfsprovides a high-performance, incremental file system abstraction, perfect for workspace-wide analysis and cross-file refactorings. - Resilient Analysis: The framework's Error Recovery ensures that your analysis tools remain responsive even when the code is in an invalid state.
- HMR-Ready: Sub-millisecond Incremental Parsing means your tools can provide instant feedback on every keystroke, even in multi-megabyte files.
- Refactoring Engine: The
Transformertrait combined with Structural Sharing allows for complex code actions to be implemented with high performance and 100% comment preservation.
While Oaks focuses on high-performance syntax analysis, it is designed to be the perfect foundation for semantic analysis:
- Semantic Hints: The
TokenTypeandElementTypetraits provide built-in hooks likeis_definition,is_reference, andis_scope_boundary, allowing external semantic engines to instantly understand the tree's logical structure. - Stable Identifiers: Red nodes provide stable pointers that semantic analyzers can use for symbol indexing and cross-referencing.
- Parent-Aware Navigation: The
RedTreeallows semantic checkers to easily bubble up from a usage to its scope or declaration. - Typed IR: The high-level Typed AST layer serves as a clean, serializable Intermediate Representation (IR) that external type-checkers and symbol solvers can consume without knowing the details of the red-green tree.
- Framework Agnostic: Oaks is unopinionated about how you handle semantics, making it trivial to integrate with databases (like Salsa) or graph-based analysis engines.
- Language Trait: A unified interface to plug in any grammar.
- Green Tree: The "What" β immutable, shared, and extremely compact.
- Red Tree: The "Where" β a lightweight, parent-aware view for easy tree walking.
- Visitor & Transformer: Standardized patterns for both read-only analysis (Linter) and read-write mutations (Refactoring).
Each language parser (e.g., oak-c, oak-json) follows a consistent pattern:
- SyntaxKind Enum: Defines all possible syntax elements
- Language Struct: Implements the Language trait
- Lexer Struct: Implements tokenization for the language
- AST Definitions: Optional typed AST structures
oaks/
βββ projects/ # Core libraries
β βββ oak-core/ # Core parsing infrastructure
β βββ oak-lsp/ # LSP integration
β βββ oak-mcp/ # MCP integration
β βββ oak-vfs/ # Virtual File System
β βββ oak-highlight/ # Syntax highlighting
β βββ oak-pretty-print/ # Code formatting
β βββ oak-hover/ # Hover support
β βββ oak-repl/ # Interactive testing
β βββ oak-visualize/ # AST visualization
β βββ oaks/ # Main unified library
βββ examples/ # Language parsers
β βββ oak-c/ # C parser
β βββ oak-rust/ # Rust parser
β βββ oak-json/ # JSON parser
β βββ ... # 50+ other languages
βββ Cargo.toml # Workspace configuration
- Rust nightly toolchain (required for
new_range_apifeature) - Cargo workspace support
Oaks is actively developed and maintained. Current status:
- Core Framework (
oak-core): Language trait, Lexer/Parser infrastructure, Green/Red tree system, SourceText with line/column tracking, and robust error recovery. - IDE & AI Infrastructure:
oak-lsp: Full Language Server Protocol support.oak-mcp: Model Context Protocol for AI agent integration.oak-vfs: High-performance Virtual File System.oak-highlight: Multi-language syntax highlighting.oak-pretty-print: Advanced code formatting.oak-repl: Interactive testing environment.
- Language Support:
- 50+ language parsers in various stages of completion.
- Mature implementations for C, JSON, Rust, Go, and more.
- Performance:
- Incremental parsing support across all core components.
- Zero-copy lexing and efficient structural sharing.
- Advanced Semantic Analysis: Symbol resolution, type checking, and cross-reference indexing.
- Extended Tooling: Enhanced
oak-visualizeand automated benchmarking suites. - Language Coverage: Finalizing AST implementations for legacy and specialized languages.
- Documentation: Comprehensive guides and API references.
- Cloud-Native Deployment: Optimized for WASM and remote development environments.
- Universal Refactoring Engine: Cross-language refactoring tools powered by
Transformer. - Advanced AI Integration: Deep learning-assisted error recovery and code completion.
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
To add a new language parser to Oaks:
- Create a new directory in
examples/following the patternoak-{language} - Implement the required components:
SyntaxKindenum insrc/kind/Languageimplementation insrc/language/Lexerimplementation insrc/lexer/- Optional: AST definitions in
src/ast/
- Add your parser to the workspace in the root
Cargo.toml - Add documentation and examples
For reference implementations, see oak-c and oak-json.
- Repository
- Issue Tracker
- Development documentation in each crate's
readme.md - Examples in the
examples/directory
Happy Parsing! π