Marser is a powerful parser-combinator library designed for creating PEG-style grammars in Rust. It emphasizes efficient zero-copy parsing, flexible input handling, and effective error recovery. With features like packrat caching and a custom TUI for debugging, marser simplifies the parsing process while ensuring high performance and reliability.
marser is an advanced parser-combinator library tailored for crafting PEG-style grammars in Rust. It prioritizes accurate error reporting, effective error recovery, and optimized performance, making it an ideal choice for developers looking to implement robust parsing solutions.
Key Features
- Zero-copy Parsing: Achieve faster parsing speeds without unnecessary allocations.
- Versatile Input Formats: Easily parse a variety of input formats, utilizing
&str,&[u8], or&[T]. Custom input formats can also be defined through theInputtrait. - Packrat-Style Caching: Enhance performance by wrapping parsers with
.memoized()to cache results at specific parse positions. - Intuitive Debugging: Simplify the debugging process with a custom TUI that allows for straightforward parser analysis.
Example Usage
The following example demonstrates how to utilize marser to parse dice notation such as 2d6 into a structured format:
use marser::capture;
use marser::matcher::one_or_more;
use marser::parser::Parser;
// Struct to represent a dice roll
#[derive(Debug, PartialEq)]
struct Roll {
count: u32,
sides: u32,
}
// Parser for a number
fn number<'src>() -> impl Parser<'src, &'src str, Output = u32> + Clone {
capture!(
bind_slice!(
one_or_more('0'..='9'),
number_slice as &'src str
)
=>
number_slice
.parse()
.expect("matched only digits")
)
}
// Parser for a roll like `2d6`
fn roll<'src>() -> impl Parser<'src, &'src str, Output = Roll> + Clone {
capture!(
(
bind!(number(), count),
'd',
bind!(number(), sides)
)
=>
Roll { count, sides }
)
}
fn main() {
let (roll, _errors) = roll().parse_str("2d6").unwrap();
assert_eq!(roll, Roll { count: 2, sides: 6 });
}
Additional runnable examples are available in the examples/ directory of the repository.
Additional Resources
- Guide: Comprehensive guide available on docs.rs
- API Documentation: Access the full API documentation here
- Cargo Features: Optimize your parsing experience with optional features outlined below:
| Feature | Description |
|---|---|
| (default) | Core library only. |
annotate-snippets | Enhance error messages with the annotate-snippets crate. |
parser-trace | Experimental: Record parser traces for playback in the trace viewer TUI. |
Real-World Examples
- JSON Parser: Implements error recovery and custom messages when parsing JSON.
- Mini Language Parser: Handles statements, operator precedence, functions, and more with robust error recovery.
Performance comparisons illustrate marser in practice, showcasing its efficiency alongside other parsing libraries. For in-depth analysis, refer to the speed tests presented in the README.
Overall, marser provides a user-friendly and high-performance solution for developers implementing complex parsers in Rust.
No comments yet.
Sign in to be the first to comment.