Oak CSS Parser
High-performance incremental CSS parser for the oak ecosystem with flexible configuration, optimized for web development and styling analysis.
🎯 Overview
Oak-css is a robust parser for CSS, designed to handle complete CSS syntax including modern features. Built on the solid foundation of oak-core, it provides both high-level convenience and detailed AST generation for web development and styling analysis.
✨ Features
- Complete CSS Syntax: Supports all CSS features including modern specifications
- Full AST Generation: Generates comprehensive Abstract Syntax Trees
- Lexer Support: Built-in tokenization with proper span information
- Error Recovery: Graceful handling of syntax errors with detailed diagnostics
🚀 Quick Start
Basic example:
use CssParser;
📋 Parsing Examples
Rule Parsing
use ;
let parser = new;
let css_content = "h1 { color: red; font-size: 2em; font-weight: bold; }";
let rule = parser.parse_rule?;
println!;
println!;
Selector Parsing
use ;
let parser = new;
let selector = parser.parse_selector?;
println!;
println!;
Declaration Parsing
use ;
let parser = new;
let declaration = parser.parse_declaration?;
println!;
println!;
🔧 Advanced Features
Token-Level Parsing
use ;
let parser = new;
let tokens = parser.tokenize?;
for token in tokens
Error Handling
use CssParser;
let parser = new;
let invalid_css = r#"
body {
color: red
font-size: 16px; // Missing semicolon
margin: 10px
}
"#;
match parser.parse_stylesheet
🏗️ AST Structure
The parser generates a comprehensive AST with the following main structures:
- Stylesheet: Root container for CSS rules
- Rule: CSS rules with selectors and declarations
- Selector: CSS selectors with specificity calculation
- Declaration: Property-value pairs
- Function: CSS functions like
rgb(),calc(), etc. - AtRule: CSS at-rules like
@media,@import, etc.
📊 Performance
- Streaming: Parse large CSS files without loading entirely into memory
- Incremental: Re-parse only changed sections
- Memory Efficient: Smart AST node allocation
- Fast Recovery: Quick error recovery for better IDE integration
🔗 Integration
Oak-css integrates seamlessly with:
- Web Development: Parse CSS for web applications
- Styling Analysis: Analyze CSS for optimization and linting
- Build Tools: Integrate with CSS processing pipelines
- IDE Support: Language server protocol compatibility
- Preprocessors: Handle CSS-like preprocessor syntax
📚 Examples
Check out the examples directory for comprehensive examples:
- Complete CSS stylesheet parsing
- Selector analysis and specificity calculation
- Property validation and transformation
- Integration with development workflows
🤝 Contributing
Contributions are welcome!
Please feel free to submit pull requests at the project repository or open issues.