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.
```rust extern crate combine; use combine::{many, Parser}; use combine::char::letter;
let result = many(letter()).parse("hello world"); asserteq!(result, Ok(("hello".tostring(), " world"))); ```
Larger examples can be found in the tests and benches folders.
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.
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 ParseError
or wrap your stream with State
.
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.
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.
Here is a list containing most of the breaking changes in older versions of combine (parser-combinators).
parse_state
renamed to parse_stream
.parse_lazy
changed to return a ConsumedResult
. To make calls to parse_lazy
return a Result
you can call parser.parse_lazy(input).into()
.char::String
renamed to char::Str
to avoid name collisions with std::string::String
.ParserExt
removed, all methods now exist directly on Parser
.Stream
split into Stream
and StreamOnce
.StreamOnce::uncons
now takes &mut self
instead of self
.Position
added as an associated type on StreamOnce
.&[T]
streams has had the Item
type changed from &T
to T
and requires a T: Copy
bound. If you need the old behavior you can wrap the &[T]
in the SliceStream
newtype i.e parser.parse(SliceStream(slice))
.Error::Unexpected
holds an Info<T, R>
instead of just a T to make it consistent with the other variants.Info<T>
and Error<T>
has had their signatures changed to Info<T, R>
and Error<T, R>
. Info
has a new variant which is specified by R
and defines the type for range errors. ParseError<T: Positioner>
has been changed to ParseError<S: Stream>
(S is the stream type of the parser).ParseResult
from primitives you should no longer specify the item type of the stream.Stream::uncons
changed its signature to allow it to return errors. Return Error::end_of_input()
instead of ()
if you implemented Stream
.Parser::parse_lazy
, should not break anything but I can't say for certain.any_char
-> any
, uncons_char
-> uncons
Positioner
trait which needs to be implemented on an custom token types.satisfy
is moved to the combinators
module and made generic, might cause type inference issues.any_char
is no longer a free function but returns a parser when called as all parser functions (and its called any
after 0.5.0)Cow
is replaced by Info
in the error messages.Error
which can hold any kind of ::std::error::Error
choice_vec
and choice_slice
is replaced by just choice
from_iter
functionIf you have trouble updating to a newer version feel free to open an issue and I can take a look.