combine
An implementation of parser combinators for Rust, inspired by the Haskell library Parsec. As in Parsec the parsers are LL(1) by default but they can opt-in to arbitrary lookahead using the try combinator.
Example
extern crate combine;
use ;
use ;
// Construct a parser that parses *many* (and at least *1) *letter*s
let word = many1;
// Construct a parser that parses many *word*s where each word is *separated by* a (white)*space*
let mut parser = sep_by
// Combine can collect into any type implementing `Default + Extend` so we need to assist rustc
// by telling it that `sep_by` should collect into a `Vec` and `many1` should collect to a `String`
.map;
let result = parser.parse;
// `parse` returns `Result` where `Ok` contains a tuple of the parsers output and any remaining input.
assert_eq!;
Larger examples can be found in the examples, tests and benches folders.
Links
About
A parser combinator is, broadly speaking, a function which takes several parsers as arguments and returns a new parser, created by combining those parsers. For instance, the many parser takes one parser, p
, as input and returns a new parser which applies p
zero or more times. Thanks to the modularity that parser combinators gives it is possible to define parsers for a wide range of tasks without needing to implement the low level plumbing while still having the full power of Rust when you need it.
The library adheres to semantic versioning.
If you end up trying it I welcome any feedback from your experience with it. I am usually reachable within a day by opening an issue, sending an email or posting a message on gitter.
FAQ
Why does my errors contain inscrutable positions?
Since combine
aims to crate parsers with little to no overhead streams over &str
and &[T]
do not carry any extra position information but instead only rely on comparing the pointer of the buffer to check which Stream
is further ahead than another Stream
. To retrieve a better position, either call translate_position
on the PointerOffset
which represents the position or wrap your stream with State
.
How does it compare to nom?
https://github.com/Marwes/combine/issues/73 contains discussion and links to comparisons to nom.
Parsers written in combine
Formats and protocols
- GraphQL https://github.com/tailhook/graphql-parser (Uses a custom tokenizer as input)
- DiffX https://github.com/brennie/diffx-rs
- Redis https://github.com/mitsuhiko/redis-rs/pull/141 (Uses partial parsing)
- Toml https://github.com/ordian/toml_edit
- Maker Interchange Format https://github.com/aidanhs/frametool (Uses combine as a lexer)
Miscellaneous
- Template language https://github.com/tailhook/trimmer
- Code excercises https://github.com/dgel/adventOfCode2017
- Programming language https://github.com/MaikKlein/spire-lang
- Query parser (+ more) https://github.com/mozilla/mentat
- Query parser https://github.com/tantivy-search/tantivy
Extra
There is an additional crate which has parsers to lex and parse programming languages in combine-language.
You can find older versions of combine (parser-combinators) here.
Contributing
Current master is the 3.0.0 branch. If you want to submit a fix or feature to the 2.x version of combine then do so to the 2.x branch or submit the PR to master and request that it be backported.
The easiest way to contribute is to just open an issue about any problems you encounter using combine but if you are interested in adding something to the library here is a list of some of the easier things to work on to get started.
- Add additional parsers If you have a suggestion for another parser just open an issue or a PR with an implementation.
- Add additional examples More examples for using combine will always be useful!
- Add and improve the docs Not the fanciest of work but one cannot overstate the importance of good documentation.