From 5aca3912fc8ee7a276686c2fe99910da4f128277 Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Sat, 25 Feb 2023 03:00:29 -0800 Subject: [PATCH] Start making representation work --- src/combinators/map.rs | 6 ++++-- src/lib.rs | 2 +- src/parser/boxed_parser.rs | 2 +- src/parser/representation.rs | 2 +- src/primitives/mod.rs | 3 ++- tests/json_parser.rs | 7 +++++++ 6 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/combinators/map.rs b/src/combinators/map.rs index 0d72d5a..e43d6be 100644 --- a/src/combinators/map.rs +++ b/src/combinators/map.rs @@ -6,9 +6,11 @@ where P: Parser, F: Fn(O1) -> O2, { - move |input| { + let rep = parser.representation(); + let p = move |input| { parser .parse(input) .map(|(result, rest)| (map_fn(result), rest)) - } + }; + (p, rep) } diff --git a/src/lib.rs b/src/lib.rs index a42e8d1..acb73d8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,4 +4,4 @@ mod parser; pub mod primitives; pub mod sequence; -pub use parser::{ParseResult, Parser, ParserInput}; +pub use parser::{ParseResult, Parser, ParserInput, Representation}; diff --git a/src/parser/boxed_parser.rs b/src/parser/boxed_parser.rs index a2b9712..53509c1 100644 --- a/src/parser/boxed_parser.rs +++ b/src/parser/boxed_parser.rs @@ -23,7 +23,7 @@ where impl<'a, I: ParserInput, O, E> Parser for BoxedParser<'a, I, O, E> { fn representation(&self) -> Representation { - Representation::new("NOT IMPL'D") + self.inner.representation() } fn parse(&self, input: I) -> ParseResult { self.inner.parse(input) diff --git a/src/parser/representation.rs b/src/parser/representation.rs index 5b7c0c4..f94dd24 100644 --- a/src/parser/representation.rs +++ b/src/parser/representation.rs @@ -1,4 +1,4 @@ -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq)] pub struct Representation { val: String, } diff --git a/src/primitives/mod.rs b/src/primitives/mod.rs index 047fd1d..b005118 100644 --- a/src/primitives/mod.rs +++ b/src/primitives/mod.rs @@ -9,11 +9,12 @@ pub fn literal_char(expected: char) -> impl Fn(&str) -> ParseResult<&str, char, pub fn literal<'a>(expected: &'static str) -> impl Parser<&'a str, &'a str, &'a str> { println!("literal call expected: {}", expected); + let rep = Representation::new(expected); let p = move |input: &'a str| match input.get(0..expected.len()) { Some(next) if next == expected => Ok((expected, &input[expected.len()..])), _ => Err(input), }; - (p, Representation::new("yolo")) + (p, rep) } pub fn any_char(input: &str) -> ParseResult<&str, char, &str> { diff --git a/tests/json_parser.rs b/tests/json_parser.rs index da3f391..7732422 100644 --- a/tests/json_parser.rs +++ b/tests/json_parser.rs @@ -3,6 +3,7 @@ use parser_combinator::combinators::repeated; use parser_combinator::primitives::{any_char, literal, literal_char, one_of, pred}; use parser_combinator::sequence::seq; use parser_combinator::Parser; +use parser_combinator::Representation; use proptest::prelude::*; @@ -53,6 +54,7 @@ impl<'a, T, P> JsonParser<'a, T> for P where P: Parser<&'a str, T, &'a str> {} fn json_null<'a>() -> impl JsonParser<'a, JsonValue> { literal("null").to(JsonValue::Null) } + fn json_bool<'a>() -> impl JsonParser<'a, JsonValue> { choice(( literal("true").to(JsonValue::Bool(true)), @@ -234,3 +236,8 @@ fn parse_json_document() { let parsed_json = json_object().parse(test_json); assert!(parsed_json.is_ok()); } + +#[test] +fn test_representations() { + assert_eq!(json_null().representation(), Representation::new("null")); +}