Crate cypress

Source
Expand description

§Cypress

crates.io docs.rs

Cypress is a parser combinator library for Rust, inspired by FParsec and chumsky. It aims to provide a simple yet expressive framework for constructing parsers, especially for building small domain-specific languages or interpreters.

This library is still a work in progress, but it already supports recursive grammars, expressive combinators, and error handling. Cypress emphasizes readability and composability, with an ergonomic macro-based DSL.

§Highlights

  • Recursive parsers via recursive
  • Combinators for sequencing, mapping, branching, etc.
  • Simple token-based parsing with custom input types

§Example

The following is a parser for Brainfuck, demonstrating recursive parsing of a toy language:

use cypress::prelude::*;

#[derive(Debug, Clone, PartialEq)]
enum Instruction {
    Left,
    Right,
    Increment,
    Decrement,
    Read,
    Write,
    Loop(Vec<Self>),
}

fn bf_parser<'a>() -> impl Parser<'a, u8, Vec<Instruction>> {
    recursive(|expr| {
        Box::new(
            choice!(
                select! {
                    '<' => Instruction::Left,
                    '>' => Instruction::Right,
                    '+' => Instruction::Increment,
                    '-' => Instruction::Decrement,
                    ',' => Instruction::Read,
                    '.' => Instruction::Write,
                },
                expr.between(just('['), just(']'))
                    .map(|expr| Instruction::Loop(expr))
            )
            .many(),
        )
    })
    .until_end()
}

let input = b"+++++[>>+<<-]".into_input();
let result = bf_parser().parse(input);

More examples, including interpreters and simple language grammars, can be found in the /examples folder.

§Getting Started

Add this to your Cargo.toml:

cypress = "0.1.0"

Then import the prelude:

use cypress::prelude::*;

§Feedback

This is my first published project — ideas, feedback, and PRs are very welcome!

§License

Cypress is licensed under the BSD 3-Clause License. See LICENSE for details.

Modules§

error
Modules for displaying and organizing errors
parser
The parser module contains the full implementation of the parser combinator library.
prelude
The prelude module re-exports commonly used items from the parser library.
text
Common trait and implementations for parsing textlike values

Macros§

choice
A convenient macro to create a choice parser from two or more parsers.
precedence
For ergonomic use of foldl for parsing with precedence.
select
Macro for conveniently creating a match and map like statement.
sequence
Macro for ergonomic parsing in sequence.
wrap
Macro for wraping expressions or literals, typically for inside other macros.