Expand description
ยง๐ ๏ธ YAML Parser Developer Guide
Yaml support for the Oak language framework.
This guide is designed to help you quickly get started with developing and integrating oak-yaml.
ยง๐ฆ Quick Start
Add the dependency to your Cargo.toml:
[dependencies]
oak-yaml = { path = "..." }ยงBasic Parsing Example
The following is a standard workflow for parsing a YAML document:
use oak_yaml::{YamlParser, SourceText, YamlLanguage};
fn main() {
// 1. Prepare source code
let code = r#"
version: "3.8"
services:
web:
image: "nginx:latest"
ports:
- "80:80"
db:
image: "postgres:13"
environment:
POSTGRES_PASSWORD: example
"#;
let source = SourceText::new(code);
// 2. Initialize parser
let config = YamlLanguage::new();
let parser = YamlParser::new(&config);
// 3. Execute parsing
let result = parser.parse(&source);
// 4. Handle results
if result.is_success() {
println!("Parsing successful! AST node count: {}", result.node_count());
} else {
eprintln!("Errors found during parsing.");
}
}ยง๐ Core API Usage
ยง1. Syntax Tree Traversal
After a successful parse, you can use the built-in visitor pattern or manually traverse the Green/Red Tree to extract YAML mappings, sequences, scalars, and handle anchors/aliases.
ยง2. Incremental Parsing
No need to re-parse massive YAML configuration files when small changes occur:
// Assuming you have an old parse result 'old_result' and new source text 'new_source'
let new_result = parser.reparse(&new_source, &old_result);ยง3. Diagnostics
oak-yaml provides precise error feedback for malformed YAML, such as indentation errors, unmatched brackets, or invalid tag usage:
for diag in result.diagnostics() {
println!("[{}:{}] {}", diag.line, diag.column, diag.message);
}ยง๐๏ธ Architecture Overview
- Lexer: Tokenizes YAML source text into a stream of tokens, handling indentation, block vs. flow styles, and various scalar formats.
- Parser: Syntax analyzer based on the Pratt parsing algorithm to handle YAMLโs hierarchical structure and complex value types.
- AST: A strongly-typed syntax abstraction layer designed for high-performance YAML analysis, formatting, and validation tools.
ยง๐ Advanced Resources
Re-exportsยง
pub use crate::builder::YamlBuilder;pub use crate::language::YamlLanguage;pub use crate::lexer::YamlLexer;pub use crate::parser::YamlParser;pub use crate::lsp::highlighter::YamlHighlighter;pub use crate::language::from_str;pub use crate::language::to_string;pub use crate::lsp::YamlLanguageService;pub use crate::lsp::formatter::YamlFormatter;pub use crate::mcp::serve_yaml_mcp;pub use lexer::token_type::YamlTokenType;pub use parser::element_type::YamlElementType;
Modulesยง
- ast
- AST module.
- builder
- Builder module.
- language
- Kind definition module. Language configuration module.
- lexer
- Lexer module.
- lsp
- LSP module.
- mcp
- MCP module.
- parser
- Parser module.
Functionsยง
- parse
- Parses a YAML string.