Oak Stylus Parser
A high-performance Stylus CSS preprocessor parser for Rust, built with the Oak parser combinator framework. Parse Stylus stylesheets with comprehensive AST generation and error handling.
Overview
Oak Stylus provides robust parsing capabilities for Stylus stylesheets, supporting variables, mixins, functions, conditionals, and all major Stylus constructs. Built on the Oak parser combinator framework, it delivers excellent performance and detailed error messages.
Features
- ✅ Complete Stylus Support: Parse variables, mixins, functions, and conditionals
- ✅ Modern Rust API: Type-safe parsing with comprehensive error handling
- ✅ High Performance: Built on the efficient Oak parser combinator framework
- ✅ Rich AST: Detailed Abstract Syntax Tree with source location tracking
- ✅ Extensible: Easy to extend for custom Stylus dialects
- ✅ Well Tested: Comprehensive test suite with real-world examples
🚀 Quick Start
📋 Parsing Examples
Basic Stylus Stylesheet Parsing
use ;
use StylusLanguage;
Advanced Stylus with Mixins and Functions
use ;
use StylusLanguage;
Advanced Features
Conditional Logic
Oak Stylus supports parsing conditional statements:
let source = r#"
theme = dark
body
if theme == dark
background-color #1a1a1a
color #ffffff
else
background-color #ffffff
color #333333
// Conditional mixins
responsive-padding(size)
padding size
if size > 20px
@media (max-width: 768px)
padding size * 0.5
else
@media (max-width: 768px)
padding size * 0.8
"#;
Iterations and Loops
Parse loops and iterations:
let source = r#"
// Generate utility classes
for size in (xs sm md lg xl)
.margin-{size}
margin lookup('spacing-' + size)
.padding-{size}
padding lookup('spacing-' + size)
// Generate color variants
colors = {
primary: #3498db,
secondary: #2ecc71,
danger: #e74c3c,
warning: #f39c12
}
for name, color in colors
.btn-{name}
background-color color
color white
&:hover
background-color darken(color, 10%)
"#;
Interpolation and String Operations
Handle string interpolation:
let source = r#"
base-font-size = 16px
scale-ratio = 1.25
// Generate heading sizes
for i in (1..6)
h{i}
font-size base-font-size * pow(scale-ratio, 6 - i)
line-height 1.2
margin-bottom 0.5em
// Dynamic class names
component = 'button'
state = 'hover'
.{component}
&.{state}
background-color lighten(primary-color, 20%)
"#;
AST Structure
The parser generates a rich AST with the following main node types:
StylusFile- Root node containing the entire stylesheetRule- CSS rules with selectors and declarationsSelector- CSS selectors (class, id, element, pseudo)Declaration- Property-value pairsVariable- Stylus variablesFunction- Function definitionsMixin- Mixin definitionsConditional- If/else statementsIteration- For loopsComment- Single-line and multi-line comments
Performance
Oak Stylus is designed for high performance:
- Zero-copy parsing where possible
- Streaming support for large stylesheets
- Efficient memory usage with minimal allocations
- Fast error recovery for better developer experience
Integration
Oak Stylus integrates seamlessly with the Oak ecosystem:
use ;
use StylusLanguage;
// Use with other Oak parsers
let mut parser = new;
let result = parser.parse;
Examples
More examples can be found in the examples directory:
🤝 Contributing
Contributions are welcome!
Please feel free to submit pull requests at the project repository or open issues.