Expand description
This is a parser combinator library that aims to provide short and consise, but easily readable parser combinators.
If you don’t know how parser combinators work, I suggest looking at
nom
first.
This library doesn’t use macros, so it is a bit limited for some applications (specifically
places where you can use map_parser
are rather limited).
It also uses operator overloading for writing parsers that are easy to read and write.
Here is one example (I have no clue why almost every closure needs its arguments specified):
use std::iter;
use combinedfun as cf;
enum End {
Repeat(String),
Add(usize),
}
let parser = (
cf::record_while(|c: &char| c.is_digit(10), 1..)
>> (|s: &str| s.parse::<usize>().unwrap())
>> (
-cf::tag(" ")
>> cf::take(..)
>> (|s: &str| End::Repeat(s.to_owned()))
|
-cf::tag("+")
>> cf::record_while(|c: &char| c.is_digit(10), 1..)
>> (|s: &str| End::Add(s.parse::<usize>().unwrap()))
)
>> (|(i, end)| match end {
End::Repeat(s) => (0..i).fold("".to_owned(), |acc, _| acc + &s),
End::Add(j) => (i + j).to_string()
})
);
assert_eq!(parser.parse("3 abc"), Ok("abcabcabc".to_owned()));
assert_eq!(parser.parse("10 x"), Ok("xxxxxxxxxx".to_owned()));
assert_eq!(parser.parse("42+123"), Ok("165".to_owned()));
assert_eq!(parser.parse("42+abc"), Err(()));
assert_eq!(parser.parse("+123"), Err(()));
Re-exports§
pub use traits::AltError;
pub use traits::Collection;
pub use traits::ConsumeError;
pub use traits::EofError;
pub use traits::HasEof;
pub use traits::NotError;
pub use traits::Position;
pub use traits::RangeLike;
pub use traits::Recordable;
pub use traits::SplitFirst;
pub use traits::Tag;
pub use traits::TagError;
pub use types::Index;
pub use types::NoCollection;
pub use types::Pos;
pub use types::Span;
pub use types::PositionedError;
Modules§
- combinators
- These combinators all correspond to one function (with some extra argument sometimes)
- str_
parsers - Defines helpful parsers for parsing
&str
s. - traits
- types
Macros§
- parser
- The parser macro allows you to easily write the type of a parser in a return position, using
impl Trait
. - parser_
dbg - Allows debugging a parser. It uses the
dbg!
macro internally, and prints what the parser returned (be it successful or an error). If it was successful, it prints the input before and after the parser was applied. - parser_
hint - Allows helping Rusts type inference by returning an ε parser with the given type parameters. This is intended to be used with the overloaded operators.
Structs§
- Element
Separator - This type is used to make the syntactic sugar work. It’s observed if you “divide” one
Parser
by another (see/
). Specifically, it allows to use operators forParser::separated
. - Ignored
- This type is used to make the syntactic sugar work. It’s observed if you put a minus in front
of a
Parser
(see-
). - MapResult
- This wrapper can be used to map to a
Result
instead of mapping to the output without being able to fail the parser. - Parser
- This type is the one all functionality is built on.
- With
Collection Generator - This type is used to make the syntactic sugar work. It’s observed if you multiply a parser or
an
ElementSeparator
ElementSeparator] by a function (see*
forElementSeparator
, or*
forParser
). Specifically, it allows to use operators forParser::separated
andParser::repeat
.
Traits§
- Parser
Impl - This trait has to be implemented for the combinators in
combinators
to provide the basic functionality required forParser
.
Functions§
- consume_
one_ where - Consumes the first element/character of the input, if it matches the given condition.
- consume_
while - Consumes elements/characters of the input that match the given condition.
- eof
- Fails if the input isn’t empty.
- epsilon
- Matches nothing, always succeeds.
- f
- Creates an
FnParser
from a function. - fail_
with - Always fails, generating the error using a closure.
- fail_
with_ const - Always fails, generating the error using
Clone::clone
. - lookahead
- Outputs the result of the given parser without consuming it.
- not
- Fails if the given parser succeeds, and succeeds if the given parser fails.
- output
- Outputs the output of the given function, or fails if the function returns
Err
. - record_
while - Like
consume_while
, but returns the matched substring. - tag
- Requires the input to start with the given subsequence/substring.
- take
- Consumes the rest of the input, and returns the matched string.