More representations
This commit is contained in:
parent
e47bcbe760
commit
575c915136
@ -1,22 +1,38 @@
|
||||
use crate::{ParseResult, Parser};
|
||||
use crate::{
|
||||
representation::{Representation, EBNF},
|
||||
ParseResult, Parser, ParserExtension,
|
||||
};
|
||||
|
||||
pub fn sequence<S, I, O, E>(sequence: S) -> impl Parser<I, O, E>
|
||||
where
|
||||
S: Sequence<I, O, E>,
|
||||
{
|
||||
move |input| -> ParseResult<I, O, E> { sequence.parse(input) }
|
||||
let repr = sequence.repr();
|
||||
|
||||
(move |input| -> ParseResult<I, O, E> { sequence.parse(input) })
|
||||
.to_anno()
|
||||
.with_repr(repr)
|
||||
}
|
||||
|
||||
pub fn surrounded_by<I, O1, O2, E>(
|
||||
main: impl Parser<I, O1, E>,
|
||||
surrounding: impl Parser<I, O2, E>,
|
||||
) -> impl Parser<I, O1, E> {
|
||||
move |input| {
|
||||
let s_prod = surrounding.representation().production();
|
||||
let main_prod = main.representation().production();
|
||||
|
||||
(move |input| {
|
||||
let (_result1, rest1) = surrounding.parse(input)?;
|
||||
let (result2, rest2) = main.parse(rest1)?;
|
||||
let (_result3, rest3) = surrounding.parse(rest2)?;
|
||||
Ok((result2, rest3))
|
||||
}
|
||||
})
|
||||
.to_anno()
|
||||
.with_repr(Representation::new().with_production(EBNF::Sequence(vec![
|
||||
s_prod.clone(),
|
||||
main_prod,
|
||||
s_prod,
|
||||
])))
|
||||
}
|
||||
|
||||
pub fn seq2<I, O1, O2, E>(
|
||||
@ -28,6 +44,7 @@ pub fn seq2<I, O1, O2, E>(
|
||||
|
||||
pub trait Sequence<I, O, E> {
|
||||
fn parse(&self, input: I) -> ParseResult<I, O, E>;
|
||||
fn repr(&self) -> Representation;
|
||||
}
|
||||
|
||||
impl<P1, P2, I, O1, O2, E> Sequence<I, (O1, O2), E> for (P1, P2)
|
||||
@ -43,6 +60,15 @@ where
|
||||
.map(|(result2, rest2)| ((result1, result2), rest2))
|
||||
})
|
||||
}
|
||||
|
||||
fn repr(&self) -> Representation {
|
||||
let p1 = &self.0;
|
||||
let p2 = &self.1;
|
||||
Representation::new().with_production(EBNF::Sequence(vec![
|
||||
p1.representation().production(),
|
||||
p2.representation().production(),
|
||||
]))
|
||||
}
|
||||
}
|
||||
|
||||
impl<P1, P2, P3, I, O1, O2, O3, E> Sequence<I, (O1, O2, O3), E> for (P1, P2, P3)
|
||||
@ -62,4 +88,15 @@ where
|
||||
|
||||
Ok(((result1, result2, result3), rest3))
|
||||
}
|
||||
|
||||
fn repr(&self) -> Representation {
|
||||
let p1 = &self.0;
|
||||
let p2 = &self.1;
|
||||
let p3 = &self.2;
|
||||
Representation::new().with_production(EBNF::Sequence(vec![
|
||||
p1.representation().production(),
|
||||
p2.representation().production(),
|
||||
p3.representation().production(),
|
||||
]))
|
||||
}
|
||||
}
|
||||
|
@ -95,3 +95,9 @@ fn test_parse_sexp() {
|
||||
let output = parse_sexp(complex_input).unwrap();
|
||||
assert_eq!(output.1, "");
|
||||
}
|
||||
|
||||
#[ignore = "won't work until representations can be passed more easily around"]
|
||||
#[test]
|
||||
fn test_parse_sexp_repr() {
|
||||
assert_eq!(parse_sexp.representation().show(), r#"["bongo" ' '+]*"#);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user