Surrounded by combinator

This commit is contained in:
Greg Shuflin 2022-10-21 17:38:08 -07:00
parent 92155a8f36
commit c5f971f7ff
2 changed files with 22 additions and 1 deletions

View File

@ -102,7 +102,7 @@ mod tests {
fn json_array() -> impl JsonParser<'static, JsonValue> {
move |input| {
let val = json_value().delimited(whitespace(), whitespace());
let val = json_value().surrounded_by(whitespace());
repeated(val)
.separated_by(literal(","), false)
@ -112,6 +112,8 @@ mod tests {
}
}
//fn json_object() -> impl JsonParser<'static, JsonValue> {}
fn json_value() -> impl JsonParser<'static, JsonValue> {
choice((
json_null(),

View File

@ -92,6 +92,25 @@ where
crate::sequence::seq((left, self, right)).map(|(_, output, _)| output)
}
fn surrounded_by<'a, P, O1>(self, surrounding: P) -> BoxedParser<'a, I, O, E>
where
Self: Sized + 'a,
I: 'a,
O1: 'a,
O: 'a,
E: 'a,
P: Parser<I, O1, E> + 'a,
{
BoxedParser::new(move |input| {
let p1 = |i| surrounding.parse(i);
let p2 = |i| surrounding.parse(i);
let main = |i| self.parse(i);
crate::sequence::seq((p1, main, p2))
.map(|(_, output, _)| output)
.parse(input)
})
}
fn optional<'a>(self) -> BoxedParser<'a, I, Option<O>, E>
where
I: Clone + 'a,