Crate dlexer

Source
Expand description

§DLexer: A Parser Combinator Library

dlexer is a flexible and composable parser combinator library for Rust, inspired by libraries like Parsec in Haskell. It provides a rich set of tools for building robust parsers for both text-based and binary formats.

§Core Concepts

The library is built around the parsec::Parsec type, which represents a parser. These parsers can be combined and transformed using a variety of functions and operators to build up complex parsing logic from simple, reusable components.

§Key Modules

  • parsec: The core parser combinator library. Contains the Parsec struct and fundamental combinators like map, bind, or, many, etc.
  • lex: Provides tools for lexical analysis, including “skippers” for handling whitespace and comments, and token-level parsers for common data types like integers and symbols.
  • binary: A specialized module for parsing binary data, with parsers for various integer and float types with controlled endianness.
  • errors: Defines the error handling system, including the ParserError trait.

§Getting Started

Here is a simple example of parsing a comma-separated list of numbers:

use dlexer::lex::{integer, WhitespaceSkipper};
use dlexer::parsec::*;

// A parser for a single decimal integer, handling surrounding whitespace.
let number_parser = integer(10);

// A parser for a list of numbers separated by commas.
let list_parser = number_parser.sep(char(','));

// Run the parser on some input.
let result = list_parser.parse("1, 2, 3", WhitespaceSkipper);

assert_eq!(result.unwrap(), vec![1, 2, 3]);

§Macros

The library also provides helpful macros like do_parse! for monadic chaining and map! for mapping multiple parsers to values.

Modules§

binary
Tools for parsing and processing binary data.
errors
Error handling types for the parser combinator library.
lex
Lexer utilities for tokenization and input stream handling.
parsec
Parser combinator library for building composable parsers.

Macros§

do_parse
A macro for writing parsers in a sequential, do-notation style.
map
A convenience macro for mapping multiple parsers to specific values.