+ 'a>,
}
impl<'a, Output> BoxedParser<'a, Output> {
fn new(parser: P) -> Self
where
P: Parser<'a, Output> + 'a,
{
BoxedParser {
parser: Box::new(parser),
}
}
}
impl<'a, Output> Parser<'a, Output> for BoxedParser<'a, Output> {
fn parse(&self, input: &'a str) -> Result<'a, Output> {
self.parser.parse(input)
}
}
pub fn either<'a, P1, P2, A>(parser1: P1, parser2: P2) -> impl Parser<'a, A>
where
P1: Parser<'a, A>,
P2: Parser<'a, A>,
{
move |input| match parser1.parse(input) {
ok @ Ok(_) => ok,
Err(_) => parser2.parse(input),
}
}
pub fn whitespace_wrap<'a, P, A>(parser: P) -> impl Parser<'a, A>
where
P: Parser<'a, A>,
{
right(space0(), left(parser, space0()))
}
pub fn pair<'a, P1, P2, R1, R2>(parser1: P1, parser2: P2) -> impl Parser<'a, (R1, R2)>
where
P1: Parser<'a, R1>,
P2: Parser<'a, R2>,
{
move |input| {
parser1.parse(input).and_then(|(next_input, result1)| {
parser2
.parse(next_input)
.map(|(last_input, result2)| (last_input, (result1, result2)))
})
}
}
pub fn prefix<'a, PN, P, R, RN>(pre: PN, parser: P) -> impl Parser<'a, R>
where
PN: Parser<'a, RN>,
P: Parser<'a, R>,
{
move |input| {
pre.parse(input).and_then(|(last_input, _)| {
parser
.parse(last_input)
.map(|(rest, result)| (rest, result))
})
}
}
pub fn suffix<'a, PN, P, R, RN>(parser: P, suf: PN) -> impl Parser<'a, R>
where
PN: Parser<'a, RN>,
P: Parser<'a, R>,
{
move |input| {
parser
.parse(input)
.and_then(|(last_input, result)| suf.parse(last_input).map(|(rest, _)| (rest, result)))
}
}
pub fn delimited<'a, PN, RN, P, R>(lparser: PN, mid: P, rparser: PN) -> impl Parser<'a, R>
where
PN: Parser<'a, RN>,
P: Parser<'a, R>,
{
move |input| {
lparser.parse(input).and_then(|(next_input, _)| {
mid.parse(next_input).and_then(|(last_input, result)| {
rparser.parse(last_input).map(|(rest, _)| (rest, result))
})
})
}
}
pub fn any_char(input: &str) -> Result {
match input.chars().next() {
Some(next) => Ok((&input[next.len_utf8()..], next)),
_ => Err(input),
}
}
pub fn string<'a>() -> impl Parser<'a, String> {
one_or_more(pred(any_char, |c| c.is_alphanumeric())).map(|r| r.into_iter().collect())
}
pub fn space1<'a>() -> impl Parser<'a, Vec> {
one_or_more(whitespace_char())
}
pub fn space0<'a>() -> impl Parser<'a, Vec> {
zero_or_more(whitespace_char())
}
pub fn and_then<'a, P, F, A, B, NextP>(parser: P, f: F) -> impl Parser<'a, B>
where
P: Parser<'a, A>,
NextP: Parser<'a, B>,
F: Fn(A) -> NextP,
{
move |input| match parser.parse(input) {
Ok((next_input, result)) => f(result).parse(next_input),
Err(err) => Err(err),
}
}
pub fn opt<'a, P, A>(opt_parser: P) -> impl Parser<'a, Option>
where
P: Parser<'a, A>,
{
move |input| match opt_parser.parse(input) {
Ok((next_input, result)) => Ok((next_input, Some(result))),
Err(_) => Ok((input, None)),
}
}
pub fn peek<'a, P, A>(parser: P) -> impl Parser<'a, A>
where
P: Parser<'a, A>,
{
move |input| match parser.parse(input) {
Ok((_, result)) => Ok((input, result)),
e @ Err(_) => e,
}
}
pub fn take_until<'a, A, P>(end: P) -> impl Parser<'a, &'a str>
where
P: Parser<'a, A>,
{
move |input: &'a str| {
let mut offset = 0;
while !input[offset..].is_empty() {
if let Ok((rest, _)) = end.parse(&input[offset..]) {
return Ok((
rest,
&input[..(offset + input[offset..].len() - rest.len())],
));
}
while offset != input.len() {
offset += 1;
if input.is_char_boundary(offset) {
break;
}
}
}
Ok((&input[offset..], input))
}
}