json_number test working

This commit is contained in:
Greg Shuflin 2023-02-26 03:52:19 -08:00
parent dfb151e2a3
commit 0d69aa81c1
7 changed files with 88 additions and 11 deletions

View File

@ -1,12 +1,17 @@
use crate::parser::{Parser, ParserInput}; use crate::parser::{Parser, ParserInput, Representation};
pub fn optional<P, I, O, E>(parser: P) -> impl Parser<I, Option<O>, E> pub fn optional<P, I, O, E>(parser: P) -> impl Parser<I, Option<O>, E>
where where
P: Parser<I, O, E>, P: Parser<I, O, E>,
I: ParserInput + Clone, I: ParserInput + Clone,
{ {
move |input: I| match parser.parse(input.clone()) { let rep = Representation::from_choice(
&mut [parser.representation(), Representation::new("ε")].into_iter(),
);
let p = move |input: I| match parser.parse(input.clone()) {
Ok((output, rest)) => Ok((Some(output), rest)), Ok((output, rest)) => Ok((Some(output), rest)),
Err(_e) => Ok((None, input)), Err(_e) => Ok((None, input)),
} };
(p, rep)
} }

View File

@ -85,6 +85,10 @@ where
} }
fn representation(&self) -> Representation { fn representation(&self) -> Representation {
Representation::new("NOT IMPL'D") Representation::repeated(
self.inner_parser.representation(),
self.at_least.unwrap_or(0),
self.at_most.unwrap_or(u16::MAX),
)
} }
} }

View File

@ -15,7 +15,7 @@ where
I: ParserInput + Clone + 'a, I: ParserInput + Clone + 'a,
{ {
fn representation(&self) -> Representation { fn representation(&self) -> Representation {
Representation::new("NOT IMPL'D") Representation::new("sepby")
} }
fn parse(&self, input: I) -> ParseResult<I, Vec<O>, I> { fn parse(&self, input: I) -> ParseResult<I, Vec<O>, I> {

View File

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

View File

@ -10,7 +10,9 @@ impl Representation {
} }
} }
pub fn from_choice(choice_parser_reps: &mut impl Iterator<Item = Representation>) -> Self { pub(crate) fn from_choice(
choice_parser_reps: &mut impl Iterator<Item = Representation>,
) -> Self {
let mut buf = String::new(); let mut buf = String::new();
let mut iter = choice_parser_reps.peekable(); let mut iter = choice_parser_reps.peekable();
loop { loop {
@ -31,4 +33,34 @@ impl Representation {
Representation::new(&buf) Representation::new(&buf)
} }
pub(crate) fn from_sequence(
sequence_representations: &mut impl Iterator<Item = Representation>,
) -> Self {
let mut buf = String::new();
let mut iter = sequence_representations.peekable();
loop {
let rep = match iter.next() {
Some(r) => r,
None => break,
};
buf.push_str(&rep.val);
match iter.peek() {
Some(_) => {
buf.push_str(" ");
}
None => {
break;
}
}
}
Representation::new(&buf)
}
// TODO use at_least, at_most
pub(crate) fn repeated(underlying: Representation, at_least: u16, _at_most: u16) -> Self {
let sigil = if at_least == 0 { "*" } else { "+" };
Representation::new(&format!("({}){}", underlying.val, sigil))
}
} }

View File

@ -26,9 +26,7 @@ where
pub trait Sequence<I, O, E> { pub trait Sequence<I, O, E> {
fn parse(&self, input: I) -> ParseResult<I, O, E>; fn parse(&self, input: I) -> ParseResult<I, O, E>;
fn representation(&self) -> Representation { fn representation(&self) -> Representation;
Representation::new("SEQ NOT DONE YET")
}
} }
impl<I, O1, O2, E, P1, P2> Sequence<I, (O1, O2), E> for (P1, P2) impl<I, O1, O2, E, P1, P2> Sequence<I, (O1, O2), E> for (P1, P2)
@ -46,6 +44,11 @@ where
.map(|(result2, rest2)| ((result1, result2), rest2)) .map(|(result2, rest2)| ((result1, result2), rest2))
}) })
} }
fn representation(&self) -> Representation {
let mut iter = [self.0.representation(), self.1.representation()].into_iter();
Representation::from_sequence(&mut iter)
}
} }
impl<I, O1, O2, O3, E, P1, P2, P3> Sequence<I, (O1, O2, O3), E> for (P1, P2, P3) impl<I, O1, O2, O3, E, P1, P2, P3> Sequence<I, (O1, O2, O3), E> for (P1, P2, P3)
@ -66,6 +69,16 @@ where
Ok(((result1, result2, result3), rest3)) Ok(((result1, result2, result3), rest3))
} }
fn representation(&self) -> Representation {
let mut iter = [
self.0.representation(),
self.1.representation(),
self.2.representation(),
]
.into_iter();
Representation::from_sequence(&mut iter)
}
} }
impl<I, O1, O2, O3, O4, E, P1, P2, P3, P4> Sequence<I, (O1, O2, O3, O4), E> for (P1, P2, P3, P4) impl<I, O1, O2, O3, O4, E, P1, P2, P3, P4> Sequence<I, (O1, O2, O3, O4), E> for (P1, P2, P3, P4)
@ -89,6 +102,17 @@ where
Ok(((result1, result2, result3, result4), rest4)) Ok(((result1, result2, result3, result4), rest4))
} }
fn representation(&self) -> Representation {
let mut iter = [
self.0.representation(),
self.1.representation(),
self.2.representation(),
self.3.representation(),
]
.into_iter();
Representation::from_sequence(&mut iter)
}
} }
impl<I, O1, O2, O3, O4, O5, E, P1, P2, P3, P4, P5> Sequence<I, (O1, O2, O3, O4, O5), E> impl<I, O1, O2, O3, O4, O5, E, P1, P2, P3, P4, P5> Sequence<I, (O1, O2, O3, O4, O5), E>
@ -116,6 +140,18 @@ where
Ok(((result1, result2, result3, result4, result5), rest5)) Ok(((result1, result2, result3, result4, result5), rest5))
} }
fn representation(&self) -> Representation {
let mut iter = [
self.0.representation(),
self.1.representation(),
self.2.representation(),
self.3.representation(),
self.4.representation(),
]
.into_iter();
Representation::from_sequence(&mut iter)
}
} }
#[cfg(test)] #[cfg(test)]

View File

@ -244,5 +244,5 @@ fn test_representations() {
json_bool().representation(), json_bool().representation(),
Representation::new("true | false") Representation::new("true | false")
); );
assert_eq!(json_number().representation(), Representation::new("null")); assert_eq!(json_number().representation(), Representation::new("- | ε (1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 0 | )+ . (1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 0 | )+ | ε | . (1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 0 | )+"));
} }