Start making representation work

This commit is contained in:
Greg Shuflin 2023-02-25 03:00:29 -08:00
parent 377d515d40
commit 5aca3912fc
6 changed files with 16 additions and 6 deletions

View File

@ -6,9 +6,11 @@ where
P: Parser<I, O1, E>,
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)
}

View File

@ -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};

View File

@ -23,7 +23,7 @@ where
impl<'a, I: ParserInput, O, E> Parser<I, O, E> for BoxedParser<'a, I, O, E> {
fn representation(&self) -> Representation {
Representation::new("NOT IMPL'D")
self.inner.representation()
}
fn parse(&self, input: I) -> ParseResult<I, O, E> {
self.inner.parse(input)

View File

@ -1,4 +1,4 @@
#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq)]
pub struct Representation {
val: String,
}

View File

@ -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> {

View File

@ -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"));
}