json_number test working
This commit is contained in:
parent
dfb151e2a3
commit
0d69aa81c1
@ -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>
|
||||
where
|
||||
P: Parser<I, O, E>,
|
||||
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)),
|
||||
Err(_e) => Ok((None, input)),
|
||||
}
|
||||
};
|
||||
|
||||
(p, rep)
|
||||
}
|
||||
|
@ -85,6 +85,10 @@ where
|
||||
}
|
||||
|
||||
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),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -15,7 +15,7 @@ where
|
||||
I: ParserInput + Clone + 'a,
|
||||
{
|
||||
fn representation(&self) -> Representation {
|
||||
Representation::new("NOT IMPL'D")
|
||||
Representation::new("sepby")
|
||||
}
|
||||
|
||||
fn parse(&self, input: I) -> ParseResult<I, Vec<O>, I> {
|
||||
|
@ -27,7 +27,7 @@ where
|
||||
|
||||
impl<'a, I: ParserInput, O, E> Parser<I, O, E> for NamedParser<'a, I, O, E> {
|
||||
fn representation(&self) -> Representation {
|
||||
Representation::new("NOT IMPL'D")
|
||||
self.inner_parser.representation()
|
||||
}
|
||||
|
||||
fn parse(&self, input: I) -> ParseResult<I, O, E> {
|
||||
|
@ -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 iter = choice_parser_reps.peekable();
|
||||
loop {
|
||||
@ -31,4 +33,34 @@ impl Representation {
|
||||
|
||||
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))
|
||||
}
|
||||
}
|
||||
|
@ -26,9 +26,7 @@ where
|
||||
|
||||
pub trait Sequence<I, O, E> {
|
||||
fn parse(&self, input: I) -> ParseResult<I, O, E>;
|
||||
fn representation(&self) -> Representation {
|
||||
Representation::new("SEQ NOT DONE YET")
|
||||
}
|
||||
fn representation(&self) -> Representation;
|
||||
}
|
||||
|
||||
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))
|
||||
})
|
||||
}
|
||||
|
||||
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)
|
||||
@ -66,6 +69,16 @@ where
|
||||
|
||||
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)
|
||||
@ -89,6 +102,17 @@ where
|
||||
|
||||
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>
|
||||
@ -116,6 +140,18 @@ where
|
||||
|
||||
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)]
|
||||
|
@ -244,5 +244,5 @@ fn test_representations() {
|
||||
json_bool().representation(),
|
||||
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 | )+"));
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user