DLexer: A Functional Parser Combinator Library for Rust
DLexer is a high-performance, functional parser combinator library for Rust, designed for building elegant and efficient parsers. It provides a monadic interface for composing simple parsers into complex ones, supporting both text and binary formats with robust error handling.
Key Features
- ✨ Elegant & Concise: Write powerful parsers with minimal code. A complete JSON parser, for instance, is implemented in about 40 lines.
- 💪 Functional Core: Built for functional programmers. Enjoy a monadic interface with familiar operators (
>>,|,+) and methods (map,bind,sep). For OCaml and Haskell lovers, there are macros with do-notation style for sequential monadic operations. - 📝 Extremely Versatile: A unified API for parsing anything from simple text formats to complex binary data structures.
- ⚡ High-Performance: Operates on input slices (
&str,&[u8]) to minimize allocations and overhead. - ⚙️ Advanced Control Flow: Use the
do_parse!macro for Haskell-style do-notation, simplifying sequential parsing logic. - 🌪️ Whitespace & Comment Handling: Built-in "skippers" automatically handle whitespace and comments (line and block), keeping your parsing logic clean.
- 🚨 Rich Error Reporting: Provides detailed error messages with context, position, and expected inputs.
Installation
Add DLexer to your Cargo.toml:
[]
= "0.1.1"
Quick Start
Here’s a simple example of parsing a parenthesized, comma-separated list of numbers like (1, 2, 3).
Example: Hex Color Parser
DLexer is well-suited for parsing real-world formats. Here is a complete parser for CSS-style hex color codes (e.g., #FF5733 or #80FF5733 with an alpha channel).
// Usage:
let color = hex_color.test.unwrap;
assert_eq!;
let color_with_alpha = hex_color.test.unwrap;
assert_eq!;
More Examples
You can find more complete examples in the src/examples directory:
- JSON Parser: A parser for the JSON data format, including support for comments and flexible whitespace.
- XML Parser: A basic parser for a subset of the XML format.
Macros
do_parse!
A macro for writing parsers in a sequential, do-notation style. This macro provides a more imperative-looking syntax for chaining parsers, which can be more readable than deeply nested calls to bind and then.
Syntax
let% <var> = <parser>;: Binds the result of<parser>to<var>. This is equivalent tobind.<parser>;: Runs<parser>and discards its result. This is equivalent tothen.let <var> = <expr>;: Binds the result of a standard Rust expression to<var>.- The last expression in the block is the final parser, which determines the return value.
Example
let p: = do_parse!;
let input = ; // Represents "abcd" with length 4
match p.parse
map!
A macro to map multiple parsers to a single value. This is useful when you want to create a parser that recognizes a set of keywords and maps them to an enum or other value.
Example
let keyword_parser: = map!;
assert_eq!;
assert_eq!;
License
This project is licensed under the MIT License. See the LICENSE file for details.