From c5f971f7ff103ae5566cbb1ea44b6c49dc2064c3 Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Fri, 21 Oct 2022 17:38:08 -0700 Subject: [PATCH] Surrounded by combinator --- src/lib.rs | 4 +++- src/parser.rs | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 149abf4..d5a9594 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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(), diff --git a/src/parser.rs b/src/parser.rs index 1ac465e..9676a3c 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -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 + '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, E> where I: Clone + 'a,