From 0a353135c04d1639cad7b0b881dd7b464a1989e1 Mon Sep 17 00:00:00 2001 From: cel 🌸 Date: Fri, 21 Jun 2024 16:05:31 +0100 Subject: WIP: CDSect parsers --- src/parser.rs | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/src/parser.rs b/src/parser.rs index bec5313..f882064 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -269,6 +269,30 @@ pub fn pi_target(input: &str) -> IResult<&str, PITarget> { } } +type CDSect<'s> = (CDStart<'s>, CData<'s>, CDEnd<'s>); +/// [18] CDSect ::= CDStart CData CDEnd +pub fn cd_sect(input: &str) -> IResult<&str, CDSect> { + tuple((cd_start, cdata, cd_end))(input) +} + +type CDStart<'s> = &'s str; +/// [19] CDStart ::= ' IResult<&str, CDStart> { + tag(" = &'s str; +/// [20] CData ::= (Char* - (Char* ']]>' Char*)) +pub fn cdata(input: &str) -> IResult<&str, CData> { + recognize(many_till(xmlchar, peek(tag("]]>"))))(input) +} + +type CDEnd<'s> = &'s str; +/// [21] CDEnd ::= ']]>' +pub fn cd_end(input: &str) -> IResult<&str, CDEnd> { + tag("]]>")(input) +} + type Prolog<'s> = ( Option, Vec>, @@ -400,4 +424,38 @@ mod tests { pi_target("xMl ") ); } + + #[test] + fn test_cd_sect() { + assert_eq!( + Ok(( + "", + ("Hello, world!", "]]>") + )), + cd_sect("Hello, world!]]>") + ) + } + + #[test] + fn test_cd_start() { + assert_eq!(Ok(("asdf", "asdf", "asdf")), cdata("asdf]]>asdf")); + assert_eq!( + Ok(("]]>asdf", "asdf") + ); + assert_eq!( + Ok(("]]>asdf", "Hello, world!")), + cdata("Hello, world!]]>asdf") + ) + } + + #[test] + fn test_cd_end() { + assert_eq!(Ok(("asdf", "]]>")), cd_end("]]>asdf")) + } } -- cgit