typeward
A parser combinator library that builds parsers by composing smaller parsers within the type system.
This crate is an alternative to nom for creating structs that can be parsed by composing type-level parsers. Define tokens as types, implement Parse for your structs, and combine them using generic combinators like combinators::Delimited.
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 - Borrowed + streaming-friendly input model: parse borrowed values from
BorrowInputtypes (&str,&[u8],ReadInput) or parse owned values from anyInputimplementation, including streaming inputs - Span support: track source locations with
SourceSpanfor error reporting - Zero global state: thread-safe parse context passed explicitly through parse calls
Installation
[]
= "0.4"
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 value = ?;
Import the prelude to get access to core traits, combinators, primitives, and the derive macro:
use *;
Borrowed vs Owned Parse Outputs
BorrowInputenables borrowed parse outputs like&str.- Plain
Inputsupports owned parse outputs likeString. - Built-in buffered inputs (
&str,&[u8],ReadInput) implement both traits. - Streaming inputs can implement
Inputonly, which naturally restricts parsing to owned output types.
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. Attributes that accept a value support both name(value) and name = value forms, such as #[parse(map = |value| value + 1)] or #[parse(from = Ws::<String>)]. Optional fields can use #[parse(default)] to fall back to Default::default(), or #[parse(default = expr)] to supply a custom fallback.
Recursive parser shapes are rejected by default; add #[parse(recursive)] on the container to opt in when your grammar is structured to consume input at each recursive step.
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 via Input + BorrowInput for owned vs borrowed parse outputs |
[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 types one after anotherand_is!(A, B, C): compose a parser that checks all parsers on the same inputor!(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