| Crates.io | hypen-parser |
| lib.rs | hypen-parser |
| version | 0.1.2 |
| created_at | 2025-12-10 15:06:50.313232+00 |
| updated_at | 2025-12-19 13:57:53.011635+00 |
| description | A Rust implementation of the Hypen DSL parser using Chumsky |
| homepage | |
| repository | https://github.com/hypen-lang/hypen-engine-rs |
| max_upload_size | |
| id | 1978222 |
| size | 101,255 |
A Rust implementation of the Hypen DSL parser using Chumsky, a parser combinator library.
This parser recreates the functionality of the original Kotlin Multiplatform parser (hypen-parser-core) in Rust, using Chumsky's combinator-based approach for elegant and composable parsing.
.padding(16), .backgroundColor(blue))Add to your Cargo.toml:
[dependencies]
hypen-parser = { path = "path/to/hypen-parser" }
use hypen_parser::parse_component;
let input = r#"
Text("Hello, World!")
.fontSize(18)
.color(blue)
"#;
match parse_component(input) {
Ok(component) => {
println!("Component: {}", component.name);
println!("Applicators: {}", component.applicators.len());
}
Err(errors) => {
for error in errors {
println!("Parse error: {}", error);
}
}
}
use hypen_parser::parse_components;
let input = r#"
Text("First")
Text("Second")
Text("Third")
"#;
let components = parse_components(input).unwrap();
assert_eq!(components.len(), 3);
let input = r#"
Column {
Text("Welcome, @state.username")
.fontSize(18)
.color(blue)
Button("@actions.signIn") {
Text("Sign In")
}
.padding(16)
Row {
Image(src: "logo.png")
.width(50)
.height(50)
Text("Logo")
}
}
.backgroundColor(white)
"#;
let component = parse_component(input).unwrap();
assert_eq!(component.name, "Column");
assert_eq!(component.children.len(), 3);
Hypen supports three types of declarations:
ComponentName(arg1, arg2, key: value)
module ProfilePage(userId: 123) {
Text("User Profile")
}
component Button(text: "Click Me") {
Text(@state.text)
}
Key Differences:
module - Maintains state throughout its lifecycle (persistent state)component - State is wiped on each re-render (ephemeral state)Text("Hello")Text(text: "Hello", color: red)width: 100, height: 50.5enabled: true, disabled: falseitems: [1, 2, 3]config: {width: 100, height: 200}@state.username, @actions.loginColumn {
Text("First")
Text("Second")
}
Text("Styled")
.fontSize(18)
.color(blue)
.padding(16)
ComponentSpecification: Represents a parsed component with name, arguments, applicators, and childrenArgumentList: Collection of named or positional argumentsArgument: Named or positional argument with a valueValue: Enum for different value types (String, Number, Boolean, List, Map, Reference)ApplicatorSpecification: Styling/modifier applied to a componentMetaData: Source location informationThe parser uses recursive descent with combinator composition:
The parser uses Chumsky for parsing and Ariadne for beautiful error reporting.
use hypen_parser::parse_component;
let input = r#"Text("Unclosed string"#;
match parse_component(input) {
Ok(component) => {
// Success - work with component
println!("Parsed: {}", component.name);
}
Err(errors) => {
// Handle errors
for error in errors {
eprintln!("{}", error);
}
}
}
For production use, use the print_parse_errors function for beautiful, colored error messages:
use hypen_parser::{parse_component, print_parse_errors};
let input = r#"
Column {
Text("Hello")
.fontSize(18
Text("Footer")
}
"#;
match parse_component(input) {
Ok(component) => { /* ... */ }
Err(errors) => {
// Prints beautiful error with context
print_parse_errors("myfile.hypen", input, &errors);
}
}
Example error output:
Error: Parse error
╭─[myfile.hypen:4:24]
│
4 │ .fontSize(18
│ ┬
│ ╰── found end of input expected ',', or ')'
───╯
Chumsky provides detailed error information including:
cargo test
This runs 15 comprehensive tests covering:
# Run a specific test
cargo test test_component_with_applicators
# Run tests matching a pattern
cargo test component
# Show test output
cargo test -- --nocapture
# Basic usage example
cargo run
# Error handling examples
cargo run --example errors
# Pretty error display
cargo run --example pretty_errors
# Comprehensive usage examples
cargo run --example usage
Current test coverage includes:
| Feature | Tests | Status |
|---|---|---|
| Simple components | ✓ | Passing |
| Arguments (named/positional) | ✓ | Passing |
| Value types | ✓ | Passing |
| Nested components | ✓ | Passing |
| Applicators | ✓ | Passing |
| Complex hierarchies | ✓ | Passing |
| Error handling | ✓ | Passing |
| Whitespace handling | ✓ | Passing |
The parser is designed for correctness and clarity first. For production use, consider:
ariadne for pretty error reportingSame as the parent Hypen project.
This parser is designed to match the behavior of the Kotlin parser in hypen-parser-core. When adding features, ensure compatibility with the original implementation.