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). - 📝 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.0"
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.
License
This project is licensed under the MIT License. See the LICENSE file for details.