surrounded by
This commit is contained in:
parent
d8a68bcbf3
commit
cdbbb8214f
@ -1,4 +1,4 @@
|
||||
use crate::{map, seq2};
|
||||
use crate::{map, seq2, surrounded_by};
|
||||
|
||||
pub type ParseResult<I, O, E> = Result<(O, I), (E, I)>;
|
||||
|
||||
@ -24,6 +24,7 @@ pub trait ParserExtension<I, O, E>: Parser<I, O, E> {
|
||||
fn then<O2, P: Parser<I, O2, E>>(self, next: P) -> impl Parser<I, (O, O2), E>;
|
||||
fn then_ignore<O2, P: Parser<I, O2, E>>(self, next: P) -> impl Parser<I, O, E>;
|
||||
fn ignore_then<O2, P: Parser<I, O2, E>>(self, next: P) -> impl Parser<I, O2, E>;
|
||||
fn surrounded_by<O2>(self, surrounding: impl Parser<I, O2, E>) -> impl Parser<I, O, E>;
|
||||
}
|
||||
|
||||
impl<T, I, O, E> ParserExtension<I, O, E> for T
|
||||
@ -51,4 +52,7 @@ where
|
||||
fn ignore_then<O2, P: Parser<I, O2, E>>(self, next: P) -> impl Parser<I, O2, E> {
|
||||
seq2(self, next).map(|(_, next)| next)
|
||||
}
|
||||
fn surrounded_by<O2>(self, surrounding: impl Parser<I, O2, E>) -> impl Parser<I, O, E> {
|
||||
surrounded_by(self, surrounding)
|
||||
}
|
||||
}
|
||||
|
@ -7,6 +7,18 @@ where
|
||||
move |input| -> ParseResult<I, O, E> { sequence.parse(input) }
|
||||
}
|
||||
|
||||
pub fn surrounded_by<I, O1, O2, E>(
|
||||
main: impl Parser<I, O1, E>,
|
||||
surrounding: impl Parser<I, O2, E>,
|
||||
) -> impl Parser<I, O1, E> {
|
||||
move |input| {
|
||||
let (_result1, rest1) = surrounding.parse(input)?;
|
||||
let (result2, rest2) = main.parse(rest1)?;
|
||||
let (_result3, rest3) = surrounding.parse(rest2)?;
|
||||
Ok((result2, rest3))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn seq2<I, O1, O2, E>(
|
||||
first: impl Parser<I, O1, E>,
|
||||
second: impl Parser<I, O2, E>,
|
||||
@ -32,3 +44,22 @@ where
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<P1, P2, P3, I, O1, O2, O3, E> Sequence<I, (O1, O2, O3), E> for (P1, P2, P3)
|
||||
where
|
||||
P1: Parser<I, O1, E>,
|
||||
P2: Parser<I, O2, E>,
|
||||
P3: Parser<I, O3, E>,
|
||||
{
|
||||
fn parse(&self, input: I) -> ParseResult<I, (O1, O2, O3), E> {
|
||||
let p1 = &self.0;
|
||||
let p2 = &self.1;
|
||||
let p3 = &self.2;
|
||||
|
||||
let (result1, rest1) = p1.parse(input)?;
|
||||
let (result2, rest2) = p2.parse(rest1)?;
|
||||
let (result3, rest3) = p3.parse(rest2)?;
|
||||
|
||||
Ok(((result1, result2, result3), rest3))
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user