From 1448a415ed421ea4e585cf7a0bb781cc0f23f27e Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Wed, 28 Jun 2023 19:16:18 -0400 Subject: [PATCH] Parse soft newlines and escaped closing brackets in propvals --- go-sgf/src/lib.rs | 43 ++++++++++++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/go-sgf/src/lib.rs b/go-sgf/src/lib.rs index e40dd6e..bde52ac 100644 --- a/go-sgf/src/lib.rs +++ b/go-sgf/src/lib.rs @@ -69,8 +69,8 @@ use nom::{ branch::alt, - bytes::complete::{escaped_transform, is_not, tag}, - character::complete::{alpha1, digit1, multispace0, multispace1}, + bytes::complete::{escaped, escaped_transform, is_not, tag}, + character::complete::{alpha1, digit1, multispace0, multispace1, none_of, one_of}, combinator::{opt, value}, multi::{many0, many1, separated_list1}, sequence::delimited, @@ -395,9 +395,13 @@ fn parse_propval_text<'a, E: nom::error::ParseError<&'a str>>( input: &'a str, ) -> IResult<&'a str, Option, E> { let (input, value) = opt(escaped_transform( - is_not(r"\]"), + none_of("\\]"), '\\', - value("]", tag(r"\]")), // alt((value("]", tag(r"\]")), value("", tag("\\\n")))), + alt(( + value("]", tag("]")), + value("\\", tag("\\")), + value("", tag("\n")), + )), ))(input)?; Ok((input, value.map(|v| v.to_owned()))) } @@ -635,29 +639,42 @@ mod tests { #[test] fn it_parses_propvals_with_hard_linebreaks() { - let (_, propval) = parse_propval::>( - "[There are hard linebreaks & soft linebreaks. -Soft linebreaks...]", + let (_, propval) = parse_propval_text::>( + "There are hard linebreaks & soft linebreaks. +Soft linebreaks...", ) .unwrap(); assert_eq!( propval, - "There are hard linebreaks & soft linebreaks. + Some( + "There are hard linebreaks & soft linebreaks. Soft linebreaks..." - .to_owned() + .to_owned() + ) + ); + } + + #[test] + fn it_parses_propvals_with_escaped_closing_brackets() { + let (_, propval) = + parse_propval_text::>(r"escaped closing \] bracket") + .unwrap(); + assert_eq!( + propval, + Some(r"escaped closing ] bracket".to_owned()).to_owned() ); } #[test] fn it_parses_propvals_with_soft_linebreaks() { - let (_, propval) = parse_propval::>( - r"[Soft linebreaks are linebreaks preceeded by '\\' like this one >o\ -k<. Hard line breaks are all other linebreaks.]", + let (_, propval) = parse_propval_text::>( + r"Soft linebreaks are linebreaks preceeded by '\\' like this one >o\ +k<. Hard line breaks are all other linebreaks.", ) .unwrap(); assert_eq!( propval, - r"Soft linebreaks are linebreaks preceeded by '\\' like this one >ok<. Hard line breaks are all other linebreaks." + Some("Soft linebreaks are linebreaks preceeded by '\\' like this one >ok<. Hard line breaks are all other linebreaks.".to_owned()) .to_owned() ); }