Parser combinators

Another major application of combinators is parser combinators. A parser combinator makes use of both the monad and combinator patterns. The monadic bind functions are used to bind data from parsers that are later returned as a parse result. The combinators join parsers into a sequence, failover, or other patterns.

The chomp parser combinator library is a good implementation of this concept. Also, the library provides a nice parse! macro that makes the combinator logic much easier to read. Here is an example:

#[macro_use]extern crate chomp;use chomp::prelude::*;#[derive(Debug, Eq, PartialEq)]struct Name<B: Buffer> {   first: B,   last:  B,}fn name<I: U8Input>(i: I) -> SimpleResult<I, Name<I::Buffer>> {   parse!{i; let first = ...

Get Hands-On Functional Programming in Rust now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.