From 575c9151362025adc192cf12ecc2ee4ee13070b7 Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Sat, 3 Feb 2024 00:38:10 -0800 Subject: [PATCH] More representations --- src/sequence.rs | 45 +++++++++++++++++++++++++++++++++++++++++---- src/test/sexp.rs | 6 ++++++ 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/src/sequence.rs b/src/sequence.rs index 7fa4aac..4a2245d 100644 --- a/src/sequence.rs +++ b/src/sequence.rs @@ -1,22 +1,38 @@ -use crate::{ParseResult, Parser}; +use crate::{ + representation::{Representation, EBNF}, + ParseResult, Parser, ParserExtension, +}; pub fn sequence(sequence: S) -> impl Parser where S: Sequence, { - move |input| -> ParseResult { sequence.parse(input) } + let repr = sequence.repr(); + + (move |input| -> ParseResult { sequence.parse(input) }) + .to_anno() + .with_repr(repr) } pub fn surrounded_by( main: impl Parser, surrounding: impl Parser, ) -> impl Parser { - 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( @@ -28,6 +44,7 @@ pub fn seq2( pub trait Sequence { fn parse(&self, input: I) -> ParseResult; + fn repr(&self) -> Representation; } impl Sequence 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 Sequence 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(), + ])) + } } diff --git a/src/test/sexp.rs b/src/test/sexp.rs index f4814ad..ca83d6f 100644 --- a/src/test/sexp.rs +++ b/src/test/sexp.rs @@ -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" ' '+]*"#); +}