(Previously named parser-combinators)
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 combine::{many, Parser};
use combine::char::letter;
let result = many(letter()).parse("hello world");
assert_eq!(result, Ok(("hello".to_string(), " 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 or sending an email. I am also testing gitter for smaller questions.
Though combine
is stable now that does not mean it is done. To make it as easy as possible to opt-in to these upcoming changes cargo features is used. If you include one or more of these features in your project you may experience breaking changes between versions. As these changes are unstable I really appreciate any and all feedback on these to help make the additions the best they can be.
- range_stream Adds parsers for zero copy parsing through the use of the
RangeStream
trait. - buffered_stream Adds the
BufferedStream
type which can be used to read input from sources which cannot be freely cloned such as files.
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.
- Add additional parsers There is a list of parsers which aren't implemented here but if you have a suggestion for another parser just leave a suggestion on the issue itself.
- 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.
Here is a list containing most of the breaking changes in older versions of combine (parser-combinators).
&[T]
streams has had theItem
type changed from&T
toT
and requires aT: Copy
bound. If you need the old behavior you can wrap the&[T]
in theSliceStream
newtype i.eparser.parse(SliceStream(slice))
.
Error::Unexpected
holds anInfo<T, R>
instead of just a T to make it consistent with the other variants.
Info<T>
andError<T>
has had their signatures changed toInfo<T, R>
andError<T, R>
.Info
has a new variant which is specified byR
and defines the type for range errors.ParseError<T: Positioner>
has been changed toParseError<S: Stream>
(S is the stream type of the parser).- If you were using
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. ReturnError::end_of_input()
instead of()
if you implementedStream
.
- Addition of
Parser::parse_lazy
, should not break anything but I can't say for certain.
any_char
->any
,uncons_char
->uncons
- Introduction of the
Positioner
trait which needs to be implemented on an custom token types. satisfy
is moved to thecombinators
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 calledany
after 0.5.0)Cow
is replaced byInfo
in the error messages.
- Added variant to
Error
which can hold any kind of::std::error::Error
choice_vec
andchoice_slice
is replaced by justchoice
- Iterators cannot directly be used as streams but must be wrapped using
from_iter
function
If you have trouble updating to a newer version feel free to open an issue and I can take a look.