pub trait ParserExt: Parser + Sized {
// Provided methods
fn with<P2>(self, p: P2) -> With<Self, P2>
where P2: Parser<Input = Self::Input> { ... }
fn skip<P2>(self, p: P2) -> Skip<Self, P2>
where P2: Parser<Input = Self::Input> { ... }
fn and<P2>(self, p: P2) -> And<Self, P2>
where P2: Parser<Input = Self::Input> { ... }
fn or<P2>(self, p: P2) -> Or<Self, P2>
where P2: Parser<Input = Self::Input> { ... }
fn then<N, F>(self, f: F) -> Then<Self, F>
where F: FnMut(Self::Output) -> N,
N: Parser<Input = Self::Input> { ... }
fn map<F, B>(self, f: F) -> Map<Self, F>
where F: FnMut(Self::Output) -> B { ... }
fn message<S>(self, msg: S) -> Message<Self>
where S: Into<Info<<Self::Input as Stream>::Item>> { ... }
fn expected<S>(self, msg: S) -> Expected<Self>
where S: Into<Info<<Self::Input as Stream>::Item>> { ... }
fn and_then<F, O, E>(self, f: F) -> AndThen<Self, F>
where F: FnMut(Self::Output) -> Result<O, E>,
E: Into<Error<<Self::Input as Stream>::Item>> { ... }
fn iter(self, input: State<Self::Input>) -> Iter<Self> ⓘ { ... }
}
Expand description
Extension trait which provides functions that are more conveniently used through method calls
Provided Methods§
Sourcefn with<P2>(self, p: P2) -> With<Self, P2>
fn with<P2>(self, p: P2) -> With<Self, P2>
Discards the value of the self
parser and returns the value of p
Fails if any of the parsers fails
let result = digit()
.with(token('i'))
.parse("9i")
.map(|x| x.0);
assert_eq!(result, Ok('i'));
Sourcefn skip<P2>(self, p: P2) -> Skip<Self, P2>
fn skip<P2>(self, p: P2) -> Skip<Self, P2>
Discards the value of the p
parser and returns the value of self
Fails if any of the parsers fails
let result = digit()
.skip(token('i'))
.parse("9i")
.map(|x| x.0);
assert_eq!(result, Ok('9'));
Sourcefn and<P2>(self, p: P2) -> And<Self, P2>
fn and<P2>(self, p: P2) -> And<Self, P2>
Parses with self
followed by p
Succeds if both parsers succed, otherwise fails
Returns a tuple with both values on success
let result = digit()
.and(token('i'))
.parse("9i")
.map(|x| x.0);
assert_eq!(result, Ok(('9', 'i')));
Sourcefn or<P2>(self, p: P2) -> Or<Self, P2>
fn or<P2>(self, p: P2) -> Or<Self, P2>
Tries to parse using self
and if it fails returns the result of parsing p
let result = digit().map(|_| "")
.or(string("let"))
.parse("let")
.map(|x| x.0);
assert_eq!(result, Ok("let"));
Sourcefn then<N, F>(self, f: F) -> Then<Self, F>
fn then<N, F>(self, f: F) -> Then<Self, F>
Parses using self
and then passes the value to f
which returns a parser used to parse
the rest of the input
let result = digit()
.then(|d| parser(move |input| {
if d == '9' {
Ok((9, Consumed::Empty(input)))
}
else {
let err = ParseError::new(input.position, Error::Message("Not a nine".into()));
Err((Consumed::Empty(err)))
}
}))
.parse("9");
assert_eq!(result, Ok((9, "")));
Sourcefn map<F, B>(self, f: F) -> Map<Self, F>
fn map<F, B>(self, f: F) -> Map<Self, F>
Uses f
to map over the parsed value
let result = digit()
.map(|c| c == '9')
.parse("9")
.map(|x| x.0);
assert_eq!(result, Ok(true));
Sourcefn message<S>(self, msg: S) -> Message<Self>
fn message<S>(self, msg: S) -> Message<Self>
Parses with self
and if it fails, adds the message msg
to the error
let result = token('9')
.message("Not a nine")
.parse("8");
assert!(result.is_err());
assert!(result.unwrap_err().errors.iter()
.find(|e| **e == Error::Message("Not a nine".into())).is_some());
Sourcefn expected<S>(self, msg: S) -> Expected<Self>
fn expected<S>(self, msg: S) -> Expected<Self>
Parses with self
and if it fails without consuming any input any expected errors are replaced by
msg
. msg
is then used in error messages as “Expected msg
”.
let result = token('9')
.expected("9")
.parse("8");
assert!(result.is_err());
assert!(result.unwrap_err().errors.iter()
.find(|e| **e == Error::Expected("9".into())).is_some());
Sourcefn and_then<F, O, E>(self, f: F) -> AndThen<Self, F>
fn and_then<F, O, E>(self, f: F) -> AndThen<Self, F>
Parses with self
and applies f
on the result if self
parses successfully
f
may optionally fail with an error which is automatically converted to a ParseError
let mut parser = many1(digit())
.and_then(|s: String| s.parse::<i32>());
let result = parser.parse("1234");
assert_eq!(result, Ok((1234, "")));
let err = parser.parse("abc");
assert!(err.is_err());
Sourcefn iter(self, input: State<Self::Input>) -> Iter<Self> ⓘ
fn iter(self, input: State<Self::Input>) -> Iter<Self> ⓘ
Creates an iterator from a parser and a state. Can be used as an alternative to many
when
collecting directly into a FromIterator
type is not desirable
let mut buffer = String::new();
let number = parser(|input| {
buffer.clear();
let mut iter = digit().iter(input);
buffer.extend(&mut iter);
let i = buffer.parse::<i32>().unwrap();
iter.into_result(i)
});
let result = sep_by(number, char(','))
.parse("123,45,6");
assert_eq!(result, Ok((vec![123, 45, 6], "")));
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.