typeward
A parser combinator library that builds parsers by composing smaller parsers within the type system.
typeward is designed as a lightweight alternative to syn for general-purpose parsing. Define tokens as types, implement Parse for your structs and enums, and combine them using generic combinators — all with minimal boilerplate.
Features
- Type-level parser composition — parsers are types, composed via generics rather than runtime closures
- Derive macro —
#[derive(Parse)]generates parser implementations for structs, enums, and unions - Multiple input types — works with
&str,&[u8], read-backedReadInput, andTokenStreamthrough a unifiedInputtrait - Span support — track source locations with
SourceSpanfor error reporting - Zero global state — thread-safe parse context passed explicitly through parse calls
Installation
[]
= "0.2"
The macros feature (enabled by default) provides the #[derive(Parse)] macro. Disable it with default-features = false if you only need the combinator library.
Quick Start
use *;
// Parse a complete input string
let = ?;
Import the prelude to get access to core traits, combinators, primitives, and the derive macro:
use *;
Building Parsers with Types
Parsers are expressed as types and composed together:
// A quoted string: "..."
type QuotedString = ;
// A comma-separated list inside brackets: [a, b, c]
type List = ;
// Try multiple alternatives
type Value = or!;
// Also implemented for `Option` and `Result`:
type OptionalValue = ;
type FallibleValue = ;
Implementing Parse for Custom Types
Using the Derive Macro
Enable the macros feature and derive Parse automatically:
;
Field-level attributes like #[parse(ws)] and #[parse(from(ParserType, mapper))] customize how each field is parsed.
Core Modules
| Module | Description |
|---|---|
[primitives] |
Parse implementations for built-in types (bool, f64, i64, String, etc.) |
[combinators] |
Parser combinators — And, Or, Delimited, Separated, Ws, Span, Peek, Not |
[collections] |
Repetition combinators — Many0, Many1, Repeat |
[input] |
Input abstraction — &str, &[u8], ReadInput, TokenStream via the Input trait |
[error] |
Error types — ParseError, ParseResult, SourceSpan |
[parse] |
Core Parse trait and entry points like parse_complete |
Macros
The crate provides helper macros for composing parsers ergonomically:
and!(A, B, C)— compose a tuple parser from multiple typesor!(A, B, C)— try parsers in sequence, returning the first matchor_match!(val, arm => expr, ...)— destructureOrresults into valuesunpack_and!(val, (A, B, C))— destructureAndtuple results
Example
See the examples/json.rs file for a complete JSON parser built with typeward.
License
MIT — see LICENSE.md