marser
marser is a parser-combinator library for writing PEG-style grammars in Rust with a focus on useful errors, error recovery and good performance.
It supports:
- Zero-copy parsing for faster parsers
- Multiple input formats - use
&strand&[u8]/&[T]or implement theInputtrait yourself. - Packrat-style caching - just wrap your parsers in
.memoized()to cache results at each position. - Simple debugging of your parsers using a custom TUI
Quickstart
To add this library to your Rust project run:
This library has a couple of optional features. You can find them below.
Example
This example parses dice notation like 2d6 into a struct:
use capture;
use one_or_more;
use Parser;
// the struct we want to parse into
// A parser that can parse a number
+ Clone
// A parser that can parse a roll like `2d6`
+ Clone
Runnable examples live under examples/ (see also below).
Learn more
- Guide on docs.rs
- API documentation
- crates.io
Cargo features
| Feature | When you need it |
|---|---|
| (default) | Core library only. |
annotate-snippets |
Enables rendering of error messages using the annotate-snippets crate |
parser-trace |
Experimental: record parser traces to replay them in the trace viewer TUI. See the tracing guide and marser-trace-viewer/. |
Compatibility: Releases follow semver for the documented public API. Everyday composition (capture!, matchers, errors) is intended to stay stable across minors; tracing and trace crates may evolve faster. Macro expansion details are not a stability guarantee — please use macros as APIs, not generated internals.
Requirements
- Rust 1.88 or later
Examples in this repository
Examples need the annotate-snippets feature for rendering of errors
| Example | What it shows |
|---|---|
examples/json/ |
A JSON parser with error recovery and custom error messages. |
examples/mini_language.rs |
Small language: statements, operator precedence, functions etc. with error recovery and custom error messages. |
Run JSON from a git clone:
Error output sample
Input:
Example diagnostic, rendered using annotate-snippets:

This parser can also still produce a recovered output:
Early release
Early release: marser is my first published Rust library. Feedback on the API, error messages, and docs is welcome — open an issue
License
This project is licensed under the MIT License.
AI assistance
Parts of this repository were drafted or expanded with AI tools (guide, library docs, tests, macros, trace crates, examples, and parts of this README). The maintainer reviewed this material. If you spot a mistake, please open an issue.