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.
&str, &[u8], or &[T]. Custom input formats can also be defined through the Input trait..memoized() to cache results at specific parse positions.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.
| 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. |
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.