Oak Core
The foundational parser framework providing core primitives for building robust, incremental parsers in Rust.
🎯 Overview
oak-core is the heart of the Oak ecosystem, offering a comprehensive set of primitives that form the building blocks for language parsers. It provides a language-agnostic architecture for building high-performance lexers and parsers with built-in support for IDE features.
✨ Features
- Language-Agnostic Design: Define your language's tokens and elements using traits.
- Zero-copy Lexing: Efficiently tokenize source text without unnecessary allocations.
- Incremental Parsing: Built-in support for incremental re-parsing using specialized caching.
- Green/Red Trees: Persistent syntax tree structures inspired by Roslyn, enabling efficient immutability and easy traversal.
- Error Recovery: Graceful handling of malformed input with integrated diagnostics and "panic mode" recovery.
- Pratt Parsing: Built-in support for operator precedence parsing.
- Source Mapping: Accurate mapping between byte offsets and line/column positions.
🚀 Quick Start
To use Oak Core, you first define your language by implementing the Language trait.
use ;
;
📋 Core Components
GreenNode: An immutable, pointer-free, and position-independent representation of the AST. Perfect for caching and sharing.RedNode: A thin wrapper aroundGreenNodethat adds parent pointers and absolute position information for easy traversal.Lexer: A high-performance lexing engine that supports custom scanners for identifiers, numbers, and strings.Parser: A flexible parsing framework that supports both recursive descent and Pratt parsing.Visitor: A trait-based utility for walking the syntax tree and performing analysis.
🔧 Advanced Usage
Incremental Parsing
Oak Core supports incremental parsing out of the box. When the source text changes, you can re-parse only the affected parts by providing an IncrementalCache.
use IncrementalCache;
use GreenBuilder;
let mut pool = new;
let cache = new;
let result = parser.parse_incremental;
Pratt Parsing for Expressions
Handle complex operator precedence with ease using the PrattParser.
use PrattParser;
let mut pratt = new;
pratt.add_postfix;
pratt.add_prefix;
pratt.add_infix;
let expr = pratt.parse?;
📊 Performance
- Optimized Memory Layout: Green nodes use a compact, cache-friendly memory representation.
- Minimal Allocations: Lexers and parsers use internal pooling to minimize heap allocations.
- Fast Traversal: Red trees provide O(1) access to parent nodes and absolute offsets.
🔗 Integration
Oak Core is the foundational dependency for all other Oak projects, including:
- Oak Highlight: Uses core lexers for syntax highlighting.
- Oak LSP: Builds on core trees to provide language server features.
- Oak Visualize: Visualizes the green and red trees.
🤝 Contributing
Contributions are welcome! Please feel free to submit issues or pull requests.
Oak Core - Building blocks for modern language tools 🚀