Expand description
§Oak Rust - Rust Language Parser
Oak Rust is a high-performance Rust language parser built on the Oak framework, providing full syntax analysis, AST generation, syntax highlighting, and code formatting.
§📋 Overview
Oak Rust parser focuses on the following core features:
- High Performance: Incremental parsing with minimal memory overhead.
- Accuracy: Full support for Rust’s complex syntax rules.
- Extensibility: Modular design, easy to customize.
- Error Recovery: Gracefully handles syntax errors.
- Syntax Highlighting: Built-in highlighter supporting keywords, strings, comments, etc.
- Code Formatting: Follows Rust official code style guidelines.
§🏗️ Architecture
§Core Components
RustParser: Main parser implementation, using Pratt parser for operator precedence.RustLexer: Lexical analysis engine providing precise position tracking.RustLanguage: Language configuration and syntax rules.RustBuilder: AST builder, converting parse trees to strongly-typed ASTs.RustRoot: AST root node, containing all parsed items.
§Optional Components
RustFormatter: Rust code formatter (requiresoak-pretty-printfeature).RustHighlighter: Rust syntax highlighter (requiresoak-highlightfeature).
§AST Structure
The parser generates a strongly-typed AST containing the following main structures:
Item: Top-level items (functions, structs, enums, traits, impls, etc.).Function: Function definitions, containing parameters and body.Statement: Various statement types (let bindings, expression statements, etc.).Expr: Expression types, covering all Rust expressions.Type: Type representations (primitive types, references, generics, etc.).Pattern: Pattern matching (identifiers, structs, tuples, etc.).Identifier: Named identifiers with position information.
§🔧 Usage Examples
§Basic Parsing
ⓘ
use oak_rust::{RustLanguage, RustParser};
use oak_core::language::Language;
let language = RustLanguage::new();
let parser = RustParser::new();
let source = r#"
fn main() {
let x = 42;
println!("Hello, world! x = {}", x);
}
"#;
let result = language.parse(source);
match result {
Ok(ast) => println!("Parsing successful: {:?}", ast),
Err(errors) => println!("Parsing error: {:?}", errors),
}§Processing AST
ⓘ
use oak_rust::{RustLanguage, RustParser, ast::*};
let language = RustLanguage::new();
let parser = RustParser::new();
let source = "fn add(a: i32, b: i32) -> i32 { a + b }";
if let Ok(result) = language.parse(source) {
if let Some(root) = result.root {
println!("Parsed {} items", root.items.len());
// Iterate through all items
for item in &root.items {
match item {
Item::Function(func) => {
println!("Function: {}", func.name.name);
println!("Parameter count: {}", func.params.len());
}
Item::Struct(s) => {
println!("Struct: {}", s.name.name);
}
_ => println!("Other item type"),
}
}
}
}§Syntax Highlighting
ⓘ
use oak_rust::RustHighlighter;
use oak_highlight::highlighter::Highlighter;
let highlighter = RustHighlighter::new();Re-exports§
pub use crate::ast::RustRoot;pub use crate::highlighter::RustHighlighter;pub use crate::lexer::RustLexer;pub use crate::lsp::RustLanguageService;pub use crate::parser::RustParser;pub use crate::mcp::serve_rust_mcp;
Modules§
- ast
- Rust Abstract Syntax Tree (AST) Module
- highlighter
- 高亮模块 Rust 语法高亮器
- lexer
- 词法分析器模块
- lsp
- LSP 模块
- mcp
- MCP integration for Rust.
- parser
- 语法分析器模块
Structs§
- Rust
Builder - Rust 语言的 AST 构建器
- Rust
Formatter - Rust Code Formatter
- Rust
Language - Rust 语言配置和元数据。