A Grammar-First Approach to Parser Combinators in Rust

rust dev.to

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 &str and &[u8] / &[T] or implement the Input trait 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:

cargo add marser
Enter fullscreen mode Exit fullscreen mode

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 marser::capture;
use marser::matcher::one_or_more;
use marser::parser::Parser;
// the struct we want to parse into
struct Roll {
    count: u32,
    sides: u32,
}

// A parser
Enter fullscreen mode Exit fullscreen mode

Source: dev.to

arrow_back Back to Tutorials