Expand description
ยง๐ ๏ธ Vue Parser Developer Guide
Vue support for the Oak language framework.
This guide is designed to help you quickly get started with developing and integrating oak-vue.
ยง๐ฆ Quick Start
Add the dependency to your Cargo.toml:
[dependencies]
oak-vue = { path = "..." }ยงBasic Parsing Example
The following is a standard workflow for parsing Vue Single File Components (SFCs), supporting template, script, and style blocks:
use oak_vue::{VueParser, SourceText, VueLanguage};
fn main() {
// 1. Prepare source code
let code = r#"
<template>
<div>{{ greeting }}</div>
</template>
<script setup>
const greeting = 'Hello from Oak!'
</script>
"#;
let source = SourceText::new(code);
// 2. Initialize parser
let config = VueLanguage::new();
let parser = VueParser::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.");
for diag in result.diagnostics() {
println!("[{}:{}] {}", diag.line, diag.column, diag.message);
}
}
}ยง๐ 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 Vue specific constructs like top-level blocks, template directives, script setup variables, and scoped styles.
ยง2. Incremental Parsing
Vue SFCs are often edited in IDEs. oak-vue supports sub-millisecond incremental updates for a fluid editing experience:
// Re-parse only the modified section
let new_result = parser.reparse(&new_source, &old_result);ยง3. Error Recovery
The parser is designed for industrial-grade fault tolerance, recovering gracefully from unclosed tags or malformed script blocks to provide continuous feedback in IDEs.
ยง๐๏ธ Architecture Overview
- Lexer: Tokenizes Vue SFC source text, handling the transitions between HTML-like template syntax, JavaScript/TypeScript in script blocks, and CSS/SCSS in style blocks.
- Parser: A high-performance parser that manages the coordination between different languages within a single SFC.
- AST: A strongly-typed, lossless syntax tree that preserves all trivia (comments/whitespace) for refactoring and formatting tools.
ยง๐ Advanced Resources
- Full Examples: Check the examples/ folder in the project root.
- API Documentation: Run
cargo doc --openfor detailed type definitions. - Test Cases: See tests/readme.md for details on our snapshot-based testing.
Re-exportsยง
pub use builder::VueBuilder;pub use language::VueLanguage;pub use lexer::VueLexer;pub use lexer::token_type::VueTokenType;pub use parser::VueParser;pub use parser::element_type::VueElementType;pub use ast::*;